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.

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