Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b005091

Browse files
Merge pull request #1 from HonestQiao/master
Upgrade lua to 5.3.6
2 parents 0b3de1e + f127c8e commit b005091

File tree

18 files changed

+300
-132
lines changed

18 files changed

+300
-132
lines changed

‎README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
# ESP Arduino Lua
1+
*[中文版本](README_CN.md)
22

3-
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.
3+
# Arduino Lua
44

5-
Along with the Lua 5.3.5 Core the following Lua standard libraries are included:
5+
This Arduino library provides the [lua](https://www.lua.org/) 5.3.6 ( [release](https://www.lua.org/ftp/lua-5.3.6.tar.gz) ) scripting engine for supported devices. This allows dynamic execution of Lua code on the Arduino without having to compile and flash a new firmware.
6+
7+
Supported devices:
8+
* ESP8266
9+
* ESP32/ESP32S2/ESP32C3/ESP32S3
10+
* Arduino Uno R4 Minima/WiFi
11+
* Raspberry Pi Pico/Pico W
12+
13+
Along with the Lua 5.3.6 Core the following Lua standard libraries are included:
614

715
- base (print(), tostring(), tonumber(), etc.)
816
- math
@@ -11,7 +19,7 @@ Along with the Lua 5.3.5 Core the following Lua standard libraries are included:
1119

1220
## Sample sketch example: ExecuteScriptFromSerial.ino
1321

14-
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:
22+
After installing the library, some sketch examples are available from the *File* menu, then *Examples* and finally under *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:
1523

1624
- pinMode()
1725
- digitalWrite()
@@ -65,8 +73,16 @@ Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes fo
6573
Sketch uses 310749 bytes (23%) of program storage space. Maximum is 1310720 bytes.
6674
Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes.
6775

76+
**Arduino Uno R4:**
77+
Sketch uses 133756 bytes (51%) of program storage space. Maximum is 262144 bytes.
78+
Global variables use 2836 bytes (8%) of dynamic memory, leaving 29932 bytes for local variables. Maximum is 32768 bytes.
79+
80+
**Raspberry Pi Pico:**
81+
Sketch uses 124736 bytes (5%) of program storage space. Maximum is 2093056 bytes.
82+
Global variables use 10352 bytes (3%) of dynamic memory, leaving 251792 bytes for local variables. Maximum is 262144 bytes.
83+
6884
## Arduino IDE Library example: HelloWorld.ino
69-
```
85+
```
7086
#include <LuaWrapper.h>
7187
7288
LuaWrapper lua;

‎README_CN.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
* [English Version](README.md)
2+
3+
# Arduino Lua
4+
5+
本Arduino扩展库为支持的设备提供 [lua](https://www.lua.org/) 5.3.6 ([正式版](https://www.lua.org/ftp/lua-5.3.6.tar.gz)) 的脚本运行引擎。 使用该扩展库,可以在Arduino上动态运行Lua代码,而不必重新编译和烧录固件。
6+
7+
支持的设备:
8+
* ESP8266
9+
* ESP32/ESP32S2/ESP32C3/ESP32S3
10+
* Arduino Uno R4 Minima/WiFi
11+
* Raspberry Pi Pico/Pico W
12+
13+
除了 Lua 5.3.6 核心功能以外,还包含了如下的 Lua 标准库:
14+
15+
- base (print(), tostring(), tonumber(), etc.)
16+
- math
17+
- table (insert(), sort(), remove(), ...)
18+
- string (len(), match(), ...)
19+
20+
## 示例: ExecuteScriptFromSerial.ino
21+
22+
安装本扩展后,在 *File* 菜单中的 *Examples* 下的 *Arduino-Lua* 中 会提供部分示例。示例中包含 **ExecuteScriptFromSerial** ,可以通过串口输入 Lua 脚本并执行。例如,如下的标准 Arduino 函数,可以被绑定从而在 lua 脚本中使用:
23+
24+
- pinMode()
25+
- digitalWrite()
26+
- delay()
27+
- millis()
28+
- print() *(only builtin binding)*
29+
30+
```
31+
# Enter the lua script and press Control-D when finished:
32+
# 输入如下的 lua脚本,然后按 Ctrl+D 运行:
33+
print("My first test!")
34+
# Executing script:
35+
# 运行脚本:
36+
My first test!
37+
38+
39+
# Enter the lua script and press Control-D when finished-
40+
print("Current uptime: " .. millis())
41+
# Executing script:
42+
Current uptime: 159926.0
43+
44+
45+
# Enter the lua script and press Control-D when finished:
46+
print("Hello world!")
47+
# Executing script:
48+
Hello world!
49+
50+
51+
# Enter the lua script and press Control-D when finished:
52+
pinLED = 2
53+
period = 500
54+
pinMode(pinLED, OUTPUT)
55+
while(true)
56+
do
57+
print("LED on")
58+
digitalWrite(pinLED, LOW)
59+
delay(period)
60+
print("LED off")
61+
digitalWrite(pinLED, HIGH)
62+
delay(period)
63+
end
64+
# Executing script:
65+
LED on
66+
LED off
67+
```
68+
## 资源使用 (ExecuteScriptFromSerial.ino)
69+
70+
**ESP8266:**
71+
Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes.
72+
Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes for local variables. Maximum is 81920 bytes.
73+
74+
**ESP32:**
75+
Sketch uses 310749 bytes (23%) of program storage space. Maximum is 1310720 bytes.
76+
Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes.
77+
78+
**Arduino Uno R4:**
79+
Sketch uses 133756 bytes (51%) of program storage space. Maximum is 262144 bytes.
80+
Global variables use 2836 bytes (8%) of dynamic memory, leaving 29932 bytes for local variables. Maximum is 32768 bytes.
81+
82+
**Raspberry Pi Pico:**
83+
Sketch uses 124736 bytes (5%) of program storage space. Maximum is 2093056 bytes.
84+
Global variables use 10352 bytes (3%) of dynamic memory, leaving 251792 bytes for local variables. Maximum is 262144 bytes.
85+
86+
## 示例: HelloWorld.ino
87+
```
88+
#include <LuaWrapper.h>
89+
90+
LuaWrapper lua;
91+
92+
void setup() {
93+
Serial.begin(115200);
94+
String script = String("print('Hello world!')");
95+
Serial.println(lua.Lua_dostring(&script));
96+
}
97+
98+
void loop() {
99+
100+
}
101+
```
102+
## 资源使用 (HelloWorld.ino)
103+
104+
**ESP8266:**
105+
Sketch uses 365276 bytes (34%) of program storage space. Maximum is 1044464 bytes.
106+
Global variables use 34712 bytes (42%) of dynamic memory, leaving 47208 bytes for local variables. Maximum is 81920 bytes.
107+
108+
**ESP32:**
109+
Sketch uses 309913 bytes (23%) of program storage space. Maximum is 1310720 bytes.
110+
Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes.
111+
112+
## Lua 语言:
113+
[Lua 5.3 参考手册](https://www.lua.org/manual/5.3/)
114+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
print(string.format("Lua version is %s", _VERSION))
2+
3+
LED_BUILTIN = 13
4+
5+
pinMode(LED_BUILTIN, OUTPUT)
6+
7+
for i=1,5,1
8+
do
9+
print(i)
10+
print("LED ON")
11+
digitalWrite(LED_BUILTIN, HIGH)
12+
delay(1000)
13+
print("LED OFF")
14+
digitalWrite(LED_BUILTIN, LOW)
15+
delay(1000)
16+
end

‎library.properties

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
name=ESP-Arduino-Lua
2-
version=0.0.50
1+
name=Arduino-Lua
2+
version=0.1.0
33
author=François Dugast <1050329+fdu@users.noreply.github.com>
44
maintainer=Scotty Franzyshen <sfranzyshen@users.noreply.github.com>
5-
sentence=Lua scripting engine integrated in Arduino IDE as a Library for ESP8266/ESP32
5+
maintainer=HonestQiao <honestqiao@users.noreply.github.com>
6+
sentence=Lua scripting engine integrated in Arduino IDE as a Library for ESP8266/ESP32/ArduinoUnoR4
67
paragraph=
78
category=Other
8-
url=https://github.com/sfranzyshen/ESP-Arduino-Lua
9-
architectures=esp8266, esp32
9+
url=https://github.com/HonestQiao/Arduino-Lua
10+
architectures=esp8266, esp32, renesas_uno
1011
includes=LuaWrapper.h

‎src/lua/lapi.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,13 +1254,12 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) {
12541254
}
12551255

12561256

1257-
static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure**pf) {
1257+
static UpVal **getupvalref (lua_State *L, int fidx, int n) {
12581258
LClosure *f;
12591259
StkId fi = index2addr(L, fidx);
12601260
api_check(L, ttisLclosure(fi), "Lua function expected");
12611261
f = clLvalue(fi);
12621262
api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
1263-
if (pf) *pf = f;
12641263
return &f->upvals[n - 1]; /* get its upvalue pointer */
12651264
}
12661265

@@ -1269,7 +1268,7 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
12691268
StkId fi = index2addr(L, fidx);
12701269
switch (ttype(fi)) {
12711270
case LUA_TLCL: { /* lua closure */
1272-
return *getupvalref(L, fidx, n, NULL);
1271+
return *getupvalref(L, fidx, n);
12731272
}
12741273
case LUA_TCCL: { /* C closure */
12751274
CClosure *f = clCvalue(fi);
@@ -1286,9 +1285,10 @@ LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
12861285

12871286
LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
12881287
int fidx2, int n2) {
1289-
LClosure *f1;
1290-
UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
1291-
UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
1288+
UpVal **up1 = getupvalref(L, fidx1, n1);
1289+
UpVal **up2 = getupvalref(L, fidx2, n2);
1290+
if (*up1 == *up2)
1291+
return;
12921292
luaC_upvdeccount(L, *up1);
12931293
*up1 = *up2;
12941294
(*up1)->refcount++;

‎src/lua/lauxlib.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,8 +1011,13 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
10111011
free(ptr);
10121012
return NULL;
10131013
}
1014-
else
1015-
return realloc(ptr, nsize);
1014+
else { /* cannot fail when shrinking a block */
1015+
void *newptr = realloc(ptr, nsize);
1016+
if (newptr == NULL && ptr != NULL && nsize <= osize)
1017+
return ptr; /* keep the original block */
1018+
else /* no fail or not shrinking */
1019+
return newptr; /* use the new block */
1020+
}
10161021
}
10171022

10181023

‎src/lua/lbaselib.c

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@
2020
#include "lauxlib.h"
2121
#include "lualib.h"
2222

23-
/*
24-
static int luaB_print (lua_State *L) {
25-
int n = lua_gettop(L); // number of arguments
26-
int i;
27-
lua_getglobal(L, "tostring");
28-
for (i=1; i<=n; i++) {
29-
const char *s;
30-
size_t l;
31-
lua_pushvalue(L, -1); // function to be called
32-
lua_pushvalue(L, i); // value to print
33-
lua_call(L, 1, 1);
34-
s = lua_tolstring(L, -1, &l); // get result
35-
if (s == NULL)
36-
return luaL_error(L, "'tostring' must return a string to 'print'");
37-
if (i>1) lua_writestring("\t", 1);
38-
lua_writestring(s, l);
39-
lua_pop(L, 1); // pop result
40-
}
41-
lua_writeline();
42-
return 0;
43-
}
44-
*/
23+
24+
// static int luaB_print (lua_State *L) {
25+
// int n = lua_gettop(L); /* number of arguments */
26+
// int i;
27+
// lua_getglobal(L, "tostring");
28+
// for (i=1; i<=n; i++) {
29+
// const char *s;
30+
// size_t l;
31+
// lua_pushvalue(L, -1); /* function to be called */
32+
// lua_pushvalue(L, i); /* value to print */
33+
// lua_call(L, 1, 1);
34+
// s = lua_tolstring(L, -1, &l); /* get result */
35+
// if (s == NULL)
36+
// return luaL_error(L, "'tostring' must return a string to 'print'");
37+
// if (i>1) lua_writestring("\t", 1);
38+
// lua_writestring(s, l);
39+
// lua_pop(L, 1); /* pop result */
40+
// }
41+
// lua_writeline();
42+
// return 0;
43+
// }
44+
4545

4646
#define SPACECHARS " \f\n\r\t\v"
4747

@@ -283,15 +283,15 @@ static int load_aux (lua_State *L, int status, int envidx) {
283283
}
284284
}
285285

286-
/*
287-
static int luaB_loadfile (lua_State *L) {
288-
const char *fname = luaL_optstring(L, 1, NULL);
289-
const char *mode = luaL_optstring(L, 2, NULL);
290-
int env = (!lua_isnone(L, 3) ? 3 : 0); // 'env' index or 0 if no 'env'
291-
int status = luaL_loadfilex(L, fname, mode);
292-
return load_aux(L, status, env);
293-
}
294-
*/
286+
287+
// static int luaB_loadfile (lua_State *L) {
288+
// const char *fname = luaL_optstring(L, 1, NULL);
289+
// const char *mode = luaL_optstring(L, 2, NULL);
290+
// int env = (!lua_isnone(L, 3) ? 3 : 0); /* 'env' index or 0 if no 'env' */
291+
// int status = luaL_loadfilex(L, fname, mode);
292+
// return load_aux(L, status, env);
293+
// }
294+
295295

296296
/*
297297
** {======================================================
@@ -352,22 +352,22 @@ static int luaB_load (lua_State *L) {
352352

353353
/* }====================================================== */
354354

355-
/*
356-
static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
357-
(void)d1; (void)d2; // only to match 'lua_Kfunction' prototype
358-
return lua_gettop(L) - 1;
359-
}
360355

356+
// static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
357+
// (void)d1; (void)d2; /* only to match 'lua_Kfunction' prototype */
358+
// return lua_gettop(L) - 1;
359+
// }
360+
361+
362+
// static int luaB_dofile (lua_State *L) {
363+
// const char *fname = luaL_optstring(L, 1, NULL);
364+
// lua_settop(L, 1);
365+
// if (luaL_loadfile(L, fname) != LUA_OK)
366+
// return lua_error(L);
367+
// lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
368+
// return dofilecont(L, 0, 0);
369+
// }
361370

362-
static int luaB_dofile (lua_State *L) {
363-
const char *fname = luaL_optstring(L, 1, NULL);
364-
lua_settop(L, 1);
365-
if (luaL_loadfile(L, fname) != LUA_OK)
366-
return lua_error(L);
367-
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
368-
return dofilecont(L, 0, 0);
369-
}
370-
*/
371371

372372
static int luaB_assert (lua_State *L) {
373373
if (lua_toboolean(L, 1)) /* condition is true? */
@@ -453,19 +453,19 @@ static int luaB_tostring (lua_State *L) {
453453
static const luaL_Reg base_funcs[] = {
454454
{"assert", luaB_assert},
455455
{"collectgarbage", luaB_collectgarbage},
456-
// {"dofile", luaB_dofile},
456+
// {"dofile", luaB_dofile},
457457
{"error", luaB_error},
458458
{"getmetatable", luaB_getmetatable},
459459
{"ipairs", luaB_ipairs},
460-
// {"loadfile", luaB_loadfile},
460+
// {"loadfile", luaB_loadfile},
461461
{"load", luaB_load},
462462
#if defined(LUA_COMPAT_LOADSTRING)
463463
{"loadstring", luaB_load},
464464
#endif
465465
{"next", luaB_next},
466466
{"pairs", luaB_pairs},
467467
{"pcall", luaB_pcall},
468-
// {"print", luaB_print},
468+
// {"print", luaB_print},
469469
{"rawequal", luaB_rawequal},
470470
{"rawlen", luaB_rawlen},
471471
{"rawget", luaB_rawget},

‎src/lua/lcode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ static void codecomp (FuncState *fs, BinOpr opr, expdesc *e1, expdesc *e2) {
10611061

10621062

10631063
/*
1064-
** Aplly prefix operation 'op' to expression 'e'.
1064+
** Apply prefix operation 'op' to expression 'e'.
10651065
*/
10661066
void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) {
10671067
static const expdesc ef = {VKINT, {0}, NO_JUMP, NO_JUMP};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /