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.

280 lines
11 KiB

  1. // -----------------------------------------------------------------------------
  2. // Energy monitor sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. #include "EmonSensor.h"
  8. #include <ADS1115.h>
  9. /*
  10. #if I2C_USE_BRZO
  11. #include <brzo_i2c.h>
  12. #else
  13. #include <Wire.h>
  14. #endif
  15. #define ADS1015_CONVERSIONDELAY (1)
  16. #define ADS1115_CONVERSIONDELAY (8)
  17. #define ADS1015_BIT_SHIFT (4)
  18. #define ADS1115_BIT_SHIFT (0)
  19. #define ADS1015_REG_POINTER_MASK (0x03)
  20. #define ADS1015_REG_POINTER_CONVERT (0x00)
  21. #define ADS1015_REG_POINTER_CONFIG (0x01)
  22. #define ADS1015_REG_POINTER_LOWTHRESH (0x02)
  23. #define ADS1015_REG_POINTER_HITHRESH (0x03)
  24. #define ADS1015_REG_CONFIG_OS_MASK (0x8000)
  25. #define ADS1015_REG_CONFIG_OS_SINGLE (0x8000) // Write: Set to start a single-conversion
  26. #define ADS1015_REG_CONFIG_OS_BUSY (0x0000) // Read: Bit = 0 when conversion is in progress
  27. #define ADS1015_REG_CONFIG_OS_NOTBUSY (0x8000) // Read: Bit = 1 when device is not performing a conversion
  28. #define ADS1015_REG_CONFIG_MUX_MASK (0x7000)
  29. #define ADS1015_REG_CONFIG_MUX_DIFF_0_1 (0x0000) // Differential P = AIN0, N = AIN1 (default)
  30. #define ADS1015_REG_CONFIG_MUX_DIFF_0_3 (0x1000) // Differential P = AIN0, N = AIN3
  31. #define ADS1015_REG_CONFIG_MUX_DIFF_1_3 (0x2000) // Differential P = AIN1, N = AIN3
  32. #define ADS1015_REG_CONFIG_MUX_DIFF_2_3 (0x3000) // Differential P = AIN2, N = AIN3
  33. #define ADS1015_REG_CONFIG_MUX_SINGLE_0 (0x4000) // Single-ended AIN0
  34. #define ADS1015_REG_CONFIG_MUX_SINGLE_1 (0x5000) // Single-ended AIN1
  35. #define ADS1015_REG_CONFIG_MUX_SINGLE_2 (0x6000) // Single-ended AIN2
  36. #define ADS1015_REG_CONFIG_MUX_SINGLE_3 (0x7000) // Single-ended AIN3
  37. #define ADS1015_REG_CONFIG_PGA_MASK (0x0E00)
  38. #define ADS1015_REG_CONFIG_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3
  39. #define ADS1015_REG_CONFIG_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1
  40. #define ADS1015_REG_CONFIG_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default)
  41. #define ADS1015_REG_CONFIG_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4
  42. #define ADS1015_REG_CONFIG_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8
  43. #define ADS1015_REG_CONFIG_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16
  44. #define ADS1015_REG_CONFIG_MODE_MASK (0x0100)
  45. #define ADS1015_REG_CONFIG_MODE_CONTIN (0x0000) // Continuous conversion mode
  46. #define ADS1015_REG_CONFIG_MODE_SINGLE (0x0100) // Power-down single-shot mode (default)
  47. #define ADS1015_REG_CONFIG_DR_MASK (0x00E0)
  48. #define ADS1015_REG_CONFIG_DR_128SPS (0x0000) // 128 samples per second
  49. #define ADS1015_REG_CONFIG_DR_250SPS (0x0020) // 250 samples per second
  50. #define ADS1015_REG_CONFIG_DR_490SPS (0x0040) // 490 samples per second
  51. #define ADS1015_REG_CONFIG_DR_920SPS (0x0060) // 920 samples per second
  52. #define ADS1015_REG_CONFIG_DR_1600SPS (0x0080) // 1600 samples per second (default)
  53. #define ADS1015_REG_CONFIG_DR_2400SPS (0x00A0) // 2400 samples per second
  54. #define ADS1015_REG_CONFIG_DR_3300SPS (0x00C0) // 3300 samples per second
  55. #define ADS1015_REG_CONFIG_CMODE_MASK (0x0010)
  56. #define ADS1015_REG_CONFIG_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default)
  57. #define ADS1015_REG_CONFIG_CMODE_WINDOW (0x0010) // Window comparator
  58. #define ADS1015_REG_CONFIG_CPOL_MASK (0x0008)
  59. #define ADS1015_REG_CONFIG_CPOL_ACTVLOW (0x0000) // ALERT/RDY pin is low when active (default)
  60. #define ADS1015_REG_CONFIG_CPOL_ACTVHI (0x0008) // ALERT/RDY pin is high when active
  61. #define ADS1015_REG_CONFIG_CLAT_MASK (0x0004) // Determines if ALERT/RDY pin latches once asserted
  62. #define ADS1015_REG_CONFIG_CLAT_NONLAT (0x0000) // Non-latching comparator (default)
  63. #define ADS1015_REG_CONFIG_CLAT_LATCH (0x0004) // Latching comparator
  64. #define ADS1015_REG_CONFIG_CQUE_MASK (0x0003)
  65. #define ADS1015_REG_CONFIG_CQUE_1CONV (0x0000) // Assert ALERT/RDY after one conversions
  66. #define ADS1015_REG_CONFIG_CQUE_2CONV (0x0001) // Assert ALERT/RDY after two conversions
  67. #define ADS1015_REG_CONFIG_CQUE_4CONV (0x0002) // Assert ALERT/RDY after four conversions
  68. #define ADS1015_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default)
  69. */
  70. #define EMON_ADS1115_CHANNELS 4
  71. #define EMON_ADS1115_MAGNITUDES_PER_PORT 2
  72. class EmonADS1115Sensor : public EmonSensor {
  73. public:
  74. EmonADS1115Sensor(unsigned char address, unsigned char mask, double voltage, unsigned char bits, double ref, double ratio): EmonSensor(voltage, bits, ref, ratio) {
  75. // Cache
  76. _address = address;
  77. _mask = mask;
  78. _ports = 0;
  79. while (mask) {
  80. if (mask & 0x01) ++_ports;
  81. mask = mask >> 1;
  82. }
  83. _count = _ports * EMON_ADS1115_MAGNITUDES_PER_PORT;
  84. // Initialize
  85. _ads = new ADS1115(_address);
  86. _ads->initialize();
  87. _ads->setMode(ADS1115_MODE_SINGLESHOT);
  88. _ads->setRate(ADS1115_RATE_860);
  89. _ads->setGain(ADS1115_PGA_4P096);
  90. _ads->setConversionReadyPinMode();
  91. // warmup
  92. read(_address);
  93. }
  94. // Descriptive name of the sensor
  95. String name() {
  96. char buffer[30];
  97. snprintf(buffer, sizeof(buffer), "EMON @ ADS1115 @ I2C (0x%02X)", _address);
  98. return String(buffer);
  99. }
  100. // Descriptive name of the slot # index
  101. String slot(unsigned char index) {
  102. char buffer[35];
  103. unsigned char port = getChannel(index / EMON_ADS1115_MAGNITUDES_PER_PORT);
  104. snprintf(buffer, sizeof(buffer), "EMON @ ADS1115 (A%d) @ I2C (0x%02X)", port, _address);
  105. return String(buffer);
  106. }
  107. // Type for slot # index
  108. magnitude_t type(unsigned char index) {
  109. if (index < _count) {
  110. _error = SENSOR_ERROR_OK;
  111. unsigned char port = getChannel(index / EMON_ADS1115_MAGNITUDES_PER_PORT);
  112. unsigned char magnitude = index % EMON_ADS1115_MAGNITUDES_PER_PORT;
  113. if (magnitude == 0) return MAGNITUDE_CURRENT;
  114. if (magnitude == 1) return MAGNITUDE_POWER_APPARENT;
  115. //if (magnitude == 2) return MAGNITUDE_ENERGY;
  116. //if (magnitude == 3) return MAGNITUDE_ENERGY_DELTA;
  117. }
  118. _error = SENSOR_ERROR_OUT_OF_RANGE;
  119. return MAGNITUDE_NONE;
  120. }
  121. void pre() {
  122. //static unsigned long last = 0;
  123. for (unsigned char index=0; index<_ports; index++) {
  124. unsigned char port = getChannel(index);
  125. _current[port] = read(port);
  126. //if (last > 0) {
  127. // _delta[port] = _current[port] * _voltage * (millis() - last) / 1000;
  128. //}
  129. //_energy[port] += _delta[port];
  130. }
  131. //last = millis();
  132. }
  133. // Current value for slot # index
  134. double value(unsigned char index) {
  135. if (index < _count) {
  136. _error = SENSOR_ERROR_OK;
  137. unsigned char port = getChannel(index / EMON_ADS1115_MAGNITUDES_PER_PORT);
  138. unsigned char magnitude = index % EMON_ADS1115_MAGNITUDES_PER_PORT;
  139. if (magnitude == 0) return _current[port];
  140. if (magnitude == 1) return _current[port] * _voltage;
  141. //if (magnitude == 2) return _energy[port];
  142. //if (magnitude == 3) return _delta[port];
  143. }
  144. _error = SENSOR_ERROR_OUT_OF_RANGE;
  145. return 0;
  146. }
  147. protected:
  148. unsigned char getChannel(unsigned char port) {
  149. unsigned char count = 0;
  150. unsigned char bit = 1;
  151. for (unsigned char channel=0; channel<EMON_ADS1115_CHANNELS; channel++) {
  152. if ((_mask & bit) == bit) {
  153. if (count == port) return channel;
  154. ++count;
  155. }
  156. bit <<= 1;
  157. }
  158. return 0;
  159. }
  160. unsigned int readADC(unsigned char channel) {
  161. if (channel < EMON_ADS1115_CHANNELS) {
  162. _ads->setMultiplexer(channel + 4);
  163. return _ads->getConversion(true);
  164. }
  165. return 0;
  166. }
  167. /*
  168. unsigned int readADC(unsigned char channel) {
  169. if (channel > 3) return 0;
  170. channel = 3;
  171. unsigned int value;
  172. // Start with default values
  173. uint16_t config = 0;
  174. config |= ADS1015_REG_CONFIG_CQUE_NONE; // Disable the comparator (default val)
  175. config |= ADS1015_REG_CONFIG_CLAT_NONLAT; // Non-latching (default val)
  176. config |= ADS1015_REG_CONFIG_CPOL_ACTVLOW; // Alert/Rdy active low (default val)
  177. config |= ADS1015_REG_CONFIG_CMODE_TRAD; // Traditional comparator (default val)
  178. config |= ADS1015_REG_CONFIG_DR_1600SPS; // 1600 samples per second (default)
  179. config |= ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
  180. config |= ADS1015_REG_CONFIG_OS_SINGLE; // Set 'start single-conversion' bit
  181. config |= EMON_ADS1115_GAIN; // Set PGA/voltage range
  182. config |= ((channel + 4) << 12); // Set single-ended input channel
  183. Serial.println(config);
  184. // Write config register to the ADC
  185. #if I2C_USE_BRZO
  186. uint8_t buffer[3];
  187. buffer[0] = ADS1015_REG_POINTER_CONFIG;
  188. buffer[1] = config >> 8;
  189. buffer[2] = config & 0xFF;
  190. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  191. brzo_i2c_write(buffer, 3, false);
  192. //brzo_i2c_end_transaction();
  193. #else
  194. Wire.beginTransmission(_address);
  195. Wire.write((uint8_t) ADS1015_REG_POINTER_CONFIG);
  196. Wire.write((uint8_t) (config >> 8));
  197. Wire.write((uint8_t) (config & 0xFF));
  198. Wire.endTransmission();
  199. #endif
  200. // Wait for the conversion to complete
  201. unsigned long start = millis();
  202. while (millis() - start < ADS1115_CONVERSIONDELAY) delay(1);
  203. // Read the conversion results
  204. // Shift 12-bit results right 4 bits for the ADS1015
  205. #if I2C_USE_BRZO
  206. buffer[0] = ADS1015_REG_POINTER_CONVERT;
  207. //brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  208. brzo_i2c_write(buffer, 1, false);
  209. brzo_i2c_read(buffer, 2, false);
  210. brzo_i2c_end_transaction();
  211. value = (buffer[0] & 0x0F) << 8;
  212. value |= buffer[1];
  213. #else
  214. Wire.beginTransmission(_address);
  215. Wire.write(ADS1015_REG_POINTER_CONVERT);
  216. Wire.endTransmission();
  217. Wire.requestFrom(_address, (unsigned char) 2);
  218. value = Wire.read() << 8;
  219. value |= Wire.read();
  220. #endif
  221. return value;
  222. }
  223. */
  224. ADS1115 * _ads;
  225. unsigned char _address;
  226. unsigned char _mask;
  227. unsigned char _ports;
  228. double _current[EMON_ADS1115_CHANNELS] = {0, 0, 0, 0};
  229. //unsigned long _energy[EMON_ADS1115_CHANNELS] = {0, 0, 0, 0};
  230. //unsigned long _delta[EMON_ADS1115_CHANNELS] = {0, 0, 0, 0};
  231. };