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.

363 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 <Arduino.h>
  8. #include "../debug.h"
  9. #include "BaseSensor.h"
  10. #include "BaseEmonSensor.h"
  11. class ECH1560Sensor : public BaseEmonSensor {
  12. public:
  13. // ---------------------------------------------------------------------
  14. // Public
  15. // ---------------------------------------------------------------------
  16. ECH1560Sensor(): _data() {
  17. _count = 3;
  18. _sensor_id = SENSOR_ECH1560_ID;
  19. }
  20. ~ECH1560Sensor() {
  21. _enableInterrupts(false);
  22. }
  23. // ---------------------------------------------------------------------
  24. void setCLK(unsigned char clk) {
  25. if (_clk == clk) return;
  26. _clk = clk;
  27. _dirty = true;
  28. }
  29. void setMISO(unsigned char miso) {
  30. if (_miso == miso) return;
  31. _miso = miso;
  32. _dirty = true;
  33. }
  34. void setInverted(bool inverted) {
  35. _inverted = inverted;
  36. }
  37. // ---------------------------------------------------------------------
  38. unsigned char getCLK() {
  39. return _clk;
  40. }
  41. unsigned char getMISO() {
  42. return _miso;
  43. }
  44. bool getInverted() {
  45. return _inverted;
  46. }
  47. // ---------------------------------------------------------------------
  48. // Sensor API
  49. // ---------------------------------------------------------------------
  50. // Initialization method, must be idempotent
  51. void begin() {
  52. if (!_dirty) return;
  53. pinMode(_clk, INPUT);
  54. pinMode(_miso, INPUT);
  55. _enableInterrupts(true);
  56. _dirty = false;
  57. _ready = true;
  58. }
  59. // Loop-like method, call it in your main loop
  60. void tick() {
  61. if (_dosync) _sync();
  62. }
  63. // Descriptive name of the sensor
  64. String description() {
  65. char buffer[35];
  66. snprintf(buffer, sizeof(buffer), "ECH1560 (CLK,SDO) @ GPIO(%u,%u)", _clk, _miso);
  67. return String(buffer);
  68. }
  69. // Descriptive name of the slot # index
  70. String description(unsigned char index) {
  71. return description();
  72. };
  73. // Address of the sensor (it could be the GPIO or I2C address)
  74. String address(unsigned char index) {
  75. char buffer[6];
  76. snprintf(buffer, sizeof(buffer), "%u:%u", _clk, _miso);
  77. return String(buffer);
  78. }
  79. // Type for slot # index
  80. unsigned char type(unsigned char index) {
  81. if (index == 0) return MAGNITUDE_CURRENT;
  82. if (index == 1) return MAGNITUDE_VOLTAGE;
  83. if (index == 2) return MAGNITUDE_POWER_APPARENT;
  84. if (index == 3) return MAGNITUDE_ENERGY;
  85. return MAGNITUDE_NONE;
  86. }
  87. // Current value for slot # index
  88. double value(unsigned char index) {
  89. if (index == 0) return _current;
  90. if (index == 1) return _voltage;
  91. if (index == 2) return _apparent;
  92. if (index == 3) return getEnergy();
  93. return 0;
  94. }
  95. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  96. UNUSED(gpio);
  97. // if we are trying to find the sync-time (CLK goes high for 1-2ms)
  98. if (_dosync == false) {
  99. _clk_count = 0;
  100. // register how long the ClkHigh is high to evaluate if we are at the part where clk goes high for 1-2 ms
  101. while (digitalRead(_clk) == HIGH) {
  102. _clk_count += 1;
  103. delayMicroseconds(30); //can only use delayMicroseconds in an interrupt.
  104. }
  105. // if the Clk was high between 1 and 2 ms than, its a start of a SPI-transmission
  106. if (_clk_count >= 33 && _clk_count <= 67) {
  107. _dosync = true;
  108. }
  109. // we are in sync and logging CLK-highs
  110. } else {
  111. // increment an integer to keep track of how many bits we have read.
  112. _bits_count += 1;
  113. _nextbit = true;
  114. }
  115. }
  116. protected:
  117. // ---------------------------------------------------------------------
  118. // Interrupt management
  119. // ---------------------------------------------------------------------
  120. void _attach(ECH1560Sensor * instance, unsigned char gpio, unsigned char mode);
  121. void _detach(unsigned char gpio);
  122. void _enableInterrupts(bool value) {
  123. static unsigned char _interrupt_clk = GPIO_NONE;
  124. if (value) {
  125. if (_interrupt_clk != _clk) {
  126. if (_interrupt_clk != GPIO_NONE) _detach(_interrupt_clk);
  127. _attach(this, _clk, RISING);
  128. _interrupt_clk = _clk;
  129. }
  130. } else if (_interrupt_clk != GPIO_NONE) {
  131. _detach(_interrupt_clk);
  132. _interrupt_clk = GPIO_NONE;
  133. }
  134. }
  135. // ---------------------------------------------------------------------
  136. // Protected
  137. // ---------------------------------------------------------------------
  138. void _sync() {
  139. unsigned int byte1 = 0;
  140. unsigned int byte2 = 0;
  141. unsigned int byte3 = 0;
  142. _bits_count = 0;
  143. while (_bits_count < 40); // skip the uninteresting 5 first bytes
  144. _bits_count = 0;
  145. while (_bits_count < 24) { // loop through the next 3 Bytes (6-8) and save byte 6 and 7 in byte1 and byte2
  146. if (_nextbit) {
  147. if (_bits_count < 9) { // first Byte/8 bits in byte1
  148. byte1 = byte1 << 1;
  149. if (digitalRead(_miso) == HIGH) byte1 |= 1;
  150. _nextbit = false;
  151. } else if (_bits_count < 17) { // bit 9-16 is byte 7, store in byte2
  152. byte2 = byte2 << 1;
  153. if (digitalRead(_miso) == HIGH) byte2 |= 1;
  154. _nextbit = false;
  155. }
  156. }
  157. }
  158. 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.
  159. // voltage = 2 * (byte1 + byte2 / 255)
  160. _voltage = 2.0 * ((float) byte1 + (float) byte2 / 255.0);
  161. // power:
  162. _bits_count = 0;
  163. while (_bits_count < 40); // skip the uninteresting 5 first bytes
  164. _bits_count = 0;
  165. byte1 = 0;
  166. byte2 = 0;
  167. byte3 = 0;
  168. while (_bits_count < 24) { //store byte 6, 7 and 8 in byte1 and byte2 & byte3.
  169. if (_nextbit) {
  170. if (_bits_count < 9) {
  171. byte1 = byte1 << 1;
  172. if (digitalRead(_miso) == HIGH) byte1 |= 1;
  173. _nextbit = false;
  174. } else if (_bits_count < 17) {
  175. byte2 = byte2 << 1;
  176. if (digitalRead(_miso) == HIGH) byte2 |= 1;
  177. _nextbit = false;
  178. } else {
  179. byte3 = byte3 << 1;
  180. if (digitalRead(_miso) == HIGH) byte3 |= 1;
  181. _nextbit = false;
  182. }
  183. }
  184. }
  185. if (_inverted) {
  186. byte1 = 255 - byte1;
  187. byte2 = 255 - byte2;
  188. byte3 = 255 - byte3;
  189. }
  190. // power = (byte1*255+byte2+byte3/255)/2
  191. _apparent = ( (float) byte1 * 255 + (float) byte2 + (float) byte3 / 255.0) / 2;
  192. _current = _apparent / _voltage;
  193. static unsigned long last = 0;
  194. if (last > 0) {
  195. _energy[0] += sensor::Ws {
  196. static_cast<uint32_t>(_apparent * (millis() - last) / 1000)
  197. };
  198. }
  199. last = millis();
  200. _dosync = false;
  201. }
  202. // If byte2 is not 3 or something else than 0, something is wrong!
  203. if (byte2 == 0) {
  204. _dosync = false;
  205. #if SENSOR_DEBUG
  206. DEBUG_MSG_P(PSTR("Nothing connected, or out of sync!\n"));
  207. #endif
  208. }
  209. }
  210. // ---------------------------------------------------------------------
  211. unsigned char _clk = 0;
  212. unsigned char _miso = 0;
  213. bool _inverted = false;
  214. volatile long _bits_count = 0;
  215. volatile long _clk_count = 0;
  216. volatile bool _dosync = false;
  217. volatile bool _nextbit = true;
  218. double _apparent = 0;
  219. double _voltage = 0;
  220. double _current = 0;
  221. unsigned char _data[24];
  222. };
  223. // -----------------------------------------------------------------------------
  224. // Interrupt helpers
  225. // -----------------------------------------------------------------------------
  226. ECH1560Sensor * _ech1560_sensor_instance[10] = {NULL};
  227. void ICACHE_RAM_ATTR _ech1560_sensor_isr(unsigned char gpio) {
  228. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  229. if (_ech1560_sensor_instance[index]) {
  230. _ech1560_sensor_instance[index]->handleInterrupt(gpio);
  231. }
  232. }
  233. void ICACHE_RAM_ATTR _ech1560_sensor_isr_0() { _ech1560_sensor_isr(0); }
  234. void ICACHE_RAM_ATTR _ech1560_sensor_isr_1() { _ech1560_sensor_isr(1); }
  235. void ICACHE_RAM_ATTR _ech1560_sensor_isr_2() { _ech1560_sensor_isr(2); }
  236. void ICACHE_RAM_ATTR _ech1560_sensor_isr_3() { _ech1560_sensor_isr(3); }
  237. void ICACHE_RAM_ATTR _ech1560_sensor_isr_4() { _ech1560_sensor_isr(4); }
  238. void ICACHE_RAM_ATTR _ech1560_sensor_isr_5() { _ech1560_sensor_isr(5); }
  239. void ICACHE_RAM_ATTR _ech1560_sensor_isr_12() { _ech1560_sensor_isr(12); }
  240. void ICACHE_RAM_ATTR _ech1560_sensor_isr_13() { _ech1560_sensor_isr(13); }
  241. void ICACHE_RAM_ATTR _ech1560_sensor_isr_14() { _ech1560_sensor_isr(14); }
  242. void ICACHE_RAM_ATTR _ech1560_sensor_isr_15() { _ech1560_sensor_isr(15); }
  243. static void (*_ech1560_sensor_isr_list[10])() = {
  244. _ech1560_sensor_isr_0, _ech1560_sensor_isr_1, _ech1560_sensor_isr_2,
  245. _ech1560_sensor_isr_3, _ech1560_sensor_isr_4, _ech1560_sensor_isr_5,
  246. _ech1560_sensor_isr_12, _ech1560_sensor_isr_13, _ech1560_sensor_isr_14,
  247. _ech1560_sensor_isr_15
  248. };
  249. void ECH1560Sensor::_attach(ECH1560Sensor * instance, unsigned char gpio, unsigned char mode) {
  250. if (!gpioValid(gpio)) return;
  251. _detach(gpio);
  252. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  253. _ech1560_sensor_instance[index] = instance;
  254. attachInterrupt(gpio, _ech1560_sensor_isr_list[index], mode);
  255. #if SENSOR_DEBUG
  256. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, instance->description().c_str());
  257. #endif
  258. }
  259. void ECH1560Sensor::_detach(unsigned char gpio) {
  260. if (!gpioValid(gpio)) return;
  261. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  262. if (_ech1560_sensor_instance[index]) {
  263. detachInterrupt(gpio);
  264. #if SENSOR_DEBUG
  265. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _ech1560_sensor_instance[index]->description().c_str());
  266. #endif
  267. _ech1560_sensor_instance[index] = NULL;
  268. }
  269. }
  270. #endif // SENSOR_SUPPORT && ECH1560_SUPPORT