From 881edf28077b526615796e3dee52d98ce9198501 Mon Sep 17 00:00:00 2001 From: Tido Klaassen Date: Mon, 27 Aug 2018 18:54:04 +0200 Subject: [PATCH] Return HTML pages on image deletion and seq reset --- main/gb_printer.c | 79 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 22 deletions(-) diff --git a/main/gb_printer.c b/main/gb_printer.c index b8ab8e3..1971d78 100644 --- a/main/gb_printer.c +++ b/main/gb_printer.c @@ -88,6 +88,15 @@ #define PR_PARAM_SIZE 4 #define PKT_RING_SIZE 4 +#define HTML_HEADER "" \ + "" \ + "" \ + "ESP32 GameBoy Printer" \ + "" \ + "" + #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) @@ -268,15 +277,11 @@ CgiStatus cgi_del_all(HttpdConnData *conn) goto err_out; } - httpdStartResponse(conn, 200); - httpdHeader(conn, "Content-Type", "text/json"); - httpdEndHeaders(conn); - dir = opendir(IMGDIR); if(dir == NULL){ ESP_LOGE(TAG, "[%s] opendir() failed", __func__); result = -1; - goto send_data; + goto send_reply; } while((ent = readdir(dir)) != NULL){ @@ -284,13 +289,32 @@ CgiStatus cgi_del_all(HttpdConnData *conn) result = unlink(buff); if(result != 0){ ESP_LOGE(TAG, "[%s] Unlink failed for %s", __func__, buff); - goto send_data; + goto send_reply; } } -send_data: - snprintf(buff, sizeof(buff), "%s", result == 0 ? "true" : "false"); - httpdSend(conn, buff, -1); +send_reply: + if(result == 0){ + httpdRedirect(conn, "/index.tpl"); + } else { + httpdStartResponse(conn, 500); + httpdHeader(conn, "Content-Type", "text/html"); + httpdEndHeaders(conn); + + httpdSend(conn, HTML_HEADER, -1); + httpdSend(conn, "" + "

ESP32 Gameboy Printer

" + "

" + "Error while deleting file ", -1); + httpdSend(conn, buff, -1); + httpdSend(conn, ".
" + "Go back" + "
" + "Retry" + "

" + "" + "", -1); + } err_out: if(dir != NULL){ @@ -302,11 +326,9 @@ err_out: CgiStatus cgi_reset_seq(HttpdConnData *conn) { - char buff[128]; nvs_handle handle; int result; - buff[0] = '\0'; result = 0; if(conn->isConnectionClosed){ @@ -323,15 +345,28 @@ CgiStatus cgi_reset_seq(HttpdConnData *conn) nvs_close(handle); send_reply: - httpdStartResponse(conn, 200); - httpdHeader(conn, "Content-Type", "text/json"); - httpdEndHeaders(conn); + if(result == ESP_OK){ + httpdRedirect(conn, "/index.tpl"); + } else { + httpdStartResponse(conn, 500); + httpdHeader(conn, "Content-Type", "text/html"); + httpdEndHeaders(conn); - snprintf(buff, sizeof(buff), "%s", result == ESP_OK ? "true" : "false"); - httpdSend(conn, buff, -1); + httpdSend(conn, HTML_HEADER, -1); + httpdSend(conn, "" + "

ESP32 Gameboy Printer

" + "

" + "Error resetting image sequence." + "
" + "Go back" + "
" + "Retry" + "

" + "" + "", -1); + } err_out: - return HTTPD_CGI_DONE; } @@ -355,8 +390,8 @@ esp_err_t http_srv_init(void) HTTPD_FLAG_NONE); if(status != InitializationSuccess){ - result = ESP_FAIL; - goto err_out; + result = ESP_FAIL; + goto err_out; } httpdFreertosStart(&httpd_instance); @@ -386,7 +421,7 @@ void draw_tile(uint8_t *data, uint8_t *image) esp_err_t draw_bitmap(struct pr_data *data) { size_t w, h, x, y; - uint8_t *tile_data, *tile_image; + uint8_t *tile_data, *tile_dest; unsigned int tile; esp_err_t result; @@ -408,8 +443,8 @@ esp_err_t draw_bitmap(struct pr_data *data) y = 8 * (tile / 20); tile_data = &(data->data[tile * 16]); - tile_image = &(render_buff[(x * 2) + (y * 40)]); - draw_tile(tile_data, tile_image); + tile_dest = &(render_buff[(x * 2) + (y * 40)]); + draw_tile(tile_data, tile_dest); } memmove(data->data, render_buff, data->data_len);