Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ESP32 support and fix compilation error on ESP #3

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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()*
Expand Down
2 changes: 2 additions & 0 deletions examples/RegisterFunction/RegisterFunction.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name=ESP8266-Arduino-Lua
version=0.0.10
author=François Dugast <[email protected]>
version=0.0.30
author=François Dugast <[email protected]>, Sasszem
maintainer=François Dugast <[email protected]>
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
33 changes: 29 additions & 4 deletions src/LuaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,42 @@ 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 *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());
Expand All @@ -31,6 +50,12 @@ extern "C" {

LuaWrapper::LuaWrapper() {
_state = luaL_newstate();

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);
lua_register(_state, "delay", lua_wrapper_delay);
Expand Down
52 changes: 0 additions & 52 deletions src/lua/lbaselib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -453,19 +404,16 @@ 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},
#endif
{"next", luaB_next},
{"pairs", luaB_pairs},
{"pcall", luaB_pcall},
{"print", luaB_print},
{"rawequal", luaB_rawequal},
{"rawlen", luaB_rawlen},
{"rawget", luaB_rawget},
Expand Down
1 change: 1 addition & 0 deletions src/lua/lmathlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

1 change: 1 addition & 0 deletions src/lua/lstrlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

7 changes: 3 additions & 4 deletions src/lua/ltablib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions src/lua/luaconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
** ensure that all software connected to Lua will be compiled with the
** same configuration.
*/
/* #define LUA_32BITS */
#define LUA_32BITS


/*
@@ LUA_USE_C89 controls the use of non-ISO-C89 features.
** 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


/*
Expand Down