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.

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