forked from Sasszem/ESP8266-Arduino-Lua
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLuaWrapper.cpp
More file actions
86 lines (75 loc) · 2.35 KB
/
LuaWrapper.cpp
File metadata and controls
86 lines (75 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "LuaWrapper.h"
extern "C" {
static int lua_wrapper_pinMode(lua_State *lua) {
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 *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());
return 1;
}
}
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);
lua_register(_state, "print", lua_wrapper_print);
lua_register(_state, "millis", lua_wrapper_millis);
}
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;
}