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.

118 lines
3.4 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. // ---------------------------------------------------------------------
  11. // Public
  12. // ---------------------------------------------------------------------
  13. EventSensor(): BaseSensor() {
  14. _count = 1;
  15. }
  16. ~EventSensor() {
  17. detachInterrupt(_gpio);
  18. }
  19. void setGPIO(unsigned char gpio, int mode = INPUT) {
  20. _gpio = gpio;
  21. pinMode(_gpio, mode);
  22. }
  23. void setinterruptMode(unsigned long mode) {
  24. _mode = mode;
  25. }
  26. void setDebounceTime(unsigned long debounce) {
  27. _debounce = debounce;
  28. }
  29. // ---------------------------------------------------------------------
  30. // Sensors API
  31. // ---------------------------------------------------------------------
  32. // Initialization method, must be idempotent
  33. // Defined outside the class body
  34. void begin() {
  35. if (_interrupt_gpio != GPIO_NONE) detach(_interrupt_gpio);
  36. attach(this, _gpio, _mode);
  37. }
  38. // Descriptive name of the sensor
  39. String name() {
  40. char buffer[20];
  41. snprintf(buffer, sizeof(buffer), "INTERRUPT @ GPIO%d", _gpio);
  42. return String(buffer);
  43. }
  44. // Descriptive name of the slot # index
  45. String slot(unsigned char index) {
  46. (void) index;
  47. return name();
  48. }
  49. // Type for slot # index
  50. magnitude_t type(unsigned char index) {
  51. _error = SENSOR_ERROR_OK;
  52. if (index == 0) return MAGNITUDE_EVENTS;
  53. _error = SENSOR_ERROR_OUT_OF_RANGE;
  54. return MAGNITUDE_NONE;
  55. }
  56. // Current value for slot # index
  57. double value(unsigned char index) {
  58. _error = SENSOR_ERROR_OK;
  59. if (index == 0) {
  60. double value = _events;
  61. _events = 0;
  62. return value;
  63. };
  64. _error = SENSOR_ERROR_OUT_OF_RANGE;
  65. return 0;
  66. }
  67. // Handle interrupt calls
  68. void handleInterrupt(unsigned char gpio) {
  69. (void) gpio;
  70. static unsigned long last = 0;
  71. if (millis() - last > _debounce) {
  72. _events = _events + 1;
  73. last = millis();
  74. }
  75. }
  76. // Interrupt attach callback
  77. void attached(unsigned char gpio) {
  78. BaseSensor::attached(gpio);
  79. _interrupt_gpio = gpio;
  80. }
  81. // Interrupt detach callback
  82. void detached(unsigned char gpio) {
  83. BaseSensor::detached(gpio);
  84. if (_interrupt_gpio == gpio) _interrupt_gpio = GPIO_NONE;
  85. }
  86. protected:
  87. // ---------------------------------------------------------------------
  88. // Protected
  89. // ---------------------------------------------------------------------
  90. volatile unsigned long _events = 0;
  91. unsigned long _debounce = EVENTS_DEBOUNCE;
  92. unsigned char _gpio;
  93. unsigned char _interrupt_gpio = GPIO_NONE;
  94. unsigned char _mode;
  95. };