diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-11-12 19:14:09 +1100 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-11-12 19:14:09 +1100 |
| commit | 8a0a167adbf3d9b6f8b6f16aaf20ca39ad5549de (patch) | |
| tree | 02b6cf23f591915747ec2994381854a79979c4a0 /lib/esp-idf-lua/examples/vfs | |
| parent | 8471046a95ab9e00f7d42b56dbbc9ce3e5b424b9 (diff) | |
| download | tangara-fw-8a0a167adbf3d9b6f8b6f16aaf20ca39ad5549de.tar.gz | |
Convert the main menu screen to lua lol
Diffstat (limited to 'lib/esp-idf-lua/examples/vfs')
| -rw-r--r-- | lib/esp-idf-lua/examples/vfs/CMakeLists.txt | 8 | ||||
| -rw-r--r-- | lib/esp-idf-lua/examples/vfs/Makefile | 8 | ||||
| -rw-r--r-- | lib/esp-idf-lua/examples/vfs/main/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | lib/esp-idf-lua/examples/vfs/main/component.mk | 2 | ||||
| -rw-r--r-- | lib/esp-idf-lua/examples/vfs/main/main.c | 95 | ||||
| -rw-r--r-- | lib/esp-idf-lua/examples/vfs/partitions.csv | 6 | ||||
| -rw-r--r-- | lib/esp-idf-lua/examples/vfs/sdkconfig.defaults | 3 | ||||
| -rw-r--r-- | lib/esp-idf-lua/examples/vfs/spiffs_image/hello.lua | 11 | ||||
| -rw-r--r-- | lib/esp-idf-lua/examples/vfs/spiffs_image/hex.lua | 13 | ||||
| -rw-r--r-- | lib/esp-idf-lua/examples/vfs/spiffs_image/main.lua | 6 |
10 files changed, 154 insertions, 0 deletions
diff --git a/lib/esp-idf-lua/examples/vfs/CMakeLists.txt b/lib/esp-idf-lua/examples/vfs/CMakeLists.txt new file mode 100644 index 00000000..1ab79407 --- /dev/null +++ b/lib/esp-idf-lua/examples/vfs/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.5) + +set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../..) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(example-lua-vfs) + +spiffs_create_partition_image(storage spiffs_image FLASH_IN_PROJECT) diff --git a/lib/esp-idf-lua/examples/vfs/Makefile b/lib/esp-idf-lua/examples/vfs/Makefile new file mode 100644 index 00000000..b4e2554e --- /dev/null +++ b/lib/esp-idf-lua/examples/vfs/Makefile @@ -0,0 +1,8 @@ +PROJECT_NAME := example-lua-vfs + +EXTRA_COMPONENT_DIRS := $(CURDIR)/../.. + +include $(IDF_PATH)/make/project.mk + +SPIFFS_IMAGE_FLASH_IN_PROJECT := 1 +$(eval $(call spiffs_create_partition_image,storage,spiffs_image)) 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 <stdio.h> +#include <inttypes.h> +#include <freertos/FreeRTOS.h> +#include <freertos/task.h> +#include <string.h> +#include <stdbool.h> +#include <lua/lua.h> +#include <lua/lauxlib.h> +#include <lua/lualib.h> +#include <esp_spiffs.h> +#include <esp_err.h> +#include <esp_log.h> + +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); +} diff --git a/lib/esp-idf-lua/examples/vfs/partitions.csv b/lib/esp-idf-lua/examples/vfs/partitions.csv new file mode 100644 index 00000000..0eebc192 --- /dev/null +++ b/lib/esp-idf-lua/examples/vfs/partitions.csv @@ -0,0 +1,6 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap +nvs, data, nvs, 0x9000, 0x6000, +phy_init, data, phy, 0xf000, 0x1000, +factory, app, factory, 0x10000, 1M, +storage, data, spiffs, , 0xF0000, diff --git a/lib/esp-idf-lua/examples/vfs/sdkconfig.defaults b/lib/esp-idf-lua/examples/vfs/sdkconfig.defaults new file mode 100644 index 00000000..8ff365b1 --- /dev/null +++ b/lib/esp-idf-lua/examples/vfs/sdkconfig.defaults @@ -0,0 +1,3 @@ +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" diff --git a/lib/esp-idf-lua/examples/vfs/spiffs_image/hello.lua b/lib/esp-idf-lua/examples/vfs/spiffs_image/hello.lua new file mode 100644 index 00000000..eda6f859 --- /dev/null +++ b/lib/esp-idf-lua/examples/vfs/spiffs_image/hello.lua @@ -0,0 +1,11 @@ +local hello = {} + +function hello.world() + return "Hello world!" +end + +function hello.lorem_ipsum() + return "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." +end + +return hello diff --git a/lib/esp-idf-lua/examples/vfs/spiffs_image/hex.lua b/lib/esp-idf-lua/examples/vfs/spiffs_image/hex.lua new file mode 100644 index 00000000..c9e7634e --- /dev/null +++ b/lib/esp-idf-lua/examples/vfs/spiffs_image/hex.lua @@ -0,0 +1,13 @@ +local hex = {} + +function hex.dump(buf) + for byte=1, #buf, 16 do + local chunk = buf:sub(byte, byte+15) + io.write(string.format('%08X ',byte-1)) + chunk:gsub('.', function (c) io.write(string.format('%02X ',string.byte(c))) end) + io.write(string.rep(' ',3*(16-#chunk))) + io.write(' ',chunk:gsub('%c','.'),"\n") + end +end + +return hex diff --git a/lib/esp-idf-lua/examples/vfs/spiffs_image/main.lua b/lib/esp-idf-lua/examples/vfs/spiffs_image/main.lua new file mode 100644 index 00000000..0873329c --- /dev/null +++ b/lib/esp-idf-lua/examples/vfs/spiffs_image/main.lua @@ -0,0 +1,6 @@ +local hello = require "hello" +local hex = require "hex" + +print(hello.world()) + +hex.dump(hello.lorem_ipsum()) |
