mirror of
https://github.com/esphome/esphome.git
synced 2024-11-08 09:40:53 +01:00
mcp3008: Tidy up and fix auto load bug (#5842)
This commit is contained in:
parent
1324d9e39a
commit
15180ee1e2
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
53
esphome/components/mcp3008/sensor/__init__.py
Normal file
53
esphome/components/mcp3008/sensor/__init__.py
Normal 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]))
|
27
esphome/components/mcp3008/sensor/mcp3008_sensor.cpp
Normal file
27
esphome/components/mcp3008/sensor/mcp3008_sensor.cpp
Normal 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
|
31
esphome/components/mcp3008/sensor/mcp3008_sensor.h
Normal file
31
esphome/components/mcp3008/sensor/mcp3008_sensor.h
Normal 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
|
Loading…
Reference in New Issue
Block a user