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.

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