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.

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