ESPHome  2024.11.0
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 
5 namespace esphome {
6 namespace key_collector {
7 
8 static const char *const TAG = "key_collector";
9 
11  : progress_trigger_(new Trigger<std::string, uint8_t>()),
12  result_trigger_(new Trigger<std::string, uint8_t, uint8_t>()),
13  timeout_trigger_(new Trigger<std::string, uint8_t>()) {}
14 
16  if ((this->timeout_ == 0) || this->result_.empty() || (millis() - this->last_key_time_ < this->timeout_))
17  return;
18  this->timeout_trigger_->trigger(this->result_, this->start_key_);
19  this->clear();
20 }
21 
23  ESP_LOGCONFIG(TAG, "Key Collector:");
24  if (this->min_length_ > 0)
25  ESP_LOGCONFIG(TAG, " min length: %d", this->min_length_);
26  if (this->max_length_ > 0)
27  ESP_LOGCONFIG(TAG, " max length: %d", this->max_length_);
28  if (!this->back_keys_.empty())
29  ESP_LOGCONFIG(TAG, " erase keys '%s'", this->back_keys_.c_str());
30  if (!this->clear_keys_.empty())
31  ESP_LOGCONFIG(TAG, " clear keys '%s'", this->clear_keys_.c_str());
32  if (!this->start_keys_.empty())
33  ESP_LOGCONFIG(TAG, " start keys '%s'", this->start_keys_.c_str());
34  if (!this->end_keys_.empty()) {
35  ESP_LOGCONFIG(TAG, " end keys '%s'", this->end_keys_.c_str());
36  ESP_LOGCONFIG(TAG, " end key is required: %s", ONOFF(this->end_key_required_));
37  }
38  if (!this->allowed_keys_.empty())
39  ESP_LOGCONFIG(TAG, " allowed keys '%s'", this->allowed_keys_.c_str());
40  if (this->timeout_ > 0)
41  ESP_LOGCONFIG(TAG, " entry timeout: %0.1f", this->timeout_ / 1000.0);
42 }
43 
45  provider->add_on_key_callback([this](uint8_t key) { this->key_pressed_(key); });
46 }
47 
48 void KeyCollector::clear(bool progress_update) {
49  this->result_.clear();
50  this->start_key_ = 0;
51  if (progress_update)
52  this->progress_trigger_->trigger(this->result_, 0);
53 }
54 
55 void KeyCollector::send_key(uint8_t key) { this->key_pressed_(key); }
56 
57 void KeyCollector::key_pressed_(uint8_t key) {
58  this->last_key_time_ = millis();
59  if (!this->start_keys_.empty() && !this->start_key_) {
60  if (this->start_keys_.find(key) != std::string::npos) {
61  this->start_key_ = key;
62  this->progress_trigger_->trigger(this->result_, this->start_key_);
63  }
64  return;
65  }
66  if (this->back_keys_.find(key) != std::string::npos) {
67  if (!this->result_.empty()) {
68  this->result_.pop_back();
69  this->progress_trigger_->trigger(this->result_, this->start_key_);
70  }
71  return;
72  }
73  if (this->clear_keys_.find(key) != std::string::npos) {
74  if (!this->result_.empty())
75  this->clear();
76  return;
77  }
78  if (this->end_keys_.find(key) != std::string::npos) {
79  if ((this->min_length_ == 0) || (this->result_.size() >= this->min_length_)) {
80  this->result_trigger_->trigger(this->result_, this->start_key_, key);
81  this->clear();
82  }
83  return;
84  }
85  if (!this->allowed_keys_.empty() && (this->allowed_keys_.find(key) == std::string::npos))
86  return;
87  if ((this->max_length_ == 0) || (this->result_.size() < this->max_length_))
88  this->result_.push_back(key);
89  if ((this->max_length_ > 0) && (this->result_.size() == this->max_length_) && (!this->end_key_required_)) {
90  this->result_trigger_->trigger(this->result_, this->start_key_, 0);
91  this->clear(false);
92  }
93  this->progress_trigger_->trigger(this->result_, this->start_key_);
94 }
95 
96 } // namespace key_collector
97 } // namespace esphome
interface for components that provide keypresses
Definition: key_provider.h:10
STL namespace.
Trigger< std::string, uint8_t > * progress_trigger_
Definition: key_collector.h:45
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
void add_on_key_callback(std::function< void(uint8_t)> &&callback)
Definition: key_provider.cpp:6
void set_provider(key_provider::KeyProvider *provider)
Trigger< std::string, uint8_t, uint8_t > * result_trigger_
Definition: key_collector.h:46
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Trigger< std::string, uint8_t > * timeout_trigger_
Definition: key_collector.h:47
void clear(bool progress_update=true)