From 4fb1decb1ffddabbf3acb08149dbd88e492029b8 Mon Sep 17 00:00:00 2001 From: Anton Viktorov Date: Tue, 12 Mar 2024 16:40:34 +0100 Subject: [PATCH] add LED support for pimoroni --- esphome/components/bh1745/bh1745.cpp | 19 +++++++++++++++++ esphome/components/bh1745/bh1745.h | 3 +++ esphome/components/bh1745/sensor.py | 2 ++ esphome/components/bh1745/switch/__init__.py | 20 ++++++++++++++++++ .../bh1745/switch/bh1745_switch_led.cpp | 17 +++++++++++++++ .../bh1745/switch/bh1745_switch_led.h | 21 +++++++++++++++++++ 6 files changed, 82 insertions(+) create mode 100644 esphome/components/bh1745/switch/__init__.py create mode 100644 esphome/components/bh1745/switch/bh1745_switch_led.cpp create mode 100644 esphome/components/bh1745/switch/bh1745_switch_led.h diff --git a/esphome/components/bh1745/bh1745.cpp b/esphome/components/bh1745/bh1745.cpp index b62c353897..f975671871 100644 --- a/esphome/components/bh1745/bh1745.cpp +++ b/esphome/components/bh1745/bh1745.cpp @@ -57,6 +57,13 @@ void BH1745Component::setup() { sys_ctrl.int_reset = false; this->reg((uint8_t) Bh1745Registers::SYSTEM_CONTROL) = sys_ctrl.raw; + // only for pimoroni boards for now - there are LEDs connected to interrupt pin + uint16_t TH_HIGH[1] = {0xFFFF}; + uint16_t TH_LOW[1] = {0x0000}; + this->write_bytes_16((uint8_t) Bh1745Registers::TH_LSB, TH_LOW, 1); + this->write_bytes_16((uint8_t) Bh1745Registers::TL_LSB, TH_HIGH, 1); + this->reg((uint8_t) Bh1745Registers::INTERRUPT_) = 0x00; + this->set_timeout(BH1745_RESET_TIMEOUT_MS, [this]() { this->configure_measurement_time_(); this->state_ = State::DELAYED_SETUP; @@ -146,6 +153,18 @@ void BH1745Component::loop() { float BH1745Component::get_setup_priority() const { return setup_priority::DATA; } +void BH1745Component::switch_led(bool on_off) { + // shall we require somehow that its a pimoroni board? + uint8_t raw = this->reg((uint8_t) Bh1745Registers::INTERRUPT_).get(); + + if (on_off) { + raw |= (1); + } else { + raw &= ~(1); + } + this->reg((uint8_t) Bh1745Registers::INTERRUPT_) = raw; +} + void BH1745Component::configure_measurement_time_() { ModeControl1Register mode_ctrl1; mode_ctrl1.reserved_3_7 = 0; diff --git a/esphome/components/bh1745/bh1745.h b/esphome/components/bh1745/bh1745.h index 3a29abe501..e08f921a80 100644 --- a/esphome/components/bh1745/bh1745.h +++ b/esphome/components/bh1745/bh1745.h @@ -102,6 +102,9 @@ class BH1745Component : public PollingComponent, public i2c::I2CDevice { color_temperature_sensor_ = color_temperature_sensor; } + // only for Pimoroni board + void switch_led(bool on_off); + protected: MeasurementTime measurement_time_{MeasurementTime::TIME_160MS}; AdcGain adc_gain_{AdcGain::GAIN_1X}; diff --git a/esphome/components/bh1745/sensor.py b/esphome/components/bh1745/sensor.py index ab75936c15..66b92e842c 100644 --- a/esphome/components/bh1745/sensor.py +++ b/esphome/components/bh1745/sensor.py @@ -19,6 +19,8 @@ from esphome.const import ( CODEOWNERS = ["@latonita"] DEPENDENCIES = ["i2c"] +CONF_BH1745_ID = "bh1745_id" + UNIT_COUNTS = "#" CONF_RED_CHANNEL = "red_channel" diff --git a/esphome/components/bh1745/switch/__init__.py b/esphome/components/bh1745/switch/__init__.py new file mode 100644 index 0000000000..769084bf90 --- /dev/null +++ b/esphome/components/bh1745/switch/__init__.py @@ -0,0 +1,20 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import switch +from ..sensor import bh1745_ns, BH1745SComponent, CONF_BH1745_ID + +BH1745SwitchLed = bh1745_ns.class_("BH1745SwitchLed", switch.Switch, cg.Component) + +CONFIG_SCHEMA = switch.SWITCH_SCHEMA.extend( + { + cv.GenerateID(): cv.declare_id(BH1745SwitchLed), + cv.GenerateID(CONF_BH1745_ID): cv.use_id(BH1745SComponent), + } +).extend(cv.COMPONENT_SCHEMA) + + +async def to_code(config): + var = await switch.new_switch(config) + await cg.register_component(var, config) + hub = await cg.get_variable(config[CONF_BH1745_ID]) + cg.add(var.set_bh1745(hub)) diff --git a/esphome/components/bh1745/switch/bh1745_switch_led.cpp b/esphome/components/bh1745/switch/bh1745_switch_led.cpp new file mode 100644 index 0000000000..9df4da5497 --- /dev/null +++ b/esphome/components/bh1745/switch/bh1745_switch_led.cpp @@ -0,0 +1,17 @@ +#include "bh1745_switch_led.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace bh1745 { + +static const char *const TAG = "bh1745.led"; + +void BH1745SwitchLed::write_state(bool state) { + bh1745_->switch_led(state); + publish_state(state); +} + +void BH1745SwitchLed::dump_config() { LOG_SWITCH("", "BH1745 Pimoroni LED", this); } + +} // namespace bh1745 +} // namespace esphome diff --git a/esphome/components/bh1745/switch/bh1745_switch_led.h b/esphome/components/bh1745/switch/bh1745_switch_led.h new file mode 100644 index 0000000000..c7d8967af6 --- /dev/null +++ b/esphome/components/bh1745/switch/bh1745_switch_led.h @@ -0,0 +1,21 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/switch/switch.h" +#include "../bh1745.h" + +namespace esphome { +namespace bh1745 { + +class BH1745SwitchLed : public switch_::Switch, public Component { + public: + void dump_config() override; + + void set_bh1745(BH1745Component *bh1745) { bh1745_ = bh1745; } + + protected: + void write_state(bool state) override; + BH1745Component *bh1745_; +}; +} // namespace bh1745 +} // namespace esphome