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.

64 lines
1.6 KiB

  1. // -----------------------------------------------------------------------------
  2. // DHT Sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. class EventSensor : public BaseSensor {
  8. public:
  9. void InterruptHandler() {
  10. static unsigned long last = 0;
  11. if (millis() - last > _debounce) {
  12. _events = _events + 1;
  13. last = millis();
  14. }
  15. }
  16. EventSensor(unsigned char gpio, int pin_mode, unsigned long debounce): BaseSensor() {
  17. _gpio = gpio;
  18. _count = 1;
  19. _debounce = debounce;
  20. pinMode(_gpio, pin_mode);
  21. }
  22. // Descriptive name of the sensor
  23. String name() {
  24. char buffer[20];
  25. snprintf(buffer, sizeof(buffer), "EVENT @ GPIO%d", _gpio);
  26. return String(buffer);
  27. }
  28. // Descriptive name of the slot # index
  29. String slot(unsigned char index) {
  30. return name();
  31. }
  32. // Type for slot # index
  33. magnitude_t type(unsigned char index) {
  34. if (index == 0) return MAGNITUDE_EVENTS;
  35. return MAGNITUDE_NONE;
  36. }
  37. // Current value for slot # index
  38. double value(unsigned char index) {
  39. double value = 0;
  40. if (index == 0) {
  41. value = _events;
  42. _events = 0;
  43. };
  44. return value;
  45. }
  46. protected:
  47. volatile unsigned long _events = 0;
  48. unsigned long _debounce = 0;
  49. unsigned char _gpio;
  50. };