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 "../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 description(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. // 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(EVENTS1_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