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.

366 lines
12 KiB

  1. // -----------------------------------------------------------------------------
  2. // ECH1560 based power monitor
  3. // Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && ECH1560_SUPPORT
  6. #pragma once
  7. #include <Arduino.h>
  8. #include "../debug.h"
  9. #include "BaseSensor.h"
  10. class ECH1560Sensor : public BaseSensor {
  11. public:
  12. // ---------------------------------------------------------------------
  13. // Public
  14. // ---------------------------------------------------------------------
  15. ECH1560Sensor(): BaseSensor(), _data() {
  16. _count = 3;
  17. _sensor_id = SENSOR_ECH1560_ID;
  18. }
  19. ~ECH1560Sensor() {
  20. _enableInterrupts(false);
  21. }
  22. // ---------------------------------------------------------------------
  23. void setCLK(unsigned char clk) {
  24. if (_clk == clk) return;
  25. _clk = clk;
  26. _dirty = true;
  27. }
  28. void setMISO(unsigned char miso) {
  29. if (_miso == miso) return;
  30. _miso = miso;
  31. _dirty = true;
  32. }
  33. void setInverted(bool inverted) {
  34. _inverted = inverted;
  35. }
  36. // ---------------------------------------------------------------------
  37. unsigned char getCLK() {
  38. return _clk;
  39. }
  40. unsigned char getMISO() {
  41. return _miso;
  42. }
  43. bool getInverted() {
  44. return _inverted;
  45. }
  46. // ---------------------------------------------------------------------
  47. void resetEnergy(double value = 0) {
  48. _energy = value;
  49. }
  50. // ---------------------------------------------------------------------
  51. // Sensor API
  52. // ---------------------------------------------------------------------
  53. // Initialization method, must be idempotent
  54. void begin() {
  55. if (!_dirty) return;
  56. pinMode(_clk, INPUT);
  57. pinMode(_miso, INPUT);
  58. _enableInterrupts(true);
  59. _dirty = false;
  60. _ready = true;
  61. }
  62. // Loop-like method, call it in your main loop
  63. void tick() {
  64. if (_dosync) _sync();
  65. }
  66. // Descriptive name of the sensor
  67. String description() {
  68. char buffer[35];
  69. snprintf(buffer, sizeof(buffer), "ECH1560 (CLK,SDO) @ GPIO(%u,%u)", _clk, _miso);
  70. return String(buffer);
  71. }
  72. // Descriptive name of the slot # index
  73. String slot(unsigned char index) {
  74. return description();
  75. };
  76. // Address of the sensor (it could be the GPIO or I2C address)
  77. String address(unsigned char index) {
  78. char buffer[6];
  79. snprintf(buffer, sizeof(buffer), "%u:%u", _clk, _miso);
  80. return String(buffer);
  81. }
  82. // Type for slot # index
  83. unsigned char type(unsigned char index) {
  84. if (index == 0) return MAGNITUDE_CURRENT;
  85. if (index == 1) return MAGNITUDE_VOLTAGE;
  86. if (index == 2) return MAGNITUDE_POWER_APPARENT;
  87. if (index == 3) return MAGNITUDE_ENERGY;
  88. return MAGNITUDE_NONE;
  89. }
  90. // Current value for slot # index
  91. double value(unsigned char index) {
  92. if (index == 0) return _current;
  93. if (index == 1) return _voltage;
  94. if (index == 2) return _apparent;
  95. if (index == 3) return _energy;
  96. return 0;
  97. }
  98. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  99. UNUSED(gpio);
  100. // if we are trying to find the sync-time (CLK goes high for 1-2ms)
  101. if (_dosync == false) {
  102. _clk_count = 0;
  103. // register how long the ClkHigh is high to evaluate if we are at the part where clk goes high for 1-2 ms
  104. while (digitalRead(_clk) == HIGH) {
  105. _clk_count += 1;
  106. delayMicroseconds(30); //can only use delayMicroseconds in an interrupt.
  107. }
  108. // if the Clk was high between 1 and 2 ms than, its a start of a SPI-transmission
  109. if (_clk_count >= 33 && _clk_count <= 67) {
  110. _dosync = true;
  111. }
  112. // we are in sync and logging CLK-highs
  113. } else {
  114. // increment an integer to keep track of how many bits we have read.
  115. _bits_count += 1;
  116. _nextbit = true;
  117. }
  118. }
  119. protected:
  120. // ---------------------------------------------------------------------
  121. // Interrupt management
  122. // ---------------------------------------------------------------------
  123. void _attach(ECH1560Sensor * instance, unsigned char gpio, unsigned char mode);
  124. void _detach(unsigned char gpio);
  125. void _enableInterrupts(bool value) {
  126. static unsigned char _interrupt_clk = GPIO_NONE;
  127. if (value) {
  128. if (_interrupt_clk != _clk) {
  129. if (_interrupt_clk != GPIO_NONE) _detach(_interrupt_clk);
  130. _attach(this, _clk, RISING);
  131. _interrupt_clk = _clk;
  132. }
  133. } else if (_interrupt_clk != GPIO_NONE) {
  134. _detach(_interrupt_clk);
  135. _interrupt_clk = GPIO_NONE;
  136. }
  137. }
  138. // ---------------------------------------------------------------------
  139. // Protected
  140. // ---------------------------------------------------------------------
  141. void _sync() {
  142. unsigned int byte1 = 0;
  143. unsigned int byte2 = 0;
  144. unsigned int byte3 = 0;
  145. _bits_count = 0;
  146. while (_bits_count < 40); // skip the uninteresting 5 first bytes
  147. _bits_count = 0;
  148. while (_bits_count < 24) { // loop through the next 3 Bytes (6-8) and save byte 6 and 7 in byte1 and byte2
  149. if (_nextbit) {
  150. if (_bits_count < 9) { // first Byte/8 bits in byte1
  151. byte1 = byte1 << 1;
  152. if (digitalRead(_miso) == HIGH) byte1 |= 1;
  153. _nextbit = false;
  154. } else if (_bits_count < 17) { // bit 9-16 is byte 7, store in byte2
  155. byte2 = byte2 << 1;
  156. if (digitalRead(_miso) == HIGH) byte2 |= 1;
  157. _nextbit = false;
  158. }
  159. }
  160. }
  161. if (byte2 != 3) { // if bit byte2 is not 3, we have reached the important part, U is allready in byte1 and byte2 and next 8 Bytes will give us the Power.
  162. // voltage = 2 * (byte1 + byte2 / 255)
  163. _voltage = 2.0 * ((float) byte1 + (float) byte2 / 255.0);
  164. // power:
  165. _bits_count = 0;
  166. while (_bits_count < 40); // skip the uninteresting 5 first bytes
  167. _bits_count = 0;
  168. byte1 = 0;
  169. byte2 = 0;
  170. byte3 = 0;
  171. while (_bits_count < 24) { //store byte 6, 7 and 8 in byte1 and byte2 & byte3.
  172. if (_nextbit) {
  173. if (_bits_count < 9) {
  174. byte1 = byte1 << 1;
  175. if (digitalRead(_miso) == HIGH) byte1 |= 1;
  176. _nextbit = false;
  177. } else if (_bits_count < 17) {
  178. byte2 = byte2 << 1;
  179. if (digitalRead(_miso) == HIGH) byte2 |= 1;
  180. _nextbit = false;
  181. } else {
  182. byte3 = byte3 << 1;
  183. if (digitalRead(_miso) == HIGH) byte3 |= 1;
  184. _nextbit = false;
  185. }
  186. }
  187. }
  188. if (_inverted) {
  189. byte1 = 255 - byte1;
  190. byte2 = 255 - byte2;
  191. byte3 = 255 - byte3;
  192. }
  193. // power = (byte1*255+byte2+byte3/255)/2
  194. _apparent = ( (float) byte1 * 255 + (float) byte2 + (float) byte3 / 255.0) / 2;
  195. _current = _apparent / _voltage;
  196. static unsigned long last = 0;
  197. if (last > 0) {
  198. _energy += (_apparent * (millis() - last) / 1000);
  199. }
  200. last = millis();
  201. _dosync = false;
  202. }
  203. // If byte2 is not 3 or something else than 0, something is wrong!
  204. if (byte2 == 0) {
  205. _dosync = false;
  206. #if SENSOR_DEBUG
  207. DEBUG_MSG_P(PSTR("Nothing connected, or out of sync!\n"));
  208. #endif
  209. }
  210. }
  211. // ---------------------------------------------------------------------
  212. unsigned char _clk = 0;
  213. unsigned char _miso = 0;
  214. bool _inverted = false;
  215. volatile long _bits_count = 0;
  216. volatile long _clk_count = 0;
  217. volatile bool _dosync = false;
  218. volatile bool _nextbit = true;
  219. double _apparent = 0;
  220. double _voltage = 0;
  221. double _current = 0;
  222. double _energy = 0;
  223. unsigned char _data[24];
  224. };
  225. // -----------------------------------------------------------------------------
  226. // Interrupt helpers
  227. // -----------------------------------------------------------------------------
  228. ECH1560Sensor * _ech1560_sensor_instance[10] = {NULL};
  229. void ICACHE_RAM_ATTR _ech1560_sensor_isr(unsigned char gpio) {
  230. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  231. if (_ech1560_sensor_instance[index]) {
  232. _ech1560_sensor_instance[index]->handleInterrupt(gpio);
  233. }
  234. }
  235. void ICACHE_RAM_ATTR _ech1560_sensor_isr_0() { _ech1560_sensor_isr(0); }
  236. void ICACHE_RAM_ATTR _ech1560_sensor_isr_1() { _ech1560_sensor_isr(1); }
  237. void ICACHE_RAM_ATTR _ech1560_sensor_isr_2() { _ech1560_sensor_isr(2); }
  238. void ICACHE_RAM_ATTR _ech1560_sensor_isr_3() { _ech1560_sensor_isr(3); }
  239. void ICACHE_RAM_ATTR _ech1560_sensor_isr_4() { _ech1560_sensor_isr(4); }
  240. void ICACHE_RAM_ATTR _ech1560_sensor_isr_5() { _ech1560_sensor_isr(5); }
  241. void ICACHE_RAM_ATTR _ech1560_sensor_isr_12() { _ech1560_sensor_isr(12); }
  242. void ICACHE_RAM_ATTR _ech1560_sensor_isr_13() { _ech1560_sensor_isr(13); }
  243. void ICACHE_RAM_ATTR _ech1560_sensor_isr_14() { _ech1560_sensor_isr(14); }
  244. void ICACHE_RAM_ATTR _ech1560_sensor_isr_15() { _ech1560_sensor_isr(15); }
  245. static void (*_ech1560_sensor_isr_list[10])() = {
  246. _ech1560_sensor_isr_0, _ech1560_sensor_isr_1, _ech1560_sensor_isr_2,
  247. _ech1560_sensor_isr_3, _ech1560_sensor_isr_4, _ech1560_sensor_isr_5,
  248. _ech1560_sensor_isr_12, _ech1560_sensor_isr_13, _ech1560_sensor_isr_14,
  249. _ech1560_sensor_isr_15
  250. };
  251. void ECH1560Sensor::_attach(ECH1560Sensor * instance, unsigned char gpio, unsigned char mode) {
  252. if (!gpioValid(gpio)) return;
  253. _detach(gpio);
  254. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  255. _ech1560_sensor_instance[index] = instance;
  256. attachInterrupt(gpio, _ech1560_sensor_isr_list[index], mode);
  257. #if SENSOR_DEBUG
  258. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, instance->description().c_str());
  259. #endif
  260. }
  261. void ECH1560Sensor::_detach(unsigned char gpio) {
  262. if (!gpioValid(gpio)) return;
  263. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  264. if (_ech1560_sensor_instance[index]) {
  265. detachInterrupt(gpio);
  266. #if SENSOR_DEBUG
  267. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _ech1560_sensor_instance[index]->description().c_str());
  268. #endif
  269. _ech1560_sensor_instance[index] = NULL;
  270. }
  271. }
  272. #endif // SENSOR_SUPPORT && ECH1560_SUPPORT