1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-01 00:18:18 +01:00

add LED support for pimoroni

This commit is contained in:
Anton Viktorov 2024-03-12 16:40:34 +01:00
parent 95a14f0020
commit 4fb1decb1f
6 changed files with 82 additions and 0 deletions

View File

@ -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;

View File

@ -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};

View File

@ -19,6 +19,8 @@ from esphome.const import (
CODEOWNERS = ["@latonita"]
DEPENDENCIES = ["i2c"]
CONF_BH1745_ID = "bh1745_id"
UNIT_COUNTS = "#"
CONF_RED_CHANNEL = "red_channel"

View File

@ -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))

View File

@ -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

View File

@ -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