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.

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