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.

326 lines
10 KiB

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