This commit is contained in:
2024-05-23 21:24:52 +02:00
commit a3efe8274b
166 changed files with 15713 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c
from esphome.const import CONF_ID
DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["sensor", "voltage_sampler"]
MULTI_CONF = True
ads1100_ns = cg.esphome_ns.namespace("ads1100")
ADS1100Component = ads1100_ns.class_("ADS1100Component", cg.Component, i2c.I2CDevice)
CONF_CONTINUOUS_MODE = "continuous_mode"
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(ADS1100Component),
cv.Optional(CONF_CONTINUOUS_MODE, default=False): cv.boolean,
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(i2c.i2c_device_schema(None))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
cg.add(var.set_continuous_mode(config[CONF_CONTINUOUS_MODE]))

View File

@@ -0,0 +1,170 @@
#include "ads1100.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace ads1100 {
static const char *const TAG = "ads1100";
static const uint8_t ADS1100_REGISTER_CONVERSION = 0x00;
static const uint8_t ADS1100_REGISTER_CONFIG = 0x01;
static const uint8_t ADS1100_DATA_RATE_860_SPS = 0b111;
void ADS1100Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up ADS1100...");
uint16_t value;
if (!this->read_byte_16(ADS1100_REGISTER_CONVERSION, &value)) {
this->mark_failed();
return;
}
uint16_t config = 0;
// Clear single-shot bit
// 0b0xxxxxxxxxxxxxxx
config |= 0b0000000000000000;
// Setup multiplexer
// 0bx000xxxxxxxxxxxx
config |= ADS1100_MULTIPLEXER_8 << 12;
// Setup Gain
// 0bxxxx000xxxxxxxxx
config |= ADS1100_GAIN_6P144 << 9;
if (this->continuous_mode_) {
// Set continuous mode
// 0bxxxxxxx0xxxxxxxx
config |= 0b0000000000000000;
} else {
// Set singleshot mode
// 0bxxxxxxx1xxxxxxxx
config |= 0b0000000100000000;
}
// Set data rate - 860 samples per second (we're in singleshot mode)
// 0bxxxxxxxx100xxxxx
config |= ADS1100_DATA_RATE_860_SPS << 5;
// Set comparator mode - hysteresis
// 0bxxxxxxxxxxx0xxxx
config |= 0b0000000000000000;
// Set comparator polarity - active low
// 0bxxxxxxxxxxxx0xxx
config |= 0b0000000000000000;
// Set comparator latch enabled - false
// 0bxxxxxxxxxxxxx0xx
config |= 0b0000000000000000;
// Set comparator que mode - disabled
// 0bxxxxxxxxxxxxxx11
config |= 0b0000000000000011;
if (!this->write_byte_16(ADS1100_REGISTER_CONFIG, config)) {
this->mark_failed();
return;
}
this->prev_config_ = config;
}
void ADS1100Component::dump_config() {
ESP_LOGCONFIG(TAG, "Setting up ADS1100...");
LOG_I2C_DEVICE(this);
if (this->is_failed()) {
ESP_LOGE(TAG, "Communication with ADS1100 failed!");
}
for (auto *sensor : this->sensors_) {
LOG_SENSOR(" ", "Sensor", sensor);
ESP_LOGCONFIG(TAG, " Rate: %u", sensor->get_rate());
ESP_LOGCONFIG(TAG, " Gain: %u", sensor->get_gain());
}
}
float ADS1100Component::request_measurement(ADS1100Sensor *sensor) {
uint16_t config = this->prev_config_;
// Multiplexer
// 0bxBBBxxxxxxxxxxxx
config &= 0b1000111111111111;
config |= (sensor->get_multiplexer() & 0b111) << 12;
// Gain
// 0bxxxxBBBxxxxxxxxx
config &= 0b1111000111111111;
config |= (sensor->get_gain() & 0b111) << 9;
if (!this->continuous_mode_) {
// Start conversion
config |= 0b1000000000000000;
}
if (!this->continuous_mode_ || this->prev_config_ != config) {
if (!this->write_byte_16(ADS1100_REGISTER_CONFIG, config)) {
this->status_set_warning();
return NAN;
}
this->prev_config_ = config;
// about 1.2 ms with 860 samples per second
delay(2);
// in continuous mode, conversion will always be running, rely on the delay
// to ensure conversion is taking place with the correct settings
// can we use the rdy pin to trigger when a conversion is done?
if (!this->continuous_mode_) {
uint32_t start = millis();
while (this->read_byte_16(ADS1100_REGISTER_CONFIG, &config) && (config >> 15) == 0) {
if (millis() - start > 100) {
ESP_LOGW(TAG, "Reading ADS1100 timed out");
this->status_set_warning();
return NAN;
}
yield();
}
}
}
uint16_t raw_conversion;
if (!this->read_byte_16(ADS1100_REGISTER_CONVERSION, &raw_conversion)) {
this->status_set_warning();
return NAN;
}
auto signed_conversion = static_cast<int16_t>(raw_conversion);
float millivolts;
switch (sensor->get_gain()) {
case ADS1100_GAIN_6P144:
millivolts = signed_conversion * 0.187500f;
break;
case ADS1100_GAIN_4P096:
millivolts = signed_conversion * 0.125000f;
break;
case ADS1100_GAIN_2P048:
millivolts = signed_conversion * 0.062500f;
break;
case ADS1100_GAIN_1P024:
millivolts = signed_conversion * 0.031250f;
break;
case ADS1100_GAIN_0P512:
millivolts = signed_conversion * 0.015625f;
break;
case ADS1100_GAIN_0P256:
millivolts = signed_conversion * 0.007813f;
break;
default:
millivolts = NAN;
}
this->status_clear_warning();
return millivolts / 1e3f;
}
float ADS1100Sensor::sample() { return this->parent_->request_measurement(this); }
void ADS1100Sensor::update() {
float v = this->parent_->request_measurement(this);
if (!std::isnan(v)) {
ESP_LOGD(TAG, "'%s': Got Voltage=%fV", this->get_name().c_str(), v);
this->publish_state(v);
}
}
} // namespace ads1100
} // namespace esphome

