45集 ESP32 ADC按键程序编写
参考例程是
D:\Espressif\esp-adf\examples\checks\check_board_buttons
1、config
这个向下兼容的要加上,如果不加会有错误。
CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY=y
2、程序里面引入如下头文件
#include “periph_adc_button.h”
#include “input_key_service.h”
3、加入如下代码
static esp_err_t input_key_service_cb(periph_service_handle_t handle, periph_service_event_t *evt, void *ctx)
{
ESP_LOGD(TAG, "[ * ] input key id is %d, %d", (int)evt->data, evt->type);
const char *key_types[INPUT_KEY_SERVICE_ACTION_PRESS_RELEASE + 1] = {"UNKNOWN", "CLICKED", "CLICK RELEASED", "PRESSED", "PRESS RELEASED"};
switch ((int)evt->data) {
case INPUT_KEY_USER_ID_REC:
ESP_LOGI(TAG, "[ * ] [Rec] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_SET:
ESP_LOGI(TAG, "[ * ] [SET] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_PLAY:
ESP_LOGI(TAG, "[ * ] [Play] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_MODE:
ESP_LOGI(TAG, "[ * ] [MODE] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_VOLDOWN:
ESP_LOGI(TAG, "[ * ] [Vol-] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_VOLUP:
ESP_LOGI(TAG, "[ * ] [Vol+] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_MUTE:
ESP_LOGI(TAG, "[ * ] [MUTE] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_CAPTURE:
ESP_LOGI(TAG, "[ * ] [CAPTURE] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_MSG:
ESP_LOGI(TAG, "[ * ] [MSG] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_BATTERY_CHARGING:
ESP_LOGI(TAG, "[ * ] [BATTERY_CHARGING] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_WAKEUP:
ESP_LOGI(TAG, "[ * ] [WAKEUP] KEY %s", key_types[evt->type]);
break;
case INPUT_KEY_USER_ID_COLOR:
ESP_LOGI(TAG, "[ * ] [COLOR] KEY %s", key_types[evt->type]);
break;
default:
ESP_LOGE(TAG, "User Key ID[%d] does not support", (int)evt->data);
break;
}
return ESP_OK;
}
4、加入key初始化函数
void key_start(esp_periph_set_handle_t set)
{
ESP_LOGI(TAG, "[ key_start ] Initialize Button peripheral with board init");
audio_board_key_init(set);
ESP_LOGI(TAG, "[ key_start ] Create and start input key service");
input_key_service_info_t input_key_info[] = INPUT_KEY_DEFAULT_INFO();
input_key_service_cfg_t input_cfg = INPUT_KEY_SERVICE_DEFAULT_CONFIG();
input_cfg.handle = set;
input_cfg.based_cfg.task_stack = 4 * 1024;
periph_service_handle_t input_ser = input_key_service_create(&input_cfg);
input_key_service_add_key(input_ser, input_key_info, INPUT_KEY_NUM);
periph_service_set_callback(input_ser, input_key_service_cb, NULL);
ESP_LOGW(TAG, "[ 4 ] Waiting for a button to be pressed ...");
}
带着