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.

141 lines
4.1 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. #pragma once
  6. #include "Arduino.h"
  7. #include "EmonAnalogSensor.h"
  8. #if I2C_USE_BRZO
  9. #include <brzo_i2c.h>
  10. #else
  11. #include <Wire.h>
  12. #endif
  13. // ADC121 Registers
  14. #define ADC121_REG_RESULT 0x00
  15. #define ADC121_REG_ALERT 0x01
  16. #define ADC121_REG_CONFIG 0x02
  17. #define ADC121_REG_LIMITL 0x03
  18. #define ADC121_REG_LIMITH 0x04
  19. #define ADC121_REG_HYST 0x05
  20. #define ADC121_REG_CONVL 0x06
  21. #define ADC121_REG_CONVH 0x07
  22. #define ADC121_RESOLUTION 12
  23. #define ADC121_CHANNELS 1
  24. class EmonADC121Sensor : public EmonAnalogSensor {
  25. public:
  26. // ---------------------------------------------------------------------
  27. // Public
  28. // ---------------------------------------------------------------------
  29. EmonADC121Sensor(): EmonAnalogSensor() {
  30. _channels = ADC121_CHANNELS;
  31. _sensor_id = SENSOR_EMON_ADC121_ID;
  32. init();
  33. }
  34. // ---------------------------------------------------------------------
  35. // Sensor API
  36. // ---------------------------------------------------------------------
  37. // Initialization method, must be idempotent
  38. void begin() {
  39. if (!_dirty) return;
  40. _dirty = false;
  41. // Discover
  42. unsigned char addresses[] = {0x50, 0x51, 0x52, 0x54, 0x55, 0x56, 0x58, 0x59, 0x5A};
  43. _address = _begin_i2c(_address, sizeof(addresses), addresses);
  44. if (_address == 0) return;
  45. // Init sensor
  46. #if I2C_USE_BRZO
  47. uint8_t buffer[2];
  48. buffer[0] = ADC121_REG_CONFIG;
  49. buffer[1] = 0x00;
  50. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  51. brzo_i2c_write(buffer, 2, false);
  52. brzo_i2c_end_transaction();
  53. #else
  54. Wire.beginTransmission(_address);
  55. Wire.write(ADC121_REG_CONFIG);
  56. Wire.write(0x00);
  57. Wire.endTransmission();
  58. #endif
  59. // Just one channel
  60. _count = _magnitudes;
  61. // Bit depth
  62. _resolution = ADC121_RESOLUTION;
  63. // Call the parent class method
  64. EmonSensor::begin();
  65. // warmup channel 0 (the only one)
  66. read(0);
  67. }
  68. // Descriptive name of the sensor
  69. String description() {
  70. char buffer[30];
  71. snprintf(buffer, sizeof(buffer), "EMON @ ADC121 @ I2C (0x%02X)", _address);
  72. return String(buffer);
  73. }
  74. // Pre-read hook (usually to populate registers with up-to-date data)
  75. void pre() {
  76. if (_address == 0) {
  77. _error = SENSOR_ERROR_UNKNOWN_ID;
  78. return;
  79. }
  80. EmonAnalogSensor:pre();
  81. }
  82. protected:
  83. // ---------------------------------------------------------------------
  84. // Protected
  85. // ---------------------------------------------------------------------
  86. unsigned int readADC(unsigned char channel) {
  87. (void) channel;
  88. unsigned int value;
  89. #if I2C_USE_BRZO
  90. uint8_t buffer[2];
  91. buffer[0] = ADC121_REG_RESULT;
  92. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  93. brzo_i2c_write(buffer, 1, false);
  94. brzo_i2c_read(buffer, 2, false);
  95. brzo_i2c_end_transaction();
  96. value = (buffer[0] & 0x0F) << 8;
  97. value |= buffer[1];
  98. #else
  99. Wire.beginTransmission(_address);
  100. Wire.write(ADC121_REG_RESULT);
  101. Wire.endTransmission();
  102. Wire.requestFrom(_address, (unsigned char) 2);
  103. value = (Wire.read() & 0x0F) << 8;
  104. value = value + Wire.read();
  105. #endif
  106. return value;
  107. }
  108. };