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.

241 lines
7.7 KiB

5 years ago
5 years ago
  1. // -----------------------------------------------------------------------------
  2. // Pulse Meter Power Monitor Sensor
  3. // Copyright (C) 2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && PULSEMETER_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. class PulseMeterSensor : public BaseSensor {
  10. public:
  11. // ---------------------------------------------------------------------
  12. // Public
  13. // ---------------------------------------------------------------------
  14. PulseMeterSensor(): BaseSensor() {
  15. _count = 2;
  16. _sensor_id = SENSOR_PULSEMETER_ID;
  17. }
  18. ~PulseMeterSensor() {
  19. _enableInterrupts(false);
  20. }
  21. void resetEnergy(double value = 0) {
  22. _energy = value;
  23. }
  24. // ---------------------------------------------------------------------
  25. void setGPIO(unsigned char gpio) {
  26. if (_gpio == gpio) return;
  27. _gpio = gpio;
  28. _dirty = true;
  29. }
  30. void setEnergyRatio(unsigned long ratio) {
  31. if (ratio > 0) _ratio = ratio;
  32. }
  33. void setInterruptMode(unsigned char interrupt_mode) {
  34. _interrupt_mode = interrupt_mode;
  35. }
  36. void setDebounceTime(unsigned long debounce) {
  37. _debounce = debounce;
  38. }
  39. // ---------------------------------------------------------------------
  40. unsigned char getGPIO() {
  41. return _gpio;
  42. }
  43. unsigned long getEnergyRatio() {
  44. return _ratio;
  45. }
  46. unsigned char getInterruptMode() {
  47. return _interrupt_mode;
  48. }
  49. unsigned long getDebounceTime() {
  50. return _debounce;
  51. }
  52. // ---------------------------------------------------------------------
  53. // Sensors API
  54. // ---------------------------------------------------------------------
  55. // Initialization method, must be idempotent
  56. // Defined outside the class body
  57. void begin() {
  58. _enableInterrupts(true);
  59. _ready = true;
  60. }
  61. // Descriptive name of the sensor
  62. String description() {
  63. char buffer[24];
  64. snprintf(buffer, sizeof(buffer), "PulseMeter @ GPIO(%u)", _gpio);
  65. return String(buffer);
  66. }
  67. // Descriptive name of the slot # index
  68. String slot(unsigned char index) {
  69. return description();
  70. };
  71. // Address of the sensor (it could be the GPIO or I2C address)
  72. String address(unsigned char index) {
  73. return String(_gpio);
  74. }
  75. // Pre-read hook (usually to populate registers with up-to-date data)
  76. void pre() {
  77. unsigned long lapse = millis() - _previous_time;
  78. _previous_time = millis();
  79. unsigned long pulses = _pulses - _previous_pulses;
  80. _previous_pulses = _pulses;
  81. unsigned long _energy_delta = 1000 * 3600 * pulses / _ratio;
  82. _energy += _energy_delta;
  83. if (lapse > 0) _active = 1000 * _energy_delta / lapse;
  84. }
  85. // Type for slot # index
  86. unsigned char type(unsigned char index) {
  87. if (index == 0) return MAGNITUDE_POWER_ACTIVE;
  88. if (index == 1) return MAGNITUDE_ENERGY;
  89. return MAGNITUDE_NONE;
  90. }
  91. // Current value for slot # index
  92. double value(unsigned char index) {
  93. if (index == 0) return _active;
  94. if (index == 1) return _energy;
  95. return 0;
  96. }
  97. // Handle interrupt calls
  98. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  99. static unsigned long last = 0;
  100. if (millis() - last > _debounce) {
  101. last = millis();
  102. _pulses++;
  103. }
  104. }
  105. protected:
  106. // ---------------------------------------------------------------------
  107. // Interrupt management
  108. // ---------------------------------------------------------------------
  109. void _attach(PulseMeterSensor * instance, unsigned char gpio, unsigned char mode);
  110. void _detach(unsigned char gpio);
  111. void _enableInterrupts(bool value) {
  112. if (value) {
  113. if (_gpio != _previous) {
  114. if (_previous != GPIO_NONE) _detach(_previous);
  115. _attach(this, _gpio, _interrupt_mode);
  116. _previous = _gpio;
  117. }
  118. } else {
  119. _detach(_previous);
  120. _previous = GPIO_NONE;
  121. }
  122. }
  123. // ---------------------------------------------------------------------
  124. unsigned char _previous = GPIO_NONE;
  125. unsigned char _gpio = GPIO_NONE;
  126. unsigned long _ratio = PULSEMETER_ENERGY_RATIO;
  127. unsigned long _debounce = PULSEMETER_DEBOUNCE;
  128. double _active = 0;
  129. double _energy = 0;
  130. volatile unsigned long _pulses = 0;
  131. unsigned long _previous_pulses = 0;
  132. unsigned long _previous_time = 0;
  133. unsigned char _interrupt_mode = FALLING;
  134. };
  135. // -----------------------------------------------------------------------------
  136. // Interrupt helpers
  137. // -----------------------------------------------------------------------------
  138. PulseMeterSensor * _pulsemeter_sensor_instance[10] = {NULL};
  139. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr(unsigned char gpio) {
  140. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  141. if (_pulsemeter_sensor_instance[index]) {
  142. _pulsemeter_sensor_instance[index]->handleInterrupt(gpio);
  143. }
  144. }
  145. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr_0() { _pulsemeter_sensor_isr(0); }
  146. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr_1() { _pulsemeter_sensor_isr(1); }
  147. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr_2() { _pulsemeter_sensor_isr(2); }
  148. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr_3() { _pulsemeter_sensor_isr(3); }
  149. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr_4() { _pulsemeter_sensor_isr(4); }
  150. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr_5() { _pulsemeter_sensor_isr(5); }
  151. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr_12() { _pulsemeter_sensor_isr(12); }
  152. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr_13() { _pulsemeter_sensor_isr(13); }
  153. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr_14() { _pulsemeter_sensor_isr(14); }
  154. void ICACHE_RAM_ATTR _pulsemeter_sensor_isr_15() { _pulsemeter_sensor_isr(15); }
  155. static void (*_pulsemeter_sensor_isr_list[10])() = {
  156. _pulsemeter_sensor_isr_0, _pulsemeter_sensor_isr_1, _pulsemeter_sensor_isr_2,
  157. _pulsemeter_sensor_isr_3, _pulsemeter_sensor_isr_4, _pulsemeter_sensor_isr_5,
  158. _pulsemeter_sensor_isr_12, _pulsemeter_sensor_isr_13, _pulsemeter_sensor_isr_14,
  159. _pulsemeter_sensor_isr_15
  160. };
  161. void PulseMeterSensor::_attach(PulseMeterSensor * instance, unsigned char gpio, unsigned char mode) {
  162. if (!gpioValid(gpio)) return;
  163. _detach(gpio);
  164. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  165. _pulsemeter_sensor_instance[index] = instance;
  166. attachInterrupt(gpio, _pulsemeter_sensor_isr_list[index], mode);
  167. #if SENSOR_DEBUG
  168. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%u interrupt attached to %s\n"), gpio, instance->description().c_str());
  169. #endif
  170. }
  171. void PulseMeterSensor::_detach(unsigned char gpio) {
  172. if (!gpioValid(gpio)) return;
  173. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  174. if (_pulsemeter_sensor_instance[index]) {
  175. detachInterrupt(gpio);
  176. #if SENSOR_DEBUG
  177. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%u interrupt detached from %s\n"), gpio, _pulsemeter_sensor_instance[index]->description().c_str());
  178. #endif
  179. _pulsemeter_sensor_instance[index] = NULL;
  180. }
  181. }
  182. #endif // SENSOR_SUPPORT && PULSEMETER_SUPPORT