1 /* Matrix Keyboard (based on dedicated GPIO) example
2 
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4 
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <stdio.h>
10 #include "esp_log.h"
11 #include "matrix_keyboard.h"
12 
13 const static char *TAG = "example";
14 
15 /**
16  * @brief Matrix keyboard event handler
17  * @note This function is run under OS timer task context
18  */
example_matrix_kbd_event_handler(matrix_kbd_handle_t mkbd_handle,matrix_kbd_event_id_t event,void * event_data,void * handler_args)19 esp_err_t example_matrix_kbd_event_handler(matrix_kbd_handle_t mkbd_handle, matrix_kbd_event_id_t event, void *event_data, void *handler_args)
20 {
21     uint32_t key_code = (uint32_t)event_data;
22     switch (event) {
23     case MATRIX_KBD_EVENT_DOWN:
24         ESP_LOGI(TAG, "press event, key code = %04x", key_code);
25         break;
26     case MATRIX_KBD_EVENT_UP:
27         ESP_LOGI(TAG, "release event, key code = %04x", key_code);
28         break;
29     }
30     return ESP_OK;
31 }
32 
app_main(void)33 void app_main(void)
34 {
35     matrix_kbd_handle_t kbd = NULL;
36     // Apply default matrix keyboard configuration
37     matrix_kbd_config_t config = MATRIX_KEYBOARD_DEFAULT_CONFIG();
38     // Set GPIOs used by row and column line
39     config.col_gpios = (int[]) {
40         10, 11, 12, 13
41     };
42     config.nr_col_gpios = 4;
43     config.row_gpios = (int[]) {
44         14, 15, 16, 17
45     };
46     config.nr_row_gpios = 4;
47     // Install matrix keyboard driver
48     matrix_kbd_install(&config, &kbd);
49     // Register keyboard input event handler
50     matrix_kbd_register_event_handler(kbd, example_matrix_kbd_event_handler, NULL);
51     // Keyboard start to work
52     matrix_kbd_start(kbd);
53 }
54