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.

304 lines
12 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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. #define EMON_ADS1X15_MAGNITUDES_PER_PORT 2
  77. class EmonADS1X15Sensor : public EmonSensor {
  78. public:
  79. EmonADS1X15Sensor(unsigned char address, bool is_ads1115, unsigned char mask, double voltage, unsigned char bits, double ref, double ratio): EmonSensor(voltage, bits, ref, ratio) {
  80. // Cache
  81. _is_ads1115 = is_ads1115;
  82. _address = address;
  83. _mask = mask;
  84. _ports = 0;
  85. while (mask) {
  86. if (mask & 0x01) ++_ports;
  87. mask = mask >> 1;
  88. }
  89. _count = _ports * EMON_ADS1X15_MAGNITUDES_PER_PORT;
  90. // warmup
  91. warmup();
  92. }
  93. // Descriptive name of the sensor
  94. String name() {
  95. char buffer[30];
  96. snprintf(buffer, sizeof(buffer), "EMON @ ADS1%d15 @ I2C (0x%02X)", _is_ads1115 ? 1 : 0, _address);
  97. return String(buffer);
  98. }
  99. // Descriptive name of the slot # index
  100. String slot(unsigned char index) {
  101. char buffer[35];
  102. unsigned char channel = getChannel(index % _ports);
  103. snprintf(buffer, sizeof(buffer), "EMON @ ADS1%d15 (A%d) @ I2C (0x%02X)", _is_ads1115 ? 1 : 0, channel, _address);
  104. return String(buffer);
  105. }
  106. // Type for slot # index
  107. magnitude_t type(unsigned char index) {
  108. if (index < _count) {
  109. _error = SENSOR_ERROR_OK;
  110. unsigned char magnitude = index / _ports;
  111. if (magnitude == 0) return MAGNITUDE_CURRENT;
  112. if (magnitude == 1) return MAGNITUDE_POWER_APPARENT;
  113. //if (magnitude == 2) return MAGNITUDE_ENERGY;
  114. //if (magnitude == 3) return MAGNITUDE_ENERGY_DELTA;
  115. }
  116. _error = SENSOR_ERROR_OUT_OF_RANGE;
  117. return MAGNITUDE_NONE;
  118. }
  119. void pre() {
  120. //static unsigned long last = 0;
  121. for (unsigned char port=0; port<_ports; port++) {
  122. unsigned char channel = getChannel(port);
  123. _current[port] = getCurrent(channel);
  124. //if (last > 0) {
  125. // _delta[port] = _current[port] * _voltage * (millis() - last) / 1000;
  126. //}
  127. //_energy[port] += _delta[port];
  128. }
  129. //last = millis();
  130. }
  131. // Current value for slot # index
  132. double value(unsigned char index) {
  133. if (index < _count) {
  134. _error = SENSOR_ERROR_OK;
  135. unsigned char port = index % _ports;
  136. unsigned char magnitude = index / _ports;
  137. if (magnitude == 0) return _current[port];
  138. if (magnitude == 1) return _current[port] * _voltage;
  139. //if (magnitude == 2) return _energy[port];
  140. //if (magnitude == 3) return _delta[port];
  141. }
  142. _error = SENSOR_ERROR_OUT_OF_RANGE;
  143. return 0;
  144. }
  145. protected:
  146. unsigned char getChannel(unsigned char port) {
  147. unsigned char count = 0;
  148. unsigned char bit = 1;
  149. for (unsigned char channel=0; channel<ADS1X15_CHANNELS; channel++) {
  150. if ((_mask & bit) == bit) {
  151. if (count == port) return channel;
  152. ++count;
  153. }
  154. bit <<= 1;
  155. }
  156. return 0;
  157. }
  158. void warmup() {
  159. for (unsigned char port=0; port<_ports; port++) {
  160. unsigned char channel = getChannel(port);
  161. getCurrent(channel);
  162. }
  163. }
  164. //----------------------------------------------------------------------
  165. // I2C
  166. //----------------------------------------------------------------------
  167. void setConfigRegistry(unsigned char channel, bool continuous, bool start) {
  168. // Start with default values
  169. uint16_t config = 0;
  170. config |= ADS1X15_REG_CONFIG_PGA_4_096V; // Set PGA/voltage range (0x0200)
  171. config |= ADS1X15_REG_CONFIG_DR_MASK; // Always at max speed (0x00E0)
  172. //config |= ADS1X15_REG_CONFIG_CMODE_TRAD; // Traditional comparator (default val) (0x0000)
  173. //config |= ADS1X15_REG_CONFIG_CPOL_ACTVLOW; // Alert/Rdy active low (default val) (0x0000)
  174. //config |= ADS1X15_REG_CONFIG_CLAT_NONLAT; // Non-latching (default val) (0x0000)
  175. config |= ADS1X15_REG_CONFIG_CQUE_NONE; // Disable the comparator (default val) (0x0003)
  176. if (start) {
  177. config |= ADS1X15_REG_CONFIG_OS_SINGLE; // Start a single-conversion (0x8000)
  178. }
  179. if (continuous) {
  180. //config |= ADS1X15_REG_CONFIG_MODE_CONTIN; // Continuous mode (default) (0x0000)
  181. } else {
  182. config |= ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (0x0100)
  183. }
  184. config |= ((channel + 4) << 12); // Set single-ended input channel (0x4000 - 0x7000)
  185. #if EMON_DEBUG
  186. Serial.printf("[EMON] ADS1X115 Config Registry: %04X\n", config);
  187. #endif
  188. // Write config register to the ADC
  189. #if I2C_USE_BRZO
  190. uint8_t buffer[3];
  191. buffer[0] = ADS1X15_REG_POINTER_CONFIG;
  192. buffer[1] = config >> 8;
  193. buffer[2] = config & 0xFF;
  194. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  195. brzo_i2c_write(buffer, 3, false);
  196. brzo_i2c_end_transaction();
  197. #else
  198. Wire.beginTransmission(_address);
  199. Wire.write((uint8_t) ADS1X15_REG_POINTER_CONFIG);
  200. Wire.write((uint8_t) (config >> 8));
  201. Wire.write((uint8_t) (config & 0xFF));
  202. Wire.endTransmission();
  203. #endif
  204. }
  205. double getCurrent(unsigned char channel) {
  206. // Force stop by setting single mode and back to continuous
  207. static unsigned char previous = 9;
  208. if (previous != channel) {
  209. setConfigRegistry(channel, true, false);
  210. setConfigRegistry(channel, false, false);
  211. setConfigRegistry(channel, false, true);
  212. delay(10);
  213. readADC(channel);
  214. previous = channel;
  215. }
  216. setConfigRegistry(channel, true, true);
  217. return read(channel, _pivot[channel]);
  218. }
  219. unsigned int readADC(unsigned char channel) {
  220. unsigned int value = 0;
  221. #if I2C_USE_BRZO
  222. uint8_t buffer[3];
  223. buffer[0] = ADS1X15_REG_POINTER_CONVERT;
  224. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  225. brzo_i2c_write(buffer, 1, false);
  226. brzo_i2c_read(buffer, 2, false);
  227. brzo_i2c_end_transaction();
  228. value |= buffer[0] << 8;
  229. value |= buffer[1];
  230. #else
  231. Wire.beginTransmission(_address);
  232. Wire.write(ADS1X15_REG_POINTER_CONVERT);
  233. Wire.endTransmission();
  234. Wire.requestFrom(_address, (unsigned char) 2);
  235. value |= Wire.read() << 8;
  236. value |= Wire.read();
  237. #endif
  238. if (!_is_ads1115) value >>= ADS1015_BIT_SHIFT;
  239. delayMicroseconds(500);
  240. return value;
  241. }
  242. bool _is_ads1115 = true;
  243. unsigned char _address;
  244. unsigned char _mask;
  245. unsigned char _ports;
  246. double _pivot[ADS1X15_CHANNELS] = {0};
  247. double _current[ADS1X15_CHANNELS] = {0};
  248. //unsigned long _energy[ADS1X15_CHANNELS] = {0};
  249. //unsigned long _delta[ADS1X15_CHANNELS] = {0};
  250. };