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.

200 lines
6.5 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. _sensor_id = SENSOR_EVENTS_ID;
  16. }
  17. ~EventSensor() {
  18. _enableInterrupts(false);
  19. }
  20. // ---------------------------------------------------------------------
  21. void setGPIO(unsigned char gpio) {
  22. _gpio = gpio;
  23. }
  24. void setMode(unsigned char mode) {
  25. _mode = mode;
  26. }
  27. void setInterruptMode(unsigned char mode) {
  28. _interrupt_mode = mode;
  29. }
  30. void setDebounceTime(unsigned long debounce) {
  31. _debounce = debounce;
  32. }
  33. // ---------------------------------------------------------------------
  34. unsigned char getGPIO() {
  35. return _gpio;
  36. }
  37. unsigned char getMode() {
  38. return _mode;
  39. }
  40. unsigned char getInterruptMode() {
  41. return _interrupt_mode;
  42. }
  43. unsigned long getDebounceTime() {
  44. return _debounce;
  45. }
  46. // ---------------------------------------------------------------------
  47. // Sensors API
  48. // ---------------------------------------------------------------------
  49. // Initialization method, must be idempotent
  50. // Defined outside the class body
  51. void begin() {
  52. pinMode(_gpio, _mode);
  53. _enableInterrupts(true);
  54. }
  55. // Descriptive name of the sensor
  56. String description() {
  57. char buffer[20];
  58. snprintf(buffer, sizeof(buffer), "INTERRUPT @ GPIO%d", _gpio);
  59. return String(buffer);
  60. }
  61. // Type for slot # index
  62. magnitude_t type(unsigned char index) {
  63. _error = SENSOR_ERROR_OK;
  64. if (index == 0) return MAGNITUDE_EVENTS;
  65. _error = SENSOR_ERROR_OUT_OF_RANGE;
  66. return MAGNITUDE_NONE;
  67. }
  68. // Current value for slot # index
  69. double value(unsigned char index) {
  70. _error = SENSOR_ERROR_OK;
  71. if (index == 0) {
  72. double value = _events;
  73. _events = 0;
  74. return value;
  75. };
  76. _error = SENSOR_ERROR_OUT_OF_RANGE;
  77. return 0;
  78. }
  79. // Handle interrupt calls
  80. void handleInterrupt(unsigned char gpio) {
  81. (void) gpio;
  82. static unsigned long last = 0;
  83. if (millis() - last > _debounce) {
  84. _events = _events + 1;
  85. last = millis();
  86. }
  87. }
  88. protected:
  89. // ---------------------------------------------------------------------
  90. // Interrupt management
  91. // ---------------------------------------------------------------------
  92. void _attach(EventSensor * instance, unsigned char gpio, unsigned char mode);
  93. void _detach(unsigned char gpio);
  94. void _enableInterrupts(bool value) {
  95. static unsigned char _interrupt_gpio = GPIO_NONE;
  96. if (value) {
  97. if (_interrupt_gpio != GPIO_NONE) _detach(_interrupt_gpio);
  98. _attach(this, _gpio, _interrupt_mode);
  99. _interrupt_gpio = _gpio;
  100. } else if (_interrupt_gpio != GPIO_NONE) {
  101. _detach(_interrupt_gpio);
  102. _interrupt_gpio = GPIO_NONE;
  103. }
  104. }
  105. // ---------------------------------------------------------------------
  106. // Protected
  107. // ---------------------------------------------------------------------
  108. volatile unsigned long _events = 0;
  109. unsigned long _debounce = EVENTS_DEBOUNCE;
  110. unsigned char _gpio;
  111. unsigned char _mode;
  112. unsigned char _interrupt_mode;
  113. };
  114. // -----------------------------------------------------------------------------
  115. // Interrupt helpers
  116. // -----------------------------------------------------------------------------
  117. EventSensor * _event_sensor_instance[10] = {NULL};
  118. void ICACHE_RAM_ATTR _event_sensor_isr(unsigned char gpio) {
  119. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  120. if (_event_sensor_instance[index]) {
  121. _event_sensor_instance[index]->handleInterrupt(gpio);
  122. }
  123. }
  124. void ICACHE_RAM_ATTR _event_sensor_isr_0() { _event_sensor_isr(0); }
  125. void ICACHE_RAM_ATTR _event_sensor_isr_1() { _event_sensor_isr(1); }
  126. void ICACHE_RAM_ATTR _event_sensor_isr_2() { _event_sensor_isr(2); }
  127. void ICACHE_RAM_ATTR _event_sensor_isr_3() { _event_sensor_isr(3); }
  128. void ICACHE_RAM_ATTR _event_sensor_isr_4() { _event_sensor_isr(4); }
  129. void ICACHE_RAM_ATTR _event_sensor_isr_5() { _event_sensor_isr(5); }
  130. void ICACHE_RAM_ATTR _event_sensor_isr_12() { _event_sensor_isr(12); }
  131. void ICACHE_RAM_ATTR _event_sensor_isr_13() { _event_sensor_isr(13); }
  132. void ICACHE_RAM_ATTR _event_sensor_isr_14() { _event_sensor_isr(14); }
  133. void ICACHE_RAM_ATTR _event_sensor_isr_15() { _event_sensor_isr(15); }
  134. static void (*_event_sensor_isr_list[10])() = {
  135. _event_sensor_isr_0, _event_sensor_isr_1, _event_sensor_isr_2,
  136. _event_sensor_isr_3, _event_sensor_isr_4, _event_sensor_isr_5,
  137. _event_sensor_isr_12, _event_sensor_isr_13, _event_sensor_isr_14,
  138. _event_sensor_isr_15
  139. };
  140. void EventSensor::_attach(EventSensor * instance, unsigned char gpio, unsigned char mode) {
  141. if (!_validGPIO(gpio)) return;
  142. _detach(gpio);
  143. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  144. _event_sensor_instance[index] = instance;
  145. attachInterrupt(gpio, _event_sensor_isr_list[index], mode);
  146. #if SENSOR_DEBUG
  147. DEBUG_MSG("[SENSOR] GPIO%d interrupt attached to %s\n", gpio, instance->description().c_str());
  148. #endif
  149. }
  150. void EventSensor::_detach(unsigned char gpio) {
  151. if (!_validGPIO(gpio)) return;
  152. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  153. if (_event_sensor_instance[index]) {
  154. detachInterrupt(gpio);
  155. #if SENSOR_DEBUG
  156. DEBUG_MSG("[SENSOR] GPIO%d interrupt detached from %s\n", gpio, _event_sensor_instance[index]->description().c_str());
  157. #endif
  158. _event_sensor_instance[index] = NULL;
  159. }
  160. }