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.

231 lines
7.4 KiB

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