#include "buttons.h" #include "iot_button.h" #include "button_gpio.h" #include "esp_log.h" #include "esp_err.h" #include "esp_check.h" static const char* TAG = "TrainThing/buttons"; const action actions[] = { {.btnAcc = FRONT_LIGHT_INCREASE, .name = "FRONT_LIGHT_INCREASE", .pin = 1, .shortcut = 'a'}, {.btnAcc = FRONT_LIGHT_DECREASE, .name = "FRONT_LIGHT_DECREASE", .pin = 2, .shortcut = 'b'}, {.btnAcc = BACK_LIGHT_INCREASE, .name = "BACK_LIGHT_INCREASE", .pin = 3, .shortcut = 'c'}, {.btnAcc = BACK_LIGHT_DECREASE, .name = "BACK_LIGHT_DECREASE", .pin = 4, .shortcut = 'd'}, {.btnAcc = CAB_LIGHT, .name = "CAB_LIGHT", .pin = 5, .shortcut = 'e'}, {.btnAcc = WIPPERS, .name = "WIPPERS", .pin = 6, .shortcut = 'f'}, {.btnAcc = SANDER, .name = "SANDER", .pin = 7, .shortcut = 'g'}, {.btnAcc = STARTER, .name = "STARTER", .pin = 8, .shortcut = 'h'}, {.btnAcc = CUT, .name = "CUT", .pin = 9, .shortcut = 'i'}, {.btnAcc = HORN, .name = "HORN", .pin = 10, .shortcut = 'j'}, }; void initialize_buttons(){ const button_config_t btn_cfg = {0}; button_handle_t btns[sizeof(actions) / sizeof(actions[0])]; for (size_t i = 0; i < sizeof(actions) / sizeof(actions[0]); i++) { const button_gpio_config_t btn_gpio_cfg = { .gpio_num = actions[i].pin, .active_level = 0, }; ESP_RETURN_VOID_ON_ERROR(iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &btns[i]), TAG, ""); iot_button_register_cb(btns[i], BUTTON_PRESS_DOWN, NULL, button_event_cb, (void *) &actions[i]); iot_button_register_cb(btns[i], BUTTON_PRESS_UP, NULL, button_event_cb, (void *) &actions[i]); } } void button_event_cb(void *arg, void *data) { action *a = (action*)data; button_event_t event = iot_button_get_event(arg); ESP_LOGI(TAG, "%s pin:%d name:%s shortcut:%c", iot_button_get_event_str(event), (int)a->pin, (char*)a->name, (char)a->shortcut); }