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.

55 lines
1.5 KiB

  1. // -----------------------------------------------------------------------------
  2. // DHT Sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. class DigitalSensor : public BaseSensor {
  8. public:
  9. DigitalSensor(unsigned char gpio, int mode = INPUT, bool default_state = false): BaseSensor() {
  10. _gpio = gpio;
  11. _default = default_state;
  12. pinMode(_gpio, mode);
  13. _count = 1;
  14. }
  15. // Descriptive name of the sensor
  16. String name() {
  17. char buffer[20];
  18. snprintf(buffer, sizeof(buffer), "DIGITAL @ GPIO%d", _gpio);
  19. return String(buffer);
  20. }
  21. // Descriptive name of the slot # index
  22. String slot(unsigned char index) {
  23. return name();
  24. }
  25. // Type for slot # index
  26. magnitude_t type(unsigned char index) {
  27. _error = SENSOR_ERROR_OK;
  28. if (index == 0) return MAGNITUDE_DIGITAL;
  29. _error = SENSOR_ERROR_OUT_OF_RANGE;
  30. return MAGNITUDE_NONE;
  31. }
  32. // Current value for slot # index
  33. double value(unsigned char index) {
  34. _error = SENSOR_ERROR_OK;
  35. if (index == 0) return (digitalRead(_gpio) == _default) ? 0 : 1;
  36. _error = SENSOR_ERROR_OUT_OF_RANGE;
  37. return 0;
  38. }
  39. protected:
  40. unsigned char _gpio;
  41. bool _default;
  42. };