matrix_keyboard_example_main.c 1.6 KB

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