Skip to content
Merged
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
31 changes: 19 additions & 12 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@ if(NOT DEFINED VAL_REMOTE_URL)
set(VAL_REMOTE_URL "XplaceholderREMOTEURL___________________________________________________________________________________________________________")
endif()

idf_component_register(SRCS "main.c"
"display.cpp"
"flash.c"
"gfx.c"
"ota.c"
"remote.c"
"wifi.c"
"ap.c"
"nvs_settings.c"
"dns_wrapper.c"
"syslog.c"
"sntp.c"
# Conditionally include touch_control.c only for Tidbyt Gen2
set(SRCS "main.c"
"display.cpp"
"flash.c"
"gfx.c"
"ota.c"
"remote.c"
"wifi.c"
"ap.c"
"nvs_settings.c"
"dns_wrapper.c"
"syslog.c"
"sntp.c")

if(CONFIG_BOARD_TIDBYT_GEN2)
list(APPEND SRCS "touch_control.c")
endif()

idf_component_register(SRCS ${SRCS}
INCLUDE_DIRS ".")

idf_build_set_property(LINK_OPTIONS "-Wl,--wrap=esp_getaddrinfo" APPEND)
Expand Down
98 changes: 97 additions & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include "sdkconfig.h"
#include "sntp.h"
#include "syslog.h"
#ifdef CONFIG_BOARD_TIDBYT_GEN2
#include "touch_control.h"
#endif
#include "version.h"
#include "wifi.h"

Expand Down Expand Up @@ -54,6 +57,13 @@ static bool button_boot = false;
static bool first_ws_image_received = false;
static bool config_received = false;

#ifdef CONFIG_BOARD_TIDBYT_GEN2
// Touch control state
static bool display_power_on = true;
static uint8_t saved_brightness = 30;
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.

medium

The magic number 30 is used to initialize saved_brightness. To improve maintainability and consistency, please use the DISPLAY_DEFAULT_BRIGHTNESS constant, which is defined in display.h and already included.

static uint8_t saved_brightness = DISPLAY_DEFAULT_BRIGHTNESS;

static void handle_touch_event(touch_event_t event);
#endif

static void config_saved_callback(void) {
config_received = true;
ESP_LOGI(TAG, "Configuration saved - signaling main task");
Expand Down Expand Up @@ -201,6 +211,11 @@ static void websocket_event_handler(void* handler_args, esp_event_base_t base,
brightness_value = DISPLAY_MAX_BRIGHTNESS;
display_set_brightness((uint8_t)brightness_value);
ESP_LOGI(TAG, "Updated brightness to %d", brightness_value);
#ifdef CONFIG_BOARD_TIDBYT_GEN2
// Sync touch control state - server brightness command means display is on
display_power_on = true;
saved_brightness = (uint8_t)brightness_value;
#endif
}

// Check for "ota_url"
Expand Down Expand Up @@ -496,6 +511,19 @@ void app_main(void) {
}
esp_register_shutdown_handler(&display_shutdown);

#ifdef CONFIG_BOARD_TIDBYT_GEN2
// Initialize touch controls (GPIO33 on Tidbyt Gen2)
ESP_LOGI(TAG, "Initializing touch control...");
esp_err_t touch_ret = touch_control_init();
if (touch_ret == ESP_OK) {
ESP_LOGI(TAG, "Touch control ready on GPIO33");
touch_control_debug_all_pads();
} else {
ESP_LOGW(TAG, "Touch control init failed: %s (continuing without touch)",
esp_err_to_name(touch_ret));
}
#endif

// Start the AP web server now that display memory is allocated
if (nvs_get_ap_mode()) {
ESP_LOGI(TAG, "Starting AP Web Server...");
Expand Down Expand Up @@ -692,7 +720,20 @@ void app_main(void) {
}

wifi_health_check();
vTaskDelay(pdMS_TO_TICKS(5000)); // check every 5s

#ifdef CONFIG_BOARD_TIDBYT_GEN2
// Poll touch frequently for 5 seconds (50ms intervals = 100 checks)
// This allows proper gesture detection while keeping health checks at 5s
for (int i = 0; i < 100; i++) {
touch_event_t touch_event = touch_control_check();
if (touch_event != TOUCH_EVENT_NONE) {
handle_touch_event(touch_event);
}
vTaskDelay(pdMS_TO_TICKS(50)); // 50ms = responsive touch
}
#else
vTaskDelay(pdMS_TO_TICKS(5000)); // 5 second health check interval
#endif
}
} else {
// normal http
Expand Down Expand Up @@ -778,7 +819,62 @@ void app_main(void) {
isAnimating = 1;
}
}

#ifdef CONFIG_BOARD_TIDBYT_GEN2
// Check for touch events
touch_event_t touch_event = touch_control_check();
if (touch_event != TOUCH_EVENT_NONE) {
handle_touch_event(touch_event);
}
Comment on lines +824 to +828
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.

high

The current implementation only checks for touch events once per image cycle in the HTTP loop. This will lead to an unresponsive UI, as the loop can be blocked for app_dwell_secs (plus fetch time). To fix this, touch events should be polled frequently, similar to the change made in the websocket loop. Consider refactoring the main HTTP loop to replace long blocking waits with shorter, repeated delays that include a touch check.

#endif

wifi_health_check();
}
}
}

#ifdef CONFIG_BOARD_TIDBYT_GEN2
/**
* Handle touch events from the single touch pad
* TAP = skip to next app
* DOUBLE_TAP = (reserved for future use)
* HOLD = toggle display power on/off
*/
static void handle_touch_event(touch_event_t event) {
ESP_LOGI(TAG, "Touch event: %s", touch_event_to_string(event));

switch (event) {
case TOUCH_EVENT_TAP:
if (display_power_on) {
ESP_LOGI(TAG, "TAP - skip to next app");
isAnimating = -1;
} else {
ESP_LOGI(TAG, "TAP ignored - display is off (hold to turn on)");
}
break;

case TOUCH_EVENT_DOUBLE_TAP:
// Reserved for future use
ESP_LOGI(TAG, "DOUBLE TAP - no action assigned");
break;

case TOUCH_EVENT_HOLD:
display_power_on = !display_power_on;

if (display_power_on) {
ESP_LOGI(TAG, "HOLD - Display ON");
display_set_brightness(saved_brightness);
isAnimating = 1;
} else {
ESP_LOGI(TAG, "HOLD - Display OFF");
saved_brightness = 30;
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.

high

When turning the display off, the current brightness level is not saved. Instead, saved_brightness is hardcoded to 30. This means any brightness level set by the user will be lost when the display is toggled off and on again. The current brightness should be read and stored in saved_brightness before setting the display brightness to 0. This may require a new function like display_get_brightness().

display_set_brightness(0);
isAnimating = 0;
}
break;

default:
break;
}
}
#endif
Loading