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.

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