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.

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