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