ESPHome  2024.4.0
tm1637.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
5 #include "esphome/core/hal.h"
6 #include "esphome/core/time.h"
7 
8 #include <vector>
9 
10 #ifdef USE_BINARY_SENSOR
12 #endif
13 
14 namespace esphome {
15 namespace tm1637 {
16 
17 class TM1637Display;
18 #ifdef USE_BINARY_SENSOR
19 class TM1637Key;
20 #endif
21 
22 using tm1637_writer_t = std::function<void(TM1637Display &)>;
23 
25  public:
26  void set_writer(tm1637_writer_t &&writer) { this->writer_ = writer; }
27 
28  void setup() override;
29 
30  void dump_config() override;
31 
32  void set_clk_pin(GPIOPin *pin) { clk_pin_ = pin; }
33  void set_dio_pin(GPIOPin *pin) { dio_pin_ = pin; }
34 
35  float get_setup_priority() const override;
36 
37  void update() override;
38 
40  uint8_t printf(uint8_t pos, const char *format, ...) __attribute__((format(printf, 3, 4)));
42  uint8_t printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
43 
45  uint8_t print(uint8_t pos, const char *str);
47  uint8_t print(const char *str);
48 
49  void set_intensity(uint8_t intensity) { this->intensity_ = intensity; }
50  void set_inverted(bool inverted) { this->inverted_ = inverted; }
51  void set_length(uint8_t length) { this->length_ = length; }
52 
53  void display();
54 
55 #ifdef USE_BINARY_SENSOR
56  void loop() override;
57  uint8_t get_keys();
58  void add_tm1637_key(TM1637Key *tm1637_key) { this->tm1637_keys_.push_back(tm1637_key); }
59 #endif
60 
62  uint8_t strftime(uint8_t pos, const char *format, ESPTime time) __attribute__((format(strftime, 3, 0)));
64  uint8_t strftime(const char *format, ESPTime time) __attribute__((format(strftime, 2, 0)));
65 
66  protected:
67  void bit_delay_();
68  void setup_pins_();
69  bool send_byte_(uint8_t b);
70  uint8_t read_byte_();
71  void start_();
72  void stop_();
73 
76  uint8_t intensity_;
77  uint8_t length_;
78  bool inverted_;
80  uint8_t buffer_[6] = {0};
81 #ifdef USE_BINARY_SENSOR
82  std::vector<TM1637Key *> tm1637_keys_{};
83 #endif
84 };
85 
86 #ifdef USE_BINARY_SENSOR
88  friend class TM1637Display;
89 
90  public:
91  void set_keycode(uint8_t key_code) { key_code_ = key_code; }
92  void process(uint8_t data) { this->publish_state(static_cast<bool>(data == this->key_code_)); }
93 
94  protected:
95  uint8_t key_code_{0};
96 };
97 #endif
98 
99 } // namespace tm1637
100 } // namespace esphome
void set_length(uint8_t length)
Definition: tm1637.h:51
bool send_byte_(uint8_t b)
Definition: tm1637.cpp:231
uint8_t strftime(uint8_t pos, const char *format, ESPTime time) __attribute__((format(strftime
Evaluate the strftime-format and print the result at the given position.
Definition: tm1637.cpp:372
void set_intensity(uint8_t intensity)
Definition: tm1637.h:49
float get_setup_priority() const override
Definition: tm1637.cpp:185
void process(uint8_t data)
Definition: tm1637.h:92
A more user-friendly version of struct tm from time.h.
Definition: time.h:17
optional< tm1637_writer_t > writer_
Definition: tm1637.h:79
void set_clk_pin(GPIOPin *pin)
Definition: tm1637.h:32
This class simplifies creating components that periodically check a state.
Definition: component.h:283
void set_writer(tm1637_writer_t &&writer)
Definition: tm1637.h:26
uint8_t printf(uint8_t pos, const char *format,...) __attribute__((format(printf
Evaluate the printf-format and print the result at the given position.
Definition: tm1637.cpp:351
void set_inverted(bool inverted)
Definition: tm1637.h:50
void set_keycode(uint8_t key_code)
Definition: tm1637.h:91
void set_dio_pin(GPIOPin *pin)
Definition: tm1637.h:33
enum esphome::EntityCategory __attribute__
std::function< void(TM1637Display &)> tm1637_writer_t
Definition: tm1637.h:22
void add_tm1637_key(TM1637Key *tm1637_key)
Definition: tm1637.h:58
uint8_t uint8_t uint8_t print(uint8_t pos, const char *str)
Print str at the given position.
Definition: tm1637.cpp:300
uint16_t length
Definition: tt21100.cpp:12
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void dump_config() override
Definition: tm1637.cpp:137
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:37
std::vector< TM1637Key * > tm1637_keys_
Definition: tm1637.h:82