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.

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