mirror of
https://github.com/esphome/esphome.git
synced 2024-11-29 10:14:13 +01:00
Add TM1651 simple level, turn on, turn off actions (#920)
* Add TM1651 simple level action * fixed brightness validation * Updated lib, fixed import * Added turn_on, turn_off actions * Fixed after lint
This commit is contained in:
parent
6ae1efcf9f
commit
4402a6eb4c
4 changed files with 125 additions and 13 deletions
|
@ -1,13 +1,19 @@
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome import pins, automation
|
from esphome import pins, automation
|
||||||
|
from esphome.automation import maybe_simple_id
|
||||||
from esphome.const import CONF_ID, CONF_CLK_PIN, CONF_DIO_PIN, CONF_LEVEL, CONF_BRIGHTNESS
|
from esphome.const import CONF_ID, CONF_CLK_PIN, CONF_DIO_PIN, CONF_LEVEL, CONF_BRIGHTNESS
|
||||||
|
|
||||||
tm1651_ns = cg.esphome_ns.namespace('tm1651')
|
tm1651_ns = cg.esphome_ns.namespace('tm1651')
|
||||||
TM1651Display = tm1651_ns.class_('TM1651Display', cg.Component)
|
TM1651Display = tm1651_ns.class_('TM1651Display', cg.Component)
|
||||||
|
|
||||||
|
SetLevelPercentAction = tm1651_ns.class_('SetLevelPercentAction', automation.Action)
|
||||||
SetLevelAction = tm1651_ns.class_('SetLevelAction', automation.Action)
|
SetLevelAction = tm1651_ns.class_('SetLevelAction', automation.Action)
|
||||||
SetBrightnessAction = tm1651_ns.class_('SetBrightnessAction', automation.Action)
|
SetBrightnessAction = tm1651_ns.class_('SetBrightnessAction', automation.Action)
|
||||||
validate_level = cv.All(cv.int_range(min=0, max=100))
|
TurnOnAction = tm1651_ns.class_('SetLevelPercentAction', automation.Action)
|
||||||
|
TurnOffAction = tm1651_ns.class_('SetLevelPercentAction', automation.Action)
|
||||||
|
|
||||||
|
CONF_LEVEL_PERCENT = 'level_percent'
|
||||||
|
|
||||||
TM1651_BRIGHTNESS_OPTIONS = {
|
TM1651_BRIGHTNESS_OPTIONS = {
|
||||||
1: TM1651Display.TM1651_BRIGHTNESS_LOW,
|
1: TM1651Display.TM1651_BRIGHTNESS_LOW,
|
||||||
|
@ -21,6 +27,10 @@ CONFIG_SCHEMA = cv.Schema({
|
||||||
cv.Required(CONF_DIO_PIN): pins.internal_gpio_output_pin_schema,
|
cv.Required(CONF_DIO_PIN): pins.internal_gpio_output_pin_schema,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
validate_level_percent = cv.All(cv.int_range(min=0, max=100))
|
||||||
|
validate_level = cv.All(cv.int_range(min=0, max=7))
|
||||||
|
validate_brightness = cv.enum(TM1651_BRIGHTNESS_OPTIONS, int=True)
|
||||||
|
|
||||||
|
|
||||||
def to_code(config):
|
def to_code(config):
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
|
@ -32,10 +42,47 @@ def to_code(config):
|
||||||
cg.add(var.set_dio_pin(dio_pin))
|
cg.add(var.set_dio_pin(dio_pin))
|
||||||
|
|
||||||
# https://platformio.org/lib/show/6865/TM1651
|
# https://platformio.org/lib/show/6865/TM1651
|
||||||
cg.add_library('6865', '1.0.0')
|
cg.add_library('6865', '1.0.1')
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action('tm1651.set_level', SetLevelAction, cv.maybe_simple_value({
|
BINARY_OUTPUT_ACTION_SCHEMA = maybe_simple_id({
|
||||||
|
cv.Required(CONF_ID): cv.use_id(TM1651Display),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@automation.register_action('tm1651.turn_on', TurnOnAction, BINARY_OUTPUT_ACTION_SCHEMA)
|
||||||
|
def output_turn_on_to_code(config, action_id, template_arg, args):
|
||||||
|
var = cg.new_Pvariable(action_id, template_arg)
|
||||||
|
yield cg.register_parented(var, config[CONF_ID])
|
||||||
|
yield var
|
||||||
|
|
||||||
|
|
||||||
|
@automation.register_action('tm1651.turn_off', TurnOffAction, BINARY_OUTPUT_ACTION_SCHEMA)
|
||||||
|
def output_turn_off_to_code(config, action_id, template_arg, args):
|
||||||
|
var = cg.new_Pvariable(action_id, template_arg)
|
||||||
|
yield cg.register_parented(var, config[CONF_ID])
|
||||||
|
yield var
|
||||||
|
|
||||||
|
|
||||||
|
@automation.register_action(
|
||||||
|
'tm1651.set_level_percent',
|
||||||
|
SetLevelPercentAction,
|
||||||
|
cv.maybe_simple_value({
|
||||||
|
cv.GenerateID(): cv.use_id(TM1651Display),
|
||||||
|
cv.Required(CONF_LEVEL_PERCENT): cv.templatable(validate_level_percent),
|
||||||
|
}, key=CONF_LEVEL_PERCENT))
|
||||||
|
def tm1651_set_level_percent_to_code(config, action_id, template_arg, args):
|
||||||
|
var = cg.new_Pvariable(action_id, template_arg)
|
||||||
|
yield cg.register_parented(var, config[CONF_ID])
|
||||||
|
template_ = yield cg.templatable(config[CONF_LEVEL_PERCENT], args, cg.uint8)
|
||||||
|
cg.add(var.set_level_percent(template_))
|
||||||
|
yield var
|
||||||
|
|
||||||
|
|
||||||
|
@automation.register_action(
|
||||||
|
'tm1651.set_level',
|
||||||
|
SetLevelAction,
|
||||||
|
cv.maybe_simple_value({
|
||||||
cv.GenerateID(): cv.use_id(TM1651Display),
|
cv.GenerateID(): cv.use_id(TM1651Display),
|
||||||
cv.Required(CONF_LEVEL): cv.templatable(validate_level),
|
cv.Required(CONF_LEVEL): cv.templatable(validate_level),
|
||||||
}, key=CONF_LEVEL))
|
}, key=CONF_LEVEL))
|
||||||
|
@ -47,9 +94,12 @@ def tm1651_set_level_to_code(config, action_id, template_arg, args):
|
||||||
yield var
|
yield var
|
||||||
|
|
||||||
|
|
||||||
@automation.register_action('tm1651.set_brightness', SetBrightnessAction, cv.maybe_simple_value({
|
@automation.register_action(
|
||||||
|
'tm1651.set_brightness',
|
||||||
|
SetBrightnessAction,
|
||||||
|
cv.maybe_simple_value({
|
||||||
cv.GenerateID(): cv.use_id(TM1651Display),
|
cv.GenerateID(): cv.use_id(TM1651Display),
|
||||||
cv.Required(CONF_BRIGHTNESS): cv.templatable(validate_level),
|
cv.Required(CONF_BRIGHTNESS): cv.templatable(validate_brightness),
|
||||||
}, key=CONF_BRIGHTNESS))
|
}, key=CONF_BRIGHTNESS))
|
||||||
def tm1651_set_brightness_to_code(config, action_id, template_arg, args):
|
def tm1651_set_brightness_to_code(config, action_id, template_arg, args):
|
||||||
var = cg.new_Pvariable(action_id, template_arg)
|
var = cg.new_Pvariable(action_id, template_arg)
|
||||||
|
|
|
@ -5,8 +5,9 @@ namespace esphome {
|
||||||
namespace tm1651 {
|
namespace tm1651 {
|
||||||
|
|
||||||
static const char *TAG = "tm1651.display";
|
static const char *TAG = "tm1651.display";
|
||||||
|
|
||||||
|
static const uint8_t MAX_INPUT_LEVEL_PERCENT = 100;
|
||||||
static const uint8_t TM1651_MAX_LEVEL = 7;
|
static const uint8_t TM1651_MAX_LEVEL = 7;
|
||||||
static const uint8_t MAX_INPUT_LEVEL = 100;
|
|
||||||
|
|
||||||
static const uint8_t TM1651_BRIGHTNESS_LOW = 0;
|
static const uint8_t TM1651_BRIGHTNESS_LOW = 0;
|
||||||
static const uint8_t TM1651_BRIGHTNESS_MEDIUM = 2;
|
static const uint8_t TM1651_BRIGHTNESS_MEDIUM = 2;
|
||||||
|
@ -29,17 +30,36 @@ void TM1651Display::dump_config() {
|
||||||
LOG_PIN(" DIO: ", dio_pin_);
|
LOG_PIN(" DIO: ", dio_pin_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TM1651Display::set_level(uint8_t new_level) {
|
void TM1651Display::set_level_percent(uint8_t new_level) {
|
||||||
this->level_ = calculate_level_(new_level);
|
this->level_ = calculate_level_(new_level);
|
||||||
this->repaint_();
|
this->repaint_();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TM1651Display::set_level(uint8_t new_level) {
|
||||||
|
this->level_ = new_level;
|
||||||
|
this->repaint_();
|
||||||
|
}
|
||||||
|
|
||||||
void TM1651Display::set_brightness(uint8_t new_brightness) {
|
void TM1651Display::set_brightness(uint8_t new_brightness) {
|
||||||
this->brightness_ = calculate_brightness_(new_brightness);
|
this->brightness_ = calculate_brightness_(new_brightness);
|
||||||
this->repaint_();
|
this->repaint_();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TM1651Display::turn_on() {
|
||||||
|
this->is_on_ = true;
|
||||||
|
this->repaint_();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TM1651Display::turn_off() {
|
||||||
|
this->is_on_ = false;
|
||||||
|
battery_display_->displayLevel(0);
|
||||||
|
}
|
||||||
|
|
||||||
void TM1651Display::repaint_() {
|
void TM1651Display::repaint_() {
|
||||||
|
if (!this->is_on_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
battery_display_->set(this->brightness_);
|
battery_display_->set(this->brightness_);
|
||||||
battery_display_->displayLevel(this->level_);
|
battery_display_->displayLevel(this->level_);
|
||||||
}
|
}
|
||||||
|
@ -49,7 +69,7 @@ uint8_t TM1651Display::calculate_level_(uint8_t new_level) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
float calculated_level = TM1651_MAX_LEVEL / (float) (MAX_INPUT_LEVEL / (float) new_level);
|
float calculated_level = TM1651_MAX_LEVEL / (float) (MAX_INPUT_LEVEL_PERCENT / (float) new_level);
|
||||||
return (uint8_t) roundf(calculated_level);
|
return (uint8_t) roundf(calculated_level);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,13 +17,18 @@ class TM1651Display : public Component {
|
||||||
void setup() override;
|
void setup() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
|
void set_level_percent(uint8_t);
|
||||||
void set_level(uint8_t);
|
void set_level(uint8_t);
|
||||||
void set_brightness(uint8_t);
|
void set_brightness(uint8_t);
|
||||||
|
|
||||||
|
void turn_on();
|
||||||
|
void turn_off();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
TM1651 *battery_display_;
|
TM1651 *battery_display_;
|
||||||
GPIOPin *clk_pin_;
|
GPIOPin *clk_pin_;
|
||||||
GPIOPin *dio_pin_;
|
GPIOPin *dio_pin_;
|
||||||
|
bool is_on_ = true;
|
||||||
|
|
||||||
uint8_t brightness_;
|
uint8_t brightness_;
|
||||||
uint8_t level_;
|
uint8_t level_;
|
||||||
|
@ -34,9 +39,20 @@ class TM1651Display : public Component {
|
||||||
uint8_t calculate_brightness_(uint8_t);
|
uint8_t calculate_brightness_(uint8_t);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename... Ts> class SetLevelPercentAction : public Action<Ts...>, public Parented<TM1651Display> {
|
||||||
|
public:
|
||||||
|
TEMPLATABLE_VALUE(uint8_t, level_percent)
|
||||||
|
|
||||||
|
void play(Ts... x) override {
|
||||||
|
auto level_percent = this->level_percent_.value(x...);
|
||||||
|
this->parent_->set_level_percent(level_percent);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
template<typename... Ts> class SetLevelAction : public Action<Ts...>, public Parented<TM1651Display> {
|
template<typename... Ts> class SetLevelAction : public Action<Ts...>, public Parented<TM1651Display> {
|
||||||
public:
|
public:
|
||||||
TEMPLATABLE_VALUE(uint8_t, level)
|
TEMPLATABLE_VALUE(uint8_t, level)
|
||||||
|
|
||||||
void play(Ts... x) override {
|
void play(Ts... x) override {
|
||||||
auto level = this->level_.value(x...);
|
auto level = this->level_.value(x...);
|
||||||
this->parent_->set_level(level);
|
this->parent_->set_level(level);
|
||||||
|
@ -46,11 +62,22 @@ template<typename... Ts> class SetLevelAction : public Action<Ts...>, public Par
|
||||||
template<typename... Ts> class SetBrightnessAction : public Action<Ts...>, public Parented<TM1651Display> {
|
template<typename... Ts> class SetBrightnessAction : public Action<Ts...>, public Parented<TM1651Display> {
|
||||||
public:
|
public:
|
||||||
TEMPLATABLE_VALUE(uint8_t, brightness)
|
TEMPLATABLE_VALUE(uint8_t, brightness)
|
||||||
|
|
||||||
void play(Ts... x) override {
|
void play(Ts... x) override {
|
||||||
auto brightness = this->brightness_.value(x...);
|
auto brightness = this->brightness_.value(x...);
|
||||||
this->parent_->set_brightness(brightness);
|
this->parent_->set_brightness(brightness);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename... Ts> class TurnOnAction : public Action<Ts...>, public Parented<TM1651Display> {
|
||||||
|
public:
|
||||||
|
void play(Ts... x) override { this->parent_->turn_on(); }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename... Ts> class TurnOffAction : public Action<Ts...>, public Parented<TM1651Display> {
|
||||||
|
public:
|
||||||
|
void play(Ts... x) override { this->parent_->turn_off(); }
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace tm1651
|
} // namespace tm1651
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
|
@ -138,6 +138,13 @@ api:
|
||||||
then:
|
then:
|
||||||
- dfplayer.random
|
- dfplayer.random
|
||||||
|
|
||||||
|
- service: battery_level_percent
|
||||||
|
variables:
|
||||||
|
level_percent: int
|
||||||
|
then:
|
||||||
|
- tm1651.set_level_percent:
|
||||||
|
id: tm1651_battery
|
||||||
|
level_percent: !lambda 'return level_percent;'
|
||||||
- service: battery_level
|
- service: battery_level
|
||||||
variables:
|
variables:
|
||||||
level: int
|
level: int
|
||||||
|
@ -152,6 +159,14 @@ api:
|
||||||
- tm1651.set_brightness:
|
- tm1651.set_brightness:
|
||||||
id: tm1651_battery
|
id: tm1651_battery
|
||||||
brightness: !lambda 'return brightness;'
|
brightness: !lambda 'return brightness;'
|
||||||
|
- service: battery_turn_on
|
||||||
|
then:
|
||||||
|
- tm1651.turn_on:
|
||||||
|
id: tm1651_battery
|
||||||
|
- service: battery_turn_on
|
||||||
|
then:
|
||||||
|
- tm1651.turn_off:
|
||||||
|
id: tm1651_battery
|
||||||
|
|
||||||
wifi:
|
wifi:
|
||||||
ssid: 'MySSID'
|
ssid: 'MySSID'
|
||||||
|
|
Loading…
Reference in a new issue