ESPHome  2022.8.0
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 class TM1651Display : public Component {
17  public:
18  void set_clk_pin(InternalGPIOPin *pin) { clk_pin_ = pin; }
19  void set_dio_pin(InternalGPIOPin *pin) { dio_pin_ = pin; }
20 
21  void setup() override;
22  void dump_config() override;
23 
24  void set_level_percent(uint8_t new_level);
25  void set_level(uint8_t new_level);
26  void set_brightness(uint8_t new_brightness);
27 
28  void turn_on();
29  void turn_off();
30 
31  protected:
32  std::unique_ptr<TM1651> battery_display_;
35  bool is_on_ = true;
36 
37  uint8_t brightness_;
38  uint8_t level_;
39 
40  void repaint_();
41 
42  uint8_t calculate_level_(uint8_t new_level);
43  uint8_t calculate_brightness_(uint8_t new_brightness);
44 };
45 
46 template<typename... Ts> class SetLevelPercentAction : public Action<Ts...>, public Parented<TM1651Display> {
47  public:
48  TEMPLATABLE_VALUE(uint8_t, level_percent)
49 
50  void play(Ts... x) override {
51  auto level_percent = this->level_percent_.value(x...);
52  this->parent_->set_level_percent(level_percent);
53  }
54 };
55 
56 template<typename... Ts> class SetLevelAction : public Action<Ts...>, public Parented<TM1651Display> {
57  public:
58  TEMPLATABLE_VALUE(uint8_t, level)
59 
60  void play(Ts... x) override {
61  auto level = this->level_.value(x...);
62  this->parent_->set_level(level);
63  }
64 };
65 
66 template<typename... Ts> class SetBrightnessAction : public Action<Ts...>, public Parented<TM1651Display> {
67  public:
68  TEMPLATABLE_VALUE(uint8_t, brightness)
69 
70  void play(Ts... x) override {
71  auto brightness = this->brightness_.value(x...);
72  this->parent_->set_brightness(brightness);
73  }
74 };
75 
76 template<typename... Ts> class TurnOnAction : public Action<Ts...>, public Parented<TM1651Display> {
77  public:
78  void play(Ts... x) override { this->parent_->turn_on(); }
79 };
80 
81 template<typename... Ts> class TurnOffAction : public Action<Ts...>, public Parented<TM1651Display> {
82  public:
83  void play(Ts... x) override { this->parent_->turn_off(); }
84 };
85 
86 } // namespace tm1651
87 } // namespace esphome
88 
89 #endif // USE_ARDUINO
void dump_config() override
Definition: tm1651.cpp:30
InternalGPIOPin * clk_pin_
Definition: tm1651.h:33
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_dio_pin(InternalGPIOPin *pin)
Definition: tm1651.h:19
InternalGPIOPin * dio_pin_
Definition: tm1651.h:34
TEMPLATABLE_VALUE(uint8_t, brightness) void play(Ts... x) override
Definition: tm1651.h:68
void set_clk_pin(InternalGPIOPin *pin)
Definition: tm1651.h:18
void set_brightness(uint8_t new_brightness)
Definition: tm1651.cpp:46
uint8_t calculate_brightness_(uint8_t new_brightness)
Definition: tm1651.cpp:79
Definition: a4988.cpp:4
TEMPLATABLE_VALUE(uint8_t, level_percent) void play(Ts... x) override
Definition: tm1651.h:48
void play(Ts... x) override
Definition: tm1651.h:78
TEMPLATABLE_VALUE(uint8_t, level) void play(Ts... x) override
Definition: tm1651.h:58
std::unique_ptr< TM1651 > battery_display_
Definition: tm1651.h:32
Helper class to easily give an object a parent of type T.
Definition: helpers.h:497
void set_level(uint8_t new_level)
Definition: tm1651.cpp:41
void play(Ts... x) override
Definition: tm1651.h:83