Handle lua scripts via POST /draw
This commit is contained in:
parent
504fe50cc7
commit
340dddc792
2 changed files with 44 additions and 26 deletions
|
|
@ -20,6 +20,10 @@
|
||||||
#define WIFI_SCAN_LIST_SIZE 10
|
#define WIFI_SCAN_LIST_SIZE 10
|
||||||
#define LUA_FILE_PATH "/assets"
|
#define LUA_FILE_PATH "/assets"
|
||||||
|
|
||||||
|
#ifndef MIN
|
||||||
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||||
|
#endif
|
||||||
|
|
||||||
static const char *TAG = "inkpot";
|
static const char *TAG = "inkpot";
|
||||||
|
|
||||||
// Function to log memory usage with the message at the end
|
// 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
|
// Function to run an embedded Lua script
|
||||||
void run_lua_string(const char *lua_script, const char *test_name) {
|
void run_lua_string(const char *lua_script, const char *script_name) {
|
||||||
ESP_LOGI(TAG, "Starting Lua test: %s", test_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();
|
lua_State *L = luaL_newstate();
|
||||||
if (L == NULL) {
|
if (L == NULL) {
|
||||||
|
|
@ -128,7 +133,7 @@ void run_lua_string(const char *lua_script, const char *test_name) {
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
log_memory_usage("After lua_close");
|
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
|
// 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) {
|
esp_err_t http_draw(httpd_req_t* req) {
|
||||||
// READING STREAM
|
// READING STREAM
|
||||||
int req_size = req->content_len;
|
int max_post_size = 4 * 1024;
|
||||||
char* content = (char*)heap_caps_malloc(req_size, MALLOC_CAP_SPIRAM);
|
char* buf = (char*)heap_caps_malloc(max_post_size, MALLOC_CAP_SPIRAM);
|
||||||
if (content == NULL) {
|
for (int i = 0; i < max_post_size; i++) {
|
||||||
|
buf[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buf == NULL) {
|
||||||
char msg[50];
|
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);
|
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, msg);
|
||||||
return ESP_ERR_INVALID_ARG;
|
return ESP_ERR_INVALID_ARG;
|
||||||
}
|
}
|
||||||
int current_pos = 0;
|
|
||||||
int amount_recieved;
|
// READING STREAM
|
||||||
while ((amount_recieved = httpd_req_recv(req, (content + current_pos), req_size)) > 0) {
|
int ret, remaining = req->content_len;
|
||||||
ESP_LOGI(__FUNCTION__, "Read %d bytes\n", amount_recieved);
|
|
||||||
current_pos += amount_recieved;
|
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
|
// TODO: Error handling
|
||||||
run_lua_string(content, "E-Paper Script via HTTP");
|
run_lua_string(buf, "E-Paper Script via HTTP");
|
||||||
heap_caps_free(content);
|
heap_caps_free(buf);
|
||||||
|
|
||||||
// Done reading
|
// Done reading
|
||||||
char response[100];
|
char response[100];
|
||||||
|
|
@ -259,6 +278,7 @@ void app_main(void) {
|
||||||
}
|
}
|
||||||
ESP_ERROR_CHECK(ret);
|
ESP_ERROR_CHECK(ret);
|
||||||
|
|
||||||
|
// FIXME: Wifi server causes some crash, failing to yield to watchdog in time
|
||||||
// Set up HTTP server
|
// Set up HTTP server
|
||||||
httpd_handle_t server = get_server();
|
httpd_handle_t server = get_server();
|
||||||
if (server != NULL) {
|
if (server != NULL) {
|
||||||
|
|
@ -268,7 +288,7 @@ void app_main(void) {
|
||||||
// Run script in assets/epaper.lua
|
// Run script in assets/epaper.lua
|
||||||
run_lua_file("epaper.lua", "E-Paper Startup Script");
|
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
|
// Prevent the task from ending
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
|
||||||
|
|
@ -20,8 +20,6 @@ static void wifi_init_sta(void) {
|
||||||
|
|
||||||
// Configure Wi-Fi connection
|
// Configure Wi-Fi connection
|
||||||
wifi_config_t wifi_config = {
|
wifi_config_t wifi_config = {
|
||||||
// TODO: Allow setting SSID at boot or build time
|
|
||||||
// For some reason I could not get https://cmake.org/cmake/help/latest/command/add_compile_definitions.html#command:add_compile_definitions to work
|
|
||||||
.sta = {
|
.sta = {
|
||||||
.ssid = WIFI_SSID,
|
.ssid = WIFI_SSID,
|
||||||
.password = WIFI_PASS,
|
.password = WIFI_PASS,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue