ESPHome  2024.4.2
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  if (!has_diodes_) {
12  pin->pin_mode(gpio::FLAG_INPUT);
13  } else {
14  pin->digital_write(!has_pulldowns_);
15  }
16  }
17  for (auto *pin : this->columns_) {
18  if (has_pulldowns_) {
19  pin->pin_mode(gpio::FLAG_INPUT);
20  } else {
21  pin->pin_mode(gpio::FLAG_INPUT | gpio::FLAG_PULLUP);
22  }
23  }
24 }
25 
27  static uint32_t active_start = 0;
28  static int active_key = -1;
29  uint32_t now = millis();
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 != active_key) {
55  if ((active_key != -1) && (this->pressed_key_ == 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->keys_.empty()) {
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  active_key = key;
71  if (key == -1)
72  return;
73  active_start = now;
74  }
75 
76  if ((this->pressed_key_ == key) || (now - 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 (!this->keys_.empty()) {
85  uint8_t keycode = this->keys_[key];
86  ESP_LOGD(TAG, "key '%c' pressed", keycode);
87  for (auto &listener : this->listeners_)
88  listener->key_pressed(keycode);
89  this->send_key_(keycode);
90  }
91  this->pressed_key_ = key;
92 }
93 
95  ESP_LOGCONFIG(TAG, "Matrix Keypad:");
96  ESP_LOGCONFIG(TAG, " Rows:");
97  for (auto &pin : this->rows_) {
98  LOG_PIN(" Pin: ", pin);
99  }
100  ESP_LOGCONFIG(TAG, " Cols:");
101  for (auto &pin : this->columns_) {
102  LOG_PIN(" Pin: ", pin);
103  }
104 }
105 
106 void MatrixKeypad::register_listener(MatrixKeypadListener *listener) { this->listeners_.push_back(listener); }
107 
108 } // namespace matrix_keypad
109 } // 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
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void register_listener(MatrixKeypadListener *listener)
std::vector< GPIOPin * > rows_
Definition: matrix_keypad.h:36