Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ SRCS
"system.c"
"work_queue.c"
"./NvsManager/src/nvs_device.c"
"lv_font_portfolio-6x8.c"
"./http_server/http_server.c"
"./http_server/handler_ota_github.c"
"./self_test/self_test.c"
Expand Down Expand Up @@ -68,9 +67,6 @@ EMBED_FILES
"http_server/ap_page.html"
)

idf_build_set_property(COMPILE_OPTIONS "-DLV_CONF_INCLUDE_SIMPLE=1" APPEND)
idf_build_set_property(COMPILE_OPTIONS "-DLV_CONF_PATH=\"${CMAKE_SOURCE_DIR}/main/lv_conf.h\"" APPEND)

set(WEB_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/http_server/forge-os")

if("$ENV{GITHUB_ACTIONS}" STREQUAL "true")
Expand Down
6 changes: 0 additions & 6 deletions main/HMI/inc/display.h

This file was deleted.

2 changes: 2 additions & 0 deletions main/HMI/inc/input.h
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_ */
23 changes: 0 additions & 23 deletions main/HMI/inc/screen.h

This file was deleted.

134 changes: 0 additions & 134 deletions main/HMI/src/display.c

This file was deleted.

99 changes: 61 additions & 38 deletions main/HMI/src/input.c
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");
Comment on lines +95 to +99

Copy link
Copy Markdown
Contributor

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.

xTaskCreate succeeds before gpio_isr_handler_add. If handler registration fails, input_init returns an error but leaves button_task blocked forever with its allocated stack. Delete the task and restore initialization state before returning the error.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@main/HMI/src/input.c` around lines 95 - 99, Update input_init around
button_task_handle creation and gpio_isr_handler_add so a failed ISR
registration deletes the created task, clears button_task_handle, and restores
the initialization state before returning the error; preserve the existing
success path.


return ESP_OK;
}
Loading
Loading