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.

145 lines
4.2 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 "EmonAnalogSensor.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 EmonAnalogSensor {
  26. public:
  27. // ---------------------------------------------------------------------
  28. // Public
  29. // ---------------------------------------------------------------------
  30. EmonADC121Sensor(): EmonAnalogSensor() {
  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. EmonAnalogSensor:pre();
  82. }
  83. protected:
  84. // ---------------------------------------------------------------------
  85. // Protected
  86. // ---------------------------------------------------------------------
  87. unsigned int readADC(unsigned char channel) {
  88. (void) channel;
  89. unsigned int value;
  90. #if I2C_USE_BRZO
  91. uint8_t buffer[2];
  92. buffer[0] = ADC121_REG_RESULT;
  93. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  94. brzo_i2c_write(buffer, 1, false);
  95. brzo_i2c_read(buffer, 2, false);
  96. brzo_i2c_end_transaction();
  97. value = (buffer[0] & 0x0F) << 8;
  98. value |= buffer[1];
  99. #else
  100. Wire.beginTransmission(_address);
  101. Wire.write(ADC121_REG_RESULT);
  102. Wire.endTransmission();
  103. Wire.requestFrom(_address, (unsigned char) 2);
  104. value = (Wire.read() & 0x0F) << 8;
  105. value = value + Wire.read();
  106. #endif
  107. return value;
  108. }
  109. };
  110. #endif // SENSOR_SUPPORT && EMON_ADC121_SUPPORT