button_adc.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  2. *
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. #pragma once
  6. #include "esp_idf_version.h"
  7. #include "driver/gpio.h"
  8. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  9. #include "esp_adc/adc_oneshot.h"
  10. #else
  11. #include "driver/adc.h"
  12. #endif
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #define ADC_BUTTON_COMBINE(channel, index) ((channel)<<8 | (index))
  17. #define ADC_BUTTON_SPLIT_INDEX(data) ((uint32_t)(data)&0xff)
  18. #define ADC_BUTTON_SPLIT_CHANNEL(data) (((uint32_t)(data) >> 8) & 0xff)
  19. /**
  20. * @brief adc button configuration
  21. *
  22. */
  23. typedef struct {
  24. uint8_t adc_channel; /**< Channel of ADC */
  25. uint8_t button_index; /**< button index on the channel */
  26. uint16_t min; /**< min voltage in mv corresponding to the button */
  27. uint16_t max; /**< max voltage in mv corresponding to the button */
  28. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  29. adc_oneshot_unit_handle_t *adc_handle; /**< handle of adc unit, if NULL will create new one internal, else will use the handle */
  30. #endif
  31. } button_adc_config_t;
  32. /**
  33. * @brief Initialize gpio button
  34. *
  35. * @param config pointer of configuration struct
  36. *
  37. * @return
  38. * - ESP_OK on success
  39. * - ESP_ERR_INVALID_ARG Arguments is NULL.
  40. * - ESP_ERR_NOT_SUPPORTED Arguments out of range.
  41. * - ESP_ERR_INVALID_STATE State is error.
  42. */
  43. esp_err_t button_adc_init(const button_adc_config_t *config);
  44. /**
  45. * @brief Deinitialize gpio button
  46. *
  47. * @param channel ADC channel
  48. * @param button_index Button index on the channel
  49. *
  50. * @return
  51. * - ESP_OK on success
  52. * - ESP_ERR_INVALID_ARG Arguments is invalid.
  53. */
  54. esp_err_t button_adc_deinit(uint8_t channel, int button_index);
  55. /**
  56. * @brief Get the adc button level
  57. *
  58. * @param button_index It is compressed by ADC channel and button index, use the macro ADC_BUTTON_COMBINE to generate. It will be treated as a uint32_t variable.
  59. *
  60. * @return
  61. * - 0 Not pressed
  62. * - 1 Pressed
  63. */
  64. uint8_t button_adc_get_key_level(void *button_index);
  65. #ifdef __cplusplus
  66. }
  67. #endif