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.

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