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.8 KiB

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