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.

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