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.

345 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[25];
  64. snprintf(buffer, sizeof(buffer), "ECH1560 @ 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 wher 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 Ba and Bb
  142. if (_nextbit) {
  143. if (_bits_count < 9) { // first Byte/8 bits in Ba
  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, stor in Bb
  148. byte2 = byte2 << 1;
  149. if (digitalRead(_miso) == HIGH) byte2 |= 1;
  150. _nextbit = false;
  151. }
  152. }
  153. }
  154. 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.
  155. // voltage = 2 * (Ba + Bb / 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 Ba and Bb & Bc.
  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 = (Ba*255+Bb+Bc/255)/2
  187. _apparent = ( (float) byte1 * 255 + (float) byte2 + (float) byte3 / 255.0) / 2;
  188. _current = _apparent / _voltage;
  189. _dosync = false;
  190. }
  191. // If Bb is not 3 or something else than 0, something is wrong!
  192. if (byte2 == 0) _dosync = false;
  193. }
  194. // ---------------------------------------------------------------------
  195. unsigned char _clk = 0;
  196. unsigned char _miso = 0;
  197. bool _inverted = false;
  198. volatile long _bits_count = 0;
  199. volatile long _clk_count = 0;
  200. volatile bool _dosync = false;
  201. volatile bool _nextbit = true;
  202. double _apparent = 0;
  203. double _voltage = 0;
  204. double _current = 0;
  205. unsigned char _data[24];
  206. };
  207. // -----------------------------------------------------------------------------
  208. // Interrupt helpers
  209. // -----------------------------------------------------------------------------
  210. ECH1560Sensor * _ech1560_sensor_instance[10] = {NULL};
  211. void ICACHE_RAM_ATTR _ech1560_sensor_isr(unsigned char gpio) {
  212. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  213. if (_ech1560_sensor_instance[index]) {
  214. _ech1560_sensor_instance[index]->handleInterrupt(gpio);
  215. }
  216. }
  217. void ICACHE_RAM_ATTR _ech1560_sensor_isr_0() { _ech1560_sensor_isr(0); }
  218. void ICACHE_RAM_ATTR _ech1560_sensor_isr_1() { _ech1560_sensor_isr(1); }
  219. void ICACHE_RAM_ATTR _ech1560_sensor_isr_2() { _ech1560_sensor_isr(2); }
  220. void ICACHE_RAM_ATTR _ech1560_sensor_isr_3() { _ech1560_sensor_isr(3); }
  221. void ICACHE_RAM_ATTR _ech1560_sensor_isr_4() { _ech1560_sensor_isr(4); }
  222. void ICACHE_RAM_ATTR _ech1560_sensor_isr_5() { _ech1560_sensor_isr(5); }
  223. void ICACHE_RAM_ATTR _ech1560_sensor_isr_12() { _ech1560_sensor_isr(12); }
  224. void ICACHE_RAM_ATTR _ech1560_sensor_isr_13() { _ech1560_sensor_isr(13); }
  225. void ICACHE_RAM_ATTR _ech1560_sensor_isr_14() { _ech1560_sensor_isr(14); }
  226. void ICACHE_RAM_ATTR _ech1560_sensor_isr_15() { _ech1560_sensor_isr(15); }
  227. static void (*_ech1560_sensor_isr_list[10])() = {
  228. _ech1560_sensor_isr_0, _ech1560_sensor_isr_1, _ech1560_sensor_isr_2,
  229. _ech1560_sensor_isr_3, _ech1560_sensor_isr_4, _ech1560_sensor_isr_5,
  230. _ech1560_sensor_isr_12, _ech1560_sensor_isr_13, _ech1560_sensor_isr_14,
  231. _ech1560_sensor_isr_15
  232. };
  233. void ECH1560Sensor::_attach(ECH1560Sensor * instance, unsigned char gpio, unsigned char mode) {
  234. if (!gpioValid(gpio)) return;
  235. _detach(gpio);
  236. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  237. _ech1560_sensor_instance[index] = instance;
  238. attachInterrupt(gpio, _ech1560_sensor_isr_list[index], mode);
  239. #if SENSOR_DEBUG
  240. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, instance->description().c_str());
  241. #endif
  242. }
  243. void ECH1560Sensor::_detach(unsigned char gpio) {
  244. if (!gpioValid(gpio)) return;
  245. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  246. if (_ech1560_sensor_instance[index]) {
  247. detachInterrupt(gpio);
  248. #if SENSOR_DEBUG
  249. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _ech1560_sensor_instance[index]->description().c_str());
  250. #endif
  251. _ech1560_sensor_instance[index] = NULL;
  252. }
  253. }
  254. #endif // SENSOR_SUPPORT && ECH1560_SUPPORT