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.

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