mirror of
https://github.com/esphome/esphome.git
synced 2025-09-21 12:42:21 +01:00
Add support for TM1638 Led and Key component (#3340)
This commit is contained in:
22
esphome/components/tm1638/binary_sensor/__init__.py
Normal file
22
esphome/components/tm1638/binary_sensor/__init__.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import binary_sensor
|
||||
from esphome.const import CONF_KEY
|
||||
from ..display import tm1638_ns, TM1638Component, CONF_TM1638_ID
|
||||
|
||||
TM1638Key = tm1638_ns.class_("TM1638Key", binary_sensor.BinarySensor)
|
||||
|
||||
CONFIG_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(TM1638Key),
|
||||
cv.GenerateID(CONF_TM1638_ID): cv.use_id(TM1638Component),
|
||||
cv.Required(CONF_KEY): cv.int_range(min=0, max=15),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await binary_sensor.new_binary_sensor(config)
|
||||
cg.add(var.set_keycode(config[CONF_KEY]))
|
||||
hub = await cg.get_variable(config[CONF_TM1638_ID])
|
||||
cg.add(hub.register_listener(var))
|
13
esphome/components/tm1638/binary_sensor/tm1638_key.cpp
Normal file
13
esphome/components/tm1638/binary_sensor/tm1638_key.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include "tm1638_key.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace tm1638 {
|
||||
|
||||
void TM1638Key::keys_update(uint8_t keys) {
|
||||
bool pressed = keys & (1 << key_code_);
|
||||
if (pressed != this->state)
|
||||
this->publish_state(pressed);
|
||||
}
|
||||
|
||||
} // namespace tm1638
|
||||
} // namespace esphome
|
19
esphome/components/tm1638/binary_sensor/tm1638_key.h
Normal file
19
esphome/components/tm1638/binary_sensor/tm1638_key.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#include "../tm1638.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace tm1638 {
|
||||
|
||||
class TM1638Key : public binary_sensor::BinarySensor, public KeyListener {
|
||||
public:
|
||||
void set_keycode(uint8_t key_code) { key_code_ = key_code; };
|
||||
void keys_update(uint8_t keys) override;
|
||||
|
||||
protected:
|
||||
uint8_t key_code_{0};
|
||||
};
|
||||
|
||||
} // namespace tm1638
|
||||
} // namespace esphome
|
Reference in New Issue
Block a user