mirror of
https://github.com/esphome/esphome.git
synced 2025-04-02 08:58:17 +01:00
add LED support for pimoroni
This commit is contained in:
parent
95a14f0020
commit
4fb1decb1f
@ -57,6 +57,13 @@ void BH1745Component::setup() {
|
|||||||
sys_ctrl.int_reset = false;
|
sys_ctrl.int_reset = false;
|
||||||
this->reg((uint8_t) Bh1745Registers::SYSTEM_CONTROL) = sys_ctrl.raw;
|
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->set_timeout(BH1745_RESET_TIMEOUT_MS, [this]() {
|
||||||
this->configure_measurement_time_();
|
this->configure_measurement_time_();
|
||||||
this->state_ = State::DELAYED_SETUP;
|
this->state_ = State::DELAYED_SETUP;
|
||||||
@ -146,6 +153,18 @@ void BH1745Component::loop() {
|
|||||||
|
|
||||||
float BH1745Component::get_setup_priority() const { return setup_priority::DATA; }
|
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_() {
|
void BH1745Component::configure_measurement_time_() {
|
||||||
ModeControl1Register mode_ctrl1;
|
ModeControl1Register mode_ctrl1;
|
||||||
mode_ctrl1.reserved_3_7 = 0;
|
mode_ctrl1.reserved_3_7 = 0;
|
||||||
|
@ -102,6 +102,9 @@ class BH1745Component : public PollingComponent, public i2c::I2CDevice {
|
|||||||
color_temperature_sensor_ = color_temperature_sensor;
|
color_temperature_sensor_ = color_temperature_sensor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// only for Pimoroni board
|
||||||
|
void switch_led(bool on_off);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
MeasurementTime measurement_time_{MeasurementTime::TIME_160MS};
|
MeasurementTime measurement_time_{MeasurementTime::TIME_160MS};
|
||||||
AdcGain adc_gain_{AdcGain::GAIN_1X};
|
AdcGain adc_gain_{AdcGain::GAIN_1X};
|
||||||
|
@ -19,6 +19,8 @@ from esphome.const import (
|
|||||||
CODEOWNERS = ["@latonita"]
|
CODEOWNERS = ["@latonita"]
|
||||||
DEPENDENCIES = ["i2c"]
|
DEPENDENCIES = ["i2c"]
|
||||||
|
|
||||||
|
CONF_BH1745_ID = "bh1745_id"
|
||||||
|
|
||||||
UNIT_COUNTS = "#"
|
UNIT_COUNTS = "#"
|
||||||
|
|
||||||
CONF_RED_CHANNEL = "red_channel"
|
CONF_RED_CHANNEL = "red_channel"
|
||||||
|
20
esphome/components/bh1745/switch/__init__.py
Normal file
20
esphome/components/bh1745/switch/__init__.py
Normal 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))
|
17
esphome/components/bh1745/switch/bh1745_switch_led.cpp
Normal file
17
esphome/components/bh1745/switch/bh1745_switch_led.cpp
Normal 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
|
21
esphome/components/bh1745/switch/bh1745_switch_led.h
Normal file
21
esphome/components/bh1745/switch/bh1745_switch_led.h
Normal 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
|
Loading…
x
Reference in New Issue
Block a user