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.

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