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.

264 lines
8.2 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 ms) {
  37. _debounce = microsecondsToClockCycles(ms * 1000);
  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. void tick() {
  67. if (!_trigger || !_callback) return;
  68. if (!_trigger_flag) return;
  69. noInterrupts();
  70. _callback(MAGNITUDE_EVENT, _trigger_value);
  71. _trigger_flag = false;
  72. interrupts();
  73. }
  74. // Descriptive name of the sensor
  75. String description() {
  76. char buffer[20];
  77. snprintf(buffer, sizeof(buffer), "INTERRUPT @ GPIO%d", _gpio);
  78. return String(buffer);
  79. }
  80. // Descriptive name of the slot # index
  81. String slot(unsigned char index) {
  82. return description();
  83. };
  84. // Address of the sensor (it could be the GPIO or I2C address)
  85. String address(unsigned char index) {
  86. return String(_gpio);
  87. }
  88. // Type for slot # index
  89. unsigned char type(unsigned char index) {
  90. if (index == 0) return MAGNITUDE_COUNT;
  91. if (index == 1) return MAGNITUDE_EVENT;
  92. return MAGNITUDE_NONE;
  93. }
  94. // Current value for slot # index
  95. double value(unsigned char index) {
  96. if (index == 0) {
  97. double value = _counter;
  98. _counter = 0;
  99. return value;
  100. };
  101. if (index == 1) {
  102. return _value;
  103. }
  104. return 0;
  105. }
  106. // Handle interrupt calls from isr[GPIO] functions
  107. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  108. UNUSED(gpio);
  109. // clock count in 32bit value, overflowing:
  110. // ~53s when F_CPU is 80MHz
  111. // ~26s when F_CPU is 160MHz
  112. // see: cores/esp8266/Arduino.h definitions
  113. //
  114. // Note:
  115. // To convert to / from normal time values, use:
  116. // - microsecondsToClockCycles(microseconds)
  117. // - clockCyclesToMicroseconds(cycles)
  118. // Since the division operation on this chip is pretty slow,
  119. // avoid doing the conversion here
  120. unsigned long cycles = ESP.getCycleCount();
  121. if (cycles - _last > _debounce) {
  122. _last = cycles;
  123. _counter += 1;
  124. // we are handling callbacks in tick()
  125. if (_trigger) {
  126. _trigger_value = digitalRead(gpio);
  127. _trigger_flag = true;
  128. }
  129. }
  130. }
  131. protected:
  132. // ---------------------------------------------------------------------
  133. // Interrupt management
  134. // ---------------------------------------------------------------------
  135. void _attach(unsigned char gpio, unsigned char mode);
  136. void _detach(unsigned char gpio);
  137. void _enableInterrupts(bool value) {
  138. if (value) {
  139. _detach(_gpio);
  140. _attach(_gpio, _interrupt_mode);
  141. } else {
  142. _detach(_gpio);
  143. }
  144. }
  145. // ---------------------------------------------------------------------
  146. // Protected
  147. // ---------------------------------------------------------------------
  148. volatile unsigned long _counter = 0;
  149. unsigned char _value = 0;
  150. unsigned long _last = 0;
  151. unsigned long _debounce = microsecondsToClockCycles(EVENTS_DEBOUNCE * 1000);
  152. bool _trigger = false;
  153. bool _trigger_flag = false;
  154. unsigned char _trigger_value = false;
  155. unsigned char _gpio = GPIO_NONE;
  156. unsigned char _pin_mode = INPUT;
  157. unsigned char _interrupt_mode = RISING;
  158. };
  159. // -----------------------------------------------------------------------------
  160. // Interrupt helpers
  161. // -----------------------------------------------------------------------------
  162. EventSensor * _event_sensor_instance[EVENTS_SENSORS_MAX] = {nullptr};
  163. void ICACHE_RAM_ATTR _event_sensor_isr(unsigned char gpio) {
  164. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  165. if (_event_sensor_instance[index]) {
  166. _event_sensor_instance[index]->handleInterrupt(gpio);
  167. }
  168. }
  169. void ICACHE_RAM_ATTR _event_sensor_isr_0() { _event_sensor_isr(0); }
  170. void ICACHE_RAM_ATTR _event_sensor_isr_1() { _event_sensor_isr(1); }
  171. void ICACHE_RAM_ATTR _event_sensor_isr_2() { _event_sensor_isr(2); }
  172. void ICACHE_RAM_ATTR _event_sensor_isr_3() { _event_sensor_isr(3); }
  173. void ICACHE_RAM_ATTR _event_sensor_isr_4() { _event_sensor_isr(4); }
  174. void ICACHE_RAM_ATTR _event_sensor_isr_5() { _event_sensor_isr(5); }
  175. void ICACHE_RAM_ATTR _event_sensor_isr_12() { _event_sensor_isr(12); }
  176. void ICACHE_RAM_ATTR _event_sensor_isr_13() { _event_sensor_isr(13); }
  177. void ICACHE_RAM_ATTR _event_sensor_isr_14() { _event_sensor_isr(14); }
  178. void ICACHE_RAM_ATTR _event_sensor_isr_15() { _event_sensor_isr(15); }
  179. static void (*_event_sensor_isr_list[10])() = {
  180. _event_sensor_isr_0, _event_sensor_isr_1, _event_sensor_isr_2,
  181. _event_sensor_isr_3, _event_sensor_isr_4, _event_sensor_isr_5,
  182. _event_sensor_isr_12, _event_sensor_isr_13, _event_sensor_isr_14,
  183. _event_sensor_isr_15
  184. };
  185. void EventSensor::_attach(unsigned char gpio, unsigned char mode) {
  186. if (!gpioValid(gpio)) return;
  187. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  188. if (_event_sensor_instance[index] == this) return;
  189. if (_event_sensor_instance[index]) detachInterrupt(gpio);
  190. _event_sensor_instance[index] = this;
  191. attachInterrupt(gpio, _event_sensor_isr_list[index], mode);
  192. #if SENSOR_DEBUG
  193. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, this->description().c_str());
  194. #endif
  195. }
  196. void EventSensor::_detach(unsigned char gpio) {
  197. if (!gpioValid(gpio)) return;
  198. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  199. if (_event_sensor_instance[index]) {
  200. detachInterrupt(gpio);
  201. _event_sensor_instance[index] = nullptr;
  202. #if SENSOR_DEBUG
  203. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _event_sensor_instance[index]->description().c_str());
  204. #endif
  205. }
  206. }
  207. #endif // SENSOR_SUPPORT && EVENTS_SUPPORT