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.

192 lines
5.7 KiB

  1. // -----------------------------------------------------------------------------
  2. // ADS121-based Energy Monitor Sensor over I2C
  3. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && EMON_ADC121_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "EmonSensor.h"
  9. #if I2C_USE_BRZO
  10. #include <brzo_i2c.h>
  11. #else
  12. #include <Wire.h>
  13. #endif
  14. // ADC121 Registers
  15. #define ADC121_REG_RESULT 0x00
  16. #define ADC121_REG_ALERT 0x01
  17. #define ADC121_REG_CONFIG 0x02
  18. #define ADC121_REG_LIMITL 0x03
  19. #define ADC121_REG_LIMITH 0x04
  20. #define ADC121_REG_HYST 0x05
  21. #define ADC121_REG_CONVL 0x06
  22. #define ADC121_REG_CONVH 0x07
  23. #define ADC121_RESOLUTION 12
  24. #define ADC121_CHANNELS 1
  25. class EmonADC121Sensor : public EmonSensor {
  26. public:
  27. // ---------------------------------------------------------------------
  28. // Public
  29. // ---------------------------------------------------------------------
  30. EmonADC121Sensor(): EmonSensor() {
  31. _channels = ADC121_CHANNELS;
  32. _sensor_id = SENSOR_EMON_ADC121_ID;
  33. init();
  34. }
  35. // ---------------------------------------------------------------------
  36. // Sensor API
  37. // ---------------------------------------------------------------------
  38. // Initialization method, must be idempotent
  39. void begin() {
  40. if (!_dirty) return;
  41. _dirty = false;
  42. // Discover
  43. unsigned char addresses[] = {0x50, 0x51, 0x52, 0x54, 0x55, 0x56, 0x58, 0x59, 0x5A};
  44. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  45. if (_address == 0) return;
  46. // Init sensor
  47. #if I2C_USE_BRZO
  48. uint8_t buffer[2];
  49. buffer[0] = ADC121_REG_CONFIG;
  50. buffer[1] = 0x00;
  51. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  52. brzo_i2c_write(buffer, 2, false);
  53. brzo_i2c_end_transaction();
  54. #else
  55. Wire.beginTransmission(_address);
  56. Wire.write(ADC121_REG_CONFIG);
  57. Wire.write(0x00);
  58. Wire.endTransmission();
  59. #endif
  60. // Just one channel
  61. _count = _magnitudes;
  62. // Bit depth
  63. _resolution = ADC121_RESOLUTION;
  64. // Call the parent class method
  65. EmonSensor::begin();
  66. // warmup channel 0 (the only one)
  67. read(0);
  68. }
  69. // Descriptive name of the sensor
  70. String description() {
  71. char buffer[30];
  72. snprintf(buffer, sizeof(buffer), "EMON @ ADC121 @ I2C (0x%02X)", _address);
  73. return String(buffer);
  74. }
  75. // Pre-read hook (usually to populate registers with up-to-date data)
  76. void pre() {
  77. if (_address == 0) {
  78. _error = SENSOR_ERROR_UNKNOWN_ID;
  79. return;
  80. }
  81. _current[0] = read(0);
  82. #if EMON_REPORT_ENERGY
  83. static unsigned long last = 0;
  84. if (last > 0) {
  85. _energy[0] += (_current[0] * _voltage * (millis() - last) / 1000);
  86. }
  87. last = millis();
  88. #endif
  89. }
  90. // Type for slot # index
  91. unsigned char type(unsigned char index) {
  92. _error = SENSOR_ERROR_OK;
  93. unsigned char i=0;
  94. #if EMON_REPORT_CURRENT
  95. if (index == i++) return MAGNITUDE_CURRENT;
  96. #endif
  97. #if EMON_REPORT_POWER
  98. if (index == i++) return MAGNITUDE_POWER_APPARENT;
  99. #endif
  100. #if EMON_REPORT_ENERGY
  101. if (index == i) return MAGNITUDE_ENERGY;
  102. #endif
  103. _error = SENSOR_ERROR_OUT_OF_RANGE;
  104. return MAGNITUDE_NONE;
  105. }
  106. // Current value for slot # index
  107. double value(unsigned char index) {
  108. _error = SENSOR_ERROR_OK;
  109. unsigned char channel = index / _magnitudes;
  110. unsigned char i=0;
  111. #if EMON_REPORT_CURRENT
  112. if (index == i++) return _current[channel];
  113. #endif
  114. #if EMON_REPORT_POWER
  115. if (index == i++) return _current[channel] * _voltage;
  116. #endif
  117. #if EMON_REPORT_ENERGY
  118. if (index == i) return _energy[channel];
  119. #endif
  120. _error = SENSOR_ERROR_OUT_OF_RANGE;
  121. return 0;
  122. }
  123. protected:
  124. // ---------------------------------------------------------------------
  125. // Protected
  126. // ---------------------------------------------------------------------
  127. unsigned int readADC(unsigned char channel) {
  128. (void) channel;
  129. unsigned int value;
  130. #if I2C_USE_BRZO
  131. uint8_t buffer[2];
  132. buffer[0] = ADC121_REG_RESULT;
  133. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  134. brzo_i2c_write(buffer, 1, false);
  135. brzo_i2c_read(buffer, 2, false);
  136. brzo_i2c_end_transaction();
  137. value = (buffer[0] & 0x0F) << 8;
  138. value |= buffer[1];
  139. #else
  140. Wire.beginTransmission(_address);
  141. Wire.write(ADC121_REG_RESULT);
  142. Wire.endTransmission();
  143. Wire.requestFrom(_address, (unsigned char) 2);
  144. value = (Wire.read() & 0x0F) << 8;
  145. value = value + Wire.read();
  146. #endif
  147. return value;
  148. }
  149. };
  150. #endif // SENSOR_SUPPORT && EMON_ADC121_SUPPORT