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.

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