ESPHome 2025.5.0
Loading...
Searching...
No Matches
tm1651.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ARDUINO
4
5#include <memory>
6
8#include "esphome/core/hal.h"
10
11#include <TM1651.h>
12
13namespace esphome {
14namespace tm1651 {
15
21
22class TM1651Display : public Component {
23 public:
24 void set_clk_pin(InternalGPIOPin *pin) { clk_pin_ = pin; }
25 void set_dio_pin(InternalGPIOPin *pin) { dio_pin_ = pin; }
26
27 void setup() override;
28 void dump_config() override;
29
30 void set_level_percent(uint8_t new_level);
31 void set_level(uint8_t new_level);
32 void set_brightness(uint8_t new_brightness);
33 void set_brightness(TM1651Brightness new_brightness) { this->set_brightness(static_cast<uint8_t>(new_brightness)); }
34
35 void turn_on();
36 void turn_off();
37
38 protected:
39 std::unique_ptr<TM1651> battery_display_;
42 bool is_on_ = true;
43
44 uint8_t brightness_;
45 uint8_t level_;
46
47 void repaint_();
48
49 uint8_t calculate_level_(uint8_t new_level);
50 uint8_t calculate_brightness_(uint8_t new_brightness);
51};
52
53template<typename... Ts> class SetLevelPercentAction : public Action<Ts...>, public Parented<TM1651Display> {
54 public:
55 TEMPLATABLE_VALUE(uint8_t, level_percent)
56
57 void play(Ts... x) override {
58 auto level_percent = this->level_percent_.value(x...);
59 this->parent_->set_level_percent(level_percent);
60 }
61};
62
63template<typename... Ts> class SetLevelAction : public Action<Ts...>, public Parented<TM1651Display> {
64 public:
65 TEMPLATABLE_VALUE(uint8_t, level)
66
67 void play(Ts... x) override {
68 auto level = this->level_.value(x...);
69 this->parent_->set_level(level);
70 }
71};
72
73template<typename... Ts> class SetBrightnessAction : public Action<Ts...>, public Parented<TM1651Display> {
74 public:
75 TEMPLATABLE_VALUE(uint8_t, brightness)
76
77 void play(Ts... x) override {
78 auto brightness = this->brightness_.value(x...);
79 this->parent_->set_brightness(brightness);
80 }
81};
82
83template<typename... Ts> class TurnOnAction : public Action<Ts...>, public Parented<TM1651Display> {
84 public:
85 void play(Ts... x) override { this->parent_->turn_on(); }
86};
87
88template<typename... Ts> class TurnOffAction : public Action<Ts...>, public Parented<TM1651Display> {
89 public:
90 void play(Ts... x) override { this->parent_->turn_off(); }
91};
92
93} // namespace tm1651
94} // namespace esphome
95
96#endif // USE_ARDUINO
virtual void play(Ts... x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
TEMPLATABLE_VALUE(uint8_t, brightness) void play(Ts... x) override
Definition tm1651.h:75
TEMPLATABLE_VALUE(uint8_t, level) void play(Ts... x) override
Definition tm1651.h:65
TEMPLATABLE_VALUE(uint8_t, level_percent) void play(Ts... x) override
Definition tm1651.h:55
void set_brightness(TM1651Brightness new_brightness)
Definition tm1651.h:33
std::unique_ptr< TM1651 > battery_display_
Definition tm1651.h:39
InternalGPIOPin * clk_pin_
Definition tm1651.h:40
void set_level_percent(uint8_t new_level)
Definition tm1651.cpp:36
uint8_t calculate_level_(uint8_t new_level)
Definition tm1651.cpp:70
void set_brightness(uint8_t new_brightness)
Definition tm1651.cpp:46
void set_clk_pin(InternalGPIOPin *pin)
Definition tm1651.h:24
void set_dio_pin(InternalGPIOPin *pin)
Definition tm1651.h:25
uint8_t calculate_brightness_(uint8_t new_brightness)
Definition tm1651.cpp:79
InternalGPIOPin * dio_pin_
Definition tm1651.h:41
void set_level(uint8_t new_level)
Definition tm1651.cpp:41
void dump_config() override
Definition tm1651.cpp:30
void play(Ts... x) override
Definition tm1651.h:90
void play(Ts... x) override
Definition tm1651.h:85
@ TM1651_BRIGHTNESS_HIGH
Definition tm1651.h:19
@ TM1651_BRIGHTNESS_MEDIUM
Definition tm1651.h:18
@ TM1651_BRIGHTNESS_LOW
Definition tm1651.h:17
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5