ESPHome  2022.6.2
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 #ifdef USE_TIME
9 #endif
10 
11 #ifdef USE_BINARY_SENSOR
13 #endif
14 
15 namespace esphome {
16 namespace tm1637 {
17 
18 class TM1637Display;
19 #ifdef USE_BINARY_SENSOR
20 class TM1637Key;
21 #endif
22 
23 using tm1637_writer_t = std::function<void(TM1637Display &)>;
24 
26  public:
27  void set_writer(tm1637_writer_t &&writer) { this->writer_ = writer; }
28 
29  void setup() override;
30 
31  void dump_config() override;
32 
33  void set_clk_pin(GPIOPin *pin) { clk_pin_ = pin; }
34  void set_dio_pin(GPIOPin *pin) { dio_pin_ = pin; }
35 
36  float get_setup_priority() const override;
37 
38  void update() override;
39 
41  uint8_t printf(uint8_t pos, const char *format, ...) __attribute__((format(printf, 3, 4)));
43  uint8_t printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
44 
46  uint8_t print(uint8_t pos, const char *str);
48  uint8_t print(const char *str);
49 
50  void set_intensity(uint8_t intensity) { this->intensity_ = intensity; }
51  void set_inverted(bool inverted) { this->inverted_ = inverted; }
52  void set_length(uint8_t length) { this->length_ = length; }
53 
54  void display();
55 
56 #ifdef USE_BINARY_SENSOR
57  void loop() override;
58  uint8_t get_keys();
59  void add_tm1637_key(TM1637Key *tm1637_key) { this->tm1637_keys_.push_back(tm1637_key); }
60 #endif
61 
62 #ifdef USE_TIME
63  uint8_t strftime(uint8_t pos, const char *format, time::ESPTime time) __attribute__((format(strftime, 3, 0)));
66  uint8_t strftime(const char *format, time::ESPTime time) __attribute__((format(strftime, 2, 0)));
67 #endif
68 
69  protected:
70  void bit_delay_();
71  void setup_pins_();
72  bool send_byte_(uint8_t b);
73  uint8_t read_byte_();
74  void start_();
75  void stop_();
76 
79  uint8_t intensity_;
80  uint8_t length_;
81  bool inverted_;
83  uint8_t buffer_[6] = {0};
84 #ifdef USE_BINARY_SENSOR
85  std::vector<TM1637Key *> tm1637_keys_{};
86 #endif
87 };
88 
89 #ifdef USE_BINARY_SENSOR
91  friend class TM1637Display;
92 
93  public:
94  void set_keycode(uint8_t key_code) { key_code_ = key_code; }
95  void process(uint8_t data) { this->publish_state(static_cast<bool>(data == this->key_code_)); }
96 
97  protected:
98  uint8_t key_code_{0};
99 };
100 #endif
101 
102 } // namespace tm1637
103 } // namespace esphome
void set_length(uint8_t length)
Definition: tm1637.h:52
bool send_byte_(uint8_t b)
Definition: tm1637.cpp:231
void set_intensity(uint8_t intensity)
Definition: tm1637.h:50
float get_setup_priority() const override
Definition: tm1637.cpp:185
void process(uint8_t data)
Definition: tm1637.h:95
optional< tm1637_writer_t > writer_
Definition: tm1637.h:82
void set_clk_pin(GPIOPin *pin)
Definition: tm1637.h:33
This class simplifies creating components that periodically check a state.
Definition: component.h:266
void set_writer(tm1637_writer_t &&writer)
Definition: tm1637.h:27
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:51
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:94
void set_dio_pin(GPIOPin *pin)
Definition: tm1637.h:34
enum esphome::EntityCategory __attribute__
std::function< void(TM1637Display &)> tm1637_writer_t
Definition: tm1637.h:23
void add_tm1637_key(TM1637Key *tm1637_key)
Definition: tm1637.h:59
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:26
std::vector< TM1637Key * > tm1637_keys_
Definition: tm1637.h:85