|
| 1 | +# Arduino Lua |
| 2 | + |
| 3 | +本Arduino扩展库为ESP8266/ESP32/ArduinoUnoR4提供 [lua](https://www.lua.org/) 5.3.6 ([正式版](https://www.lua.org/ftp/lua-5.3.6.tar.gz)) 的脚本运行引擎。 使用该扩展库,可以在Arduino上动态运行Lua代码,而不必重新编译和烧录固件。 |
| 4 | + |
| 5 | +除了 Lua 5.3.6 核心功能以外,还包含了如下的 Lua 标准库: |
| 6 | + |
| 7 | +- base (print(), tostring(), tonumber(), etc.) |
| 8 | +- math |
| 9 | +- table (insert(), sort(), remove(), ...) |
| 10 | +- string (len(), match(), ...) |
| 11 | + |
| 12 | +## 示例: ExecuteScriptFromSerial.ino |
| 13 | + |
| 14 | +安装本扩展后,在 *File* 菜单中的 *Examples* 下的 *Arduino-Lua* 中 会提供部分示例。示例中包含 **ExecuteScriptFromSerial** ,可以通过串口输入 Lua 脚本并执行。例如,如下的标准 Arduino 函数,可以被绑定从而在 lua 脚本中使用: |
| 15 | + |
| 16 | +- pinMode() |
| 17 | +- digitalWrite() |
| 18 | +- delay() |
| 19 | +- millis() |
| 20 | +- print() *(only builtin binding)* |
| 21 | + |
| 22 | +``` |
| 23 | +# Enter the lua script and press Control-D when finished: |
| 24 | +# 输入如下的 lua脚本,然后按 Ctrl+D 运行: |
| 25 | +print("My first test!") |
| 26 | +# Executing script: |
| 27 | +# 运行脚本: |
| 28 | +My first test! |
| 29 | + |
| 30 | + |
| 31 | +# Enter the lua script and press Control-D when finished- |
| 32 | +print("Current uptime: " .. millis()) |
| 33 | +# Executing script: |
| 34 | +Current uptime: 159926.0 |
| 35 | + |
| 36 | + |
| 37 | +# Enter the lua script and press Control-D when finished: |
| 38 | +print("Hello world!") |
| 39 | +# Executing script: |
| 40 | +Hello world! |
| 41 | + |
| 42 | + |
| 43 | +# Enter the lua script and press Control-D when finished: |
| 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 | +# Executing script: |
| 57 | +LED on |
| 58 | +LED off |
| 59 | +``` |
| 60 | +## 资源使用 (ExecuteScriptFromSerial.ino) |
| 61 | + |
| 62 | +**ESP8266:** |
| 63 | +Sketch uses 327776 bytes (31%) of program storage space. Maximum is 1044464 bytes. |
| 64 | +Global variables use 31276 bytes (38%) of dynamic memory, leaving 50644 bytes for local variables. Maximum is 81920 bytes. |
| 65 | + |
| 66 | +**ESP32:** |
| 67 | +Sketch uses 310749 bytes (23%) of program storage space. Maximum is 1310720 bytes. |
| 68 | +Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes. |
| 69 | + |
| 70 | +**Arduino Uno R4:** |
| 71 | +Sketch uses 133756 bytes (51%) of program storage space. Maximum is 262144 bytes. |
| 72 | +Global variables use 2836 bytes (8%) of dynamic memory, leaving 29932 bytes for local variables. Maximum is 32768 bytes. |
| 73 | + |
| 74 | +## 示例: HelloWorld.ino |
| 75 | +``` |
| 76 | +#include <LuaWrapper.h> |
| 77 | + |
| 78 | +LuaWrapper lua; |
| 79 | + |
| 80 | +void setup() { |
| 81 | + Serial.begin(115200); |
| 82 | + String script = String("print('Hello world!')"); |
| 83 | + Serial.println(lua.Lua_dostring(&script)); |
| 84 | +} |
| 85 | + |
| 86 | +void loop() { |
| 87 | + |
| 88 | +} |
| 89 | +``` |
| 90 | +## 资源使用 (HelloWorld.ino) |
| 91 | + |
| 92 | +**ESP8266:** |
| 93 | +Sketch uses 365276 bytes (34%) of program storage space. Maximum is 1044464 bytes. |
| 94 | +Global variables use 34712 bytes (42%) of dynamic memory, leaving 47208 bytes for local variables. Maximum is 81920 bytes. |
| 95 | + |
| 96 | +**ESP32:** |
| 97 | +Sketch uses 309913 bytes (23%) of program storage space. Maximum is 1310720 bytes. |
| 98 | +Global variables use 15388 bytes (4%) of dynamic memory, leaving 312292 bytes for local variables. Maximum is 327680 bytes. |
| 99 | + |
| 100 | +## Lua 语言: |
| 101 | +[Lua 5.3 参考手册](https://www.lua.org/manual/5.3/) |
| 102 | + |
0 commit comments