-
Notifications
You must be signed in to change notification settings - Fork 3
cleanup #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WantClue
wants to merge
1
commit into
main
Choose a base branch
from
cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
cleanup #37
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| #ifndef INPUT_H_ | ||
| #define INPUT_H_ | ||
|
|
||
| #include "esp_err.h" | ||
|
|
||
| esp_err_t input_init(void (*button_short_clicked_cb)(void), void (*button_long_pressed_cb)(void)); | ||
|
|
||
| #endif /* INPUT_H_ */ |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,79 +1,102 @@ | ||
| #include "freertos/FreeRTOS.h" | ||
| #include "freertos/task.h" | ||
| #include "esp_log.h" | ||
| #include "esp_err.h" | ||
| #include "esp_check.h" | ||
| #include "lvgl.h" | ||
| #include "esp_lvgl_port.h" | ||
| #include "esp_timer.h" | ||
| #include "driver/gpio.h" | ||
|
|
||
| #define GPIO_BUTTON_BOOT CONFIG_GPIO_BUTTON_BOOT | ||
|
|
||
| #define LONG_PRESS_DURATION_MS 2000 | ||
| #define DEBOUNCE_MS 30 | ||
| #define POLL_INTERVAL_MS 20 | ||
|
|
||
| static const char * TAG = "input"; | ||
|
|
||
| static lv_indev_state_t button_state = LV_INDEV_STATE_RELEASED; | ||
| static TaskHandle_t button_task_handle = NULL; | ||
|
|
||
| static void (*button_short_clicked_fn)(void) = NULL; | ||
| static void (*button_long_pressed_fn)(void) = NULL; | ||
|
|
||
| static void button_read(lv_indev_t *indev, lv_indev_data_t *data) | ||
| static bool button_is_pressed(void) | ||
| { | ||
| data->key = LV_KEY_ENTER; | ||
| data->state = button_state; | ||
| return gpio_get_level(GPIO_BUTTON_BOOT) == 0; // LOW when pressed | ||
| } | ||
|
|
||
| static void IRAM_ATTR button_isr_handler(void *arg) | ||
| static void IRAM_ATTR button_isr_handler(void *arg) | ||
| { | ||
| bool pressed = gpio_get_level(GPIO_BUTTON_BOOT) == 0; // LOW when pressed | ||
| button_state = pressed ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED; | ||
| BaseType_t higher_prio_task_woken = pdFALSE; | ||
| vTaskNotifyGiveFromISR(button_task_handle, &higher_prio_task_woken); | ||
| portYIELD_FROM_ISR(higher_prio_task_woken); | ||
| } | ||
|
|
||
| static void button_short_clicked_event_cb(lv_event_t *e) | ||
| static void button_task(void *pvParameters) | ||
| { | ||
| ESP_LOGI(TAG, "Short button click detected"); | ||
| button_short_clicked_fn(); | ||
| } | ||
| while (1) { | ||
| // Sleep until an edge wakes us up | ||
| ulTaskNotifyTake(pdTRUE, portMAX_DELAY); | ||
|
|
||
| static void button_long_pressed_event_cb(lv_event_t *e) | ||
| { | ||
| ESP_LOGI(TAG, "Long button press detected"); | ||
| button_long_pressed_fn(); | ||
| vTaskDelay(pdMS_TO_TICKS(DEBOUNCE_MS)); | ||
| if (!button_is_pressed()) { | ||
| continue; // bounce or a release we already handled | ||
| } | ||
|
|
||
| // Held down: fire the long press as soon as the threshold passes, like the | ||
| // ISR-free equivalent of LV_EVENT_LONG_PRESSED did | ||
| bool long_press_fired = false; | ||
| int64_t press_start = esp_timer_get_time(); | ||
|
|
||
| while (button_is_pressed()) { | ||
| if (!long_press_fired && (esp_timer_get_time() - press_start) >= LONG_PRESS_DURATION_MS * 1000LL) { | ||
| long_press_fired = true; | ||
| if (button_long_pressed_fn != NULL) { | ||
| ESP_LOGI(TAG, "Long button press detected"); | ||
| button_long_pressed_fn(); | ||
| } | ||
| } | ||
| vTaskDelay(pdMS_TO_TICKS(POLL_INTERVAL_MS)); | ||
| } | ||
|
|
||
| if (!long_press_fired && button_short_clicked_fn != NULL) { | ||
| ESP_LOGI(TAG, "Short button click detected"); | ||
| button_short_clicked_fn(); | ||
| } | ||
|
|
||
| // Drop the edges the release bounce queued up while we were polling | ||
| vTaskDelay(pdMS_TO_TICKS(DEBOUNCE_MS)); | ||
| ulTaskNotifyTake(pdTRUE, 0); | ||
| } | ||
| } | ||
|
|
||
| esp_err_t input_init(void (*button_short_clicked_cb)(void), void (*button_long_pressed_cb)(void)) | ||
| { | ||
| ESP_LOGI(TAG, "Install button driver"); | ||
|
|
||
| button_short_clicked_fn = button_short_clicked_cb; | ||
| button_long_pressed_fn = button_long_pressed_cb; | ||
|
|
||
| // Button handling | ||
| gpio_config_t io_conf = { | ||
| .pin_bit_mask = (1ULL << GPIO_BUTTON_BOOT), | ||
| .mode = GPIO_MODE_INPUT, | ||
| .pull_up_en = GPIO_PULLUP_ENABLE, | ||
| .intr_type = GPIO_INTR_ANYEDGE | ||
| }; | ||
| gpio_config(&io_conf); | ||
|
|
||
| // Install ISR service and hook the interrupt handler | ||
| ESP_RETURN_ON_ERROR(gpio_isr_handler_add(GPIO_BUTTON_BOOT, button_isr_handler, NULL), TAG, "Error adding ISR handler"); | ||
| ESP_RETURN_ON_ERROR(gpio_config(&io_conf), TAG, "Button GPIO config failed"); | ||
|
|
||
| lv_group_t * group = lv_group_create(); | ||
| lv_group_set_default(group); | ||
| lv_group_add_obj(group, lv_obj_create(NULL)); // dummy screen for event handling, in case no display is attached | ||
|
|
||
| // Create input device | ||
| lv_indev_t * indev = lv_indev_create(); | ||
| lv_indev_set_type(indev, LV_INDEV_TYPE_KEYPAD); | ||
| lv_indev_set_long_press_time(indev, LONG_PRESS_DURATION_MS); | ||
| lv_indev_set_read_cb(indev, button_read); | ||
| lv_indev_set_group(indev, group); | ||
| if (button_short_clicked_cb != NULL) { | ||
| button_short_clicked_fn = button_short_clicked_cb; | ||
| lv_indev_add_event_cb(indev, button_short_clicked_event_cb, LV_EVENT_SHORT_CLICKED, NULL); | ||
| } | ||
| if (button_long_pressed_cb != NULL) { | ||
| button_long_pressed_fn = button_long_pressed_cb; | ||
| lv_indev_add_event_cb(indev, button_long_pressed_event_cb, LV_EVENT_LONG_PRESSED, NULL); | ||
| // The ISR service is installed during peripheral init, but self test and normal | ||
| // boot get there by different paths, so tolerate it already being up | ||
| esp_err_t isr_service_err = gpio_install_isr_service(0); | ||
| if (isr_service_err != ESP_OK && isr_service_err != ESP_ERR_INVALID_STATE) { | ||
| ESP_RETURN_ON_ERROR(isr_service_err, TAG, "Error installing ISR service"); | ||
| } | ||
|
|
||
| // Task has to exist before the ISR can notify it | ||
| ESP_RETURN_ON_FALSE(xTaskCreate(button_task, "button", 3072, NULL, 5, &button_task_handle) == pdPASS, | ||
| ESP_ERR_NO_MEM, TAG, "Failed to create button task"); | ||
|
|
||
| ESP_RETURN_ON_ERROR(gpio_isr_handler_add(GPIO_BUTTON_BOOT, button_isr_handler, NULL), TAG, "Error adding ISR handler"); | ||
|
|
||
| return ESP_OK; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Roll back the task when ISR handler registration fails.
xTaskCreatesucceeds beforegpio_isr_handler_add. If handler registration fails,input_initreturns an error but leavesbutton_taskblocked forever with its allocated stack. Delete the task and restore initialization state before returning the error.🤖 Prompt for AI Agents