View File

@@ -0,0 +1,76 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/voltage_sampler/voltage_sampler.h"
namespace esphome {
namespace ads1100 {
enum ADS1100OSMode {
ADS1100_OSMODE_SINGLE = 0x80,
ADS1100_OSMODE_BUSY = 0x00,
ADS1100_OSMODE_NOTBUSY = 0x80,
};
enum ADS1100Mode {
ADS1100_MODE_CONTINUOUS = 0x00,
ADS1100_MODE_SINGLE = 0x10,
};
enum ADS1100Rate {
ADS1100_RATE_128 = 0x00,
ADS1100_RATE_32 = 0x04,
ADS1100_RATE_16 = 0x08,
ADS1100_RATE_8 = 0x0c,
};
enum ADS1100Gain {
ADS1100_GAIN_1 = 0x01,
ADS1100_GAIN_2 = 0x02,
ADS1100_GAIN_4 = 0x03,
ADS1100_GAIN_8 = 0x04,
};
class ADS1100Sensor;
class ADS1100Component : public Component, public i2c::I2CDevice {
public:
void register_sensor(ADS1100Sensor *obj) { this->sensors_.push_back(obj); }
/// Set up the internal sensor array.
void setup() override;
void dump_config() override;
/// HARDWARE_LATE setup priority
float get_setup_priority() const override { return setup_priority::DATA; }
void set_continuous_mode(bool continuous_mode) { continuous_mode_ = continuous_mode; }
/// Helper method to request a measurement from a sensor.
float request_measurement(ADS1100Sensor *sensor);
protected:
std::vector<ADS1100Sensor *> sensors_;
uint16_t prev_config_{0};
bool continuous_mode_;
};
/// Internal holder class that is in instance of Sensor so that the hub can create individual sensors.
class ADS1100Sensor : public sensor::Sensor, public PollingComponent, public voltage_sampler::VoltageSampler {
public:
ADS1100Sensor(ADS1100Component *parent) : parent_(parent) {}
void update() override;
void set_rate(ADS1100Rate rate) { rate_ = rate; }
void set_gain(ADS1100Gain gain) { gain_ = gain; }
float sample() override;
uint8_t get_rate() const { return rate_; }
uint8_t get_gain() const { return gain_; }
protected:
ADS1100Component *parent_;
ADS1100Rate rate_;
ADS1100Gain gain_;
};
} // namespace ads1100
} // namespace esphome

