From 6ac7db766a397a6d902a1d87b76cef20e768e5aa Mon Sep 17 00:00:00 2001 From: Sasszem Date: Sun, 27 Jan 2019 00:06:35 +0100 Subject: [PATCH 01/11] Add base library import Removed print(), dofile() and loadfile() --- src/LuaWrapper.cpp | 3 +++ src/lua/lbaselib.c | 52 ---------------------------------------------- 2 files changed, 3 insertions(+), 52 deletions(-) diff --git a/src/LuaWrapper.cpp b/src/LuaWrapper.cpp index fb9b551..99da3e8 100644 --- a/src/LuaWrapper.cpp +++ b/src/LuaWrapper.cpp @@ -31,6 +31,9 @@ extern "C" { LuaWrapper::LuaWrapper() { _state = luaL_newstate(); + + luaopen_base(_state); + lua_register(_state, "pinMode", lua_wrapper_pinMode); lua_register(_state, "digitalWrite", lua_wrapper_digitalWrite); lua_register(_state, "delay", lua_wrapper_delay); diff --git a/src/lua/lbaselib.c b/src/lua/lbaselib.c index 6460e4f..1fe8d7d 100644 --- a/src/lua/lbaselib.c +++ b/src/lua/lbaselib.c @@ -20,29 +20,6 @@ #include "lauxlib.h" #include "lualib.h" - -static int luaB_print (lua_State *L) { - int n = lua_gettop(L); /* number of arguments */ - int i; - lua_getglobal(L, "tostring"); - for (i=1; i<=n; i++) { - const char *s; - size_t l; - lua_pushvalue(L, -1); /* function to be called */ - lua_pushvalue(L, i); /* value to print */ - lua_call(L, 1, 1); - s = lua_tolstring(L, -1, &l); /* get result */ - if (s == NULL) - return luaL_error(L, "'tostring' must return a string to 'print'"); - if (i>1) lua_writestring("\t", 1); - lua_writestring(s, l); - lua_pop(L, 1); /* pop result */ - } - lua_writeline(); - return 0; -} - - #define SPACECHARS " \f\n\r\t\v" static const char *b_str2int (const char *s, int base, lua_Integer *pn) { @@ -283,16 +260,6 @@ static int load_aux (lua_State *L, int status, int envidx) { } } - -static int luaB_loadfile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - const char *mode = luaL_optstring(L, 2, NULL); - int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */ - int status = luaL_loadfilex(L, fname, mode); - return load_aux(L, status, env); -} - - /* ** {====================================================== ** Generic Read function @@ -353,22 +320,6 @@ static int luaB_load (lua_State *L) { /* }====================================================== */ -static int dofilecont (lua_State *L, int d1, lua_KContext d2) { - (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */ - return lua_gettop(L) - 1; -} - - -static int luaB_dofile (lua_State *L) { - const char *fname = luaL_optstring(L, 1, NULL); - lua_settop(L, 1); - if (luaL_loadfile(L, fname) != LUA_OK) - return lua_error(L); - lua_callk(L, 0, LUA_MULTRET, 0, dofilecont); - return dofilecont(L, 0, 0); -} - - static int luaB_assert (lua_State *L) { if (lua_toboolean(L, 1)) /* condition is true? */ return lua_gettop(L); /* return all arguments */ @@ -453,11 +404,9 @@ static int luaB_tostring (lua_State *L) { static const luaL_Reg base_funcs[] = { {"assert", luaB_assert}, {"collectgarbage", luaB_collectgarbage}, - {"dofile", luaB_dofile}, {"error", luaB_error}, {"getmetatable", luaB_getmetatable}, {"ipairs", luaB_ipairs}, - {"loadfile", luaB_loadfile}, {"load", luaB_load}, #if defined(LUA_COMPAT_LOADSTRING) {"loadstring", luaB_load}, @@ -465,7 +414,6 @@ static const luaL_Reg base_funcs[] = { {"next", luaB_next}, {"pairs", luaB_pairs}, {"pcall", luaB_pcall}, - {"print", luaB_print}, {"rawequal", luaB_rawequal}, {"rawlen", luaB_rawlen}, {"rawget", luaB_rawget}, From 9897b682f5350c0bc9a907732e39415ce7bb99f1 Mon Sep 17 00:00:00 2001 From: Sasszem Date: Sun, 27 Jan 2019 00:49:19 +0100 Subject: [PATCH 02/11] Add table library Added table library Modified it to work with this setup --- src/LuaWrapper.cpp | 1 + src/lua/ltablib.c | 7 +++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/LuaWrapper.cpp b/src/LuaWrapper.cpp index 99da3e8..1c4af40 100644 --- a/src/LuaWrapper.cpp +++ b/src/LuaWrapper.cpp @@ -33,6 +33,7 @@ LuaWrapper::LuaWrapper() { _state = luaL_newstate(); luaopen_base(_state); + luaopen_table(_state); lua_register(_state, "pinMode", lua_wrapper_pinMode); lua_register(_state, "digitalWrite", lua_wrapper_digitalWrite); diff --git a/src/lua/ltablib.c b/src/lua/ltablib.c index c534957..3f4ca3e 100644 --- a/src/lua/ltablib.c +++ b/src/lua/ltablib.c @@ -256,12 +256,10 @@ typedef unsigned int IdxT; ** is to copy them to an array of a known type and use the array values. */ static unsigned int l_randomizePivot (void) { - clock_t c = clock(); time_t t = time(NULL); - unsigned int buff[sof(c) + sof(t)]; + unsigned int buff[sof(t)]; unsigned int i, rnd = 0; - memcpy(buff, &c, sof(c) * sizeof(unsigned int)); - memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); + memcpy(buff, &t, sof(t) * sizeof(unsigned int)); for (i = 0; i < sof(buff); i++) rnd += buff[i]; return rnd; @@ -440,6 +438,7 @@ static const luaL_Reg tab_funcs[] = { LUAMOD_API int luaopen_table (lua_State *L) { luaL_newlib(L, tab_funcs); + lua_setglobal(L, "table"); #if defined(LUA_COMPAT_UNPACK) /* _G.unpack = table.unpack */ lua_getfield(L, -1, "unpack"); From 424b2a1a0853ccdebaac86d39d51af36014cfb75 Mon Sep 17 00:00:00 2001 From: Sasszem Date: Sun, 27 Jan 2019 01:19:22 +0100 Subject: [PATCH 03/11] Add string library --- src/LuaWrapper.cpp | 1 + src/lua/lstrlib.c | 1 + 2 files changed, 2 insertions(+) diff --git a/src/LuaWrapper.cpp b/src/LuaWrapper.cpp index 1c4af40..066c93c 100644 --- a/src/LuaWrapper.cpp +++ b/src/LuaWrapper.cpp @@ -34,6 +34,7 @@ LuaWrapper::LuaWrapper() { luaopen_base(_state); luaopen_table(_state); + luaopen_string(_state); lua_register(_state, "pinMode", lua_wrapper_pinMode); lua_register(_state, "digitalWrite", lua_wrapper_digitalWrite); diff --git a/src/lua/lstrlib.c b/src/lua/lstrlib.c index b4bed7e..e3b5a67 100644 --- a/src/lua/lstrlib.c +++ b/src/lua/lstrlib.c @@ -1579,6 +1579,7 @@ static void createmetatable (lua_State *L) { LUAMOD_API int luaopen_string (lua_State *L) { luaL_newlib(L, strlib); createmetatable(L); + lua_setglobal(L, "string"); return 1; } From 9e0f258e933cbae94495d5a9098eb240eae4ce8f Mon Sep 17 00:00:00 2001 From: Sasszem Date: Sun, 27 Jan 2019 01:19:46 +0100 Subject: [PATCH 04/11] Add math library --- src/LuaWrapper.cpp | 1 + src/lua/lmathlib.c | 1 + 2 files changed, 2 insertions(+) diff --git a/src/LuaWrapper.cpp b/src/LuaWrapper.cpp index 066c93c..d108026 100644 --- a/src/LuaWrapper.cpp +++ b/src/LuaWrapper.cpp @@ -35,6 +35,7 @@ LuaWrapper::LuaWrapper() { luaopen_base(_state); luaopen_table(_state); luaopen_string(_state); + luaopen_math(_state); lua_register(_state, "pinMode", lua_wrapper_pinMode); lua_register(_state, "digitalWrite", lua_wrapper_digitalWrite); diff --git a/src/lua/lmathlib.c b/src/lua/lmathlib.c index 7ef7e59..77e7996 100644 --- a/src/lua/lmathlib.c +++ b/src/lua/lmathlib.c @@ -405,6 +405,7 @@ LUAMOD_API int luaopen_math (lua_State *L) { lua_setfield(L, -2, "maxinteger"); lua_pushinteger(L, LUA_MININTEGER); lua_setfield(L, -2, "mininteger"); + lua_setglobal(L, "math"); return 1; } From 14db8ba8364a142eb4c6b123ce86bf8015fa7338 Mon Sep 17 00:00:00 2001 From: Sasszem Date: Sun, 27 Jan 2019 01:29:26 +0100 Subject: [PATCH 05/11] Improve print() function Basically the original, but with Serial --- src/LuaWrapper.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/LuaWrapper.cpp b/src/LuaWrapper.cpp index d108026..c2e9436 100644 --- a/src/LuaWrapper.cpp +++ b/src/LuaWrapper.cpp @@ -18,10 +18,26 @@ extern "C" { delay(a); } - static int lua_wrapper_print(lua_State *lua) { - String a = String(luaL_checkstring(lua, 1)); - Serial.println(a); - } + static int lua_wrapper_print (lua_State *L) { + int n = lua_gettop(L); /* number of arguments */ + int i; + lua_getglobal(L, "tostring"); + for (i=1; i<=n; i++) { + const char *s; + size_t l; + lua_pushvalue(L, -1); /* function to be called */ + lua_pushvalue(L, i); /* value to print */ + lua_call(L, 1, 1); + s = lua_tolstring(L, -1, &l); /* get result */ + if (s == NULL) + return luaL_error(L, "'tostring' must return a string to 'print'"); + if (i>1) Serial.write("\t"); + Serial.write(s); + lua_pop(L, 1); /* pop result */ + } + Serial.println(); + return 0; +} static int lua_wrapper_millis(lua_State *lua) { lua_pushnumber(lua, (lua_Number) millis()); From 6126c4c863303d431fc3e3ac6f51ccd5e3b126c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Bar=C3=A1th?= Date: Sun, 27 Jan 2019 01:51:02 +0100 Subject: [PATCH 06/11] Update library.properties Added myself to the authors Updated the version number --- library.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library.properties b/library.properties index b23f65b..c9e7305 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=ESP8266-Arduino-Lua -version=0.0.10 -author=François Dugast <1050329+fdu@users.noreply.github.com> +version=0.0.20 +author=François Dugast <1050329+fdu@users.noreply.github.com>, Sasszem maintainer=François Dugast <1050329+fdu@users.noreply.github.com> sentence=Lua scripting engine integrated in Arduino for ESP8266 paragraph= From cffacd4e2bdbbf743be1fa3544720670bc2134f8 Mon Sep 17 00:00:00 2001 From: Luc Date: Sat, 5 Oct 2019 19:11:47 +0800 Subject: [PATCH 07/11] Update luaconf.h fix compilation error `Error while detecting libraries included` --- src/lua/luaconf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lua/luaconf.h b/src/lua/luaconf.h index 9eeeea6..58660db 100644 --- a/src/lua/luaconf.h +++ b/src/lua/luaconf.h @@ -33,7 +33,7 @@ ** ensure that all software connected to Lua will be compiled with the ** same configuration. */ -/* #define LUA_32BITS */ +#define LUA_32BITS /* From 4c24f03fa0f69ae7ce4060f2114b1c10393ed875 Mon Sep 17 00:00:00 2001 From: Luc Date: Sat, 5 Oct 2019 20:06:54 +0800 Subject: [PATCH 08/11] Update luaconf.h --- src/lua/luaconf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lua/luaconf.h b/src/lua/luaconf.h index 58660db..456b54f 100644 --- a/src/lua/luaconf.h +++ b/src/lua/luaconf.h @@ -41,7 +41,7 @@ ** Define it if you want Lua to avoid the use of a few C99 features ** or Windows-specific features on Windows. */ -/* #define LUA_USE_C89 */ +#define LUA_USE_C89 /* From 31314847f381dadde3e3f4093212b68ddf56bdce Mon Sep 17 00:00:00 2001 From: Luc Date: Sat, 5 Oct 2019 20:07:17 +0800 Subject: [PATCH 09/11] Fix some warnings --- examples/RegisterFunction/RegisterFunction.ino | 2 ++ src/LuaWrapper.cpp | 3 +++ 2 files changed, 5 insertions(+) diff --git a/examples/RegisterFunction/RegisterFunction.ino b/examples/RegisterFunction/RegisterFunction.ino index 8f96309..438c6d2 100644 --- a/examples/RegisterFunction/RegisterFunction.ino +++ b/examples/RegisterFunction/RegisterFunction.ino @@ -3,7 +3,9 @@ LuaWrapper lua; static int myFunction(lua_State *lua_state) { + (void*)lua_state; Serial.println("Hi from my C function"); + return 0; } void setup() { diff --git a/src/LuaWrapper.cpp b/src/LuaWrapper.cpp index c2e9436..d3cee4e 100644 --- a/src/LuaWrapper.cpp +++ b/src/LuaWrapper.cpp @@ -5,17 +5,20 @@ extern "C" { int a = luaL_checkinteger(lua, 1); int b = luaL_checkinteger(lua, 2); pinMode(a, b); + return 0; } static int lua_wrapper_digitalWrite(lua_State *lua) { int a = luaL_checkinteger(lua, 1); int b = luaL_checkinteger(lua, 2); digitalWrite(a, b); + return 0; } static int lua_wrapper_delay(lua_State *lua) { int a = luaL_checkinteger(lua, 1); delay(a); + return 0; } static int lua_wrapper_print (lua_State *L) { From d7519492975100dbd4a9195553dd4c6acaba9009 Mon Sep 17 00:00:00 2001 From: Luc Date: Sat, 5 Oct 2019 20:07:47 +0800 Subject: [PATCH 10/11] Update library.properties add ESP32 as supported platform --- library.properties | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library.properties b/library.properties index c9e7305..20560c3 100644 --- a/library.properties +++ b/library.properties @@ -1,10 +1,10 @@ name=ESP8266-Arduino-Lua -version=0.0.20 +version=0.0.30 author=François Dugast <1050329+fdu@users.noreply.github.com>, Sasszem maintainer=François Dugast <1050329+fdu@users.noreply.github.com> -sentence=Lua scripting engine integrated in Arduino for ESP8266 +sentence=Lua scripting engine integrated in Arduino for ESP8266 / ESP32 paragraph= category=Other url=https://github.com/fdu/ESP8266-Arduino-Lua -architectures=esp8266 +architectures=esp8266, esp32 includes=LuaWrapper.h From 89fe630b9ac6b7fa200fe7ae0721ab7bd1517c54 Mon Sep 17 00:00:00 2001 From: Luc Date: Sat, 5 Oct 2019 20:10:33 +0800 Subject: [PATCH 11/11] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ad9a66c..bcba2af 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# ESP8266 Arduino Lua +# ESP8266/ESP32 Arduino Lua -This Arduino library provides the [lua](https://www.lua.org/) 5.3.4 scripting engine for ESP8266 sketches. This allows dynamic execution of code on Arduino without having to compile and flash a new firmware. As an example, the following standard Arduino functions are available in lua scripts as bindings: +This Arduino library provides the [lua](https://www.lua.org/) 5.3.4 scripting engine for ESP8266/ESP32 sketches. This allows dynamic execution of code on Arduino without having to compile and flash a new firmware. As an example, the following standard Arduino functions are available in lua scripts as bindings: * *pinMode()* * *digitalWrite()* * *delay()*