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.

333 lines
11 KiB

7 years ago
  1. // -----------------------------------------------------------------------------
  2. // Event Counter Sensor
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && HLW8012_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. #include <HLW8012.h>
  10. class HLW8012Sensor : public BaseSensor {
  11. public:
  12. // ---------------------------------------------------------------------
  13. // Public
  14. // ---------------------------------------------------------------------
  15. HLW8012Sensor(): BaseSensor() {
  16. _count = 7;
  17. _sensor_id = SENSOR_HLW8012_ID;
  18. _hlw8012 = new HLW8012();
  19. }
  20. ~HLW8012Sensor() {
  21. _enableInterrupts(false);
  22. delete _hlw8012;
  23. }
  24. void expectedCurrent(double expected) {
  25. _hlw8012->expectedCurrent(expected);
  26. }
  27. void expectedVoltage(unsigned int expected) {
  28. _hlw8012->expectedVoltage(expected);
  29. }
  30. void expectedPower(unsigned int expected) {
  31. _hlw8012->expectedActivePower(expected);
  32. }
  33. void resetRatios() {
  34. _hlw8012->resetMultipliers();
  35. }
  36. void resetEnergy(double value = 0) {
  37. _energy_offset = value;
  38. _hlw8012->resetEnergy();
  39. }
  40. // ---------------------------------------------------------------------
  41. void setSEL(unsigned char sel) {
  42. if (_sel == sel) return;
  43. _sel = sel;
  44. _dirty = true;
  45. }
  46. void setCF(unsigned char cf) {
  47. if (_cf == cf) return;
  48. _cf = cf;
  49. _dirty = true;
  50. }
  51. void setCF1(unsigned char cf1) {
  52. if (_cf1 == cf1) return;
  53. _cf1 = cf1;
  54. _dirty = true;
  55. }
  56. void setSELCurrent(bool value) {
  57. _sel_current = value;
  58. }
  59. void setCurrentRatio(double value) {
  60. _hlw8012->setCurrentMultiplier(value);
  61. };
  62. void setVoltageRatio(double value) {
  63. _hlw8012->setVoltageMultiplier(value);
  64. };
  65. void setPowerRatio(double value) {
  66. _hlw8012->setPowerMultiplier(value);
  67. };
  68. // ---------------------------------------------------------------------
  69. unsigned char getSEL() {
  70. return _sel;
  71. }
  72. unsigned char getCF() {
  73. return _cf;
  74. }
  75. unsigned char getCF1() {
  76. return _cf1;
  77. }
  78. unsigned char getSELCurrent() {
  79. return _sel_current;
  80. }
  81. double getCurrentRatio() {
  82. return _hlw8012->getCurrentMultiplier();
  83. };
  84. double getVoltageRatio() {
  85. return _hlw8012->getVoltageMultiplier();
  86. };
  87. double getPowerRatio() {
  88. return _hlw8012->getPowerMultiplier();
  89. };
  90. // ---------------------------------------------------------------------
  91. // Sensors API
  92. // ---------------------------------------------------------------------
  93. // Initialization method, must be idempotent
  94. // Defined outside the class body
  95. void begin() {
  96. // Initialize HLW8012
  97. // void begin(unsigned char cf_pin, unsigned char cf1_pin, unsigned char sel_pin, unsigned char currentWhen = HIGH, bool use_interrupts = false, unsigned long pulse_timeout = PULSE_TIMEOUT);
  98. // * cf_pin, cf1_pin and sel_pin are GPIOs to the HLW8012 IC
  99. // * currentWhen is the value in sel_pin to select current sampling
  100. // * set use_interrupts to true to use interrupts to monitor pulse widths
  101. // * leave pulse_timeout to the default value, recommended when using interrupts
  102. #if HLW8012_USE_INTERRUPTS
  103. _hlw8012->begin(_cf, _cf1, _sel, _sel_current, true);
  104. #else
  105. _hlw8012->begin(_cf, _cf1, _sel, _sel_current, false, 1000000);
  106. #endif
  107. // These values are used to calculate current, voltage and power factors as per datasheet formula
  108. // These are the nominal values for the Sonoff POW resistors:
  109. // * The CURRENT_RESISTOR is the 1milliOhm copper-manganese resistor in series with the main line
  110. // * The VOLTAGE_RESISTOR_UPSTREAM are the 5 470kOhm resistors in the voltage divider that feeds the V2P pin in the HLW8012
  111. // * The VOLTAGE_RESISTOR_DOWNSTREAM is the 1kOhm resistor in the voltage divider that feeds the V2P pin in the HLW8012
  112. _hlw8012->setResistors(HLW8012_CURRENT_R, HLW8012_VOLTAGE_R_UP, HLW8012_VOLTAGE_R_DOWN);
  113. // Handle interrupts
  114. #if HLW8012_USE_INTERRUPTS
  115. #if HLW8012_WAIT_FOR_WIFI == 0
  116. _enableInterrupts(false);
  117. _enableInterrupts(true);
  118. #endif
  119. #endif
  120. _ready = true;
  121. }
  122. // Descriptive name of the sensor
  123. String description() {
  124. char buffer[25];
  125. snprintf(buffer, sizeof(buffer), "HLW8012 @ GPIO(%u,%u,%u)", _sel, _cf, _cf1);
  126. return String(buffer);
  127. }
  128. // Descriptive name of the slot # index
  129. String slot(unsigned char index) {
  130. return description();
  131. };
  132. // Address of the sensor (it could be the GPIO or I2C address)
  133. String address(unsigned char index) {
  134. char buffer[10];
  135. snprintf(buffer, sizeof(buffer), "%u:%u:%u", _sel, _cf, _cf1);
  136. return String(buffer);
  137. }
  138. // Type for slot # index
  139. unsigned char type(unsigned char index) {
  140. if (index == 0) return MAGNITUDE_CURRENT;
  141. if (index == 1) return MAGNITUDE_VOLTAGE;
  142. if (index == 2) return MAGNITUDE_POWER_ACTIVE;
  143. if (index == 3) return MAGNITUDE_POWER_REACTIVE;
  144. if (index == 4) return MAGNITUDE_POWER_APPARENT;
  145. if (index == 5) return MAGNITUDE_POWER_FACTOR;
  146. if (index == 6) return MAGNITUDE_ENERGY;
  147. return MAGNITUDE_NONE;
  148. }
  149. // Current value for slot # index
  150. double value(unsigned char index) {
  151. if (index == 0) return _hlw8012->getCurrent();
  152. if (index == 1) return _hlw8012->getVoltage();
  153. if (index == 2) return _hlw8012->getActivePower();
  154. if (index == 3) return _hlw8012->getReactivePower();
  155. if (index == 4) return _hlw8012->getApparentPower();
  156. if (index == 5) return 100 * _hlw8012->getPowerFactor();
  157. if (index == 6) return (_energy_offset + _hlw8012->getEnergy());
  158. return 0;
  159. }
  160. // Pre-read hook (usually to populate registers with up-to-date data)
  161. #if HLW8012_USE_INTERRUPTS
  162. #if HLW8012_WAIT_FOR_WIFI
  163. void pre() {
  164. _enableInterrupts(wifiConnected());
  165. }
  166. #endif
  167. #endif
  168. // Toggle between current and voltage monitoring
  169. #if HLW8012_USE_INTERRUPTS == 0
  170. // Post-read hook (usually to reset things)
  171. void post() { _hlw8012->toggleMode(); }
  172. #endif // HLW8012_USE_INTERRUPTS == 0
  173. // Handle interrupt calls
  174. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  175. if (gpio == _cf) _hlw8012->cf_interrupt();
  176. if (gpio == _cf1) _hlw8012->cf1_interrupt();
  177. }
  178. protected:
  179. // ---------------------------------------------------------------------
  180. // Interrupt management
  181. // ---------------------------------------------------------------------
  182. void _attach(HLW8012Sensor * instance, unsigned char gpio, unsigned char mode);
  183. void _detach(unsigned char gpio);
  184. void _enableInterrupts(bool value) {
  185. static unsigned char _interrupt_cf = GPIO_NONE;
  186. static unsigned char _interrupt_cf1 = GPIO_NONE;
  187. if (value) {
  188. if (_interrupt_cf != _cf) {
  189. if (_interrupt_cf != GPIO_NONE) _detach(_interrupt_cf);
  190. _attach(this, _cf, HLW8012_INTERRUPT_ON);
  191. _interrupt_cf = _cf;
  192. }
  193. if (_interrupt_cf1 != _cf1) {
  194. if (_interrupt_cf1 != GPIO_NONE) _detach(_interrupt_cf1);
  195. _attach(this, _cf1, HLW8012_INTERRUPT_ON);
  196. _interrupt_cf1 = _cf1;
  197. }
  198. } else {
  199. if (GPIO_NONE != _interrupt_cf) {
  200. _detach(_interrupt_cf);
  201. _interrupt_cf = GPIO_NONE;
  202. }
  203. if (GPIO_NONE != _interrupt_cf1) {
  204. _detach(_interrupt_cf1);
  205. _interrupt_cf1 = GPIO_NONE;
  206. }
  207. }
  208. }
  209. // ---------------------------------------------------------------------
  210. unsigned char _sel = GPIO_NONE;
  211. unsigned char _cf = GPIO_NONE;
  212. unsigned char _cf1 = GPIO_NONE;
  213. bool _sel_current = true;
  214. double _energy_offset = 0;
  215. HLW8012 * _hlw8012 = NULL;
  216. };
  217. // -----------------------------------------------------------------------------
  218. // Interrupt helpers
  219. // -----------------------------------------------------------------------------
  220. HLW8012Sensor * _hlw8012_sensor_instance[10] = {NULL};
  221. void ICACHE_RAM_ATTR _hlw8012_sensor_isr(unsigned char gpio) {
  222. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  223. if (_hlw8012_sensor_instance[index]) {
  224. _hlw8012_sensor_instance[index]->handleInterrupt(gpio);
  225. }
  226. }
  227. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_0() { _hlw8012_sensor_isr(0); }
  228. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_1() { _hlw8012_sensor_isr(1); }
  229. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_2() { _hlw8012_sensor_isr(2); }
  230. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_3() { _hlw8012_sensor_isr(3); }
  231. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_4() { _hlw8012_sensor_isr(4); }
  232. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_5() { _hlw8012_sensor_isr(5); }
  233. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_12() { _hlw8012_sensor_isr(12); }
  234. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_13() { _hlw8012_sensor_isr(13); }
  235. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_14() { _hlw8012_sensor_isr(14); }
  236. void ICACHE_RAM_ATTR _hlw8012_sensor_isr_15() { _hlw8012_sensor_isr(15); }
  237. static void (*_hlw8012_sensor_isr_list[10])() = {
  238. _hlw8012_sensor_isr_0, _hlw8012_sensor_isr_1, _hlw8012_sensor_isr_2,
  239. _hlw8012_sensor_isr_3, _hlw8012_sensor_isr_4, _hlw8012_sensor_isr_5,
  240. _hlw8012_sensor_isr_12, _hlw8012_sensor_isr_13, _hlw8012_sensor_isr_14,
  241. _hlw8012_sensor_isr_15
  242. };
  243. void HLW8012Sensor::_attach(HLW8012Sensor * instance, unsigned char gpio, unsigned char mode) {
  244. if (!gpioValid(gpio)) return;
  245. _detach(gpio);
  246. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  247. _hlw8012_sensor_instance[index] = instance;
  248. attachInterrupt(gpio, _hlw8012_sensor_isr_list[index], mode);
  249. #if SENSOR_DEBUG
  250. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%u interrupt attached to %s\n"), gpio, instance->description().c_str());
  251. #endif
  252. }
  253. void HLW8012Sensor::_detach(unsigned char gpio) {
  254. if (!gpioValid(gpio)) return;
  255. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  256. if (_hlw8012_sensor_instance[index]) {
  257. detachInterrupt(gpio);
  258. #if SENSOR_DEBUG
  259. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%u interrupt detached from %s\n"), gpio, _hlw8012_sensor_instance[index]->description().c_str());
  260. #endif
  261. _hlw8012_sensor_instance[index] = NULL;
  262. }
  263. }
  264. #endif // SENSOR_SUPPORT && HLW8012_SUPPORT