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.

228 lines
7.2 KiB

  1. // -----------------------------------------------------------------------------
  2. // Event Counter Sensor
  3. // Copyright (C) 2017-2018 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 "BaseSensor.h"
  9. class EventSensor : public BaseSensor {
  10. public:
  11. // ---------------------------------------------------------------------
  12. // Public
  13. // ---------------------------------------------------------------------
  14. EventSensor(): BaseSensor() {
  15. _count = 1;
  16. _sensor_id = SENSOR_EVENTS_ID;
  17. }
  18. ~EventSensor() {
  19. _enableInterrupts(false);
  20. }
  21. // ---------------------------------------------------------------------
  22. void setGPIO(unsigned char gpio) {
  23. _gpio = gpio;
  24. }
  25. void setTrigger(bool trigger) {
  26. _trigger = trigger;
  27. }
  28. void setPinMode(unsigned char pin_mode) {
  29. _pin_mode = pin_mode;
  30. }
  31. void setInterruptMode(unsigned char interrupt_mode) {
  32. _interrupt_mode = interrupt_mode;
  33. }
  34. void setDebounceTime(unsigned long debounce) {
  35. _debounce = debounce;
  36. }
  37. // ---------------------------------------------------------------------
  38. unsigned char getGPIO() {
  39. return _gpio;
  40. }
  41. bool getTrigger() {
  42. return _trigger;
  43. }
  44. unsigned char getPinMode() {
  45. return _pin_mode;
  46. }
  47. unsigned char getInterruptMode() {
  48. return _interrupt_mode;
  49. }
  50. unsigned long getDebounceTime() {
  51. return _debounce;
  52. }
  53. // ---------------------------------------------------------------------
  54. // Sensors API
  55. // ---------------------------------------------------------------------
  56. // Initialization method, must be idempotent
  57. // Defined outside the class body
  58. void begin() {
  59. pinMode(_gpio, _pin_mode);
  60. _enableInterrupts(true);
  61. _count = _trigger ? 2 : 1;
  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 slot(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. double value = _events;
  88. _events = 0;
  89. return value;
  90. };
  91. return 0;
  92. }
  93. // Handle interrupt calls
  94. void handleInterrupt(unsigned char gpio) {
  95. (void) gpio;
  96. static unsigned long last = 0;
  97. if (millis() - last > _debounce) {
  98. last = millis();
  99. _events = _events + 1;
  100. if (_trigger) {
  101. if (_callback) _callback(MAGNITUDE_EVENT, digitalRead(gpio));
  102. }
  103. }
  104. }
  105. protected:
  106. // ---------------------------------------------------------------------
  107. // Interrupt management
  108. // ---------------------------------------------------------------------
  109. void _attach(EventSensor * instance, unsigned char gpio, unsigned char mode);
  110. void _detach(unsigned char gpio);
  111. void _enableInterrupts(bool value) {
  112. static unsigned char _interrupt_gpio = GPIO_NONE;
  113. if (value) {
  114. if (_interrupt_gpio != GPIO_NONE) _detach(_interrupt_gpio);
  115. _attach(this, _gpio, _interrupt_mode);
  116. _interrupt_gpio = _gpio;
  117. } else if (_interrupt_gpio != GPIO_NONE) {
  118. _detach(_interrupt_gpio);
  119. _interrupt_gpio = GPIO_NONE;
  120. }
  121. }
  122. // ---------------------------------------------------------------------
  123. // Protected
  124. // ---------------------------------------------------------------------
  125. volatile unsigned long _events = 0;
  126. unsigned long _debounce = EVENTS_DEBOUNCE;
  127. unsigned char _gpio = GPIO_NONE;
  128. bool _trigger = false;
  129. unsigned char _pin_mode = INPUT;
  130. unsigned char _interrupt_mode = RISING;
  131. };
  132. // -----------------------------------------------------------------------------
  133. // Interrupt helpers
  134. // -----------------------------------------------------------------------------
  135. EventSensor * _event_sensor_instance[10] = {NULL};
  136. void ICACHE_RAM_ATTR _event_sensor_isr(unsigned char gpio) {
  137. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  138. if (_event_sensor_instance[index]) {
  139. _event_sensor_instance[index]->handleInterrupt(gpio);
  140. }
  141. }
  142. void ICACHE_RAM_ATTR _event_sensor_isr_0() { _event_sensor_isr(0); }
  143. void ICACHE_RAM_ATTR _event_sensor_isr_1() { _event_sensor_isr(1); }
  144. void ICACHE_RAM_ATTR _event_sensor_isr_2() { _event_sensor_isr(2); }
  145. void ICACHE_RAM_ATTR _event_sensor_isr_3() { _event_sensor_isr(3); }
  146. void ICACHE_RAM_ATTR _event_sensor_isr_4() { _event_sensor_isr(4); }
  147. void ICACHE_RAM_ATTR _event_sensor_isr_5() { _event_sensor_isr(5); }
  148. void ICACHE_RAM_ATTR _event_sensor_isr_12() { _event_sensor_isr(12); }
  149. void ICACHE_RAM_ATTR _event_sensor_isr_13() { _event_sensor_isr(13); }
  150. void ICACHE_RAM_ATTR _event_sensor_isr_14() { _event_sensor_isr(14); }
  151. void ICACHE_RAM_ATTR _event_sensor_isr_15() { _event_sensor_isr(15); }
  152. static void (*_event_sensor_isr_list[10])() = {
  153. _event_sensor_isr_0, _event_sensor_isr_1, _event_sensor_isr_2,
  154. _event_sensor_isr_3, _event_sensor_isr_4, _event_sensor_isr_5,
  155. _event_sensor_isr_12, _event_sensor_isr_13, _event_sensor_isr_14,
  156. _event_sensor_isr_15
  157. };
  158. void EventSensor::_attach(EventSensor * instance, unsigned char gpio, unsigned char mode) {
  159. if (!gpioValid(gpio)) return;
  160. _detach(gpio);
  161. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  162. _event_sensor_instance[index] = instance;
  163. attachInterrupt(gpio, _event_sensor_isr_list[index], mode);
  164. #if SENSOR_DEBUG
  165. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, instance->description().c_str());
  166. #endif
  167. }
  168. void EventSensor::_detach(unsigned char gpio) {
  169. if (!gpioValid(gpio)) return;
  170. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  171. if (_event_sensor_instance[index]) {
  172. detachInterrupt(gpio);
  173. #if SENSOR_DEBUG
  174. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _event_sensor_instance[index]->description().c_str());
  175. #endif
  176. _event_sensor_instance[index] = NULL;
  177. }
  178. }
  179. #endif // SENSOR_SUPPORT && EVENTS_SUPPORT