-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d96eb3f
commit 52a30c0
Showing
70 changed files
with
25,137 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# ESP Arduino Lua | ||
|
||
This Arduino library provides the [lua](https://www.lua.org/) 5.3.5 ( [release](https://www.lua.org/ftp/lua-5.3.5.tar.gz) ) scripting engine for ESP8266/ESP32 sketches. This allows dynamic execution of Lua code on the Arduino without having to compile and flash a new firmware. | ||
|
||
Along with the Lua 5.3.5 Core the following Lua standard libraries are included: | ||
|
||
- base (print(), tostring(), tonumber(), etc.) | ||
- math | ||
- table (insert(), sort(), remove(), ...) | ||
- string (len(), match(), ...) | ||
|
||
## Sample sketch example: ExecuteScriptFromSerial.ino | ||
|
||
After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it. As an example, the following standard Arduino functions are available in lua scripts as bindings: | ||
|
||
- pinMode() | ||
- digitalWrite() | ||
- delay() | ||
- millis() | ||
- print() *(only builtin binding)* | ||
|
||
``` | ||
# Enter the lua script and press Control-D when finished: | ||
print("My first test!") | ||
# Executing script: | ||
My first test! | ||
# Enter the lua script and press Control-D when finished- | ||
print("Current uptime: " .. millis()) | ||
# Executing script: | ||
Current uptime: 159926.0 | ||
# Enter the lua script and press Control-D when finished: | ||
print("Hello world!") | ||
# Executing script: | ||
Hello world! | ||
# Enter the lua script and press Control-D when finished: | ||
pinLED = 2 | ||
period = 500 | ||
pinMode(pinLED, OUTPUT) | ||
while(true) | ||
do | ||
print("LED on") | ||
digitalWrite(pinLED, LOW) | ||
delay(period) | ||
print("LED off") | ||
digitalWrite(pinLED, HIGH) | ||
delay(period) | ||
end | ||
# Executing script: | ||
LED on | ||
LED off | ||
``` | ||
## Resources Used (ExecuteScriptFromSerial.ino) | ||
|
||
**ESP8266:** | ||
Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes. | ||
Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes for local variables. Maximum is 81920 bytes. | ||
|
||
**ESP32:** | ||
Sketch uses 310749 bytes (23%) of program storage space. Maximum is 1310720 bytes. | ||
Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes. | ||
|
||
## Arduino IDE Library example: HelloWorld.ino | ||
``` | ||
#include <LuaWrapper.h> | ||
LuaWrapper lua; | ||
void setup() { | ||
Serial.begin(115200); | ||
String script = String("print('Hello world!')"); | ||
Serial.println(lua.Lua_dostring(&script)); | ||
} | ||
void loop() { | ||
} | ||
``` | ||
## Resources Used (HelloWorld.ino) | ||
|
||
**ESP8266:** | ||
Sketch uses 365276 bytes (34%) of program storage space. Maximum is 1044464 bytes. | ||
Global variables use 34712 bytes (42%) of dynamic memory, leaving 47208 bytes for local variables. Maximum is 81920 bytes. | ||
|
||
**ESP32:** | ||
Sketch uses 309913 bytes (23%) of program storage space. Maximum is 1310720 bytes. | ||
Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes. | ||
|
||
## The Lua Language: | ||
[Lua 5.3 Reference Manual](https://www.lua.org/manual/5.3/) | ||
|
62 changes: 62 additions & 0 deletions
62
lib/ESP-Arduino-Lua/examples/ExecuteScriptFromSerial/ExecuteScriptFromSerial.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <LuaWrapper.h> | ||
|
||
LuaWrapper lua; | ||
|
||
static int lua_wrapper_pinMode(lua_State *lua_state) { | ||
int a = luaL_checkinteger(lua_state, 1); | ||
int b = luaL_checkinteger(lua_state, 2); | ||
pinMode(a, b); | ||
return 0; | ||
} | ||
|
||
static int lua_wrapper_digitalWrite(lua_State *lua_state) { | ||
int a = luaL_checkinteger(lua_state, 1); | ||
int b = luaL_checkinteger(lua_state, 2); | ||
digitalWrite(a, b); | ||
return 0; | ||
} | ||
|
||
static int lua_wrapper_delay(lua_State *lua_state) { | ||
int a = luaL_checkinteger(lua_state, 1); | ||
delay(a); | ||
return 0; | ||
} | ||
|
||
static int lua_wrapper_millis(lua_State *lua_state) { | ||
lua_pushnumber(lua_state, (lua_Number) millis()); | ||
return 1; | ||
} | ||
|
||
void setup() { | ||
lua.Lua_register("pinMode", (const lua_CFunction) &lua_wrapper_pinMode); | ||
lua.Lua_register("digitalWrite", (const lua_CFunction) &lua_wrapper_digitalWrite); | ||
lua.Lua_register("delay", (const lua_CFunction) &lua_wrapper_delay); | ||
lua.Lua_register("millis", (const lua_CFunction) &lua_wrapper_millis); | ||
Serial.begin(115200); | ||
} | ||
|
||
void loop() { | ||
String script = ""; | ||
char c = 0; | ||
Serial.println(); | ||
Serial.println("# Enter the lua script and press Control-D when finished:"); | ||
while(1) { | ||
if(Serial.available() > 0) { | ||
c = Serial.read(); | ||
if(c == 4) { | ||
break; | ||
} | ||
Serial.write(c); | ||
script += c; | ||
if(c == '\r') { | ||
Serial.write('\n'); | ||
script += '\n'; | ||
} | ||
} | ||
} | ||
if(script.length() > 0) { | ||
Serial.println(); | ||
Serial.println("# Executing script:"); | ||
Serial.println(lua.Lua_dostring(&script)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <LuaWrapper.h> | ||
|
||
LuaWrapper lua; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
String script = String("print('Hello world!')"); | ||
Serial.println(lua.Lua_dostring(&script)); | ||
} | ||
|
||
void loop() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <LuaWrapper.h> | ||
|
||
LuaWrapper lua; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
String script = String("thisFunctionDoesNotExistsSoThisWillFail()"); | ||
Serial.println(lua.Lua_dostring(&script)); | ||
} | ||
|
||
void loop() { | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
lib/ESP-Arduino-Lua/examples/RegisterFunction/RegisterFunction.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <LuaWrapper.h> | ||
|
||
LuaWrapper lua; | ||
|
||
static int myFunction(lua_State *lua_state) { | ||
Serial.println("Hi from my C function"); | ||
return 0; | ||
} | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
lua.Lua_register("myFunction", (const lua_CFunction) &myFunction); | ||
String script = String("myFunction()"); | ||
Serial.println(lua.Lua_dostring(&script)); | ||
} | ||
|
||
void loop() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name=ESP-Arduino-Lua | ||
version=0.0.50 | ||
author=François Dugast <1050329+fdu@users.noreply.github.com> | ||
maintainer=Scotty Franzyshen <sfranzyshen@users.noreply.github.com> | ||
sentence=Lua scripting engine integrated in Arduino IDE as a Library for ESP8266/ESP32 | ||
paragraph= | ||
category=Other | ||
url=https://github.com/sfranzyshen/ESP-Arduino-Lua | ||
architectures=esp8266, esp32 | ||
includes=LuaWrapper.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "LuaWrapper.h" | ||
|
||
extern "C" { | ||
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; | ||
} | ||
} | ||
|
||
LuaWrapper::LuaWrapper() { | ||
_state = luaL_newstate(); | ||
luaopen_base(_state); | ||
luaopen_table(_state); | ||
luaopen_string(_state); | ||
luaopen_math(_state); | ||
lua_register(_state, "print", lua_wrapper_print); | ||
Serial.printf("I'm lua and i'am born!\n"); | ||
} | ||
|
||
LuaWrapper::~LuaWrapper() { | ||
Serial.printf("I'm lua and i'am killing myself !\n"); | ||
lua_close(_state); | ||
} | ||
|
||
|
||
String LuaWrapper::Lua_dostring(const String *script) { | ||
String scriptWithConstants = addConstants() + *script; | ||
String result; | ||
if (luaL_dostring(_state, scriptWithConstants.c_str())) { | ||
result += "# lua error:\n" + String(lua_tostring(_state, -1)); | ||
lua_pop(_state, 1); | ||
} | ||
return result; | ||
} | ||
|
||
void LuaWrapper::Lua_register(const String name, const lua_CFunction function) { | ||
lua_register(_state, name.c_str(), function); | ||
} | ||
|
||
String LuaWrapper::addConstants() { | ||
String constants = "INPUT = " + String(INPUT) + "\r\n"; | ||
constants += "OUTPUT = " + String(OUTPUT) + "\r\n"; | ||
constants += "LOW = " + String(LOW) + "\r\n"; | ||
constants += "HIGH = " + String(HIGH) + "\r\n"; | ||
return constants; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef LUA_WRAPPER_H | ||
#define LUA_WRAPPER_H | ||
|
||
#include "Arduino.h" | ||
|
||
#define LUA_USE_C89 | ||
#include "lua/lua.hpp" | ||
|
||
class LuaWrapper { | ||
public: | ||
LuaWrapper(); | ||
~LuaWrapper(); | ||
String Lua_dostring(const String *script); | ||
void Lua_register(const String name, const lua_CFunction function); | ||
|
||
private: | ||
lua_State *_state; | ||
String addConstants(); | ||
}; | ||
|
||
#endif |
Oops, something went wrong.