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.

211 lines
6.7 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 setMode(unsigned char mode) {
  26. _mode = mode;
  27. }
  28. void setInterruptMode(unsigned char mode) {
  29. _interrupt_mode = mode;
  30. }
  31. void setDebounceTime(unsigned long debounce) {
  32. _debounce = debounce;
  33. }
  34. // ---------------------------------------------------------------------
  35. unsigned char getGPIO() {
  36. return _gpio;
  37. }
  38. unsigned char getMode() {
  39. return _mode;
  40. }
  41. unsigned char getInterruptMode() {
  42. return _interrupt_mode;
  43. }
  44. unsigned long getDebounceTime() {
  45. return _debounce;
  46. }
  47. // ---------------------------------------------------------------------
  48. // Sensors API
  49. // ---------------------------------------------------------------------
  50. // Initialization method, must be idempotent
  51. // Defined outside the class body
  52. void begin() {
  53. pinMode(_gpio, _mode);
  54. _enableInterrupts(true);
  55. _ready = true;
  56. }
  57. // Descriptive name of the sensor
  58. String description() {
  59. char buffer[20];
  60. snprintf(buffer, sizeof(buffer), "INTERRUPT @ GPIO%d", _gpio);
  61. return String(buffer);
  62. }
  63. // Descriptive name of the slot # index
  64. String slot(unsigned char index) {
  65. return description();
  66. };
  67. // Address of the sensor (it could be the GPIO or I2C address)
  68. String address(unsigned char index) {
  69. return String(_gpio);
  70. }
  71. // Type for slot # index
  72. unsigned char type(unsigned char index) {
  73. if (index == 0) return MAGNITUDE_EVENTS;
  74. return MAGNITUDE_NONE;
  75. }
  76. // Current value for slot # index
  77. double value(unsigned char index) {
  78. if (index == 0) {
  79. double value = _events;
  80. _events = 0;
  81. return value;
  82. };
  83. return 0;
  84. }
  85. // Handle interrupt calls
  86. void handleInterrupt(unsigned char gpio) {
  87. (void) gpio;
  88. static unsigned long last = 0;
  89. if (millis() - last > _debounce) {
  90. _events = _events + 1;
  91. last = millis();
  92. }
  93. }
  94. protected:
  95. // ---------------------------------------------------------------------
  96. // Interrupt management
  97. // ---------------------------------------------------------------------
  98. void _attach(EventSensor * instance, unsigned char gpio, unsigned char mode);
  99. void _detach(unsigned char gpio);
  100. void _enableInterrupts(bool value) {
  101. static unsigned char _interrupt_gpio = GPIO_NONE;
  102. if (value) {
  103. if (_interrupt_gpio != GPIO_NONE) _detach(_interrupt_gpio);
  104. _attach(this, _gpio, _interrupt_mode);
  105. _interrupt_gpio = _gpio;
  106. } else if (_interrupt_gpio != GPIO_NONE) {
  107. _detach(_interrupt_gpio);
  108. _interrupt_gpio = GPIO_NONE;
  109. }
  110. }
  111. // ---------------------------------------------------------------------
  112. // Protected
  113. // ---------------------------------------------------------------------
  114. volatile unsigned long _events = 0;
  115. unsigned long _debounce = EVENTS_DEBOUNCE;
  116. unsigned char _gpio;
  117. unsigned char _mode;
  118. unsigned char _interrupt_mode;
  119. };
  120. // -----------------------------------------------------------------------------
  121. // Interrupt helpers
  122. // -----------------------------------------------------------------------------
  123. EventSensor * _event_sensor_instance[10] = {NULL};
  124. void ICACHE_RAM_ATTR _event_sensor_isr(unsigned char gpio) {
  125. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  126. if (_event_sensor_instance[index]) {
  127. _event_sensor_instance[index]->handleInterrupt(gpio);
  128. }
  129. }
  130. void ICACHE_RAM_ATTR _event_sensor_isr_0() { _event_sensor_isr(0); }
  131. void ICACHE_RAM_ATTR _event_sensor_isr_1() { _event_sensor_isr(1); }
  132. void ICACHE_RAM_ATTR _event_sensor_isr_2() { _event_sensor_isr(2); }
  133. void ICACHE_RAM_ATTR _event_sensor_isr_3() { _event_sensor_isr(3); }
  134. void ICACHE_RAM_ATTR _event_sensor_isr_4() { _event_sensor_isr(4); }
  135. void ICACHE_RAM_ATTR _event_sensor_isr_5() { _event_sensor_isr(5); }
  136. void ICACHE_RAM_ATTR _event_sensor_isr_12() { _event_sensor_isr(12); }
  137. void ICACHE_RAM_ATTR _event_sensor_isr_13() { _event_sensor_isr(13); }
  138. void ICACHE_RAM_ATTR _event_sensor_isr_14() { _event_sensor_isr(14); }
  139. void ICACHE_RAM_ATTR _event_sensor_isr_15() { _event_sensor_isr(15); }
  140. static void (*_event_sensor_isr_list[10])() = {
  141. _event_sensor_isr_0, _event_sensor_isr_1, _event_sensor_isr_2,
  142. _event_sensor_isr_3, _event_sensor_isr_4, _event_sensor_isr_5,
  143. _event_sensor_isr_12, _event_sensor_isr_13, _event_sensor_isr_14,
  144. _event_sensor_isr_15
  145. };
  146. void EventSensor::_attach(EventSensor * instance, unsigned char gpio, unsigned char mode) {
  147. if (!gpioValid(gpio)) return;
  148. _detach(gpio);
  149. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  150. _event_sensor_instance[index] = instance;
  151. attachInterrupt(gpio, _event_sensor_isr_list[index], mode);
  152. #if SENSOR_DEBUG
  153. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, instance->description().c_str());
  154. #endif
  155. }
  156. void EventSensor::_detach(unsigned char gpio) {
  157. if (!gpioValid(gpio)) return;
  158. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  159. if (_event_sensor_instance[index]) {
  160. detachInterrupt(gpio);
  161. #if SENSOR_DEBUG
  162. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _event_sensor_instance[index]->description().c_str());
  163. #endif
  164. _event_sensor_instance[index] = NULL;
  165. }
  166. }
  167. #endif // SENSOR_SUPPORT && EVENTS_SUPPORT