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.

237 lines
7.3 KiB

  1. // -----------------------------------------------------------------------------
  2. // Event Counter Sensor
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && EVENTS_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. // we are bound by usable GPIOs
  10. #define EVENTS_SENSORS_MAX 10
  11. class EventSensor : public BaseSensor {
  12. public:
  13. // ---------------------------------------------------------------------
  14. // Public
  15. // ---------------------------------------------------------------------
  16. EventSensor(): BaseSensor() {
  17. _count = 1;
  18. _sensor_id = SENSOR_EVENTS_ID;
  19. }
  20. ~EventSensor() {
  21. _enableInterrupts(false);
  22. }
  23. // ---------------------------------------------------------------------
  24. void setGPIO(unsigned char gpio) {
  25. _gpio = gpio;
  26. }
  27. void setTrigger(bool trigger) {
  28. _trigger = trigger;
  29. }
  30. void setPinMode(unsigned char pin_mode) {
  31. _pin_mode = pin_mode;
  32. }
  33. void setInterruptMode(unsigned char interrupt_mode) {
  34. _interrupt_mode = interrupt_mode;
  35. }
  36. void setDebounceTime(unsigned long debounce) {
  37. _debounce = debounce;
  38. }
  39. // ---------------------------------------------------------------------
  40. unsigned char getGPIO() {
  41. return _gpio;
  42. }
  43. bool getTrigger() {
  44. return _trigger;
  45. }
  46. unsigned char getPinMode() {
  47. return _pin_mode;
  48. }
  49. unsigned char getInterruptMode() {
  50. return _interrupt_mode;
  51. }
  52. unsigned long getDebounceTime() {
  53. return _debounce;
  54. }
  55. // ---------------------------------------------------------------------
  56. // Sensors API
  57. // ---------------------------------------------------------------------
  58. // Initialization method, must be idempotent
  59. // Defined outside the class body
  60. void begin() {
  61. pinMode(_gpio, _pin_mode);
  62. _enableInterrupts(true);
  63. _count = _trigger ? 2 : 1;
  64. _ready = true;
  65. }
  66. // Descriptive name of the sensor
  67. String description() {
  68. char buffer[20];
  69. snprintf(buffer, sizeof(buffer), "INTERRUPT @ GPIO%d", _gpio);
  70. return String(buffer);
  71. }
  72. // Descriptive name of the slot # index
  73. String slot(unsigned char index) {
  74. return description();
  75. };
  76. // Address of the sensor (it could be the GPIO or I2C address)
  77. String address(unsigned char index) {
  78. return String(_gpio);
  79. }
  80. // Type for slot # index
  81. unsigned char type(unsigned char index) {
  82. if (index == 0) return MAGNITUDE_COUNT;
  83. if (index == 1) return MAGNITUDE_EVENT;
  84. return MAGNITUDE_NONE;
  85. }
  86. // Current value for slot # index
  87. double value(unsigned char index) {
  88. if (index == 0) {
  89. double value = _events;
  90. _events = 0;
  91. return value;
  92. };
  93. return 0;
  94. }
  95. // Handle interrupt calls
  96. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  97. (void) gpio;
  98. static unsigned long last = 0;
  99. // ~27000ms when F_CPU is 80MHz
  100. // ~12000ms when F_CPU is 160MHz
  101. unsigned long ms = clockCyclesToMicroseconds(ESP.getCycleCount()) / 1000u;
  102. if (ms - last > _debounce) {
  103. last = ms;
  104. _events = _events + 1;
  105. if (_trigger) {
  106. if (_callback) _callback(MAGNITUDE_EVENT, digitalRead(gpio));
  107. }
  108. }
  109. }
  110. protected:
  111. // ---------------------------------------------------------------------
  112. // Interrupt management
  113. // ---------------------------------------------------------------------
  114. void _attach(unsigned char gpio, unsigned char mode);
  115. void _detach(unsigned char gpio);
  116. void _enableInterrupts(bool value) {
  117. if (value) {
  118. _detach(_gpio);
  119. _attach(_gpio, _interrupt_mode);
  120. } else {
  121. _detach(_gpio);
  122. }
  123. }
  124. // ---------------------------------------------------------------------
  125. // Protected
  126. // ---------------------------------------------------------------------
  127. volatile unsigned long _events = 0;
  128. unsigned long _debounce = EVENTS_DEBOUNCE;
  129. unsigned char _gpio = GPIO_NONE;
  130. bool _trigger = false;
  131. unsigned char _pin_mode = INPUT;
  132. unsigned char _interrupt_mode = RISING;
  133. };
  134. // -----------------------------------------------------------------------------
  135. // Interrupt helpers
  136. // -----------------------------------------------------------------------------
  137. EventSensor * _event_sensor_instance[EVENTS_SENSORS_MAX] = {nullptr};
  138. void ICACHE_RAM_ATTR _event_sensor_isr(unsigned char gpio) {
  139. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  140. if (_event_sensor_instance[index]) {
  141. _event_sensor_instance[index]->handleInterrupt(gpio);
  142. }
  143. }
  144. void ICACHE_RAM_ATTR _event_sensor_isr_0() { _event_sensor_isr(0); }
  145. void ICACHE_RAM_ATTR _event_sensor_isr_1() { _event_sensor_isr(1); }
  146. void ICACHE_RAM_ATTR _event_sensor_isr_2() { _event_sensor_isr(2); }
  147. void ICACHE_RAM_ATTR _event_sensor_isr_3() { _event_sensor_isr(3); }
  148. void ICACHE_RAM_ATTR _event_sensor_isr_4() { _event_sensor_isr(4); }
  149. void ICACHE_RAM_ATTR _event_sensor_isr_5() { _event_sensor_isr(5); }
  150. void ICACHE_RAM_ATTR _event_sensor_isr_12() { _event_sensor_isr(12); }
  151. void ICACHE_RAM_ATTR _event_sensor_isr_13() { _event_sensor_isr(13); }
  152. void ICACHE_RAM_ATTR _event_sensor_isr_14() { _event_sensor_isr(14); }
  153. void ICACHE_RAM_ATTR _event_sensor_isr_15() { _event_sensor_isr(15); }
  154. static void (*_event_sensor_isr_list[10])() = {
  155. _event_sensor_isr_0, _event_sensor_isr_1, _event_sensor_isr_2,
  156. _event_sensor_isr_3, _event_sensor_isr_4, _event_sensor_isr_5,
  157. _event_sensor_isr_12, _event_sensor_isr_13, _event_sensor_isr_14,
  158. _event_sensor_isr_15
  159. };
  160. void EventSensor::_attach(unsigned char gpio, unsigned char mode) {
  161. if (!gpioValid(gpio)) return;
  162. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  163. if (_event_sensor_instance[index] == this) return;
  164. if (_event_sensor_instance[index]) detachInterrupt(gpio);
  165. _event_sensor_instance[index] = this;
  166. attachInterrupt(gpio, _event_sensor_isr_list[index], mode);
  167. #if SENSOR_DEBUG
  168. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, this->description().c_str());
  169. #endif
  170. }
  171. void EventSensor::_detach(unsigned char gpio) {
  172. if (!gpioValid(gpio)) return;
  173. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  174. if (_event_sensor_instance[index]) {
  175. detachInterrupt(gpio);
  176. _event_sensor_instance[index] = nullptr;
  177. #if SENSOR_DEBUG
  178. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _event_sensor_instance[index]->description().c_str());
  179. #endif
  180. }
  181. }
  182. #endif // SENSOR_SUPPORT && EVENTS_SUPPORT