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.

318 lines
13 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. // -----------------------------------------------------------------------------
  2. // Energy monitor sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. #include "EmonSensor.h"
  8. #if I2C_USE_BRZO
  9. #include <brzo_i2c.h>
  10. #else
  11. #include <Wire.h>
  12. #endif
  13. #define ADS1015_CONVERSIONDELAY (1)
  14. #define ADS1115_CONVERSIONDELAY (8)
  15. #define ADS1015_BIT_SHIFT (4)
  16. #define ADS1115_BIT_SHIFT (0)
  17. #define ADS1X15_REG_POINTER_MASK (0x03)
  18. #define ADS1X15_REG_POINTER_CONVERT (0x00)
  19. #define ADS1X15_REG_POINTER_CONFIG (0x01)
  20. #define ADS1X15_REG_POINTER_LOWTHRESH (0x02)
  21. #define ADS1X15_REG_POINTER_HITHRESH (0x03)
  22. #define ADS1X15_REG_CONFIG_OS_MASK (0x8000)
  23. #define ADS1X15_REG_CONFIG_OS_SINGLE (0x8000) // Write: Set to start a single-conversion
  24. #define ADS1X15_REG_CONFIG_OS_BUSY (0x0000) // Read: Bit = 0 when conversion is in progress
  25. #define ADS1X15_REG_CONFIG_OS_NOTBUSY (0x8000) // Read: Bit = 1 when device is not performing a conversion
  26. #define ADS1X15_REG_CONFIG_MUX_MASK (0x7000)
  27. #define ADS1X15_REG_CONFIG_MUX_DIFF_0_1 (0x0000) // Differential P = AIN0, N = AIN1 (default)
  28. #define ADS1X15_REG_CONFIG_MUX_DIFF_0_3 (0x1000) // Differential P = AIN0, N = AIN3
  29. #define ADS1X15_REG_CONFIG_MUX_DIFF_1_3 (0x2000) // Differential P = AIN1, N = AIN3
  30. #define ADS1X15_REG_CONFIG_MUX_DIFF_2_3 (0x3000) // Differential P = AIN2, N = AIN3
  31. #define ADS1X15_REG_CONFIG_MUX_SINGLE_0 (0x4000) // Single-ended AIN0
  32. #define ADS1X15_REG_CONFIG_MUX_SINGLE_1 (0x5000) // Single-ended AIN1
  33. #define ADS1X15_REG_CONFIG_MUX_SINGLE_2 (0x6000) // Single-ended AIN2
  34. #define ADS1X15_REG_CONFIG_MUX_SINGLE_3 (0x7000) // Single-ended AIN3
  35. #define ADS1X15_REG_CONFIG_PGA_MASK (0x0E00)
  36. #define ADS1X15_REG_CONFIG_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3
  37. #define ADS1X15_REG_CONFIG_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1
  38. #define ADS1X15_REG_CONFIG_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default)
  39. #define ADS1X15_REG_CONFIG_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4
  40. #define ADS1X15_REG_CONFIG_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8
  41. #define ADS1X15_REG_CONFIG_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16
  42. #define ADS1X15_REG_CONFIG_MODE_MASK (0x0100)
  43. #define ADS1X15_REG_CONFIG_MODE_CONTIN (0x0000) // Continuous conversion mode
  44. #define ADS1X15_REG_CONFIG_MODE_SINGLE (0x0100) // Power-down single-shot mode (default)
  45. #define ADS1X15_REG_CONFIG_DR_MASK (0x00E0)
  46. #define ADS1015_REG_CONFIG_DR_128SPS (0x0000) // 128 samples per second
  47. #define ADS1015_REG_CONFIG_DR_250SPS (0x0020) // 250 samples per second
  48. #define ADS1015_REG_CONFIG_DR_490SPS (0x0040) // 490 samples per second
  49. #define ADS1015_REG_CONFIG_DR_920SPS (0x0060) // 920 samples per second
  50. #define ADS1015_REG_CONFIG_DR_1600SPS (0x0080) // 1600 samples per second (default)
  51. #define ADS1015_REG_CONFIG_DR_2400SPS (0x00A0) // 2400 samples per second
  52. #define ADS1015_REG_CONFIG_DR_3300SPS (0x00C0) // 3300 samples per second
  53. #define ADS1115_REG_CONFIG_DR_8SPS (0x0000) // 8 samples per second
  54. #define ADS1115_REG_CONFIG_DR_16SPS (0x0020) // 16 samples per second
  55. #define ADS1115_REG_CONFIG_DR_32SPS (0x0040) // 32 samples per second
  56. #define ADS1115_REG_CONFIG_DR_64SPS (0x0060) // 64 samples per second
  57. #define ADS1115_REG_CONFIG_DR_128SPS (0x0080) // 128 samples per second (default)
  58. #define ADS1115_REG_CONFIG_DR_250SPS (0x00A0) // 250 samples per second
  59. #define ADS1115_REG_CONFIG_DR_475SPS (0x00C0) // 475 samples per second
  60. #define ADS1115_REG_CONFIG_DR_860SPS (0x00E0) // 860 samples per second
  61. #define ADS1X15_REG_CONFIG_CMODE_MASK (0x0010)
  62. #define ADS1X15_REG_CONFIG_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default)
  63. #define ADS1X15_REG_CONFIG_CMODE_WINDOW (0x0010) // Window comparator
  64. #define ADS1X15_REG_CONFIG_CPOL_MASK (0x0008)
  65. #define ADS1X15_REG_CONFIG_CPOL_ACTVLOW (0x0000) // ALERT/RDY pin is low when active (default)
  66. #define ADS1X15_REG_CONFIG_CPOL_ACTVHI (0x0008) // ALERT/RDY pin is high when active
  67. #define ADS1X15_REG_CONFIG_CLAT_MASK (0x0004) // Determines if ALERT/RDY pin latches once asserted
  68. #define ADS1X15_REG_CONFIG_CLAT_NONLAT (0x0000) // Non-latching comparator (default)
  69. #define ADS1X15_REG_CONFIG_CLAT_LATCH (0x0004) // Latching comparator
  70. #define ADS1X15_REG_CONFIG_CQUE_MASK (0x0003)
  71. #define ADS1X15_REG_CONFIG_CQUE_1CONV (0x0000) // Assert ALERT/RDY after one conversions
  72. #define ADS1X15_REG_CONFIG_CQUE_2CONV (0x0001) // Assert ALERT/RDY after two conversions
  73. #define ADS1X15_REG_CONFIG_CQUE_4CONV (0x0002) // Assert ALERT/RDY after four conversions
  74. #define ADS1X15_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default)
  75. #define ADS1X15_CHANNELS 4
  76. class EmonADS1X15Sensor : public EmonSensor {
  77. public:
  78. EmonADS1X15Sensor(unsigned char address, bool is_ads1115, unsigned char mask, double voltage, unsigned char bits, double ref, double ratio): EmonSensor(voltage, bits, ref, ratio) {
  79. // Cache
  80. _is_ads1115 = is_ads1115;
  81. _address = address;
  82. _mask = mask;
  83. _ports = 0;
  84. while (mask) {
  85. if (mask & 0x01) ++_ports;
  86. mask = mask >> 1;
  87. }
  88. _count = _ports * _magnitudes;
  89. // warmup
  90. warmup();
  91. }
  92. // Descriptive name of the sensor
  93. String name() {
  94. char buffer[30];
  95. snprintf(buffer, sizeof(buffer), "EMON @ ADS1%d15 @ I2C (0x%02X)", _is_ads1115 ? 1 : 0, _address);
  96. return String(buffer);
  97. }
  98. // Descriptive name of the slot # index
  99. String slot(unsigned char index) {
  100. char buffer[35];
  101. unsigned char channel = getChannel(index % _ports);
  102. snprintf(buffer, sizeof(buffer), "EMON @ ADS1%d15 (A%d) @ I2C (0x%02X)", _is_ads1115 ? 1 : 0, channel, _address);
  103. return String(buffer);
  104. }
  105. // Type for slot # index
  106. magnitude_t type(unsigned char index) {
  107. if (index < _count) {
  108. _error = SENSOR_ERROR_OK;
  109. unsigned char magnitude = index / _ports;
  110. unsigned char i=0;
  111. #if EMON_REPORT_CURRENT
  112. if (magnitude == i++) return MAGNITUDE_CURRENT;
  113. #endif
  114. #if EMON_REPORT_POWER
  115. if (magnitude == i++) return MAGNITUDE_POWER_APPARENT;
  116. #endif
  117. #if EMON_REPORT_ENERGY
  118. if (magnitude == i) return MAGNITUDE_ENERGY;
  119. #endif
  120. }
  121. _error = SENSOR_ERROR_OUT_OF_RANGE;
  122. return MAGNITUDE_NONE;
  123. }
  124. void pre() {
  125. static unsigned long last = 0;
  126. for (unsigned char port=0; port<_ports; port++) {
  127. unsigned char channel = getChannel(port);
  128. _current[port] = getCurrent(channel);
  129. #if EMON_REPORT_ENERGY
  130. _energy[port] += (_current[port] * _voltage * (millis() - last) / 1000);
  131. #endif
  132. }
  133. last = millis();
  134. }
  135. // Current value for slot # index
  136. double value(unsigned char index) {
  137. if (index < _count) {
  138. _error = SENSOR_ERROR_OK;
  139. unsigned char port = index % _ports;
  140. unsigned char magnitude = index / _ports;
  141. unsigned char i=0;
  142. #if EMON_REPORT_CURRENT
  143. if (magnitude == i++) return _current[port];
  144. #endif
  145. #if EMON_REPORT_POWER
  146. if (magnitude == i++) return _current[port] * _voltage;
  147. #endif
  148. #if EMON_REPORT_ENERGY
  149. if (magnitude == i) return _energy[port];
  150. #endif
  151. }
  152. _error = SENSOR_ERROR_OUT_OF_RANGE;
  153. return 0;
  154. }
  155. protected:
  156. unsigned char getChannel(unsigned char port) {
  157. unsigned char count = 0;
  158. unsigned char bit = 1;
  159. for (unsigned char channel=0; channel<ADS1X15_CHANNELS; channel++) {
  160. if ((_mask & bit) == bit) {
  161. if (count == port) return channel;
  162. ++count;
  163. }
  164. bit <<= 1;
  165. }
  166. return 0;
  167. }
  168. void warmup() {
  169. for (unsigned char port=0; port<_ports; port++) {
  170. unsigned char channel = getChannel(port);
  171. getCurrent(channel);
  172. }
  173. }
  174. //----------------------------------------------------------------------
  175. // I2C
  176. //----------------------------------------------------------------------
  177. void setConfigRegistry(unsigned char channel, bool continuous, bool start) {
  178. // Start with default values
  179. uint16_t config = 0;
  180. config |= ADS1X15_REG_CONFIG_PGA_4_096V; // Set PGA/voltage range (0x0200)
  181. config |= ADS1X15_REG_CONFIG_DR_MASK; // Always at max speed (0x00E0)
  182. //config |= ADS1X15_REG_CONFIG_CMODE_TRAD; // Traditional comparator (default val) (0x0000)
  183. //config |= ADS1X15_REG_CONFIG_CPOL_ACTVLOW; // Alert/Rdy active low (default val) (0x0000)
  184. //config |= ADS1X15_REG_CONFIG_CLAT_NONLAT; // Non-latching (default val) (0x0000)
  185. config |= ADS1X15_REG_CONFIG_CQUE_NONE; // Disable the comparator (default val) (0x0003)
  186. if (start) {
  187. config |= ADS1X15_REG_CONFIG_OS_SINGLE; // Start a single-conversion (0x8000)
  188. }
  189. if (continuous) {
  190. //config |= ADS1X15_REG_CONFIG_MODE_CONTIN; // Continuous mode (default) (0x0000)
  191. } else {
  192. config |= ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (0x0100)
  193. }
  194. config |= ((channel + 4) << 12); // Set single-ended input channel (0x4000 - 0x7000)
  195. #if EMON_DEBUG
  196. Serial.printf("[EMON] ADS1X115 Config Registry: %04X\n", config);
  197. #endif
  198. // Write config register to the ADC
  199. #if I2C_USE_BRZO
  200. uint8_t buffer[3];
  201. buffer[0] = ADS1X15_REG_POINTER_CONFIG;
  202. buffer[1] = config >> 8;
  203. buffer[2] = config & 0xFF;
  204. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  205. brzo_i2c_write(buffer, 3, false);
  206. brzo_i2c_end_transaction();
  207. #else
  208. Wire.beginTransmission(_address);
  209. Wire.write((uint8_t) ADS1X15_REG_POINTER_CONFIG);
  210. Wire.write((uint8_t) (config >> 8));
  211. Wire.write((uint8_t) (config & 0xFF));
  212. Wire.endTransmission();
  213. #endif
  214. }
  215. double getCurrent(unsigned char channel) {
  216. // Force stop by setting single mode and back to continuous
  217. static unsigned char previous = 9;
  218. if (previous != channel) {
  219. setConfigRegistry(channel, true, false);
  220. setConfigRegistry(channel, false, false);
  221. setConfigRegistry(channel, false, true);
  222. delay(10);
  223. readADC(channel);
  224. previous = channel;
  225. }
  226. setConfigRegistry(channel, true, true);
  227. return read(channel, _pivot[channel]);
  228. }
  229. unsigned int readADC(unsigned char channel) {
  230. unsigned int value = 0;
  231. #if I2C_USE_BRZO
  232. uint8_t buffer[3];
  233. buffer[0] = ADS1X15_REG_POINTER_CONVERT;
  234. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  235. brzo_i2c_write(buffer, 1, false);
  236. brzo_i2c_read(buffer, 2, false);
  237. brzo_i2c_end_transaction();
  238. value |= buffer[0] << 8;
  239. value |= buffer[1];
  240. #else
  241. Wire.beginTransmission(_address);
  242. Wire.write(ADS1X15_REG_POINTER_CONVERT);
  243. Wire.endTransmission();
  244. Wire.requestFrom(_address, (unsigned char) 2);
  245. value |= Wire.read() << 8;
  246. value |= Wire.read();
  247. #endif
  248. if (!_is_ads1115) value >>= ADS1015_BIT_SHIFT;
  249. delayMicroseconds(500);
  250. return value;
  251. }
  252. bool _is_ads1115 = true;
  253. unsigned char _address;
  254. unsigned char _mask;
  255. unsigned char _ports;
  256. double _pivot[ADS1X15_CHANNELS] = {0};
  257. double _current[ADS1X15_CHANNELS] = {0};
  258. #if EMON_REPORT_ENERGY
  259. unsigned long _energy[ADS1X15_CHANNELS] = {0};
  260. #endif
  261. };