From 138de643a27fc5cd41c0efcdb5432d27626fbb76 Mon Sep 17 00:00:00 2001 From: Maximilian <43999966+DeerMaximum@users.noreply.github.com> Date: Wed, 19 Oct 2022 06:06:22 +0200 Subject: [PATCH] Add adc128s102 sensor (#3822) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- CODEOWNERS | 1 + esphome/components/adc128s102/__init__.py | 23 ++++++++++++ esphome/components/adc128s102/adc128s102.cpp | 35 +++++++++++++++++++ esphome/components/adc128s102/adc128s102.h | 23 ++++++++++++ .../components/adc128s102/sensor/__init__.py | 35 +++++++++++++++++++ .../adc128s102/sensor/adc128s102_sensor.cpp | 24 +++++++++++++ .../adc128s102/sensor/adc128s102_sensor.h | 29 +++++++++++++++ tests/test3.yaml | 8 +++++ 8 files changed, 178 insertions(+) create mode 100644 esphome/components/adc128s102/__init__.py create mode 100644 esphome/components/adc128s102/adc128s102.cpp create mode 100644 esphome/components/adc128s102/adc128s102.h create mode 100644 esphome/components/adc128s102/sensor/__init__.py create mode 100644 esphome/components/adc128s102/sensor/adc128s102_sensor.cpp create mode 100644 esphome/components/adc128s102/sensor/adc128s102_sensor.h diff --git a/CODEOWNERS b/CODEOWNERS index 86e6cd978b..688c58b2a4 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -13,6 +13,7 @@ esphome/core/* @esphome/core # Integrations esphome/components/ac_dimmer/* @glmnet esphome/components/adc/* @esphome/core +esphome/components/adc128s102/* @DeerMaximum esphome/components/addressable_light/* @justfalter esphome/components/airthings_ble/* @jeromelaban esphome/components/airthings_wave_mini/* @ncareau diff --git a/esphome/components/adc128s102/__init__.py b/esphome/components/adc128s102/__init__.py new file mode 100644 index 0000000000..c4e9d5831e --- /dev/null +++ b/esphome/components/adc128s102/__init__.py @@ -0,0 +1,23 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import spi +from esphome.const import CONF_ID + +DEPENDENCIES = ["spi"] +MULTI_CONF = True +CODEOWNERS = ["@DeerMaximum"] + +adc128s102_ns = cg.esphome_ns.namespace("adc128s102") +ADC128S102 = adc128s102_ns.class_("ADC128S102", cg.Component, spi.SPIDevice) + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(): cv.declare_id(ADC128S102), + } +).extend(spi.spi_device_schema(cs_pin_required=True)) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await cg.register_component(var, config) + await spi.register_spi_device(var, config) diff --git a/esphome/components/adc128s102/adc128s102.cpp b/esphome/components/adc128s102/adc128s102.cpp new file mode 100644 index 0000000000..c27a354dd3 --- /dev/null +++ b/esphome/components/adc128s102/adc128s102.cpp @@ -0,0 +1,35 @@ +#include "adc128s102.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace adc128s102 { + +static const char *const TAG = "adc128s102"; + +float ADC128S102::get_setup_priority() const { return setup_priority::HARDWARE; } + +void ADC128S102::setup() { + ESP_LOGCONFIG(TAG, "Setting up adc128s102"); + this->spi_setup(); +} + +void ADC128S102::dump_config() { + ESP_LOGCONFIG(TAG, "ADC128S102:"); + LOG_PIN(" CS Pin:", this->cs_); +} + +uint16_t ADC128S102::read_data(uint8_t channel) { + uint8_t control = channel << 3; + + this->enable(); + uint8_t adc_primary_byte = this->transfer_byte(control); + uint8_t adc_secondary_byte = this->transfer_byte(0x00); + this->disable(); + + uint16_t digital_value = adc_primary_byte << 8 | adc_secondary_byte; + + return digital_value; +} + +} // namespace adc128s102 +} // namespace esphome diff --git a/esphome/components/adc128s102/adc128s102.h b/esphome/components/adc128s102/adc128s102.h new file mode 100644 index 0000000000..bd6b7f7af1 --- /dev/null +++ b/esphome/components/adc128s102/adc128s102.h @@ -0,0 +1,23 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/hal.h" +#include "esphome/components/spi/spi.h" + +namespace esphome { +namespace adc128s102 { + +class ADC128S102 : public Component, + public spi::SPIDevice { + public: + ADC128S102() = default; + + void setup() override; + void dump_config() override; + float get_setup_priority() const override; + uint16_t read_data(uint8_t channel); +}; + +} // namespace adc128s102 +} // namespace esphome diff --git a/esphome/components/adc128s102/sensor/__init__.py b/esphome/components/adc128s102/sensor/__init__.py new file mode 100644 index 0000000000..3ab6fc4c38 --- /dev/null +++ b/esphome/components/adc128s102/sensor/__init__.py @@ -0,0 +1,35 @@ +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_CHANNEL + +from .. import adc128s102_ns, ADC128S102 + +AUTO_LOAD = ["voltage_sampler"] +DEPENDENCIES = ["adc128s102"] + +ADC128S102Sensor = adc128s102_ns.class_( + "ADC128S102Sensor", + sensor.Sensor, + cg.PollingComponent, + voltage_sampler.VoltageSampler, +) +CONF_ADC128S102_ID = "adc128s102_id" + +CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend( + { + cv.GenerateID(): cv.declare_id(ADC128S102Sensor), + cv.GenerateID(CONF_ADC128S102_ID): cv.use_id(ADC128S102), + cv.Required(CONF_CHANNEL): cv.int_range(min=0, max=7), + } +).extend(cv.polling_component_schema("60s")) + + +async def to_code(config): + var = cg.new_Pvariable( + config[CONF_ID], + config[CONF_CHANNEL], + ) + await cg.register_parented(var, config[CONF_ADC128S102_ID]) + await cg.register_component(var, config) + await sensor.register_sensor(var, config) diff --git a/esphome/components/adc128s102/sensor/adc128s102_sensor.cpp b/esphome/components/adc128s102/sensor/adc128s102_sensor.cpp new file mode 100644 index 0000000000..03ce31d3cb --- /dev/null +++ b/esphome/components/adc128s102/sensor/adc128s102_sensor.cpp @@ -0,0 +1,24 @@ +#include "adc128s102_sensor.h" + +#include "esphome/core/log.h" + +namespace esphome { +namespace adc128s102 { + +static const char *const TAG = "adc128s102.sensor"; + +ADC128S102Sensor::ADC128S102Sensor(uint8_t channel) : channel_(channel) {} + +float ADC128S102Sensor::get_setup_priority() const { return setup_priority::DATA; } + +void ADC128S102Sensor::dump_config() { + LOG_SENSOR("", "ADC128S102 Sensor", this); + ESP_LOGCONFIG(TAG, " Pin: %u", this->channel_); + LOG_UPDATE_INTERVAL(this); +} + +float ADC128S102Sensor::sample() { return this->parent_->read_data(this->channel_); } +void ADC128S102Sensor::update() { this->publish_state(this->sample()); } + +} // namespace adc128s102 +} // namespace esphome diff --git a/esphome/components/adc128s102/sensor/adc128s102_sensor.h b/esphome/components/adc128s102/sensor/adc128s102_sensor.h new file mode 100644 index 0000000000..234500c2f4 --- /dev/null +++ b/esphome/components/adc128s102/sensor/adc128s102_sensor.h @@ -0,0 +1,29 @@ +#pragma once + +#include "esphome/components/sensor/sensor.h" +#include "esphome/components/voltage_sampler/voltage_sampler.h" +#include "esphome/core/component.h" +#include "esphome/core/hal.h" + +#include "../adc128s102.h" + +namespace esphome { +namespace adc128s102 { + +class ADC128S102Sensor : public PollingComponent, + public Parented, + public sensor::Sensor, + public voltage_sampler::VoltageSampler { + public: + ADC128S102Sensor(uint8_t channel); + + void update() override; + void dump_config() override; + float get_setup_priority() const override; + float sample() override; + + protected: + uint8_t channel_; +}; +} // namespace adc128s102 +} // namespace esphome diff --git a/tests/test3.yaml b/tests/test3.yaml index 8fc66f4918..8150f43e02 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -789,6 +789,11 @@ sensor: voltage: name: Voltage update_interval: 60s + + - platform: adc128s102 + id: adc128s102_channel_0 + channel: 0 + time: - platform: homeassistant @@ -1540,3 +1545,6 @@ cd74hc4067: pin_s1: GPIO13 pin_s2: GPIO14 pin_s3: GPIO15 + +adc128s102: + cs_pin: GPIO12