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/vfs/main/CMakeLists.txt | 2 + lib/esp-idf-lua/examples/vfs/main/component.mk | 2 + lib/esp-idf-lua/examples/vfs/main/main.c | 95 ++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 lib/esp-idf-lua/examples/vfs/main/CMakeLists.txt create mode 100644 lib/esp-idf-lua/examples/vfs/main/component.mk create mode 100644 lib/esp-idf-lua/examples/vfs/main/main.c (limited to 'lib/esp-idf-lua/examples/vfs/main') diff --git a/lib/esp-idf-lua/examples/vfs/main/CMakeLists.txt b/lib/esp-idf-lua/examples/vfs/main/CMakeLists.txt new file mode 100644 index 00000000..09a8616b --- /dev/null +++ b/lib/esp-idf-lua/examples/vfs/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRC_DIRS "." + INCLUDE_DIRS ".") diff --git a/lib/esp-idf-lua/examples/vfs/main/component.mk b/lib/esp-idf-lua/examples/vfs/main/component.mk new file mode 100644 index 00000000..037751c1 --- /dev/null +++ b/lib/esp-idf-lua/examples/vfs/main/component.mk @@ -0,0 +1,2 @@ +COMPONENT_ADD_INCLUDEDIRS = . +COMPONENT_SRCDIRS = . diff --git a/lib/esp-idf-lua/examples/vfs/main/main.c b/lib/esp-idf-lua/examples/vfs/main/main.c new file mode 100644 index 00000000..e9267ea5 --- /dev/null +++ b/lib/esp-idf-lua/examples/vfs/main/main.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static const char *TAG = "LUA-VFS"; + +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 void halt() +{ + ESP_LOGE(TAG, "System halted"); + while (1) + vTaskDelay(1000); +} + +static void mount_fs() +{ + esp_vfs_spiffs_conf_t conf = { + .base_path = "/lua", + .partition_label = NULL, + .max_files = 5, + .format_if_mount_failed = false + }; + + esp_err_t ret = esp_vfs_spiffs_register(&conf); + switch (ret) + { + case ESP_OK: + break; + case ESP_FAIL: + ESP_LOGE(TAG, "Failed to mount or format filesystem"); + halt(); + break; + case ESP_ERR_NOT_FOUND: + ESP_LOGE(TAG, "Failed to find SPIFFS partition"); + halt(); + break; + default: + ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret)); + halt(); + } + size_t total = 0, used = 0; + ESP_ERROR_CHECK(esp_spiffs_info(NULL, &total, &used)); + ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used); +} + + +void test(void *arg) +{ + mount_fs(); + + lua_State *L = luaL_newstate(); + ESP_ERROR_CHECK(L ? ESP_OK : ESP_FAIL); + + luaL_openlibs(L); + + int r = luaL_loadfilex(L, "/lua/main.lua", NULL); + if (r != LUA_OK) + printf("Failed to execute /lua/main.lua\n"); + else + r = lua_pcall(L, 0, LUA_MULTRET, 0); + + report(L, r); + lua_close(L); + + printf("State closed, heap: %" PRIu32 "\n", xPortGetFreeHeapSize()); + + while (1) + { + printf("."); + fflush(stdout); + vTaskDelay(100); + } +} + +void app_main() +{ + xTaskCreate(test, "test", 0x10000, NULL, 5, NULL); +} -- cgit v1.2.3