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.

68 lines
1.8 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. _error = SENSOR_ERROR_OK;
  35. if (index == 0) return MAGNITUDE_EVENTS;
  36. _error = SENSOR_ERROR_OUT_OF_RANGE;
  37. return MAGNITUDE_NONE;
  38. }
  39. // Current value for slot # index
  40. double value(unsigned char index) {
  41. _error = SENSOR_ERROR_OK;
  42. if (index == 0) {
  43. double value = _events;
  44. _events = 0;
  45. return value;
  46. };
  47. _error = SENSOR_ERROR_OUT_OF_RANGE;
  48. return 0;
  49. }
  50. protected:
  51. volatile unsigned long _events = 0;
  52. unsigned long _debounce = 0;
  53. unsigned char _gpio;
  54. };