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.

69 lines
1.9 KiB

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