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.

147 lines
4.3 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. if (_address != address) _dirty = true;
  32. _address = address;
  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. if (_address == 0) {
  43. unsigned char addresses[] = {0x50, 0x51, 0x52, 0x54, 0x55, 0x56, 0x58, 0x59, 0x5A};
  44. _address = i2cFindFirst(9, addresses);
  45. }
  46. if (_address == 0) {
  47. _error = SENSOR_ERROR_UNKNOWN_ID;
  48. return;
  49. }
  50. // Init sensor
  51. #if I2C_USE_BRZO
  52. uint8_t buffer[2];
  53. buffer[0] = ADC121_REG_CONFIG;
  54. buffer[1] = 0x00;
  55. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  56. brzo_i2c_write(buffer, 2, false);
  57. brzo_i2c_end_transaction();
  58. #else
  59. Wire.beginTransmission(_address);
  60. Wire.write(ADC121_REG_CONFIG);
  61. Wire.write(0x00);
  62. Wire.endTransmission();
  63. #endif
  64. // Just one channel
  65. _count = _magnitudes;
  66. // Bit depth
  67. _resolution = ADC121_RESOLUTION;
  68. // Call the parent class method
  69. EmonSensor::begin();
  70. // warmup channel 0 (the only one)
  71. _pivot = _adc_counts >> 1;
  72. read(0, _pivot);
  73. }
  74. // Descriptive name of the sensor
  75. String name() {
  76. char buffer[30];
  77. snprintf(buffer, sizeof(buffer), "EMON @ ADC121 @ I2C (0x%02X)", _address);
  78. return String(buffer);
  79. }
  80. // Pre-read hook (usually to populate registers with up-to-date data)
  81. void pre() {
  82. if (_address == 0) {
  83. _error = SENSOR_ERROR_UNKNOWN_ID;
  84. return;
  85. }
  86. EmonAnalogSensor:pre();
  87. }
  88. protected:
  89. // ---------------------------------------------------------------------
  90. // Protected
  91. // ---------------------------------------------------------------------
  92. unsigned int readADC(unsigned char channel) {
  93. unsigned int value;
  94. #if I2C_USE_BRZO
  95. uint8_t buffer[2];
  96. buffer[0] = ADC121_REG_RESULT;
  97. brzo_i2c_start_transaction(_address, I2C_SCL_FREQUENCY);
  98. brzo_i2c_write(buffer, 1, false);
  99. brzo_i2c_read(buffer, 2, false);
  100. brzo_i2c_end_transaction();
  101. value = (buffer[0] & 0x0F) << 8;
  102. value |= buffer[1];
  103. #else
  104. Wire.beginTransmission(_address);
  105. Wire.write(ADC121_REG_RESULT);
  106. Wire.endTransmission();
  107. Wire.requestFrom(_address, (unsigned char) 2);
  108. value = (Wire.read() & 0x0F) << 8;
  109. value = value + Wire.read();
  110. #endif
  111. return value;
  112. }
  113. unsigned char _address = 0;
  114. };