ESPHome  2024.11.3
matrix_keypad.cpp
Go to the documentation of this file.
1 #include "matrix_keypad.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace matrix_keypad {
6 
7 static 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 
29  static uint32_t active_start = 0;
30  static int active_key = -1;
31  uint32_t now = millis();
32  int key = -1;
33  bool error = false;
34  int pos = 0, row, col;
35  for (auto *row : this->rows_) {
36  if (!has_diodes_)
37  row->pin_mode(gpio::FLAG_OUTPUT);
38  row->digital_write(has_pulldowns_);
39  for (auto *col : this->columns_) {
40  if (col->digital_read() == has_pulldowns_) {
41  if (key != -1) {
42  error = true;
43  } else {
44  key = pos;
45  }
46  }
47  pos++;
48  }
49  row->digital_write(!has_pulldowns_);
50  if (!has_diodes_)
51  row->pin_mode(gpio::FLAG_INPUT);
52  }
53  if (error)
54  return;
55 
56  if (key != active_key) {
57  if ((active_key != -1) && (this->pressed_key_ == active_key)) {
58  row = this->pressed_key_ / this->columns_.size();
59  col = this->pressed_key_ % this->columns_.size();
60  ESP_LOGD(TAG, "key @ row %d, col %d released", row, col);
61  for (auto &listener : this->listeners_)
62  listener->button_released(row, col);
63  if (!this->keys_.empty()) {
64  uint8_t keycode = this->keys_[this->pressed_key_];
65  ESP_LOGD(TAG, "key '%c' released", keycode);
66  for (auto &listener : this->listeners_)
67  listener->key_released(keycode);
68  }
69  this->pressed_key_ = -1;
70  }
71 
72  active_key = key;
73  if (key == -1)
74  return;
75  active_start = now;
76  }
77 
78  if ((this->pressed_key_ == key) || (now - active_start < this->debounce_time_))
79  return;
80 
81  row = key / this->columns_.size();
82  col = key % this->columns_.size();
83  ESP_LOGD(TAG, "key @ row %d, col %d pressed", row, col);
84  for (auto &listener : this->listeners_)
85  listener->button_pressed(row, col);
86  if (!this->keys_.empty()) {
87  uint8_t keycode = this->keys_[key];
88  ESP_LOGD(TAG, "key '%c' pressed", 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:");
98  ESP_LOGCONFIG(TAG, " 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 
108 void MatrixKeypad::register_listener(MatrixKeypadListener *listener) { this->listeners_.push_back(listener); }
109 
110 } // namespace matrix_keypad
111 } // namespace esphome
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
std::vector< GPIOPin * > columns_
Definition: matrix_keypad.h:37
std::vector< MatrixKeypadListener * > listeners_
Definition: matrix_keypad.h:44
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void register_listener(MatrixKeypadListener *listener)
std::vector< GPIOPin * > rows_
Definition: matrix_keypad.h:36