From 186fd85422f99dfb59d89ae9338dc50627f7c3e5 Mon Sep 17 00:00:00 2001 From: Luke Hoersten Date: Sun, 14 Jun 2026 18:30:44 -0500 Subject: Tidy: delete dead code, inline single-use helpers, fix double-lock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Code-review pass after the M5 colour fix. -126 net lines (-215 / +89). Dead code removed: - display_fill, display_test_pattern — M3 bring-up self-tests, no callers - net_eth_is_up — no callers - nvs_config_reset — no callers (factory-reset gesture was removed earlier; NVS wipe now goes through `idf.py erase-flash`) Helpers inlined or collapsed (each had one call site): - state_name / orientation_name — ternaries at the cJSON site - fmt_bytes / fmt_uptime — inline scope in local_screens_show_info - expected_dims (http_api) + effective_dims (local_screens) — merged into viewport_state_effective_dims() in viewport_state, one canonical source state_machine cleanup: - arm_idle_timer_unlocked / disarm_idle_timer collapsed to one arm_idle_timer(ms) that takes the snapshotted timeout (ms==0 disables). Removes the misleading name and the second viewport_state lock acquisition per painted frame. - state_machine_set + state_machine_frame_painted now snapshot state + idle_ms under one lock. mdns_service cleanup: - snapshot_state / apply_hostname / apply_txt collapsed into a single apply_state(include_hostname) helper; mdns_service_start grabs hostname once for hostname_set + log instead of going under the lock three times. http_api cleanup: - POST /config brightness-changed path uses the local `bright` it already validated instead of re-locking + re-reading st->brightness. - Trimmed verbose bring-up comments (stack-size justification, M5 saga, DSI shadow struct) to one line each — kept the load-bearing facts. Smoke-tested on hardware: boot clean, display + JPEG + touch all up ([DJT]), info screen renders, no crashes. --- main/http_api.c | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) (limited to 'main/http_api.c') diff --git a/main/http_api.c b/main/http_api.c index dce57bb..028d3a7 100644 --- a/main/http_api.c +++ b/main/http_api.c @@ -24,16 +24,6 @@ static const char *TAG = "http_api"; #define MAX_BODY_BYTES 2048 #define MIN_IDLE_TIMEOUT 5000 -static const char *state_name(viewport_run_state_t s) -{ - return (s == VIEWPORT_STATE_AWAKE) ? "awake" : "asleep"; -} - -static const char *orientation_name(viewport_orientation_t o) -{ - return (o == VIEWPORT_ORIENTATION_LANDSCAPE) ? "landscape" : "portrait"; -} - // ============================================================================ // GET /state // ============================================================================ @@ -54,7 +44,8 @@ static esp_err_t state_get_handler(httpd_req_t *req) : cJSON_CreateNull()); cJSON_AddStringToObject(root, "version", VIEWPORT_VERSION); cJSON_AddBoolToObject (root, "configured", st->configured); - cJSON_AddStringToObject(root, "state", state_name(st->state)); + cJSON_AddStringToObject(root, "state", + (st->state == VIEWPORT_STATE_AWAKE) ? "awake" : "asleep"); cJSON_AddNumberToObject(root, "uptime_ms", (double)up_ms); cJSON_AddItemToObject (root, "last_frame_ms_ago", last_age_ms < 0 ? cJSON_CreateNull() @@ -97,7 +88,8 @@ static esp_err_t config_get_handler(httpd_req_t *req) st->scrypted_url[0] ? cJSON_CreateString(st->scrypted_url) : cJSON_CreateNull()); cJSON_AddNumberToObject(root, "idle_timeout_ms", (double)st->idle_timeout_ms); - cJSON_AddStringToObject(root, "orientation", orientation_name(st->orientation)); + cJSON_AddStringToObject(root, "orientation", + (st->orientation == VIEWPORT_ORIENTATION_LANDSCAPE) ? "landscape" : "portrait"); cJSON_AddNumberToObject(root, "brightness", (double)st->brightness); viewport_state_unlock(); @@ -254,10 +246,7 @@ static esp_err_t config_post_handler(httpd_req_t *req) } if (brightness_changed && display_is_up()) { - viewport_state_lock(); - uint8_t b = viewport_state_get()->brightness; - viewport_state_unlock(); - display_set_brightness(b); + display_set_brightness(bright); } if (name_or_orient_changed) { mdns_service_refresh(); @@ -317,15 +306,6 @@ static esp_err_t respond_status(httpd_req_t *req, const char *status, const char return httpd_resp_send(req, body, body ? HTTPD_RESP_USE_STRLEN : 0); } -static void expected_dims(uint16_t *w, uint16_t *h) -{ - viewport_state_lock(); - bool portrait = (viewport_state_get()->orientation == VIEWPORT_ORIENTATION_PORTRAIT); - viewport_state_unlock(); - if (portrait) { *w = 480; *h = 800; } - else { *w = 800; *h = 480; } -} - static esp_err_t frame_post_handler(httpd_req_t *req) { // Content-Type must be image/jpeg. @@ -383,7 +363,7 @@ static esp_err_t frame_post_handler(httpd_req_t *req) } uint16_t want_w, want_h; - expected_dims(&want_w, &want_h); + viewport_state_effective_dims(&want_w, &want_h); if (w != want_w || h != want_h) { viewport_state_lock(); viewport_state_get()->decode_errors++; @@ -439,11 +419,7 @@ esp_err_t http_api_start(void) cfg.server_port = 80; cfg.max_uri_handlers = 8; cfg.lru_purge_enable = true; - // Default 4 KiB is tight: /config alone has ~2.4 KiB of stack locals - // (2 KiB body buffer + 256 B scrypted URL + 64 B name + cJSON frames) - // and POST /frame pushes a JPEG header onto the stack too. 8 KiB gives - // both handlers comfortable headroom without doubling RAM usage. - cfg.stack_size = 8192; + cfg.stack_size = 8192; // POST /config alone has ~2.4 KiB of stack locals httpd_handle_t server = NULL; ESP_RETURN_ON_ERROR(httpd_start(&server, &cfg), TAG, "httpd_start"); -- cgit v1.2.3