From 8a0a167adbf3d9b6f8b6f16aaf20ca39ad5549de Mon Sep 17 00:00:00 2001 From: jacqueline Date: Sun, 12 Nov 2023 19:14:09 +1100 Subject: Convert the main menu screen to lua lol --- lib/esp-idf-lua/examples/alloc/main/main.c | 89 ++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 lib/esp-idf-lua/examples/alloc/main/main.c (limited to 'lib/esp-idf-lua/examples/alloc/main/main.c') diff --git a/lib/esp-idf-lua/examples/alloc/main/main.c b/lib/esp-idf-lua/examples/alloc/main/main.c new file mode 100644 index 00000000..38b374ef --- /dev/null +++ b/lib/esp-idf-lua/examples/alloc/main/main.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define LUA_MAX_MEMSIZE 0x10000 // 64k + +static const char *prg = + "local t = {}\n" + "for i = 1, 10000 do\n" + " print(string.format(\"Iteration %d...\", i))\n" + " t[i] = string.format(\"Lorem ipsum asklfhskldjf hskljfh slkjfd sdklfjh slkfjh sdkljfjh sdklfjh sdklf %d...\", i)\n" + "end\n"; + +static void report(lua_State *L, int status) +{ + if (status == LUA_OK) + return; + + const char *msg = lua_tostring(L, -1); + printf("%s\n", msg); + lua_pop(L, 1); +} + +static int lua_mem_size = 0; + +static void* l_alloc(void *ud, void *ptr, size_t osize, size_t nsize) +{ + int new_size = lua_mem_size - osize + nsize; + if (new_size > LUA_MAX_MEMSIZE) + { + printf("Error! Lua wants more memory than we can allocate: %u > %u\n", new_size, LUA_MAX_MEMSIZE); + return NULL; + } + //printf("Reallocating: old = %u, new = %u\n", lua_mem_size, new_size); + lua_mem_size = new_size; + + if (nsize == 0) + { + free(ptr); + return NULL; + } + else + return realloc(ptr, nsize); +} + +void test(void *arg) +{ + printf("Program:\n%s\n", prg); + + lua_State *L = lua_newstate(l_alloc, NULL); + if (!L) + { + printf("Could not create state\n"); + while (1) { vTaskDelay(1000); } + } + + luaL_openlibs(L); + + int r = luaL_loadstring(L, prg); + if (r) + { + report(L, r); + while (1) { vTaskDelay(1000); } + } + + r = lua_pcall(L, 0, 0, 0); + if (r) + report(L, r); + + lua_close(L); + + printf("State closed, heap: %" PRIu32 "\n", xPortGetFreeHeapSize()); + while (1) + { + printf("."); + fflush(stdout); + vTaskDelay(pdMS_TO_TICKS(100)); + } +} + +void app_main() +{ + xTaskCreate(test, "test", 0x10000, NULL, 5, NULL); +} -- cgit v1.2.3