ESPHome 2026.3.0
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
5namespace esphome {
6namespace matrix_keypad {
7
8static const char *const TAG = "matrix_keypad";
9
11 for (auto *pin : this->rows_) {
12 pin->setup();
13 if (!has_diodes_) {
14 pin->pin_mode(gpio::FLAG_INPUT);
15 } else {
16 pin->digital_write(!has_pulldowns_);
17 }
18 }
19 for (auto *pin : this->columns_) {
20 pin->setup();
21 if (has_pulldowns_) {
22 pin->pin_mode(gpio::FLAG_INPUT);
23 } else {
24 pin->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
25 }
26 }
27}
28
31 int key = -1;
32 bool error = false;
33 int pos = 0, row, col;
34 for (auto *row : this->rows_) {
35 if (!has_diodes_)
36 row->pin_mode(gpio::FLAG_OUTPUT);
37 row->digital_write(has_pulldowns_);
38 for (auto *col : this->columns_) {
39 if (col->digital_read() == has_pulldowns_) {
40 if (key != -1) {
41 error = true;
42 } else {
43 key = pos;
44 }
45 }
46 pos++;
47 }
48 row->digital_write(!has_pulldowns_);
49 if (!has_diodes_)
50 row->pin_mode(gpio::FLAG_INPUT);
51 }
52 if (error)
53 return;
54
55 if (key != this->active_key_) {
56 if ((this->active_key_ != -1) && (this->pressed_key_ == this->active_key_)) {
57 row = this->pressed_key_ / this->columns_.size();
58 col = this->pressed_key_ % this->columns_.size();
59 ESP_LOGD(TAG, "key @ row %d, col %d released", row, col);
60 for (auto &listener : this->listeners_)
61 listener->button_released(row, col);
62 if (this->pressed_key_ < (int) this->keys_.size()) {
63 uint8_t keycode = this->keys_[this->pressed_key_];
64 ESP_LOGD(TAG, "key '%c' released", keycode);
65 for (auto &listener : this->listeners_)
66 listener->key_released(keycode);
67 }
68 this->pressed_key_ = -1;
69 }
70
71 this->active_key_ = key;
72 if (key == -1)
73 return;
74 this->active_start_ = now;
75 }
76
77 if ((this->pressed_key_ == key) || (now - this->active_start_ < this->debounce_time_))
78 return;
79
80 row = key / this->columns_.size();
81 col = key % this->columns_.size();
82 ESP_LOGD(TAG, "key @ row %d, col %d pressed", row, col);
83 for (auto &listener : this->listeners_)
84 listener->button_pressed(row, col);
85 if (key < (int) this->keys_.size()) {
86 uint8_t keycode = this->keys_[key];
87 ESP_LOGD(TAG, "key '%c' pressed", keycode);
88 for (auto &trigger : this->key_triggers_)
89 trigger->trigger(keycode);
90 for (auto &listener : this->listeners_)
91 listener->key_pressed(keycode);
92 this->send_key_(keycode);
93 }
94 this->pressed_key_ = key;
95}
96
98 ESP_LOGCONFIG(TAG, "Matrix Keypad:\n"
99 " Rows:");
100 for (auto &pin : this->rows_) {
101 LOG_PIN(" Pin: ", pin);
102 }
103 ESP_LOGCONFIG(TAG, " Cols:");
104 for (auto &pin : this->columns_) {
105 LOG_PIN(" Pin: ", pin);
106 }
107}
108
109void MatrixKeypad::register_listener(MatrixKeypadListener *listener) { this->listeners_.push_back(listener); }
110
112
113} // namespace matrix_keypad
114} // namespace esphome
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
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
size_t size_t pos
Definition helpers.h:929
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t