ESPHome  2024.11.1
tm1651.cpp
Go to the documentation of this file.
1 #ifdef USE_ARDUINO
2 
3 #include "tm1651.h"
4 #include "esphome/core/log.h"
5 #include "esphome/core/helpers.h"
6 
7 namespace esphome {
8 namespace tm1651 {
9 
10 static const char *const TAG = "tm1651.display";
11 
12 static const uint8_t MAX_INPUT_LEVEL_PERCENT = 100;
13 static const uint8_t TM1651_MAX_LEVEL = 7;
14 
15 static const uint8_t TM1651_BRIGHTNESS_LOW_HW = 0;
16 static const uint8_t TM1651_BRIGHTNESS_MEDIUM_HW = 2;
17 static const uint8_t TM1651_BRIGHTNESS_HIGH_HW = 7;
18 
20  ESP_LOGCONFIG(TAG, "Setting up TM1651...");
21 
22  uint8_t clk = clk_pin_->get_pin();
23  uint8_t dio = dio_pin_->get_pin();
24 
25  battery_display_ = make_unique<TM1651>(clk, dio);
26  battery_display_->init();
27  battery_display_->clearDisplay();
28 }
29 
31  ESP_LOGCONFIG(TAG, "TM1651 Battery Display");
32  LOG_PIN(" CLK: ", clk_pin_);
33  LOG_PIN(" DIO: ", dio_pin_);
34 }
35 
36 void TM1651Display::set_level_percent(uint8_t new_level) {
37  this->level_ = calculate_level_(new_level);
38  this->repaint_();
39 }
40 
41 void TM1651Display::set_level(uint8_t new_level) {
42  this->level_ = new_level;
43  this->repaint_();
44 }
45 
46 void TM1651Display::set_brightness(uint8_t new_brightness) {
47  this->brightness_ = calculate_brightness_(new_brightness);
48  this->repaint_();
49 }
50 
52  this->is_on_ = true;
53  this->repaint_();
54 }
55 
57  this->is_on_ = false;
58  battery_display_->displayLevel(0);
59 }
60 
62  if (!this->is_on_) {
63  return;
64  }
65 
66  battery_display_->set(this->brightness_);
67  battery_display_->displayLevel(this->level_);
68 }
69 
70 uint8_t TM1651Display::calculate_level_(uint8_t new_level) {
71  if (new_level == 0) {
72  return 0;
73  }
74 
75  float calculated_level = TM1651_MAX_LEVEL / (float) (MAX_INPUT_LEVEL_PERCENT / (float) new_level);
76  return (uint8_t) roundf(calculated_level);
77 }
78 
79 uint8_t TM1651Display::calculate_brightness_(uint8_t new_brightness) {
80  if (new_brightness <= 1) {
81  return TM1651_BRIGHTNESS_LOW_HW;
82  } else if (new_brightness == 2) {
83  return TM1651_BRIGHTNESS_MEDIUM_HW;
84  } else if (new_brightness >= 3) {
85  return TM1651_BRIGHTNESS_HIGH_HW;
86  }
87 
88  return TM1651_BRIGHTNESS_LOW_HW;
89 }
90 
91 } // namespace tm1651
92 } // namespace esphome
93 
94 #endif // USE_ARDUINO
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
uint8_t calculate_level_(uint8_t new_level)
Definition: tm1651.cpp:70
virtual uint8_t get_pin() const =0
InternalGPIOPin * dio_pin_
Definition: tm1651.h:41
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
std::unique_ptr< TM1651 > battery_display_
Definition: tm1651.h:39
void set_level(uint8_t new_level)
Definition: tm1651.cpp:41