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