summaryrefslogtreecommitdiff
path: root/lib/esp-idf-lua/examples/simple
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-11-12 19:14:09 +1100
committerjacqueline <me@jacqueline.id.au>2023-11-12 19:14:09 +1100
commit8a0a167adbf3d9b6f8b6f16aaf20ca39ad5549de (patch)
tree02b6cf23f591915747ec2994381854a79979c4a0 /lib/esp-idf-lua/examples/simple
parent8471046a95ab9e00f7d42b56dbbc9ce3e5b424b9 (diff)
downloadtangara-fw-8a0a167adbf3d9b6f8b6f16aaf20ca39ad5549de.tar.gz
Convert the main menu screen to lua lol
Diffstat (limited to 'lib/esp-idf-lua/examples/simple')
-rw-r--r--lib/esp-idf-lua/examples/simple/CMakeLists.txt6
-rw-r--r--lib/esp-idf-lua/examples/simple/Makefile5
-rw-r--r--lib/esp-idf-lua/examples/simple/main/CMakeLists.txt2
-rw-r--r--lib/esp-idf-lua/examples/simple/main/component.mk2
-rw-r--r--lib/esp-idf-lua/examples/simple/main/main.c77
5 files changed, 92 insertions, 0 deletions
diff --git a/lib/esp-idf-lua/examples/simple/CMakeLists.txt b/lib/esp-idf-lua/examples/simple/CMakeLists.txt
new file mode 100644
index 00000000..ba61d4eb
--- /dev/null
+++ b/lib/esp-idf-lua/examples/simple/CMakeLists.txt
@@ -0,0 +1,6 @@
+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-simple)
diff --git a/lib/esp-idf-lua/examples/simple/Makefile b/lib/esp-idf-lua/examples/simple/Makefile
new file mode 100644
index 00000000..ee373b18
--- /dev/null
+++ b/lib/esp-idf-lua/examples/simple/Makefile
@@ -0,0 +1,5 @@
+PROJECT_NAME := example-lua-simple
+
+EXTRA_COMPONENT_DIRS := $(CURDIR)/../..
+
+include $(IDF_PATH)/make/project.mk
diff --git a/lib/esp-idf-lua/examples/simple/main/CMakeLists.txt b/lib/esp-idf-lua/examples/simple/main/CMakeLists.txt
new file mode 100644
index 00000000..09a8616b
--- /dev/null
+++ b/lib/esp-idf-lua/examples/simple/main/CMakeLists.txt
@@ -0,0 +1,2 @@
+idf_component_register(SRC_DIRS "."
+ INCLUDE_DIRS ".")
diff --git a/lib/esp-idf-lua/examples/simple/main/component.mk b/lib/esp-idf-lua/examples/simple/main/component.mk
new file mode 100644
index 00000000..037751c1
--- /dev/null
+++ b/lib/esp-idf-lua/examples/simple/main/component.mk
@@ -0,0 +1,2 @@
+COMPONENT_ADD_INCLUDEDIRS = .
+COMPONENT_SRCDIRS = .
diff --git a/lib/esp-idf-lua/examples/simple/main/main.c b/lib/esp-idf-lua/examples/simple/main/main.c
new file mode 100644
index 00000000..ff671618
--- /dev/null
+++ b/lib/esp-idf-lua/examples/simple/main/main.c
@@ -0,0 +1,77 @@
+#include <stdio.h>
+#include <inttypes.h>
+#include <freertos/FreeRTOS.h>
+#include <freertos/task.h>
+#include <string.h>
+#include <lua/lua.h>
+#include <lua/lauxlib.h>
+#include <lua/lualib.h>
+
+static const char *prg =
+ "v = {'value1', 'value2', 1.21, 'gigawatts'}\n"
+ "for i = 1, #v do\n"
+ " print(v[i])\n"
+ "end\n";
+
+static int report(lua_State *L, int status)
+{
+ if (status != LUA_OK)
+ {
+ const char *msg = lua_tostring(L, -1);
+ lua_writestring(msg, strlen(msg));
+ lua_writeline();
+ lua_pop(L, 1); /* remove message */
+ }
+ return status;
+}
+
+void test(void *arg)
+{
+ printf("\n\n%s\n\n", prg);
+ printf("Start, heap: %" PRIu32 "\n", xPortGetFreeHeapSize());
+
+ lua_State *L = luaL_newstate();
+ if (!L)
+ {
+ printf("Could not create state....\n");
+ while (1) { vTaskDelay(1000); }
+ }
+
+ printf("State ready, heap: %" PRIu32 "\n", xPortGetFreeHeapSize());
+
+ luaL_openlibs(L);
+
+ printf("Libs ready, heap: %" PRIu32 "\n", xPortGetFreeHeapSize());
+
+ int r = luaL_loadstring(L, prg);
+ if (r)
+ {
+ report(L, r);
+ while (1) { vTaskDelay(1000); }
+ }
+
+ printf("Prg loaded, heap: %" PRIu32 "\n", xPortGetFreeHeapSize());
+
+ printf("-------------------------------------------\n");
+ r = lua_pcall(L, 0, 0, 0);
+ if (r)
+ report(L, r);
+ printf("-------------------------------------------\n");
+
+ printf("Prg done, heap: %" PRIu32 "\n", xPortGetFreeHeapSize());
+
+ lua_close(L);
+
+ printf("State closed, heap: %" PRIu32 "\n", xPortGetFreeHeapSize());
+ while (1)
+ {
+ printf(".");
+ fflush(NULL);
+ vTaskDelay(100);
+ }
+}
+
+void app_main()
+{
+ xTaskCreate(test, "test", 0x10000, NULL, 5, NULL);
+}