ESPHome  2022.12.8
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 
7 #include <vector>
8 
9 #ifdef USE_TIME
11 #endif
12 
13 #ifdef USE_BINARY_SENSOR
15 #endif
16 
17 namespace esphome {
18 namespace tm1637 {
19 
20 class TM1637Display;
21 #ifdef USE_BINARY_SENSOR
22 class TM1637Key;
23 #endif
24 
25 using tm1637_writer_t = std::function<void(TM1637Display &)>;
26 
28  public:
29  void set_writer(tm1637_writer_t &&writer) { this->writer_ = writer; }
30 
31  void setup() override;
32 
33  void dump_config() override;
34 
35  void set_clk_pin(GPIOPin *pin) { clk_pin_ = pin; }
36  void set_dio_pin(GPIOPin *pin) { dio_pin_ = pin; }
37 
38  float get_setup_priority() const override;
39 
40  void update() override;
41 
43  uint8_t printf(uint8_t pos, const char *format, ...) __attribute__((format(printf, 3, 4)));
45  uint8_t printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
46 
48  uint8_t print(uint8_t pos, const char *str);
50  uint8_t print(const char *str);
51 
52  void set_intensity(uint8_t intensity) { this->intensity_ = intensity; }
53  void set_inverted(bool inverted) { this->inverted_ = inverted; }
54  void set_length(uint8_t length) { this->length_ = length; }
55 
56  void display();
57 
58 #ifdef USE_BINARY_SENSOR
59  void loop() override;
60  uint8_t get_keys();
61  void add_tm1637_key(TM1637Key *tm1637_key) { this->tm1637_keys_.push_back(tm1637_key); }
62 #endif
63 
64 #ifdef USE_TIME
65  uint8_t strftime(uint8_t pos, const char *format, time::ESPTime time) __attribute__((format(strftime, 3, 0)));
68  uint8_t strftime(const char *format, time::ESPTime time) __attribute__((format(strftime, 2, 0)));
69 #endif
70 
71  protected:
72  void bit_delay_();
73  void setup_pins_();
74  bool send_byte_(uint8_t b);
75  uint8_t read_byte_();
76  void start_();
77  void stop_();
78 
81  uint8_t intensity_;
82  uint8_t length_;
83  bool inverted_;
85  uint8_t buffer_[6] = {0};
86 #ifdef USE_BINARY_SENSOR
87  std::vector<TM1637Key *> tm1637_keys_{};
88 #endif
89 };
90 
91 #ifdef USE_BINARY_SENSOR
93  friend class TM1637Display;
94 
95  public:
96  void set_keycode(uint8_t key_code) { key_code_ = key_code; }
97  void process(uint8_t data) { this->publish_state(static_cast<bool>(data == this->key_code_)); }
98 
99  protected:
100  uint8_t key_code_{0};
101 };
102 #endif
103 
104 } // namespace tm1637
105 } // namespace esphome
void set_length(uint8_t length)
Definition: tm1637.h:54
bool send_byte_(uint8_t b)
Definition: tm1637.cpp:231
void set_intensity(uint8_t intensity)
Definition: tm1637.h:52
float get_setup_priority() const override
Definition: tm1637.cpp:185
void process(uint8_t data)
Definition: tm1637.h:97
optional< tm1637_writer_t > writer_
Definition: tm1637.h:84
void set_clk_pin(GPIOPin *pin)
Definition: tm1637.h:35
This class simplifies creating components that periodically check a state.
Definition: component.h:267
void set_writer(tm1637_writer_t &&writer)
Definition: tm1637.h:29
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:350
void set_inverted(bool inverted)
Definition: tm1637.h:53
A more user-friendly version of struct tm from time.h.
uint8_t strftime(uint8_t pos, const char *format, time::ESPTime time) __attribute__((format(strftime
Evaluate the strftime-format and print the result at the given position.
Definition: tm1637.cpp:372
void set_keycode(uint8_t key_code)
Definition: tm1637.h:96
void set_dio_pin(GPIOPin *pin)
Definition: tm1637.h:36
enum esphome::EntityCategory __attribute__
std::function< void(TM1637Display &)> tm1637_writer_t
Definition: tm1637.h:25
void add_tm1637_key(TM1637Key *tm1637_key)
Definition: tm1637.h:61
uint8_t uint8_t uint8_t print(uint8_t pos, const char *str)
Print str at the given position.
Definition: tm1637.cpp:300
Definition: a4988.cpp:4
void dump_config() override
Definition: tm1637.cpp:137
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:28
std::vector< TM1637Key * > tm1637_keys_
Definition: tm1637.h:87