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.

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