Skip to content

Commit 8025bea

Browse files
committed
Initial import
1 parent ff51580 commit 8025bea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+24386
-0
lines changed

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# ESP8266 Arduino Lua
2+
3+
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:
4+
* *pinMode()*
5+
* *digitalWrite()*
6+
* *delay()*
7+
* *millis()*
8+
* *print()* which redirects to *Serial.println()*
9+
10+
## Installation into the Arduino IDE
11+
12+
Download the content of this Github repository as a ZIP archive by clicking on *Clone or download* then *Download ZIP*. Follow the instructions on [installing additional Arduino libraries](https://www.arduino.cc/en/Guide/Libraries#toc4) and navigate to the file downloaded previously.
13+
14+
## Arduino sketch examples
15+
16+
After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *ESP8266-Arduino-Lua*. The examples include **ExecuteScriptFromSerial** which takes a lua script from the serial line and executes it:
17+
18+
```
19+
# Enter the lua script and press Control-D when finished:
20+
print("My first test!")
21+
# Executing script:
22+
My first test!
23+
24+
25+
# Enter the lua script and press Control-D when finished:
26+
print("Current uptime: "..millis())
27+
# Executing script:
28+
Current uptime: 159926.0
29+
```
30+
31+
## Lua script examples
32+
33+
The lua language syntax is described in the [reference manual](https://www.lua.org/manual/). It is extended by the Arduino functions listed above.
34+
35+
### Hello world
36+
37+
```
38+
print("Hello world!")
39+
```
40+
41+
### Blinking LED
42+
43+
```
44+
pinLED = 2
45+
period = 500
46+
pinMode(pinLED, OUTPUT)
47+
while(true)
48+
do
49+
print("LED on")
50+
digitalWrite(pinLED, LOW)
51+
delay(period)
52+
print("LED off")
53+
digitalWrite(pinLED, HIGH)
54+
delay(period)
55+
end
56+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <LuaWrapper.h>
2+
3+
LuaWrapper lua;
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
}
8+
9+
void loop() {
10+
String script = "";
11+
char c = 0;
12+
Serial.println();
13+
Serial.println("# Enter the lua script and press Control-D when finished:");
14+
while(1) {
15+
if(Serial.available() > 0) {
16+
c = Serial.read();
17+
if(c == 4) {
18+
break;
19+
}
20+
Serial.write(c);
21+
script += c;
22+
if(c == '\r') {
23+
Serial.write('\n');
24+
script += '\n';
25+
}
26+
}
27+
}
28+
if(script.length() > 0) {
29+
Serial.println();
30+
Serial.println("# Executing script:");
31+
Serial.println(lua.Lua_dostring(&script));
32+
}
33+
}

examples/HelloWorld/HelloWorld.ino

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <LuaWrapper.h>
2+
3+
LuaWrapper lua;
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
String script = String("print('Hello world!')");
8+
Serial.println(lua.Lua_dostring(&script));
9+
}
10+
11+
void loop() {
12+
13+
}

examples/LuaError/LuaError.ino

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <LuaWrapper.h>
2+
3+
LuaWrapper lua;
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
String script = String("thisFunctionDoesNotExistsSoThisWillFail()");
8+
Serial.println(lua.Lua_dostring(&script));
9+
}
10+
11+
void loop() {
12+
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <LuaWrapper.h>
2+
3+
LuaWrapper lua;
4+
5+
static int myFunction(lua_State *lua_state) {
6+
Serial.println("Hi from my C function");
7+
}
8+
9+
void setup() {
10+
Serial.begin(115200);
11+
lua.Lua_register("myFunction", (const lua_CFunction) &myFunction);
12+
String script = String("myFunction()");
13+
Serial.println(lua.Lua_dostring(&script));
14+
}
15+
16+
void loop() {
17+
18+
}

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=ESP8266-Arduino-Lua
2+
version=0.0.10
3+
author=Francois Dugast <[email protected]>
4+
maintainer=Francois Dugast <[email protected]>
5+
sentence=Lua scripting engine integrated in Arduino for ESP8266
6+
paragraph=
7+
category=Other
8+
url=https://github.com/fdu/ESP8266-Arduino-Lua
9+
architectures=esp8266
10+
includes=LuaWrapper.h

src/LuaWrapper.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include "LuaWrapper.h"
2+
3+
extern "C" {
4+
static int lua_wrapper_pinMode(lua_State *lua) {
5+
int a = luaL_checkinteger(lua, 1);
6+
int b = luaL_checkinteger(lua, 2);
7+
pinMode(a, b);
8+
}
9+
10+
static int lua_wrapper_digitalWrite(lua_State *lua) {
11+
int a = luaL_checkinteger(lua, 1);
12+
int b = luaL_checkinteger(lua, 2);
13+
digitalWrite(a, b);
14+
}
15+
16+
static int lua_wrapper_delay(lua_State *lua) {
17+
int a = luaL_checkinteger(lua, 1);
18+
delay(a);
19+
}
20+
21+
static int lua_wrapper_print(lua_State *lua) {
22+
String a = String(luaL_checkstring(lua, 1));
23+
Serial.println(a);
24+
}
25+
26+
static int lua_wrapper_millis(lua_State *lua) {
27+
lua_pushnumber(lua, (lua_Number) millis());
28+
return 1;
29+
}
30+
}
31+
32+
LuaWrapper::LuaWrapper() {
33+
_state = luaL_newstate();
34+
lua_register(_state, "pinMode", lua_wrapper_pinMode);
35+
lua_register(_state, "digitalWrite", lua_wrapper_digitalWrite);
36+
lua_register(_state, "delay", lua_wrapper_delay);
37+
lua_register(_state, "print", lua_wrapper_print);
38+
lua_register(_state, "millis", lua_wrapper_millis);
39+
}
40+
41+
String LuaWrapper::Lua_dostring(const String *script) {
42+
String scriptWithConstants = addConstants() + *script;
43+
String result;
44+
if (luaL_dostring(_state, scriptWithConstants.c_str())) {
45+
result += "# lua error:\n" + String(lua_tostring(_state, -1));
46+
lua_pop(_state, 1);
47+
}
48+
return result;
49+
}
50+
51+
void LuaWrapper::Lua_register(const String name, const lua_CFunction function) {
52+
lua_register(_state, name.c_str(), function);
53+
}
54+
55+
String LuaWrapper::addConstants() {
56+
String constants = "INPUT = " + String(INPUT) + "\r\n";
57+
constants += "OUTPUT = " + String(OUTPUT) + "\r\n";
58+
constants += "LOW = " + String(LOW) + "\r\n";
59+
constants += "HIGH = " + String(HIGH) + "\r\n";
60+
return constants;
61+
}

src/LuaWrapper.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef LUA_WRAPPER_H
2+
#define LUA_WRAPPER_H
3+
4+
#include "Arduino.h"
5+
6+
#define LUA_USE_C89
7+
#include "lua/lua.hpp"
8+
9+
class LuaWrapper {
10+
public:
11+
LuaWrapper();
12+
String Lua_dostring(const String *script);
13+
void Lua_register(const String name, const lua_CFunction function);
14+
15+
private:
16+
lua_State *_state;
17+
String addConstants();
18+
};
19+
20+
#endif

0 commit comments

Comments
 (0)