#include "input.h" #include "board.h" #include "iot_knob.h" #include "iot_button.h" #include "button_gpio.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" static const char *TAG = "input"; static QueueHandle_t queue; static void post(input_event_t ev) { xQueueSend(queue, &ev, 0); } static void knob_cb(void *arg, void *data) { post((input_event_t)(intptr_t)data); } static void button_cb(void *arg, void *data) { post((input_event_t)(intptr_t)data); } void input_init(void) { queue = xQueueCreate(8, sizeof(input_event_t)); knob_config_t kcfg = {.default_direction = 0, .gpio_encoder_a = board->enc_a, .gpio_encoder_b = board->enc_b}; knob_handle_t knob = iot_knob_create(&kcfg); if (!knob) { ESP_LOGE(TAG, "knob init failed"); return; } iot_knob_register_cb(knob, KNOB_RIGHT, knob_cb, (void *)INPUT_CW); iot_knob_register_cb(knob, KNOB_LEFT, knob_cb, (void *)INPUT_CCW); button_config_t bcfg = {.long_press_time = 800, .short_press_time = 180}; button_gpio_config_t gcfg = {.gpio_num = board->enc_btn, .active_level = 0}; button_handle_t btn = NULL; ESP_ERROR_CHECK(iot_button_new_gpio_device(&bcfg, &gcfg, &btn)); iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, NULL, button_cb, (void *)INPUT_CLICK); iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, NULL, button_cb, (void *)INPUT_DOUBLE); iot_button_register_cb(btn, BUTTON_LONG_PRESS_START, NULL, button_cb, (void *)INPUT_LONG); if (board->user_key >= 0) { button_gpio_config_t ucfg = {.gpio_num = board->user_key, .active_level = 0}; button_handle_t ukey = NULL; if (iot_button_new_gpio_device(&bcfg, &ucfg, &ukey) == ESP_OK) iot_button_register_cb(ukey, BUTTON_SINGLE_CLICK, NULL, button_cb, (void *)INPUT_KEY); } ESP_LOGI(TAG, "encoder A%d/B%d, button GPIO%d", board->enc_a, board->enc_b, board->enc_btn); } input_event_t input_poll(void) { input_event_t ev; return xQueueReceive(queue, &ev, 0) == pdTRUE ? ev : INPUT_NONE; } input_event_t input_wait(int timeout_ms) { input_event_t ev; return xQueueReceive(queue, &ev, pdMS_TO_TICKS(timeout_ms)) == pdTRUE ? ev : INPUT_NONE; }