ESPHome  2024.11.1
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 
13 namespace esphome {
14 namespace tm1651 {
15 
16 enum TM1651Brightness : uint8_t {
20 };
21 
22 class 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 
53 template<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 
63 template<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 
73 template<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 
83 template<typename... Ts> class TurnOnAction : public Action<Ts...>, public Parented<TM1651Display> {
84  public:
85  void play(Ts... x) override { this->parent_->turn_on(); }
86 };
87 
88 template<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
void set_brightness(TM1651Brightness new_brightness)
Definition: tm1651.h:33
void dump_config() override
Definition: tm1651.cpp:30
InternalGPIOPin * clk_pin_
Definition: tm1651.h:40
void set_level_percent(uint8_t new_level)
Definition: tm1651.cpp:36
uint16_t x
Definition: tt21100.cpp:17
uint8_t calculate_level_(uint8_t new_level)
Definition: tm1651.cpp:70
void set_dio_pin(InternalGPIOPin *pin)
Definition: tm1651.h:25
InternalGPIOPin * dio_pin_
Definition: tm1651.h:41
TEMPLATABLE_VALUE(uint8_t, brightness) void play(Ts... x) override
Definition: tm1651.h:75
void set_clk_pin(InternalGPIOPin *pin)
Definition: tm1651.h:24
void set_brightness(uint8_t new_brightness)
Definition: tm1651.cpp:46
uint8_t calculate_brightness_(uint8_t new_brightness)
Definition: tm1651.cpp:79
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
TEMPLATABLE_VALUE(uint8_t, level_percent) void play(Ts... x) override
Definition: tm1651.h:55
void play(Ts... x) override
Definition: tm1651.h:85
TEMPLATABLE_VALUE(uint8_t, level) void play(Ts... x) override
Definition: tm1651.h:65
std::unique_ptr< TM1651 > battery_display_
Definition: tm1651.h:39
Helper class to easily give an object a parent of type T.
Definition: helpers.h:522
void set_level(uint8_t new_level)
Definition: tm1651.cpp:41
void play(Ts... x) override
Definition: tm1651.h:90