mcp3008: Tidy up and fix auto load bug (#5842)

This commit is contained in:
Jesse Hills 2023-11-28 09:35:31 +13:00 committed by GitHub
parent 1324d9e39a
commit 15180ee1e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 115 additions and 82 deletions

View File

@ -1,4 +1,6 @@
#include "mcp3008.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
@ -32,28 +34,10 @@ float MCP3008::read_data(uint8_t pin) {
this->disable();
int data = data_msb << 8 | data_lsb;
uint16_t data = encode_uint16(data_msb, data_lsb);
return data / 1023.0f;
}
MCP3008Sensor::MCP3008Sensor(MCP3008 *parent, uint8_t pin, float reference_voltage)
: PollingComponent(1000), parent_(parent), pin_(pin), reference_voltage_(reference_voltage) {}
float MCP3008Sensor::get_setup_priority() const { return setup_priority::DATA; }
void MCP3008Sensor::setup() { LOG_SENSOR("", "Setting up MCP3008 Sensor '%s'...", this); }
void MCP3008Sensor::dump_config() {
ESP_LOGCONFIG(TAG, "MCP3008Sensor:");
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
ESP_LOGCONFIG(TAG, " Reference Voltage: %.2fV", this->reference_voltage_);
}
float MCP3008Sensor::sample() {
float value_v = this->parent_->read_data(pin_);
value_v = (value_v * this->reference_voltage_);
return value_v;
}
void MCP3008Sensor::update() { this->publish_state(this->sample()); }
} // namespace mcp3008
} // namespace esphome

View File

@ -1,10 +1,8 @@
#pragma once
#include "esphome/components/spi/spi.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/spi/spi.h"
#include "esphome/components/voltage_sampler/voltage_sampler.h"
namespace esphome {
namespace mcp3008 {
@ -14,31 +12,10 @@ class MCP3008 : public Component,
spi::DATA_RATE_75KHZ> { // Running at the slowest max speed supported by the
// mcp3008. 2.7v = 75ksps
public:
MCP3008() = default;
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
float read_data(uint8_t pin);
protected:
};
class MCP3008Sensor : public PollingComponent, public sensor::Sensor, public voltage_sampler::VoltageSampler {
public:
MCP3008Sensor(MCP3008 *parent, uint8_t pin, float reference_voltage);
void set_reference_voltage(float reference_voltage) { reference_voltage_ = reference_voltage; }
void setup() override;
void update() override;
void dump_config() override;
float get_setup_priority() const override;
float sample() override;
protected:
MCP3008 *parent_;
uint8_t pin_;
float reference_voltage_;
};
} // namespace mcp3008

View File

@ -1,39 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, voltage_sampler
from esphome.const import CONF_ID, CONF_NUMBER
from . import mcp3008_ns, MCP3008
AUTO_LOAD = ["voltage_sampler"]
DEPENDENCIES = ["mcp3008"]
MCP3008Sensor = mcp3008_ns.class_(
"MCP3008Sensor", sensor.Sensor, cg.PollingComponent, voltage_sampler.VoltageSampler
)
CONF_REFERENCE_VOLTAGE = "reference_voltage"
CONF_MCP3008_ID = "mcp3008_id"
CONFIG_SCHEMA = (
sensor.sensor_schema(MCP3008Sensor)
.extend(
{
cv.GenerateID(CONF_MCP3008_ID): cv.use_id(MCP3008),
cv.Required(CONF_NUMBER): cv.int_,
cv.Optional(CONF_REFERENCE_VOLTAGE, default="3.3V"): cv.voltage,
}
)
.extend(cv.polling_component_schema("1s"))
)
async def to_code(config):
parent = await cg.get_variable(config[CONF_MCP3008_ID])
var = cg.new_Pvariable(
config[CONF_ID],
parent,
config[CONF_NUMBER],
config[CONF_REFERENCE_VOLTAGE],
)
await cg.register_component(var, config)
await sensor.register_sensor(var, config)

View File

@ -0,0 +1,53 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, voltage_sampler
from esphome.const import (
CONF_ID,
CONF_NUMBER,
UNIT_VOLT,
STATE_CLASS_MEASUREMENT,
DEVICE_CLASS_VOLTAGE,
)
from .. import mcp3008_ns, MCP3008
AUTO_LOAD = ["voltage_sampler"]
DEPENDENCIES = ["mcp3008"]
MCP3008Sensor = mcp3008_ns.class_(
"MCP3008Sensor",
sensor.Sensor,
cg.PollingComponent,
voltage_sampler.VoltageSampler,
cg.Parented.template(MCP3008),
)
CONF_REFERENCE_VOLTAGE = "reference_voltage"
CONF_MCP3008_ID = "mcp3008_id"
CONFIG_SCHEMA = (
sensor.sensor_schema(
MCP3008Sensor,
unit_of_measurement=UNIT_VOLT,
state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_VOLTAGE,
)
.extend(
{
cv.GenerateID(CONF_MCP3008_ID): cv.use_id(MCP3008),
cv.Required(CONF_NUMBER): cv.int_,
cv.Optional(CONF_REFERENCE_VOLTAGE, default="3.3V"): cv.voltage,
}
)
.extend(cv.polling_component_schema("60s"))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_parented(var, config[CONF_MCP3008_ID])
await cg.register_component(var, config)
await sensor.register_sensor(var, config)
cg.add(var.set_pin(config[CONF_NUMBER]))
cg.add(var.set_reference_voltage(config[CONF_REFERENCE_VOLTAGE]))

View File

@ -0,0 +1,27 @@
#include "mcp3008_sensor.h"
#include "esphome/core/log.h"
namespace esphome {
namespace mcp3008 {
static const char *const TAG = "mcp3008.sensor";
float MCP3008Sensor::get_setup_priority() const { return setup_priority::DATA; }
void MCP3008Sensor::dump_config() {
ESP_LOGCONFIG(TAG, "MCP3008Sensor:");
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
ESP_LOGCONFIG(TAG, " Reference Voltage: %.2fV", this->reference_voltage_);
}
float MCP3008Sensor::sample() {
float value_v = this->parent_->read_data(pin_);
value_v = (value_v * this->reference_voltage_);
return value_v;
}
void MCP3008Sensor::update() { this->publish_state(this->sample()); }
} // namespace mcp3008
} // namespace esphome

View File

@ -0,0 +1,31 @@
#pragma once
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/voltage_sampler/voltage_sampler.h"
#include "esphome/core/component.h"
#include "../mcp3008.h"
namespace esphome {
namespace mcp3008 {
class MCP3008Sensor : public PollingComponent,
public sensor::Sensor,
public voltage_sampler::VoltageSampler,
public Parented<MCP3008> {
public:
void set_reference_voltage(float reference_voltage) { this->reference_voltage_ = reference_voltage; }
void set_pin(uint8_t pin) { this->pin_ = pin; }
void update() override;
void dump_config() override;
float get_setup_priority() const override;
float sample() override;
protected:
uint8_t pin_;
float reference_voltage_;
};
} // namespace mcp3008
} // namespace esphome