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.

335 lines
11 KiB

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