ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
matrix_keypad.cpp
Go to the documentation of this file.
1#include "matrix_keypad.h"
2#include "esphome/core/log.h"
4
6
7static const char *const TAG = "matrix_keypad";
8
10 for (auto *pin : this->rows_) {
11 pin->setup();
12 if (!has_diodes_) {
13 pin->pin_mode(gpio::FLAG_INPUT);
14 } else {
15 pin->digital_write(!has_pulldowns_);
16 }
17 }
18 for (auto *pin : this->columns_) {
19 pin->setup();
20 if (has_pulldowns_) {
21 pin->pin_mode(gpio::FLAG_INPUT);
22 } else {
23 pin->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
24 }
25 }
26}
27
30 int key = -1;
31 bool error = false;
32 int pos = 0, row, col;
33 for (auto *row : this->rows_) {
34 if (!has_diodes_)
35 row->pin_mode(gpio::FLAG_OUTPUT);
36 row->digital_write(has_pulldowns_);
37 for (auto *col : this->columns_) {
38 if (col->digital_read() == has_pulldowns_) {
39 if (key != -1) {
40 error = true;
41 } else {
42 key = pos;
43 }
44 }
45 pos++;
46 }
47 row->digital_write(!has_pulldowns_);
48 if (!has_diodes_)
49 row->pin_mode(gpio::FLAG_INPUT);
50 }
51 if (error)
52 return;
53
54 if (key != this->active_key_) {
55 if ((this->active_key_ != -1) && (this->pressed_key_ == this->active_key_)) {
56 row = this->pressed_key_ / this->columns_.size();
57 col = this->pressed_key_ % this->columns_.size();
58 ESP_LOGD(TAG, "key @ row %d, col %d released", row, col);
59 for (auto &listener : this->listeners_)
60 listener->button_released(row, col);
61 if (this->pressed_key_ < (int) this->keys_.size()) {
62 uint8_t keycode = this->keys_[this->pressed_key_];
63 ESP_LOGD(TAG, "key '%c' released", keycode);
64 for (auto &listener : this->listeners_)
65 listener->key_released(keycode);
66 }
67 this->pressed_key_ = -1;
68 }
69
70 this->active_key_ = key;
71 if (key == -1)
72 return;
73 this->active_start_ = now;
74 }
75
76 if ((this->pressed_key_ == key) || (now - this->active_start_ < this->debounce_time_))
77 return;
78
79 row = key / this->columns_.size();
80 col = key % this->columns_.size();
81 ESP_LOGD(TAG, "key @ row %d, col %d pressed", row, col);
82 for (auto &listener : this->listeners_)
83 listener->button_pressed(row, col);
84 if (key < (int) this->keys_.size()) {
85 uint8_t keycode = this->keys_[key];
86 ESP_LOGD(TAG, "key '%c' pressed", keycode);
87 for (auto &trigger : this->key_triggers_)
88 trigger->trigger(keycode);
89 for (auto &listener : this->listeners_)
90 listener->key_pressed(keycode);
91 this->send_key_(keycode);
92 }
93 this->pressed_key_ = key;
94}
95
97 ESP_LOGCONFIG(TAG, "Matrix Keypad:\n"
98 " Rows:");
99 for (auto &pin : this->rows_) {
100 LOG_PIN(" Pin: ", pin);
101 }
102 ESP_LOGCONFIG(TAG, " Cols:");
103 for (auto &pin : this->columns_) {
104 LOG_PIN(" Pin: ", pin);
105 }
106}
107
108void MatrixKeypad::register_listener(MatrixKeypadListener *listener) { this->listeners_.push_back(listener); }
109
111
112} // namespace esphome::matrix_keypad
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
std::vector< MatrixKeypadListener * > listeners_
void register_key_trigger(MatrixKeyTrigger *trig)
std::vector< GPIOPin * > columns_
std::vector< MatrixKeyTrigger * > key_triggers_
std::vector< GPIOPin * > rows_
void register_listener(MatrixKeypadListener *listener)
@ FLAG_OUTPUT
Definition gpio.h:28
@ FLAG_PULLUP
Definition gpio.h:30
@ FLAG_INPUT
Definition gpio.h:27
size_t size_t pos
Definition helpers.h:1038
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t