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.

358 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. void resetEnergy(double value = 0) {
  47. _energy = value;
  48. }
  49. // ---------------------------------------------------------------------
  50. // Sensor API
  51. // ---------------------------------------------------------------------
  52. // Initialization method, must be idempotent
  53. void begin() {
  54. if (!_dirty) return;
  55. _enableInterrupts(true);
  56. pinMode(_clk, INPUT);
  57. pinMode(_miso, INPUT);
  58. _dirty = false;
  59. _ready = true;
  60. }
  61. // Pre-read hook (usually to populate registers with up-to-date data)
  62. void pre() {
  63. _enableInterrupts(false);
  64. }
  65. // Post-read hook (usually to reset things)
  66. void post() {
  67. if (_ready) _enableInterrupts(true);
  68. }
  69. // Loop-like method, call it in your main loop
  70. void tick() {
  71. _read();
  72. }
  73. // Descriptive name of the sensor
  74. String description() {
  75. char buffer[35];
  76. snprintf(buffer, sizeof(buffer), "ECH1560 (CLK,SDO) @ GPIO(%u,%u)", _clk, _miso);
  77. return String(buffer);
  78. }
  79. // Descriptive name of the slot # index
  80. String slot(unsigned char index) {
  81. return description();
  82. };
  83. // Address of the sensor (it could be the GPIO or I2C address)
  84. String address(unsigned char index) {
  85. char buffer[6];
  86. snprintf(buffer, sizeof(buffer), "%u:%u", _clk, _miso);
  87. return String(buffer);
  88. }
  89. // Type for slot # index
  90. unsigned char type(unsigned char index) {
  91. if (index == 0) return MAGNITUDE_CURRENT;
  92. if (index == 1) return MAGNITUDE_VOLTAGE;
  93. if (index == 2) return MAGNITUDE_POWER_APPARENT;
  94. if (index == 3) return MAGNITUDE_ENERGY;
  95. return MAGNITUDE_NONE;
  96. }
  97. // Current value for slot # index
  98. double value(unsigned char index) {
  99. if (index == 0) return _current;
  100. if (index == 1) return _voltage;
  101. if (index == 2) return _apparent;
  102. if (index == 3) return _energy;
  103. return 0;
  104. }
  105. void ICACHE_RAM_ATTR handleInterrupt(unsigned char gpio) {
  106. (void) gpio;
  107. // if we are trying to find the sync-time (CLK goes high for 1-2ms)
  108. if (false == _loading) {
  109. volatile long _clk_count = 0;
  110. // register how long the ClkHigh is high to evaluate if we are at the part where clk goes high for 1-2 ms
  111. while (digitalRead(_clk) == HIGH) {
  112. _clk_count += 1;
  113. delayMicroseconds(30); //can only use delayMicroseconds in an interrupt.
  114. }
  115. // if the Clk was high between 1 and 2 ms than, its a start of a SPI-transmission
  116. if (_clk_count >= 33 && _clk_count <= 67) {
  117. _loading = true;
  118. }
  119. // we are in sync and logging CLK-highs
  120. } else if (false == _loaded) {
  121. unsigned char value = (digitalRead(_miso) == HIGH) ? 1 : 0;
  122. _data[_byte] = (_data[_byte] << 1) + value;
  123. if (8 == ++_bit) {
  124. _bit = 0;
  125. if (16 == ++_byte) {
  126. _byte = 0;
  127. _loaded = true;
  128. }
  129. }
  130. }
  131. }
  132. protected:
  133. // ---------------------------------------------------------------------
  134. // Interrupt management
  135. // ---------------------------------------------------------------------
  136. void _attach(ECH1560Sensor * instance, unsigned char gpio, unsigned char mode);
  137. void _detach(unsigned char gpio);
  138. void _enableInterrupts(bool value) {
  139. static unsigned char _interrupt_clk = GPIO_NONE;
  140. if (value) {
  141. if (_interrupt_clk != _clk) {
  142. if (_interrupt_clk != GPIO_NONE) _detach(_interrupt_clk);
  143. _attach(this, _clk, RISING);
  144. _interrupt_clk = _clk;
  145. }
  146. } else if (_interrupt_clk != GPIO_NONE) {
  147. _detach(_interrupt_clk);
  148. _interrupt_clk = GPIO_NONE;
  149. }
  150. }
  151. // ---------------------------------------------------------------------
  152. // Protected
  153. // ---------------------------------------------------------------------
  154. void _reset() {
  155. // Clean data array
  156. for (unsigned char i=0; i<16; i++) {
  157. _data[i] = 0;
  158. }
  159. _loaded = false;
  160. _loading = false;
  161. _start = 0;
  162. }
  163. void _read() {
  164. // Check if stalled
  165. if (false == _loaded) {
  166. if (true == _loading) {
  167. if (0 == _start) {
  168. _start = millis();
  169. } else if (millis() - _start > ECH1560_TIMEOUT) {
  170. _reset();
  171. }
  172. }
  173. return;
  174. }
  175. // Structure:
  176. // byte
  177. // ====
  178. // 0..4 ??
  179. // 5 V = 2 * ([5] + [6]/255)
  180. // 6 must not be 3
  181. // 7 ??
  182. // 8..12 ??
  183. // 13 P = (255*[13] + [14] + [15]/255) / 2
  184. // 14
  185. // 15
  186. // If inverted logic invert values
  187. if (_inverted) {
  188. for (unsigned char i=0; i<16; i++) {
  189. _data[i] = 255 - _data[i];
  190. }
  191. }
  192. #if SENSOR_DEBUG
  193. DEBUG_MSG("[ECH1560] Parsing data: ");
  194. char buffer[4];
  195. for (unsigned char i=0; i<16; i++) {
  196. snprintf(buffer, sizeof(buffer), "%02X ", _data[i]);
  197. DEBUG_MSG(buffer);
  198. }
  199. DEBUG_MSG("\n");
  200. #endif
  201. if (_data[6] != 3) {
  202. _voltage = 2.0 * ((float) _data[5] + (float) _data[6] / 255.0);
  203. _apparent = ( (float) _data[13] * 255 + (float) _data[14] + (float) _data[15] / 255.0) / 2;
  204. _current = _apparent / _voltage;
  205. static unsigned long last = 0;
  206. if (last > 0) {
  207. _energy += (_apparent * (millis() - last) / 1000);
  208. }
  209. last = millis();
  210. } else if (_data[6] != 0) {
  211. #if SENSOR_DEBUG
  212. DEBUG_MSG("[ECH1560] Nothing connected, or out of sync!\n");
  213. #endif
  214. }
  215. _reset();
  216. }
  217. // ---------------------------------------------------------------------
  218. unsigned char _clk = 0;
  219. unsigned char _miso = 0;
  220. bool _inverted = false;
  221. volatile unsigned char _bit = 0;
  222. volatile unsigned char _byte = 0;
  223. volatile unsigned char _data[16];
  224. volatile bool _loading = false;
  225. volatile bool _loaded = false;
  226. unsigned long _start = 0;
  227. double _apparent = 0;
  228. double _voltage = 0;
  229. double _current = 0;
  230. double _energy = 0;
  231. };
  232. // -----------------------------------------------------------------------------
  233. // Interrupt helpers
  234. // -----------------------------------------------------------------------------
  235. ECH1560Sensor * _ech1560_sensor_instance[10] = {NULL};
  236. void ICACHE_RAM_ATTR _ech1560_sensor_isr(unsigned char gpio) {
  237. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  238. if (_ech1560_sensor_instance[index]) {
  239. _ech1560_sensor_instance[index]->handleInterrupt(gpio);
  240. }
  241. }
  242. void ICACHE_RAM_ATTR _ech1560_sensor_isr_0() { _ech1560_sensor_isr(0); }
  243. void ICACHE_RAM_ATTR _ech1560_sensor_isr_1() { _ech1560_sensor_isr(1); }
  244. void ICACHE_RAM_ATTR _ech1560_sensor_isr_2() { _ech1560_sensor_isr(2); }
  245. void ICACHE_RAM_ATTR _ech1560_sensor_isr_3() { _ech1560_sensor_isr(3); }
  246. void ICACHE_RAM_ATTR _ech1560_sensor_isr_4() { _ech1560_sensor_isr(4); }
  247. void ICACHE_RAM_ATTR _ech1560_sensor_isr_5() { _ech1560_sensor_isr(5); }
  248. void ICACHE_RAM_ATTR _ech1560_sensor_isr_12() { _ech1560_sensor_isr(12); }
  249. void ICACHE_RAM_ATTR _ech1560_sensor_isr_13() { _ech1560_sensor_isr(13); }
  250. void ICACHE_RAM_ATTR _ech1560_sensor_isr_14() { _ech1560_sensor_isr(14); }
  251. void ICACHE_RAM_ATTR _ech1560_sensor_isr_15() { _ech1560_sensor_isr(15); }
  252. static void (*_ech1560_sensor_isr_list[10])() = {
  253. _ech1560_sensor_isr_0, _ech1560_sensor_isr_1, _ech1560_sensor_isr_2,
  254. _ech1560_sensor_isr_3, _ech1560_sensor_isr_4, _ech1560_sensor_isr_5,
  255. _ech1560_sensor_isr_12, _ech1560_sensor_isr_13, _ech1560_sensor_isr_14,
  256. _ech1560_sensor_isr_15
  257. };
  258. void ECH1560Sensor::_attach(ECH1560Sensor * instance, unsigned char gpio, unsigned char mode) {
  259. if (!gpioValid(gpio)) return;
  260. _detach(gpio);
  261. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  262. _ech1560_sensor_instance[index] = instance;
  263. attachInterrupt(gpio, _ech1560_sensor_isr_list[index], mode);
  264. #if SENSOR_DEBUG
  265. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt attached to %s\n"), gpio, instance->description().c_str());
  266. #endif
  267. }
  268. void ECH1560Sensor::_detach(unsigned char gpio) {
  269. if (!gpioValid(gpio)) return;
  270. unsigned char index = gpio > 5 ? gpio-6 : gpio;
  271. if (_ech1560_sensor_instance[index]) {
  272. detachInterrupt(gpio);
  273. #if SENSOR_DEBUG
  274. DEBUG_MSG_P(PSTR("[SENSOR] GPIO%d interrupt detached from %s\n"), gpio, _ech1560_sensor_instance[index]->description().c_str());
  275. #endif
  276. _ech1560_sensor_instance[index] = NULL;
  277. }
  278. }
  279. #endif // SENSOR_SUPPORT && ECH1560_SUPPORT