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.

220 lines
7.1 KiB

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