summaryrefslogtreecommitdiff
path: root/lib/esp-idf-lua/examples/custom_lib/main
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/custom_lib/main
parent8471046a95ab9e00f7d42b56dbbc9ce3e5b424b9 (diff)
downloadtangara-fw-8a0a167adbf3d9b6f8b6f16aaf20ca39ad5549de.tar.gz
Convert the main menu screen to lua lol
Diffstat (limited to 'lib/esp-idf-lua/examples/custom_lib/main')
-rw-r--r--lib/esp-idf-lua/examples/custom_lib/main/CMakeLists.txt2
-rw-r--r--lib/esp-idf-lua/examples/custom_lib/main/component.mk2
-rw-r--r--lib/esp-idf-lua/examples/custom_lib/main/gpiolib.c61
-rw-r--r--lib/esp-idf-lua/examples/custom_lib/main/gpiolib.h9
-rw-r--r--lib/esp-idf-lua/examples/custom_lib/main/main.c83
-rw-r--r--lib/esp-idf-lua/examples/custom_lib/main/rtoslib.c24
-rw-r--r--lib/esp-idf-lua/examples/custom_lib/main/rtoslib.h9
7 files changed, 190 insertions, 0 deletions
diff --git a/lib/esp-idf-lua/examples/custom_lib/main/CMakeLists.txt b/lib/esp-idf-lua/examples/custom_lib/main/CMakeLists.txt
new file mode 100644
index 00000000..09a8616b
--- /dev/null
+++ b/lib/esp-idf-lua/examples/custom_lib/main/CMakeLists.txt
@@ -0,0 +1,2 @@
+idf_component_register(SRC_DIRS "."
+ INCLUDE_DIRS ".")
diff --git a/lib/esp-idf-lua/examples/custom_lib/main/component.mk b/lib/esp-idf-lua/examples/custom_lib/main/component.mk
new file mode 100644
index 00000000..037751c1
--- /dev/null
+++ b/lib/esp-idf-lua/examples/custom_lib/main/component.mk
@@ -0,0 +1,2 @@
+COMPONENT_ADD_INCLUDEDIRS = .
+COMPONENT_SRCDIRS = .
diff --git a/lib/esp-idf-lua/examples/custom_lib/main/gpiolib.c b/lib/esp-idf-lua/examples/custom_lib/main/gpiolib.c
new file mode 100644
index 00000000..022b2ba2
--- /dev/null
+++ b/lib/esp-idf-lua/examples/custom_lib/main/gpiolib.c
@@ -0,0 +1,61 @@
+#include "gpiolib.h"
+#include <driver/gpio.h>
+
+static void lua_esp_check_err(lua_State *L, esp_err_t err)
+{
+ if (err != ESP_OK)
+ luaL_error(L, "Error: %s", esp_err_to_name(err));
+}
+
+static int lgpio_reset_pin(lua_State *L)
+{
+ gpio_num_t pin = luaL_checkinteger(L, 1);
+
+ lua_esp_check_err(L, gpio_reset_pin(pin));
+
+ return 0;
+}
+
+static int lgpio_set_level(lua_State *L)
+{
+ gpio_num_t pin = luaL_checkinteger(L, 1);
+ int level = luaL_checkinteger(L, 2);
+
+ lua_esp_check_err(L, gpio_set_level(pin, level));
+
+ return 0;
+}
+
+static int lgpio_get_level(lua_State *L)
+{
+ gpio_num_t pin = luaL_checkinteger(L, 1);
+ lua_pushinteger(L, gpio_get_level(pin));
+
+ return 1;
+}
+
+static int lgpio_set_direction(lua_State *L)
+{
+ gpio_num_t pin = luaL_checkinteger(L, 1);
+ int mode = luaL_checkinteger(L, 2);
+
+ lua_esp_check_err(L, gpio_set_direction(pin, mode));
+
+ return 0;
+}
+
+static const struct luaL_Reg lgpio_funcs[] = {
+ { "reset_pin", lgpio_reset_pin },
+ { "set_direction", lgpio_set_direction },
+ { "set_level", lgpio_set_level },
+ { "get_level", lgpio_get_level },
+ { NULL, NULL }
+};
+
+int luaopen_lgpio(lua_State *L)
+{
+ luaL_newlib(L, lgpio_funcs);
+
+ return 1;
+}
+
diff --git a/lib/esp-idf-lua/examples/custom_lib/main/gpiolib.h b/lib/esp-idf-lua/examples/custom_lib/main/gpiolib.h
new file mode 100644
index 00000000..f5d61651
--- /dev/null
+++ b/lib/esp-idf-lua/examples/custom_lib/main/gpiolib.h
@@ -0,0 +1,9 @@
+#ifndef MAIN_GPIOLIB_H_
+#define MAIN_GPIOLIB_H_
+
+#include <lua/lua.h>
+#include <lua/lauxlib.h>
+
+int luaopen_lgpio(lua_State *L);
+
+#endif /* MAIN_GPIOLIB_H_ */
diff --git a/lib/esp-idf-lua/examples/custom_lib/main/main.c b/lib/esp-idf-lua/examples/custom_lib/main/main.c
new file mode 100644
index 00000000..d5820b2d
--- /dev/null
+++ b/lib/esp-idf-lua/examples/custom_lib/main/main.c
@@ -0,0 +1,83 @@
+#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>
+
+#include "gpiolib.h"
+#include "rtoslib.h"
+
+#define GPIO_PIN "5"
+
+static void load_custom_libs(lua_State *L)
+{
+ luaL_requiref(L, "gpio", luaopen_lgpio, 1);
+ lua_pop(L, 1);
+
+ luaL_requiref(L, "rtos", luaopen_lrtos, 1);
+ lua_pop(L, 1);
+}
+
+static const char *prg =
+ "gpio.set_direction(" GPIO_PIN ", 2)\n"
+ "level = 1\n"
+ "for i = 1, 100 do\n"
+ " print(string.format(\"Iteration %d..., GPIO5=%d\", i, level))\n"
+ " gpio.set_level(" GPIO_PIN ", level)\n"
+ " level = 1 - level\n"
+ " rtos.delay(500)\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);
+}
+
+void test(void *arg)
+{
+ printf("Program:\n%s\n", prg);
+
+ lua_State *L = luaL_newstate();
+ if (!L)
+ {
+ printf("Could not create state\n");
+ while (1) { vTaskDelay(1000); }
+ }
+
+ luaL_openlibs(L);
+ load_custom_libs(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);
+}
diff --git a/lib/esp-idf-lua/examples/custom_lib/main/rtoslib.c b/lib/esp-idf-lua/examples/custom_lib/main/rtoslib.c
new file mode 100644
index 00000000..6c5e219a
--- /dev/null
+++ b/lib/esp-idf-lua/examples/custom_lib/main/rtoslib.c
@@ -0,0 +1,24 @@
+#include "rtoslib.h"
+#include <freertos/FreeRTOS.h>
+#include <freertos/task.h>
+
+static int ldelay(lua_State *L)
+{
+ int ms = luaL_checkinteger(L, 1);
+
+ vTaskDelay(pdMS_TO_TICKS(ms));
+
+ return 0;
+}
+
+static const struct luaL_Reg lrtos_funcs[] = {
+ { "delay", ldelay },
+ { NULL, NULL }
+};
+
+int luaopen_lrtos(lua_State *L)
+{
+ luaL_newlib(L, lrtos_funcs);
+
+ return 1;
+}
diff --git a/lib/esp-idf-lua/examples/custom_lib/main/rtoslib.h b/lib/esp-idf-lua/examples/custom_lib/main/rtoslib.h
new file mode 100644
index 00000000..9f62f736
--- /dev/null
+++ b/lib/esp-idf-lua/examples/custom_lib/main/rtoslib.h
@@ -0,0 +1,9 @@
+#ifndef MAIN_RTOSLIB_H_
+#define MAIN_RTOSLIB_H_
+
+#include <lua/lua.h>
+#include <lua/lauxlib.h>
+
+int luaopen_lrtos(lua_State *L);
+
+#endif /* MAIN_RTOSLIB_H_ */