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.

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