View File

@@ -0,0 +1,81 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, voltage_sampler
from esphome.const import (
CONF_GAIN,
CONF_MULTIPLEXER,
DEVICE_CLASS_VOLTAGE,
STATE_CLASS_MEASUREMENT,
UNIT_VOLT,
CONF_ID,
)
from . import ads1100_ns, ADS1100Component
DEPENDENCIES = ["ads1100"]
ADS1100Multiplexer = ads1100_ns.enum("ADS1100Multiplexer")
MUX = {
"A0_A1": ADS1100Multiplexer.ADS1100_MULTIPLEXER_P0_N1,
"A0_A3": ADS1100Multiplexer.ADS1100_MULTIPLEXER_P0_N3,
"A1_A3": ADS1100Multiplexer.ADS1100_MULTIPLEXER_P1_N3,
"A2_A3": ADS1100Multiplexer.ADS1100_MULTIPLEXER_P2_N3,
"A0_GND": ADS1100Multiplexer.ADS1100_MULTIPLEXER_P0_NG,
"A1_GND": ADS1100Multiplexer.ADS1100_MULTIPLEXER_P1_NG,
"A2_GND": ADS1100Multiplexer.ADS1100_MULTIPLEXER_P2_NG,
"A3_GND": ADS1100Multiplexer.ADS1100_MULTIPLEXER_P3_NG,
}
ADS1100Gain = ads1100_ns.enum("ADS1100Gain")
GAIN = {
"6.144": ADS1100Gain.ADS1100_GAIN_6P144,
"4.096": ADS1100Gain.ADS1100_GAIN_4P096,
"2.048": ADS1100Gain.ADS1100_GAIN_2P048,
"1.024": ADS1100Gain.ADS1100_GAIN_1P024,
"0.512": ADS1100Gain.ADS1100_GAIN_0P512,
"0.256": ADS1100Gain.ADS1100_GAIN_0P256,
}
def validate_gain(value):
if isinstance(value, float):
value = f"{value:0.03f}"
elif not isinstance(value, str):
raise cv.Invalid(f'invalid gain "{value}"')
return cv.enum(GAIN)(value)
ADS1100Sensor = ads1100_ns.class_(
"ADS1100Sensor", sensor.Sensor, cg.PollingComponent, voltage_sampler.VoltageSampler
)
CONF_ADS1100_ID = "ads1100_id"
CONFIG_SCHEMA = (
sensor.sensor_schema(
unit_of_measurement=UNIT_VOLT,
accuracy_decimals=3,
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
)
.extend(
{
cv.GenerateID(): cv.declare_id(ADS1100Sensor),
cv.GenerateID(CONF_ADS1100_ID): cv.use_id(ADS1100Component),
cv.Required(CONF_MULTIPLEXER): cv.enum(MUX, upper=True, space="_"),
cv.Required(CONF_GAIN): validate_gain,
}
)
.extend(cv.polling_component_schema("60s"))
)
async def to_code(config):
paren = await cg.get_variable(config[CONF_ADS1100_ID])
var = cg.new_Pvariable(config[CONF_ID], paren)
await sensor.register_sensor(var, config)
await cg.register_component(var, config)
cg.add(var.set_multiplexer(config[CONF_MULTIPLEXER]))
cg.add(var.set_gain(config[CONF_GAIN]))
cg.add(paren.register_sensor(var))