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.

266 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 "../debug.h"
  9. #include "BaseSensor.h"
  10. // we are bound by usable GPIOs
  11. #define EVENTS_SENSORS_MAX 10
  12. class EventSensor : public BaseSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. EventSensor() {
  18. _count = 1;
  19. _sensor_id = SENSOR_EVENTS_ID;
  20. }
  21. ~EventSensor() {
  22. _enableInterrupts(false);
  23. }
  24. // ---------------------------------------------------------------------
  25. void setGPIO(unsigned char gpio) {
  26. _gpio = gpio;
  27. }
  28. void setTrigger(bool trigger) {
  29. _trigger = trigger;
  30. }
  31. void setPinMode(unsigned char pin_mode) {
  32. _pin_mode = pin_mode;
  33. }
  34. void setInterruptMode(unsigned char interrupt_mode) {
  35. _interrupt_mode = interrupt_mode;
  36. }
  37. void setDebounceTime(unsigned long ms) {
  38. _debounce = microsecondsToClockCycles(ms * 1000);
  39. }
  40. // ---------------------------------------------------------------------
  41. unsigned char getGPIO() {
  42. return _gpio;
  43. }
  44. bool getTrigger() {
  45. return _trigger;
  46. }
  47. unsigned char getPinMode() {
  48. return _pin_mode;
  49. }
  50. unsigned char getInterruptMode() {
  51. return _interrupt_mode;
  52. }
  53. unsigned long getDebounceTime() {
  54. return _debounce;
  55. }
  56. // ---------------------------------------------------------------------
  57. // Sensors API
  58. // ---------------------------------------------------------------------
  59. // Initialization method, must be idempotent
  60. // Defined outside the class body
  61. void begin() {
  62. pinMode(_gpio, _pin_mode);
  63. _enableInterrupts(true);
  64. _count = _trigger ? 2 : 1;
  65. _ready = true;
  66. }
  67. void tick() {
  68. if (!_trigger || !_callback) return;
  69. if (!_trigger_flag) return;
  70. noInterrupts();
  71. _callback(MAGNITUDE_EVENT, _trigger_value);
  72. _trigger_flag = false;
  73. interrupts();
  74. }
  75. // Descriptive name of the sensor
  76. String description() {
  77. char buffer[20];
  78. snprintf(buffer, sizeof(buffer), "INTERRUPT @ GPIO%d", _gpio);
  79. return String(buffer);
  80. }
  81. // Descriptive name of the slot # index
  82. String slot(unsigned char index) {
  83. return description();
  84. };
  85. // Address of the sensor (it could be the GPIO or I2C address)
  86. String address(unsigned char index) {
  87. return String(_gpio);
  88. }
  89. // Type for slot # index
  90. unsigned char type(unsigned char index) {
  91. if (index == 0) return MAGNITUDE_COUNT;
  92. if (index == 1) return MAGNITUDE_EVENT;
  93. return MAGNITUDE_NONE;
  94. }
  95. // Current value for slot # index
  96. double value(unsigned char index) {
  97. if (index == 0) {
  98. double value = _counter;
  99. _counter = 0;
  100. return value;
  101. };
  102. if (index == 1) {
  103. return _value;
  104. }
  105. return 0;
  106. }
  107. // Handle interrupt calls from isr[GPIO] functions
  108. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  109. UNUSED(gpio);
  110. // clock count in 32bit value, overflowing:
  111. // ~53s when F_CPU is 80MHz
  112. // ~26s when F_CPU is 160MHz
  113. // see: cores/esp8266/Arduino.h definitions
  114. //
  115. // Note:
  116. // To convert to / from normal time values, use:
  117. // - microsecondsToClockCycles(microseconds)
  118. // - clockCyclesToMicroseconds(cycles)
  119. // Since the division operation on this chip is pretty slow,
  120. // avoid doing the conversion here
  121. unsigned long cycles = ESP.getCycleCount();
  122. if (cycles - _last > _debounce) {
  123. _last = cycles;
  124. _counter += 1;
  125. // we are handling callbacks in tick()
  126. if (_trigger) {
  127. _trigger_value = digitalRead(gpio);
  128. _trigger_flag = true;
  129. }
  130. }
  131. }
  132. protected:
  133. // ---------------------------------------------------------------------
  134. // Interrupt management
  135. // ---------------------------------------------------------------------
  136. void _attach(unsigned char gpio, unsigned char mode);
  137. void _detach(unsigned char gpio);
  138. void _enableInterrupts(bool value) {
  139. if (value) {
  140. _detach(_gpio);
  141. _attach(_gpio, _interrupt_mode);
  142. } else {
  143. _detach(_gpio);
  144. }
  145. }
  146. // ---------------------------------------------------------------------
  147. // Protected
  148. // ---------------------------------------------------------------------
  149. volatile unsigned long _counter = 0;
  150. unsigned char _value = 0;
  151. unsigned long _last = 0;
  152. unsigned long _debounce = microsecondsToClockCycles(EVENTS1_DEBOUNCE * 1000);
  153. bool _trigger = false;
  154. bool _trigger_flag = false;
  155. unsigned char _trigger_value = false;
  156. unsigned char _gpio = GPIO_NONE;
  157. unsigned char _pin_mode = INPUT;
  158. unsigned char _interrupt_mode = RISING;
  159. };
  160. // -----------------------------------------------------------------------------
  161. // Interrupt helpers
  162. // -----------------------------------------------------------------------------
  163. EventSensor * _event_sensor_instance[EVENTS_SENSORS_MAX] = {nullptr};
  164. void ICACHE_RAM_ATTR _event_sensor_isr(unsigned char gpio) {
  165. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  166. if (_event_sensor_instance[index]) {
  167. _event_sensor_instance[index]->handleInterrupt(gpio);
  168. }
  169. }
  170. void ICACHE_RAM_ATTR _event_sensor_isr_0() { _event_sensor_isr(0); }
  171. void ICACHE_RAM_ATTR _event_sensor_isr_1() { _event_sensor_isr(1); }
  172. void ICACHE_RAM_ATTR _event_sensor_isr_2() { _event_sensor_isr(2); }
  173. void ICACHE_RAM_ATTR _event_sensor_isr_3() { _event_sensor_isr(3); }
  174. void ICACHE_RAM_ATTR _event_sensor_isr_4() { _event_sensor_isr(4); }
  175. void ICACHE_RAM_ATTR _event_sensor_isr_5() { _event_sensor_isr(5); }
  176. void ICACHE_RAM_ATTR _event_sensor_isr_12() { _event_sensor_isr(12); }
  177. void ICACHE_RAM_ATTR _event_sensor_isr_13() { _event_sensor_isr(13); }
  178. void ICACHE_RAM_ATTR _event_sensor_isr_14() { _event_sensor_isr(14); }
  179. void ICACHE_RAM_ATTR _event_sensor_isr_15() { _event_sensor_isr(15); }
  180. static void (*_event_sensor_isr_list[10])() = {
  181. _event_sensor_isr_0, _event_sensor_isr_1, _event_sensor_isr_2,
  182. _event_sensor_isr_3, _event_sensor_isr_4, _event_sensor_isr_5,
  183. _event_sensor_isr_12, _event_sensor_isr_13, _event_sensor_isr_14,
  184. _event_sensor_isr_15
  185. };
  186. void EventSensor::_attach(unsigned char gpio, unsigned char mode) {
  187. if (!gpioValid(gpio)) return;
  188. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  189. if (_event_sensor_instance[index] == this) return;
  190. if (_event_sensor_instance[index]) detachInterrupt(gpio);
  191. _event_sensor_instance[index] = this;
  192. attachInterrupt(gpio, _event_sensor_isr_list[index], mode);
  193. #if SENSOR_DEBUG
  194. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, this->description().c_str());
  195. #endif
  196. }
  197. void EventSensor::_detach(unsigned char gpio) {
  198. if (!gpioValid(gpio)) return;
  199. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  200. if (_event_sensor_instance[index]) {
  201. detachInterrupt(gpio);
  202. _event_sensor_instance[index] = nullptr;
  203. #if SENSOR_DEBUG
  204. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _event_sensor_instance[index]->description().c_str());
  205. #endif
  206. }
  207. }
  208. #endif // SENSOR_SUPPORT && EVENTS_SUPPORT