Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.9 KiB

  1. // -----------------------------------------------------------------------------
  2. // Digital Sensor (maps to a digitalRead)
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && DIGITAL_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. class DigitalSensor : public BaseSensor {
  10. public:
  11. // ---------------------------------------------------------------------
  12. // Public
  13. // ---------------------------------------------------------------------
  14. DigitalSensor(): BaseSensor() {
  15. _count = 1;
  16. _sensor_id = SENSOR_DIGITAL_ID;
  17. }
  18. // ---------------------------------------------------------------------
  19. void setGPIO(unsigned char gpio) {
  20. _gpio = gpio;
  21. }
  22. void setMode(unsigned char mode) {
  23. _mode = mode;
  24. }
  25. void setDefault(bool value) {
  26. _default = value;
  27. }
  28. // ---------------------------------------------------------------------
  29. unsigned char getGPIO() {
  30. return _gpio;
  31. }
  32. unsigned char getMode() {
  33. return _mode;
  34. }
  35. bool getDefault() {
  36. return _default;
  37. }
  38. // ---------------------------------------------------------------------
  39. // Sensor API
  40. // ---------------------------------------------------------------------
  41. // Initialization method, must be idempotent
  42. void begin() {
  43. pinMode(_gpio, _mode);
  44. _ready = true;
  45. }
  46. // Descriptive name of the sensor
  47. String description() {
  48. char buffer[20];
  49. snprintf(buffer, sizeof(buffer), "DIGITAL @ GPIO%d", _gpio);
  50. return String(buffer);
  51. }
  52. // Descriptive name of the slot # index
  53. String slot(unsigned char index) {
  54. return description();
  55. };
  56. // Address of the sensor (it could be the GPIO or I2C address)
  57. String address(unsigned char index) {
  58. return String(_gpio);
  59. }
  60. // Type for slot # index
  61. unsigned char type(unsigned char index) {
  62. if (index == 0) return MAGNITUDE_DIGITAL;
  63. return MAGNITUDE_NONE;
  64. }
  65. // Current value for slot # index
  66. double value(unsigned char index) {
  67. if (index == 0) return (digitalRead(_gpio) == _default) ? 0 : 1;
  68. return 0;
  69. }
  70. protected:
  71. // ---------------------------------------------------------------------
  72. // Protected
  73. // ---------------------------------------------------------------------
  74. unsigned char _gpio;
  75. unsigned char _mode;
  76. bool _default = false;
  77. };
  78. #endif // SENSOR_SUPPORT && DIGITAL_SUPPORT