Handle lua scripts via POST /draw

This commit is contained in:
arne 2025-10-05 11:41:10 +02:00
commit 340dddc792
2 changed files with 44 additions and 26 deletions

View file

@ -20,6 +20,10 @@
#define WIFI_SCAN_LIST_SIZE 10
#define LUA_FILE_PATH "/assets"
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
static const char *TAG = "inkpot";
// Function to log memory usage with the message at the end
@ -97,10 +101,11 @@ void run_lua_file(const char *file_name, const char *test_name) {
}
// Function to run an embedded Lua script
void run_lua_string(const char *lua_script, const char *test_name) {
ESP_LOGI(TAG, "Starting Lua test: %s", test_name);
void run_lua_string(const char *lua_script, const char *script_name) {
ESP_LOGI(TAG, "Running lua script: %s", script_name);
ESP_LOGI(TAG, "%s", lua_script);
log_memory_usage("Start of test");
log_memory_usage("Start running script");
lua_State *L = luaL_newstate();
if (L == NULL) {
@ -128,7 +133,7 @@ void run_lua_string(const char *lua_script, const char *test_name) {
lua_close(L);
log_memory_usage("After lua_close");
ESP_LOGI(TAG, "End of Lua test: %s", test_name);
ESP_LOGI(TAG, "End of Lua script: %s", script_name);
}
// Function to scan Wi-Fi networks
@ -194,32 +199,46 @@ static esp_err_t http_index(httpd_req_t* req) {
esp_err_t http_draw(httpd_req_t* req) {
// READING STREAM
int req_size = req->content_len;
char* content = (char*)heap_caps_malloc(req_size, MALLOC_CAP_SPIRAM);
if (content == NULL) {
int max_post_size = 4 * 1024;
char* buf = (char*)heap_caps_malloc(max_post_size, MALLOC_CAP_SPIRAM);
for (int i = 0; i < max_post_size; i++) {
buf[i] = 0;
}
if (buf == NULL) {
char msg[50];
sprintf(msg, "Failed to allocate %d chars\n", req_size);
sprintf(msg, "Failed to allocate %d chars\n", max_post_size);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, msg);
return ESP_ERR_INVALID_ARG;
}
int current_pos = 0;
int amount_recieved;
while ((amount_recieved = httpd_req_recv(req, (content + current_pos), req_size)) > 0) {
ESP_LOGI(__FUNCTION__, "Read %d bytes\n", amount_recieved);
current_pos += amount_recieved;
// READING STREAM
int ret, remaining = req->content_len;
while (remaining > 0) {
/* Read the data for the request */
if ((ret = httpd_req_recv(req, buf,
MIN(remaining, max_post_size))) <= 0) {
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
/* Retry receiving if timeout occurred */
continue;
}
return ESP_FAIL;
}
/* Send back the same data */
httpd_resp_send_chunk(req, buf, ret);
remaining -= ret;
/* Log data received */
ESP_LOGI(TAG, "=========== RECEIVED DATA ==========");
ESP_LOGI(TAG, "%.*s", ret, buf);
ESP_LOGI(TAG, "====================================");
}
if (amount_recieved < 0) {
char msg[50];
heap_caps_free(content);
ESP_LOGE(msg, "Failed to read bytes. Error code %d\n", amount_recieved);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, msg);
return ESP_ERR_INVALID_ARG;
}
ESP_LOGI(__FUNCTION__, "Done reading %d bytes out of %d\n", current_pos, req_size);
// TODO: Error handling
run_lua_string(content, "E-Paper Script via HTTP");
heap_caps_free(content);
run_lua_string(buf, "E-Paper Script via HTTP");
heap_caps_free(buf);
// Done reading
char response[100];
@ -259,6 +278,7 @@ 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) {
@ -268,7 +288,7 @@ void app_main(void) {
// Run script in assets/epaper.lua
run_lua_file("epaper.lua", "E-Paper Startup Script");
ESP_LOGI(TAG, "End of testing application.");
ESP_LOGI(TAG, "End of application.");
// Prevent the task from ending
while (1) {