button_matrix.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #define MATRIX_BUTTON_COMBINE(row_gpio, col_gpio) ((row_gpio)<<8 | (col_gpio))
  11. #define MATRIX_BUTTON_SPLIT_COL(data) ((uint32_t)(data)&0xff)
  12. #define MATRIX_BUTTON_SPLIT_ROW(data) (((uint32_t)(data) >> 8) & 0xff)
  13. /**
  14. * @brief Button matrix key configuration.
  15. * Just need to configure the GPIO associated with this GPIO in the matrix keyboard.
  16. *
  17. * Matrix Keyboard Layout (3x3):
  18. * ----------------------------------------
  19. * | Button 1 | Button 2 | Button 3 |
  20. * | (R1-C1) | (R1-C2) | (R1-C3) |
  21. * |--------------------------------------|
  22. * | Button 4 | Button 5 | Button 6 |
  23. * | (R2-C1) | (R2-C2) | (R2-C3) |
  24. * |--------------------------------------|
  25. * | Button 7 | Button 8 | Button 9 |
  26. * | (R3-C1) | (R3-C2) | (R3-C3) |
  27. * ----------------------------------------
  28. *
  29. * - Button matrix key is driven using row scanning.
  30. * - Buttons within the same column cannot be detected simultaneously,
  31. * but buttons within the same row can be detected without conflicts.
  32. */
  33. typedef struct {
  34. int32_t row_gpio_num; /**< GPIO number associated with the row */
  35. int32_t col_gpio_num; /**< GPIO number associated with the column */
  36. } button_matrix_config_t;
  37. /**
  38. * @brief Initialize a button matrix keyboard.
  39. *
  40. * @param config Pointer to the button matrix key configuration.
  41. * @return
  42. * - ESP_OK on success
  43. * - ESP_ERR_INVALID_ARG if the argument is NULL.
  44. *
  45. * @note When initializing the button matrix keyboard, the row GPIO pins will be set as outputs,
  46. * and the column GPIO pins will be set as inputs, both with pull-down resistors enabled.
  47. */
  48. esp_err_t button_matrix_init(const button_matrix_config_t *config);
  49. /**
  50. * @brief Deinitialize a button in the matrix keyboard.
  51. *
  52. * @param row_gpio_num GPIO number of the row where the button is located.
  53. * @param col_gpio_num GPIO number of the column where the button is located.
  54. * @return
  55. * - ESP_OK if the button is successfully deinitialized
  56. *
  57. * @note When deinitializing a button, please exercise caution and avoid deinitializing a button individually, as it may affect the proper functioning of other buttons in the same row or column.
  58. */
  59. esp_err_t button_matrix_deinit(int row_gpio_num, int col_gpio_num);
  60. /**
  61. * @brief Get the key level from the button matrix hardware.
  62. *
  63. * @param hardware_data Pointer to hardware-specific data containing information about row GPIO and column GPIO.
  64. * @return uint8_t[out] The key level read from the hardware.
  65. *
  66. * @note This function retrieves the key level from the button matrix hardware.
  67. * The `hardware_data` parameter should contain information about the row and column GPIO pins,
  68. * and you can access this information using the `MATRIX_BUTTON_SPLIT_COL` and `MATRIX_BUTTON_SPLIT_ROW` macros.
  69. */
  70. uint8_t button_matrix_get_key_level(void *hardware_data);
  71. #ifdef __cplusplus
  72. }
  73. #endif