mirror of
https://github.com/esphome/esphome.git
synced 2025-02-12 01:02:06 +01:00
Some bugfixes
This commit is contained in:
parent
13f555418d
commit
5a4b9b4938
@ -1,7 +1,12 @@
|
|||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
|
from esphome import pins
|
||||||
from esphome.components import spi
|
from esphome.components import spi
|
||||||
from esphome.const import CONF_ID
|
from esphome import pins
|
||||||
|
from esphome.const import (
|
||||||
|
CONF_ID,
|
||||||
|
CONF_DC_PIN,
|
||||||
|
)
|
||||||
|
|
||||||
DEPENDENCIES = ["spi"]
|
DEPENDENCIES = ["spi"]
|
||||||
AUTO_LOAD = ["sensor", "voltage_sampler"]
|
AUTO_LOAD = ["sensor", "voltage_sampler"]
|
||||||
@ -10,20 +15,21 @@ MULTI_CONF = True
|
|||||||
ads1220_ns = cg.esphome_ns.namespace("ads1220")
|
ads1220_ns = cg.esphome_ns.namespace("ads1220")
|
||||||
ADS1220Component = ads1220_ns.class_("ADS1220Component", cg.Component, spi.SPIDevice)
|
ADS1220Component = ads1220_ns.class_("ADS1220Component", cg.Component, spi.SPIDevice)
|
||||||
|
|
||||||
CONF_CONTINUOUS_MODE = "continuous_mode"
|
|
||||||
CONFIG_SCHEMA = (
|
CONFIG_SCHEMA = (
|
||||||
cv.Schema(
|
cv.Schema(
|
||||||
{
|
{
|
||||||
cv.GenerateID(): cv.declare_id(ADS1220Component),
|
cv.GenerateID(): cv.declare_id(ADS1220Component),
|
||||||
cv.Optional(CONF_CONTINUOUS_MODE, default=False): cv.boolean,
|
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.extend(cv.COMPONENT_SCHEMA)
|
.extend(cv.COMPONENT_SCHEMA)
|
||||||
.extend(spi.spi_device_schema(None))
|
.extend(spi.spi_device_schema(None))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
await spi.register_spi_device(var, config)
|
await spi.register_spi_device(var, config)
|
||||||
|
|
||||||
|
drdy = await cg.gpio_pin_expression(config[CONF_DC_PIN])
|
||||||
|
cg.add(var.set_drdy_pin(drdy))
|
||||||
|
@ -37,7 +37,12 @@ void ADS1220Component::setup() {
|
|||||||
this->spi_setup();
|
this->spi_setup();
|
||||||
//pinMode(csPin, OUTPUT);
|
//pinMode(csPin, OUTPUT);
|
||||||
//digitalWrite(csPin, HIGH);
|
//digitalWrite(csPin, HIGH);
|
||||||
pinMode(drdyPin, INPUT);
|
//drdyPin = 21;
|
||||||
|
//pinMode(sensor->drdy_pin, INPUT_PULLUP);
|
||||||
|
|
||||||
|
this->drdy_pin->setup();
|
||||||
|
this->drdy_pin->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
|
||||||
|
|
||||||
|
|
||||||
//vRef = 2.048;
|
//vRef = 2.048;
|
||||||
vRef = 3.300;
|
vRef = 3.300;
|
||||||
@ -158,28 +163,29 @@ float ADS1220Component::request_measurement(ADS1220Sensor *sensor) {
|
|||||||
// Setup temperature sensor
|
// Setup temperature sensor
|
||||||
config = sensor->get_temp_sensor_mode();
|
config = sensor->get_temp_sensor_mode();
|
||||||
ESP_LOGI(TAG, "ADS1220 temp sensor mode: 0x%02x", config);
|
ESP_LOGI(TAG, "ADS1220 temp sensor mode: 0x%02x", config);
|
||||||
if (prev_temp_sensor != config) {
|
if (prev_temp_sensor_mode != config) {
|
||||||
//enableTemperatureSensor(config);
|
//enableTemperatureSensor(config);
|
||||||
prev_temp_sensor = config;
|
prev_temp_sensor_mode = config;
|
||||||
}
|
}
|
||||||
// Setup burnout current sources
|
// Setup burnout current sources
|
||||||
config = sensor->get_burnout_current_sources();
|
config = sensor->get_fault_test_mode();
|
||||||
ESP_LOGI(TAG, "ADS1220 fault test mode: 0x%02x", config);
|
ESP_LOGI(TAG, "ADS1220 fault test mode: 0x%02x", config);
|
||||||
if (prev_burnout_current_sources != config) {
|
if (prev_fault_test_mode != config) {
|
||||||
//enableBurnOutCurrentSources(config);
|
//enableBurnOutCurrentSources(config);
|
||||||
prev_burnout_current_sources = config;
|
prev_fault_test_mode = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Setup multiplexer
|
// Setup multiplexer
|
||||||
config = sensor->get_multiplexer();
|
config = sensor->get_multiplexer();
|
||||||
//ESP_LOGI(TAG, "ADS1220 mux: 0x%02x", (ads1220Multiplexer)config);
|
ESP_LOGI(TAG, "ADS1220 mux: 0x%02x", (ads1220Multiplexer)config);
|
||||||
if (prev_multiplexer != config) {
|
if (prev_multiplexer != config) {
|
||||||
setCompareChannels((ads1220Multiplexer)config);
|
setCompareChannels((ads1220Multiplexer)config);
|
||||||
prev_multiplexer = (ads1220Multiplexer)config;
|
prev_multiplexer = (ads1220Multiplexer)config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Start conversion!");
|
||||||
resultInMV = getVoltage_mV();
|
resultInMV = getVoltage_mV();
|
||||||
resultInVoltage = resultInMV / 1000.0;
|
resultInVoltage = resultInMV / 1000.0;
|
||||||
|
|
||||||
@ -462,28 +468,22 @@ uint32_t ADS1220Component::readResult()
|
|||||||
uint8_t data[4] = { 0x00 };
|
uint8_t data[4] = { 0x00 };
|
||||||
uint32_t rawResult = 0;
|
uint32_t rawResult = 0;
|
||||||
|
|
||||||
//ESP_LOGI(TAG, "Read result from ADS1220!");
|
|
||||||
//delay(1);
|
|
||||||
|
|
||||||
if(convMode == ADS1220_SINGLE_SHOT){
|
if(convMode == ADS1220_SINGLE_SHOT){
|
||||||
//ESP_LOGI(TAG, "Start conversion!");
|
|
||||||
//delay(1);
|
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
while(digitalRead(drdyPin)) {}
|
while(this->drdy_pin->digital_read()) {
|
||||||
|
delay(1);
|
||||||
//ESP_LOGI(TAG, "Store result to memory!");
|
}
|
||||||
//delay(1);
|
|
||||||
|
|
||||||
this->enable();
|
this->enable();
|
||||||
delay(1);
|
//delay(1);
|
||||||
|
|
||||||
data[0] = this->read_byte();
|
data[0] = this->read_byte();
|
||||||
data[1] = this->read_byte();
|
data[1] = this->read_byte();
|
||||||
data[2] = this->read_byte();
|
data[2] = this->read_byte();
|
||||||
|
|
||||||
this->disable();
|
this->disable();
|
||||||
delay(1);
|
//delay(1);
|
||||||
|
|
||||||
rawResult = data[0];
|
rawResult = data[0];
|
||||||
rawResult = (rawResult << 8) | data[1];
|
rawResult = (rawResult << 8) | data[1];
|
||||||
@ -518,7 +518,7 @@ uint8_t ADS1220Component::readRegister(uint8_t reg)
|
|||||||
uint8_t data[4] = { 0x00 };;
|
uint8_t data[4] = { 0x00 };;
|
||||||
|
|
||||||
this->enable();
|
this->enable();
|
||||||
delay(1);
|
//delay(1);
|
||||||
|
|
||||||
this->write_byte(ADS1220_RREG | (reg<<2));
|
this->write_byte(ADS1220_RREG | (reg<<2));
|
||||||
|
|
||||||
@ -527,7 +527,7 @@ uint8_t ADS1220Component::readRegister(uint8_t reg)
|
|||||||
data[2] = this->read_byte();
|
data[2] = this->read_byte();
|
||||||
|
|
||||||
this->disable();
|
this->disable();
|
||||||
delay(1);
|
//delay(1);
|
||||||
|
|
||||||
return data[0];
|
return data[0];
|
||||||
}
|
}
|
||||||
@ -544,13 +544,13 @@ void ADS1220Component::writeRegister(uint8_t reg, uint8_t val)
|
|||||||
spi_device_register_3_buffered = val;
|
spi_device_register_3_buffered = val;
|
||||||
|
|
||||||
this->enable();
|
this->enable();
|
||||||
delay(1);
|
//delay(1);
|
||||||
|
|
||||||
this->write_byte(ADS1220_WREG | (reg<<2));
|
this->write_byte(ADS1220_WREG | (reg<<2));
|
||||||
this->write_byte(val);
|
this->write_byte(val);
|
||||||
|
|
||||||
this->disable();
|
this->disable();
|
||||||
delay(1);
|
//delay(1);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -559,12 +559,12 @@ void ADS1220Component::send_command(uint8_t cmd)
|
|||||||
//ESP_LOGI(TAG, "Send command %u to ADS1220", cmd);
|
//ESP_LOGI(TAG, "Send command %u to ADS1220", cmd);
|
||||||
|
|
||||||
this->enable();
|
this->enable();
|
||||||
delay(1);
|
//delay(1);
|
||||||
|
|
||||||
this->write_byte(cmd);
|
this->write_byte(cmd);
|
||||||
|
|
||||||
this->disable();
|
this->disable();
|
||||||
delay(1);
|
//delay(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -116,13 +116,16 @@ class ADS1220Sensor;
|
|||||||
|
|
||||||
class ADS1220Component : public Component, public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_TRAILING, spi::DATA_RATE_1MHZ> {
|
class ADS1220Component : public Component, public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_TRAILING, spi::DATA_RATE_1MHZ> {
|
||||||
public:
|
public:
|
||||||
|
ADS1220Sensor *sensor;
|
||||||
void register_sensor(ADS1220Sensor *obj) { this->sensors_.push_back(obj); }
|
void register_sensor(ADS1220Sensor *obj) { this->sensors_.push_back(obj); }
|
||||||
/// Set up the internal sensor array.
|
/// Set up the internal sensor array.
|
||||||
void setup() override;
|
void setup() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
/// HARDWARE_LATE setup priority
|
/// HARDWARE_LATE setup priority
|
||||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||||
void set_continuous_mode(bool continuous_mode) { continuous_mode_ = continuous_mode; }
|
|
||||||
|
void set_drdy_pin(GPIOPin *pin) { drdy_pin = pin; }
|
||||||
|
//GPIOPin *get_drdy_pin() { return drdy_pin; }
|
||||||
|
|
||||||
/// Helper method to request a measurement from a sensor.
|
/// Helper method to request a measurement from a sensor.
|
||||||
float request_measurement(ADS1220Sensor *sensor);
|
float request_measurement(ADS1220Sensor *sensor);
|
||||||
@ -132,8 +135,9 @@ class ADS1220Component : public Component, public spi::SPIDevice<spi::BIT_ORDER_
|
|||||||
uint16_t prev_config_{0};
|
uint16_t prev_config_{0};
|
||||||
bool continuous_mode_ = false;
|
bool continuous_mode_ = false;
|
||||||
|
|
||||||
uint8_t csPin;
|
//uint8_t csPin;
|
||||||
uint8_t drdyPin = 0;
|
//uint8_t drdy_pin;
|
||||||
|
GPIOPin *drdy_pin{nullptr};
|
||||||
uint8_t regValue;
|
uint8_t regValue;
|
||||||
float vRef;
|
float vRef;
|
||||||
uint8_t gain;
|
uint8_t gain;
|
||||||
@ -141,7 +145,6 @@ class ADS1220Component : public Component, public spi::SPIDevice<spi::BIT_ORDER_
|
|||||||
bool refMeasurement;
|
bool refMeasurement;
|
||||||
ads1220ConvMode convMode;
|
ads1220ConvMode convMode;
|
||||||
bool doNotBypassPgaIfPossible = false;
|
bool doNotBypassPgaIfPossible = false;
|
||||||
InternalGPIOPin *drdy_pin{nullptr};
|
|
||||||
|
|
||||||
uint8_t spi_device_register_0_buffered;
|
uint8_t spi_device_register_0_buffered;
|
||||||
uint8_t spi_device_register_1_buffered;
|
uint8_t spi_device_register_1_buffered;
|
||||||
@ -167,7 +170,7 @@ class ADS1220Component : public Component, public spi::SPIDevice<spi::BIT_ORDER_
|
|||||||
ADS1220IdacRouting prev_idac_2_routing;
|
ADS1220IdacRouting prev_idac_2_routing;
|
||||||
|
|
||||||
bool prev_temp_sensor_mode;
|
bool prev_temp_sensor_mode;
|
||||||
bool prev_burnout_current_sources;
|
bool prev_fault_test_mode;
|
||||||
|
|
||||||
/* Configuration Register 0 settings */
|
/* Configuration Register 0 settings */
|
||||||
void setCompareChannels(ads1220Multiplexer mux);
|
void setCompareChannels(ads1220Multiplexer mux);
|
||||||
@ -225,34 +228,31 @@ class ADS1220Sensor : public sensor::Sensor, public PollingComponent, public vol
|
|||||||
public:
|
public:
|
||||||
ADS1220Sensor(ADS1220Component *parent) : parent_(parent) {}
|
ADS1220Sensor(ADS1220Component *parent) : parent_(parent) {}
|
||||||
void update() override;
|
void update() override;
|
||||||
|
|
||||||
void set_multiplexer(ADS1220Multiplexer multiplexer) { multiplexer_ = multiplexer; }
|
void set_multiplexer(ADS1220Multiplexer multiplexer) { multiplexer_ = multiplexer; }
|
||||||
void set_gain(ADS1220Gain gain) { gain_ = gain; }
|
void set_gain(ADS1220Gain gain) { gain_ = gain; }
|
||||||
|
|
||||||
void set_datarate(ADS1220DataRate datarate) { datarate_ = datarate; }
|
void set_datarate(ADS1220DataRate datarate) { datarate_ = datarate; }
|
||||||
void set_operating_mode(ADS1220OpMode op_mode ) { op_mode_ = op_mode; }
|
void set_operating_mode(ADS1220OpMode op_mode ) { op_mode_ = op_mode; }
|
||||||
void set_conversion_mode(ADS1220ConvMode conv_mode) { conv_mode_ = conv_mode; }
|
void set_conversion_mode(ADS1220ConvMode conv_mode) { conv_mode_ = conv_mode; }
|
||||||
void set_temp_sensor_mode(bool temp_sensor) { temp_sensor_mode_ = temp_sensor_mode; }
|
void set_temp_sensor_mode(bool temp_sensor_mode) { temp_sensor_mode_ = temp_sensor_mode; }
|
||||||
void set_burnout_current_sources(bool burnout_current_sources) { burnout_current_sources_ = burnout_current_sources; }
|
void set_fault_test_mode(bool fault_test_mode) { fault_test_mode_ = fault_test_mode; }
|
||||||
void set_vref_source(ADS1220VRef vref_source) { vref_source_ = vref_source; }
|
void set_vref_source(ADS1220VRef vref_source) { vref_source_ = vref_source; }
|
||||||
void set_drdy_mode(ADS1220DrdyMode drdy_mode) { drdy_mode_ = drdy_mode; }
|
void set_drdy_mode(ADS1220DrdyMode drdy_mode) { drdy_mode_ = drdy_mode; }
|
||||||
|
|
||||||
void set_fir_filter(ADS1220FIR fir) { fir_ = fir; }
|
void set_fir_filter(ADS1220FIR fir) { fir_ = fir; }
|
||||||
void set_low_side_power_switch(ADS1220PSW psw) { psw_ = psw; }
|
void set_low_side_power_switch(ADS1220PSW psw) { psw_ = psw; }
|
||||||
void set_idac_current(ADS1220IdacCurrent idac_current) { idac_current_ = idac_current; }
|
void set_idac_current(ADS1220IdacCurrent idac_current) { idac_current_ = idac_current; }
|
||||||
void set_idac_1_routing(ADS1220IdacRouting idac_1_routing) { idac_1_routing_ = idac_1_routing; }
|
void set_idac_1_routing(ADS1220IdacRouting idac_1_routing) { idac_1_routing_ = idac_1_routing; }
|
||||||
void set_idac_2_routing(ADS1220IdacRouting idac_2_routing) { idac_2_routing_ = idac_2_routing; }
|
void set_idac_2_routing(ADS1220IdacRouting idac_2_routing) { idac_2_routing_ = idac_2_routing; }
|
||||||
|
|
||||||
//void set_temperature_mode(bool temp) { this->temperature_mode_ = temp; }
|
|
||||||
|
|
||||||
float sample() override;
|
float sample() override;
|
||||||
|
|
||||||
uint8_t get_multiplexer() const { return multiplexer_; }
|
uint8_t get_multiplexer() const { return multiplexer_; }
|
||||||
uint8_t get_gain() const { return gain_; }
|
uint8_t get_gain() const { return gain_; }
|
||||||
|
|
||||||
uint8_t get_datarate() const { return datarate_; }
|
uint8_t get_datarate() const { return datarate_; }
|
||||||
uint8_t get_operating_mode() const { return op_mode_; }
|
uint8_t get_operating_mode() const { return op_mode_; }
|
||||||
uint8_t get_conversion_mode() const { return conv_mode_; }
|
uint8_t get_conversion_mode() const { return conv_mode_; }
|
||||||
uint8_t get_temp_sensor_mode() const { return temp_sensor_mode_; }
|
uint8_t get_temp_sensor_mode() const { return temp_sensor_mode_; }
|
||||||
uint8_t get_burnout_current_sources() const { return burnout_current_sources_; }
|
uint8_t get_fault_test_mode() const { return fault_test_mode_; }
|
||||||
uint8_t get_vref_source() const { return vref_source_; }
|
uint8_t get_vref_source() const { return vref_source_; }
|
||||||
uint8_t get_drdy_mode() const { return drdy_mode_; }
|
uint8_t get_drdy_mode() const { return drdy_mode_; }
|
||||||
|
|
||||||
@ -271,8 +271,8 @@ class ADS1220Sensor : public sensor::Sensor, public PollingComponent, public vol
|
|||||||
ADS1220IdacRouting idac_1_routing_;
|
ADS1220IdacRouting idac_1_routing_;
|
||||||
ADS1220IdacRouting idac_2_routing_;
|
ADS1220IdacRouting idac_2_routing_;
|
||||||
|
|
||||||
bool temp_sensor_mode_;
|
uint8_t temp_sensor_mode_;
|
||||||
bool burnout_current_sources_;
|
uint8_t fault_test_mode_;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
|
from esphome import pins
|
||||||
from esphome.components import sensor, voltage_sampler
|
from esphome.components import sensor, voltage_sampler
|
||||||
from esphome.const import (
|
from esphome.const import (
|
||||||
CONF_GAIN,
|
CONF_GAIN,
|
||||||
@ -14,7 +15,7 @@ from esphome.const import (
|
|||||||
)
|
)
|
||||||
from . import ads1220_ns, ADS1220Component
|
from . import ads1220_ns, ADS1220Component
|
||||||
|
|
||||||
|
CODEOWNERS = ["@miikasyvanen"]
|
||||||
DEPENDENCIES = ["ads1220"]
|
DEPENDENCIES = ["ads1220"]
|
||||||
|
|
||||||
|
|
||||||
@ -138,11 +139,12 @@ def validate_gain(value):
|
|||||||
|
|
||||||
return cv.enum(GAIN)(value)
|
return cv.enum(GAIN)(value)
|
||||||
|
|
||||||
|
|
||||||
ADS1220Sensor = ads1220_ns.class_(
|
ADS1220Sensor = ads1220_ns.class_(
|
||||||
"ADS1220Sensor", sensor.Sensor, cg.PollingComponent, voltage_sampler.VoltageSampler
|
"ADS1220Sensor", sensor.Sensor, cg.PollingComponent, voltage_sampler.VoltageSampler
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ADS1220GPIOPin = ads1220_ns.class_("ADS1220GPIOPin", cg.GPIOPin)
|
||||||
|
|
||||||
CONF_ADS1220_ID = "ads1220_id"
|
CONF_ADS1220_ID = "ads1220_id"
|
||||||
CONF_DATARATE = "datarate"
|
CONF_DATARATE = "datarate"
|
||||||
CONF_OP_MODE = "operation_mode"
|
CONF_OP_MODE = "operation_mode"
|
||||||
@ -155,6 +157,7 @@ CONF_IDAC_1_ROUTING = "idac_1_routing"
|
|||||||
CONF_IDAC_2_ROUTING = "idac_2_routing"
|
CONF_IDAC_2_ROUTING = "idac_2_routing"
|
||||||
CONF_DRDY_MODE = "drdy_mode"
|
CONF_DRDY_MODE = "drdy_mode"
|
||||||
CONF_FAULT_TEST_MODE = "fault_test_mode"
|
CONF_FAULT_TEST_MODE = "fault_test_mode"
|
||||||
|
#CONF_DRDY_PIN = "drdy_pin"
|
||||||
|
|
||||||
TYPE_ADC = "adc"
|
TYPE_ADC = "adc"
|
||||||
TYPE_TEMPERATURE = "temperature"
|
TYPE_TEMPERATURE = "temperature"
|
||||||
@ -205,10 +208,6 @@ CONFIG_SCHEMA = cv.typed_schema(
|
|||||||
)
|
)
|
||||||
|
|
||||||
async def to_code(config):
|
async def to_code(config):
|
||||||
#var = await sensor.new_sensor(config)
|
|
||||||
#await cg.register_component(var, config)
|
|
||||||
#await cg.register_parented(var, config[CONF_ADS1220_ID])
|
|
||||||
|
|
||||||
paren = await cg.get_variable(config[CONF_ADS1220_ID])
|
paren = await cg.get_variable(config[CONF_ADS1220_ID])
|
||||||
var = cg.new_Pvariable(config[CONF_ID], paren)
|
var = cg.new_Pvariable(config[CONF_ID], paren)
|
||||||
await sensor.register_sensor(var, config)
|
await sensor.register_sensor(var, config)
|
||||||
|
Loading…
Reference in New Issue
Block a user