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.

264 lines
7.8 KiB

  1. // -----------------------------------------------------------------------------
  2. // ECH1560 based power monitor
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #pragma once
  6. #include "Arduino.h"
  7. #include "BaseSensor.h"
  8. class ECH1560Sensor : public BaseSensor {
  9. public:
  10. // ---------------------------------------------------------------------
  11. // Public
  12. // ---------------------------------------------------------------------
  13. ECH1560Sensor(): BaseSensor() {
  14. _count = 3;
  15. _sensor_id = SENSOR_ECH1560_ID;
  16. }
  17. ~ECH1560Sensor() {
  18. if (_interrupt_gpio != GPIO_NONE) detach(_interrupt_gpio);
  19. }
  20. // ---------------------------------------------------------------------
  21. void setCLK(unsigned char clk) {
  22. if (_clk == clk) return;
  23. _clk = clk;
  24. _dirty = true;
  25. }
  26. void setMISO(unsigned char miso) {
  27. if (_miso == miso) return;
  28. _miso = miso;
  29. _dirty = true;
  30. }
  31. void setInverted(bool inverted) {
  32. _inverted = inverted;
  33. }
  34. // ---------------------------------------------------------------------
  35. unsigned char getCLK() {
  36. return _clk;
  37. }
  38. unsigned char getMISO() {
  39. return _miso;
  40. }
  41. bool getInverted() {
  42. return _inverted;
  43. }
  44. // ---------------------------------------------------------------------
  45. // Sensor API
  46. // ---------------------------------------------------------------------
  47. // Initialization method, must be idempotent
  48. void begin() {
  49. if (!_dirty) return;
  50. _dirty = false;
  51. pinMode(_clk, INPUT);
  52. pinMode(_miso, INPUT);
  53. if (_interrupt_gpio != GPIO_NONE) detach(_interrupt_gpio);
  54. attach(this, _clk, RISING);
  55. }
  56. // Interrupt attach callback
  57. void attached(unsigned char gpio) {
  58. BaseSensor::attached(gpio);
  59. _interrupt_gpio = gpio;
  60. }
  61. // Interrupt detach callback
  62. void detached(unsigned char gpio) {
  63. BaseSensor::detached(gpio);
  64. if (_interrupt_gpio == gpio) _interrupt_gpio = GPIO_NONE;
  65. }
  66. void handleInterrupt() {
  67. _isr();
  68. }
  69. // Descriptive name of the sensor
  70. String description() {
  71. char buffer[25];
  72. snprintf(buffer, sizeof(buffer), "ECH1560 @ GPIO(%i,%i)", _clk, _miso);
  73. return String(buffer);
  74. }
  75. // Type for slot # index
  76. magnitude_t type(unsigned char index) {
  77. _error = SENSOR_ERROR_OK;
  78. if (index == 0) return MAGNITUDE_CURRENT;
  79. if (index == 1) return MAGNITUDE_VOLTAGE;
  80. if (index == 2) return MAGNITUDE_POWER_APPARENT;
  81. _error = SENSOR_ERROR_OUT_OF_RANGE;
  82. return MAGNITUDE_NONE;
  83. }
  84. // Current value for slot # index
  85. double value(unsigned char index) {
  86. _error = SENSOR_ERROR_OK;
  87. if (index == 0) return _current;
  88. if (index == 1) return _voltage;
  89. if (index == 2) return _apparent;
  90. _error = SENSOR_ERROR_OUT_OF_RANGE;
  91. return 0;
  92. }
  93. protected:
  94. // ---------------------------------------------------------------------
  95. // Protected
  96. // ---------------------------------------------------------------------
  97. void ICACHE_RAM_ATTR _isr() {
  98. // if we are trying to find the sync-time (CLK goes high for 1-2ms)
  99. if (_dosync == false) {
  100. _clk_count = 0;
  101. // register how long the ClkHigh is high to evaluate if we are at the part wher clk goes high for 1-2 ms
  102. while (digitalRead(_clk) == HIGH) {
  103. _clk_count += 1;
  104. delayMicroseconds(30); //can only use delayMicroseconds in an interrupt.
  105. }
  106. // if the Clk was high between 1 and 2 ms than, its a start of a SPI-transmission
  107. if (_clk_count >= 33 && _clk_count <= 67) {
  108. _dosync = true;
  109. }
  110. // we are in sync and logging CLK-highs
  111. } else {
  112. // increment an integer to keep track of how many bits we have read.
  113. _bits_count += 1;
  114. _nextbit = true;
  115. }
  116. }
  117. void _sync() {
  118. unsigned int byte1 = 0;
  119. unsigned int byte2 = 0;
  120. unsigned int byte3 = 0;
  121. _bits_count = 0;
  122. while (_bits_count < 40); // skip the uninteresting 5 first bytes
  123. _bits_count = 0;
  124. while (_bits_count < 24) { // loop through the next 3 Bytes (6-8) and save byte 6 and 7 in Ba and Bb
  125. if (_nextbit) {
  126. if (_bits_count < 9) { // first Byte/8 bits in Ba
  127. byte1 = byte1 << 1;
  128. if (digitalRead(_miso) == HIGH) byte1 |= 1;
  129. _nextbit = false;
  130. } else if (_bits_count < 17) { // bit 9-16 is byte 7, stor in Bb
  131. byte2 = byte2 << 1;
  132. if (digitalRead(_miso) == HIGH) byte2 |= 1;
  133. _nextbit = false;
  134. }
  135. }
  136. }
  137. 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.
  138. // voltage = 2 * (Ba + Bb / 255)
  139. _voltage = 2.0 * ((float) byte1 + (float) byte2 / 255.0);
  140. // power:
  141. _bits_count = 0;
  142. while (_bits_count < 40); // skip the uninteresting 5 first bytes
  143. _bits_count = 0;
  144. byte1 = 0;
  145. byte2 = 0;
  146. byte3 = 0;
  147. while (_bits_count < 24) { //store byte 6, 7 and 8 in Ba and Bb & Bc.
  148. if (_nextbit) {
  149. if (_bits_count < 9) {
  150. byte1 = byte1 << 1;
  151. if (digitalRead(_miso) == HIGH) byte1 |= 1;
  152. _nextbit = false;
  153. } else if (_bits_count < 17) {
  154. byte2 = byte2 << 1;
  155. if (digitalRead(_miso) == HIGH) byte2 |= 1;
  156. _nextbit = false;
  157. } else {
  158. byte3 = byte3 << 1;
  159. if (digitalRead(_miso) == HIGH) byte3 |= 1;
  160. _nextbit = false;
  161. }
  162. }
  163. }
  164. if (_inverted) {
  165. byte1 = 255 - byte1;
  166. byte2 = 255 - byte2;
  167. byte3 = 255 - byte3;
  168. }
  169. // power = (Ba*255+Bb+Bc/255)/2
  170. _apparent = ( (float) byte1 * 255 + (float) byte2 + (float) byte3 / 255.0) / 2;
  171. _current = _apparent / _voltage;
  172. _dosync = false;
  173. }
  174. // If Bb is not 3 or something else than 0, something is wrong!
  175. if (byte2 == 0) _dosync = false;
  176. }
  177. // ---------------------------------------------------------------------
  178. unsigned char _clk = 0;
  179. unsigned char _miso = 0;
  180. unsigned char _interrupt_gpio = GPIO_NONE;
  181. bool _inverted = false;
  182. volatile long _bits_count = 0;
  183. volatile long _clk_count = 0;
  184. volatile bool _dosync = false;
  185. volatile bool _nextbit = true;
  186. double _apparent = 0;
  187. double _voltage = 0;
  188. double _current = 0;
  189. unsigned char _data[24];
  190. };