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.

356 lines
12 KiB

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