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.

219 lines
7.1 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. // ---------------------------------------------------------------------
  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[24];
  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. unsigned long lapse = millis() - _previous_time;
  66. _previous_time = millis();
  67. unsigned long pulses = _pulses - _previous_pulses;
  68. _previous_pulses = _pulses;
  69. unsigned long _energy_delta = 1000 * 3600 * pulses / _ratio;
  70. _energy += _energy_delta;
  71. if (lapse > 0) _active = 1000 * _energy_delta / lapse;
  72. }
  73. // Type for slot # index
  74. unsigned char type(unsigned char index) {
  75. if (index == 0) return MAGNITUDE_POWER_ACTIVE;
  76. if (index == 1) return MAGNITUDE_ENERGY;
  77. return MAGNITUDE_NONE;
  78. }
  79. // Current value for slot # index
  80. double value(unsigned char index) {
  81. if (index == 0) return _active;
  82. if (index == 1) return _energy;
  83. return 0;
  84. }
  85. // Handle interrupt calls
  86. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  87. if (gpio == _gpio) {
  88. _pulses++;
  89. }
  90. }
  91. protected:
  92. // ---------------------------------------------------------------------
  93. // Interrupt management
  94. // ---------------------------------------------------------------------
  95. void _attach(PulseMeterSensor * instance, unsigned char gpio, unsigned char mode);
  96. void _detach(unsigned char gpio);
  97. void _enableInterrupts(bool value) {
  98. if (value) {
  99. if (_gpio != _previous) {
  100. if (_previous != GPIO_NONE) _detach(_previous);
  101. _attach(this, _gpio, PULSEMETER_INTERRUPT_ON);
  102. _previous = _gpio;
  103. }
  104. } else {
  105. _detach(_previous);
  106. _previous = GPIO_NONE;
  107. }
  108. }
  109. // ---------------------------------------------------------------------
  110. unsigned char _previous = GPIO_NONE;
  111. unsigned char _gpio = GPIO_NONE;
  112. unsigned long _ratio = PULSEMETER_ENERGY_RATIO;
  113. double _active = 0;
  114. double _energy = 0;
  115. volatile unsigned long _pulses = 0;
  116. unsigned long _previous_pulses = 0;
  117. unsigned long _previous_time = 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