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.

330 lines
11 KiB

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