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.

239 lines
7.4 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. UNUSED(gpio);
  98. static unsigned long last = 0;
  99. // clock count in 32bit value, overflowing:
  100. // ~53s when F_CPU is 80MHz
  101. // ~26s when F_CPU is 160MHz
  102. // see: cores/esp8266/Arduino.h definitions
  103. unsigned long ms = clockCyclesToMicroseconds(ESP.getCycleCount()) / 1000u;
  104. if (ms - last > _debounce) {
  105. last = ms;
  106. _events = _events + 1;
  107. if (_trigger) {
  108. if (_callback) _callback(MAGNITUDE_EVENT, digitalRead(gpio));
  109. }
  110. }
  111. }
  112. protected:
  113. // ---------------------------------------------------------------------
  114. // Interrupt management
  115. // ---------------------------------------------------------------------
  116. void _attach(unsigned char gpio, unsigned char mode);
  117. void _detach(unsigned char gpio);
  118. void _enableInterrupts(bool value) {
  119. if (value) {
  120. _detach(_gpio);
  121. _attach(_gpio, _interrupt_mode);
  122. } else {
  123. _detach(_gpio);
  124. }
  125. }
  126. // ---------------------------------------------------------------------
  127. // Protected
  128. // ---------------------------------------------------------------------
  129. volatile unsigned long _events = 0;
  130. unsigned long _debounce = EVENTS_DEBOUNCE;
  131. unsigned char _gpio = GPIO_NONE;
  132. bool _trigger = false;
  133. unsigned char _pin_mode = INPUT;
  134. unsigned char _interrupt_mode = RISING;
  135. };
  136. // -----------------------------------------------------------------------------
  137. // Interrupt helpers
  138. // -----------------------------------------------------------------------------
  139. EventSensor * _event_sensor_instance[EVENTS_SENSORS_MAX] = {nullptr};
  140. void ICACHE_RAM_ATTR _event_sensor_isr(unsigned char gpio) {
  141. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  142. if (_event_sensor_instance[index]) {
  143. _event_sensor_instance[index]->handleInterrupt(gpio);
  144. }
  145. }
  146. void ICACHE_RAM_ATTR _event_sensor_isr_0() { _event_sensor_isr(0); }
  147. void ICACHE_RAM_ATTR _event_sensor_isr_1() { _event_sensor_isr(1); }
  148. void ICACHE_RAM_ATTR _event_sensor_isr_2() { _event_sensor_isr(2); }
  149. void ICACHE_RAM_ATTR _event_sensor_isr_3() { _event_sensor_isr(3); }
  150. void ICACHE_RAM_ATTR _event_sensor_isr_4() { _event_sensor_isr(4); }
  151. void ICACHE_RAM_ATTR _event_sensor_isr_5() { _event_sensor_isr(5); }
  152. void ICACHE_RAM_ATTR _event_sensor_isr_12() { _event_sensor_isr(12); }
  153. void ICACHE_RAM_ATTR _event_sensor_isr_13() { _event_sensor_isr(13); }
  154. void ICACHE_RAM_ATTR _event_sensor_isr_14() { _event_sensor_isr(14); }
  155. void ICACHE_RAM_ATTR _event_sensor_isr_15() { _event_sensor_isr(15); }
  156. static void (*_event_sensor_isr_list[10])() = {
  157. _event_sensor_isr_0, _event_sensor_isr_1, _event_sensor_isr_2,
  158. _event_sensor_isr_3, _event_sensor_isr_4, _event_sensor_isr_5,
  159. _event_sensor_isr_12, _event_sensor_isr_13, _event_sensor_isr_14,
  160. _event_sensor_isr_15
  161. };
  162. void EventSensor::_attach(unsigned char gpio, unsigned char mode) {
  163. if (!gpioValid(gpio)) return;
  164. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  165. if (_event_sensor_instance[index] == this) return;
  166. if (_event_sensor_instance[index]) detachInterrupt(gpio);
  167. _event_sensor_instance[index] = this;
  168. attachInterrupt(gpio, _event_sensor_isr_list[index], mode);
  169. #if SENSOR_DEBUG
  170. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, this->description().c_str());
  171. #endif
  172. }
  173. void EventSensor::_detach(unsigned char gpio) {
  174. if (!gpioValid(gpio)) return;
  175. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  176. if (_event_sensor_instance[index]) {
  177. detachInterrupt(gpio);
  178. _event_sensor_instance[index] = nullptr;
  179. #if SENSOR_DEBUG
  180. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _event_sensor_instance[index]->description().c_str());
  181. #endif
  182. }
  183. }
  184. #endif // SENSOR_SUPPORT && EVENTS_SUPPORT