ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
key_collector.cpp
Go to the documentation of this file.
1#include "key_collector.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
6
7static const char *const TAG = "key_collector";
8
10 if ((this->timeout_ == 0) || this->result_.empty() || (millis() - this->last_key_time_ < this->timeout_))
11 return;
12 this->timeout_callbacks_.call(this->result_, this->start_key_);
13 this->clear();
14}
15
17 ESP_LOGCONFIG(TAG, "Key Collector:");
18 if (this->min_length_ > 0)
19 ESP_LOGCONFIG(TAG, " min length: %d", this->min_length_);
20 if (this->max_length_ > 0)
21 ESP_LOGCONFIG(TAG, " max length: %d", this->max_length_);
22 if (!this->back_keys_.empty())
23 ESP_LOGCONFIG(TAG, " erase keys '%s'", this->back_keys_.c_str());
24 if (!this->clear_keys_.empty())
25 ESP_LOGCONFIG(TAG, " clear keys '%s'", this->clear_keys_.c_str());
26 if (!this->start_keys_.empty())
27 ESP_LOGCONFIG(TAG, " start keys '%s'", this->start_keys_.c_str());
28 if (!this->end_keys_.empty()) {
29 ESP_LOGCONFIG(TAG,
30 " end keys '%s'\n"
31 " end key is required: %s",
32 this->end_keys_.c_str(), ONOFF(this->end_key_required_));
33 }
34 if (!this->allowed_keys_.empty())
35 ESP_LOGCONFIG(TAG, " allowed keys '%s'", this->allowed_keys_.c_str());
36 if (this->timeout_ > 0)
37 ESP_LOGCONFIG(TAG, " entry timeout: %0.1f", this->timeout_ / 1000.0);
38}
39
41 provider->add_on_key_callback([this](uint8_t key) { this->send_key(key); });
42}
43
44void KeyCollector::set_enabled(bool enabled) {
45 this->enabled_ = enabled;
46 if (!enabled) {
47 this->clear(false);
48 }
49}
50
51void KeyCollector::clear(bool progress_update) {
52 auto had_state = !this->result_.empty() || this->start_key_ != 0;
53 this->result_.clear();
54 this->start_key_ = 0;
55 if (progress_update && had_state) {
56 this->progress_callbacks_.call(this->result_, 0);
57 }
58 this->disable_loop();
59}
60
61void KeyCollector::send_key(uint8_t key) {
62 if (!this->enabled_)
63 return;
64 this->last_key_time_ = millis();
65 if (!this->start_keys_.empty() && !this->start_key_) {
66 if (this->start_keys_.find(key) != std::string::npos) {
67 this->start_key_ = key;
68 this->progress_callbacks_.call(this->result_, this->start_key_);
69 }
70 return;
71 }
72 if (this->back_keys_.find(key) != std::string::npos) {
73 if (!this->result_.empty()) {
74 this->result_.pop_back();
75 this->progress_callbacks_.call(this->result_, this->start_key_);
76 }
77 return;
78 }
79 if (this->clear_keys_.find(key) != std::string::npos) {
80 this->clear();
81 return;
82 }
83 if (this->end_keys_.find(key) != std::string::npos) {
84 if ((this->min_length_ == 0) || (this->result_.size() >= this->min_length_)) {
85 this->result_callbacks_.call(this->result_, this->start_key_, key);
86 this->clear();
87 }
88 return;
89 }
90 if (!this->allowed_keys_.empty() && this->allowed_keys_.find(key) == std::string::npos)
91 return;
92 if ((this->max_length_ == 0) || (this->result_.size() < this->max_length_)) {
93 if (this->result_.empty())
94 this->enable_loop();
95 this->result_.push_back(key);
96 }
97 if ((this->max_length_ > 0) && (this->result_.size() == this->max_length_) && (!this->end_key_required_)) {
98 this->result_callbacks_.call(this->result_, this->start_key_, 0);
99 this->clear(false);
100 }
101 this->progress_callbacks_.call(this->result_, this->start_key_);
102}
103
104} // namespace esphome::key_collector
void enable_loop()
Enable this component's loop.
Definition component.h:246
void disable_loop()
Disable this component's loop.
LazyCallbackManager< void(const std::string &, uint8_t)> timeout_callbacks_
void add_provider(key_provider::KeyProvider *provider)
LazyCallbackManager< void(const std::string &, uint8_t)> progress_callbacks_
void clear(bool progress_update=true)
LazyCallbackManager< void(const std::string &, uint8_t, uint8_t)> result_callbacks_
interface for components that provide keypresses
Definition key_provider.h:9
void add_on_key_callback(F &&callback)
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28