diff --git a/README.md b/README.md index 819705f..26cf621 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ You need to give it a 2.4Ghz Wifi SSID and password by adjusting `main/settings. You can send scripts like this: ``` bash -cat assets/post_test.lua | curl -X POST --data-binary @- 10.0.0.100/draw +cat assets/scripts/post_test.lua | curl -X POST --data-binary @- 10.0.0.100/draw ``` The IP address of the display is logged to its serial output and can be read via `idf.py monitor` (see below). @@ -41,7 +41,9 @@ You can stop the monitor by pressing `Ctrl + ]`. - `main/` - `inkpot.c`: Main program containing embedded Lua script and file-based Lua execution. - `paper.c`: Defines the lua bindings to epdiy that can be used for drawing on the screen. -- `assets/`: Directory containing all Lua scripts; some are older example scripts that are not executed - - `boot.lua` contains the sketch that runs at startup - - `post_test.lua` contains another sketch +- `assets/` + - `http/`: Static assets to be served by the HTTP server + - `scripts/`: Directory containing all Lua scripts; some are older example scripts that are not executed + - `boot.lua` contains the sketch that runs at startup + - `post_test.lua` contains another sketch diff --git a/assets/http/index.html b/assets/http/index.html new file mode 100644 index 0000000..0e54714 --- /dev/null +++ b/assets/http/index.html @@ -0,0 +1,161 @@ + + + + + + Inkpot + + + + + + + +

inkblot

+

Welcome to your inkblot. You can use the text field below to enter and send Lua scripts that will change what is displayed on your screen. At the bottom of the screen you can get a short list of functions available for drawing.

+
+ + +
+

How to draw

+

Lua is a very simple language. Here is a brief overview of the syntax: Learn Lua in Y Minutes. You can find a description of the drawing library below.

+

The screen is monochrome and knows 16 shades. Whenever you see a color below, it can be set to values from 0 (black) to 255 (white). You can also use hexadecimal notation (from 0x00 to 0xFF) which means the same thing.

+

The coordinate system starts at (0,0) in the top left corner.

+
+
local paper = require "paper"
+
Loads the library to run drawing commands.
+ +
paper.init()
+
Initializes the screen. Needs to be called at least once.
+ +
paper.clear()
+
Clears the screen. If you don't call this, you will draw on top of your previous drawing.
+ +
paper.update()
+
Sends your drawing to the screen. Needs to be called after a sequence of drawing commands to display them..
+ +
paper.set_orientation(orientation)
+
Set the screen orientation. Allowed values for orientation are "portrait", "landscape", "inverse_portrait" and "inverse_landscape"
+ +
paper.get_orientation()
+
Get current screen orientation. Returned values are "portrait", "landscape", "inverse_portrait" and "inverse_landscape"
+ +
paper.get_width()
+
Get current screen width, according to orientation.
+ +
paper.get_height()
+
Get current screen height, according to orientation.
+ +
paper.draw_pixel(x, y, color)
+
Draw a single pixel at (x,y) in the given color.
+ +
paper.draw_line(x1, y1, x2, y2, color)
+
Draw a line from (x1,y1) to (x2,y2) in the given color.
+ +
paper.draw_hline(x, y, length, color)
+
Draw a horizontal line from (x,y) with the given length in the given color.
+ +
paper.draw_vline(x, y, length, color)
+
Draw a vertical line from (x,y) with the given length in the given color.
+ +
paper.draw_circle(x, y, radius, color) / paper.fill_circle(x, y, radius, color)
+
Draw an outline around a circle at (x,y) with the given radius in the given color. fill_circle draws the same circle with a solid filling.
+ +
paper.draw_rect(x1, y1, x2, y2, color) / paper.fill_rect(x1, y1, x2, y2, color)
+
Draw an outline around a rectangle with the upper left corner at (x1,y1) and lower right corner at (x2,y2) in the given color. fill_rect draws the same rectangle with a solid filling.
+ +
paper.draw_triangle(x1, y1, x2, y2, x3, y3, color) / paper.fill_triangle(x1, y1, x2, y2, x3, y3, color)
+
Draw an outline around a triangle with corners (x1,y1), (x2,y2) and (x3,y3) in the given color. fill_triangle draws the same triangle with a solid filling.
+
+

Have fun!

+ + diff --git a/assets/boot.lua b/assets/scripts/boot.lua similarity index 100% rename from assets/boot.lua rename to assets/scripts/boot.lua diff --git a/assets/post_test.lua b/assets/scripts/post_test.lua similarity index 100% rename from assets/post_test.lua rename to assets/scripts/post_test.lua diff --git a/main/inkpot.c b/main/inkpot.c index 57775c3..3c70111 100644 --- a/main/inkpot.c +++ b/main/inkpot.c @@ -18,12 +18,20 @@ #include "server.h" #define WIFI_SCAN_LIST_SIZE 10 -#define LUA_FILE_PATH "/assets" +#define LFS_PATH "/assets" #ifndef MIN #define MIN(a, b) (((a) < (b)) ? (a) : (b)) #endif +/* Scratch buffer size for sending files to HTTP clients */ +#define SCRATCH_BUFSIZE 8192 + +struct file_server_data { + /* Scratch buffer for temporary storage during file transfer */ + char scratch[SCRATCH_BUFSIZE]; +}; + static const char *TAG = "inkpot"; // Function to log memory usage with the message at the end @@ -40,7 +48,7 @@ void init_filesystem() { ESP_LOGI(TAG, "Initializing File System"); esp_vfs_littlefs_conf_t conf = { - .base_path = LUA_FILE_PATH, + .base_path = LFS_PATH, .partition_label = "assets", .format_if_mount_failed = false, .dont_mount = false, @@ -50,7 +58,7 @@ void init_filesystem() { if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to mount or format filesystem"); } else { - ESP_LOGI(TAG, "Filesystem mounted at %s", LUA_FILE_PATH); + ESP_LOGI(TAG, "Filesystem mounted at %s", LFS_PATH); } } @@ -84,7 +92,7 @@ void run_lua_file(const char *file_name, const char *test_name) { // Construct the full file path char full_path[128]; - snprintf(full_path, sizeof(full_path), LUA_FILE_PATH"/%s", file_name); + snprintf(full_path, sizeof(full_path), LFS_PATH"/%s", file_name); if (luaL_dofile(L, full_path) == LUA_OK) { lua_pop(L, lua_gettop(L)); @@ -184,16 +192,37 @@ void scan_wifi_networks(void) { // HTTP Server -#define WRITE_HEADER(req, buffer, name, format, src) \ - sprintf(buffer, format, src); \ - ESP_ERROR_CHECK(httpd_resp_set_hdr(req, name, buffer)); - static esp_err_t http_index(httpd_req_t* req) { - // TODO: Serve HTML file with form POSTing to `/draw` - const char* response = "Hello world!\n"; - httpd_resp_set_type(req, "text/plain"); - httpd_resp_set_status(req, "200"); - httpd_resp_send(req, response, HTTPD_RESP_USE_STRLEN); + char index_path[128]; + strcpy(index_path, LFS_PATH); + strcat(index_path, "/http/index.html"); + + FILE *fd = fopen(index_path, "r"); + char *chunk = ((struct file_server_data *)req->user_ctx)->scratch; + size_t chunksize; + do { + /* Read file in chunks into the scratch buffer */ + chunksize = fread(chunk, 1, SCRATCH_BUFSIZE, fd); + + if (chunksize > 0) { + /* Send the buffer contents as HTTP response chunk */ + if (httpd_resp_send_chunk(req, chunk, chunksize) != ESP_OK) { + fclose(fd); + ESP_LOGE(TAG, "File sending failed!"); + /* Abort sending file */ + httpd_resp_sendstr_chunk(req, NULL); + /* Respond with 500 Internal Server Error */ + httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to send file"); + return ESP_FAIL; + } + } + + /* Keep looping till the whole file is sent */ + } while (chunksize != 0); + fclose(fd); + httpd_resp_set_hdr(req, "Connection", "close"); + httpd_resp_send_chunk(req, NULL, 0); + ESP_LOGI(TAG, "Sent index.html"); return ESP_OK; } @@ -252,17 +281,33 @@ esp_err_t http_draw(httpd_req_t* req) { return ESP_OK; } -void register_http_routes(httpd_handle_t server) { +esp_err_t register_http_routes(httpd_handle_t server) { + static struct file_server_data *server_data = NULL; + + if (server_data) { + ESP_LOGE(TAG, "File server already started"); + return ESP_ERR_INVALID_STATE; + } + + // Allocate memory for server data + server_data = calloc(1, sizeof(struct file_server_data)); + if (!server_data) { + ESP_LOGE(TAG, "Failed to allocate memory for server data"); + return ESP_ERR_NO_MEM; + } + { httpd_uri_t uri - = { .uri = "/", .method = HTTP_GET, .handler = http_index, .user_ctx = NULL }; + = { .uri = "/", .method = HTTP_GET, .handler = http_index, .user_ctx = server_data }; httpd_register_uri_handler(server, &uri); } { httpd_uri_t uri - = { .uri = "/draw", .method = HTTP_POST, .handler = http_draw, .user_ctx = NULL }; + = { .uri = "/draw", .method = HTTP_POST, .handler = http_draw, .user_ctx = server_data }; httpd_register_uri_handler(server, &uri); } + + return ESP_OK; } // init @@ -279,17 +324,15 @@ void app_main(void) { } ESP_ERROR_CHECK(ret); - // FIXME: Wifi server causes some crash, failing to yield to watchdog in time - // Set up HTTP server httpd_handle_t server = get_server(); if (server != NULL) { register_http_routes(server); } - // Run script in assets/boot.lua; this is executed as a FreeRTOS task + // Run script in assets/scripts/boot.lua; this is executed as a FreeRTOS task // that may not be interrupted void runLuaFile (void* arg) { - run_lua_file("boot.lua", "E-Paper Startup Script"); + run_lua_file("scripts/boot.lua", "E-Paper Startup Script"); vTaskDelete(NULL); } xTaskCreate(runLuaFile, "run_lua_file", 4096, NULL, tskIDLE_PRIORITY, NULL);