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.

243 lines
7.7 KiB

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