-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaManager.cpp
More file actions
269 lines (216 loc) · 6.99 KB
/
LuaManager.cpp
File metadata and controls
269 lines (216 loc) · 6.99 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*-------------------------------------------------------------------------
This source file is a part of mil
For the latest info, see http://www.marrin.org/
Copyright (c) 2025, Chris Marrin
All rights reserved.
-------------------------------------------------------------------------*/
#include "LuaManager.h"
#include "LuaWFS.h"
#include "System.h"
#include "WebFileSystem.h"
#include <cstring>
using namespace mil;
static const char* TAG = "LuaManager";
std::map<uint8_t, std::shared_ptr<LuaManager>> LuaManager::_managers;
std::bitset<LuaManager::MaxIds> LuaManager::_usedIds;
std::mutex LuaManager::_mutex;
void
LuaManager::printHandler(lua_State *L)
{
std::unique_lock<std::mutex> lk(_mutex);
int nargs = lua_gettop(L);
for (int i = 1; i <= nargs; i++) {
const char* s = lua_tostring(L, i);
if (s) {
size_t size = strlen(s);
print(_printCB, s, size);
}
}
}
LuaManager::LuaManager(PrintCB printCB)
: _printCB(printCB)
{
_id = nextId();
System::logI(TAG, "creating manager with id=%d\n", int(_id));
_luaState = luaL_newstate();
luaL_openlibs(_luaState);
luaL_requiref(_luaState, "wfs", luaopen_wfs, 1);
// Set this as a lua global
lua_pushlightuserdata(_luaState, this);
lua_setglobal(_luaState, "__LuaManager__");
}
LuaManager::~LuaManager()
{
System::logI(TAG, "deleting manager with id=%d\n", int(_id));
}
static int printLua(lua_State *L)
{
// Get the LuaManager ptr
lua_getglobal(L, "__LuaManager__");
LuaManager* self = (LuaManager*) lua_touserdata(L, -1);
lua_pop(L, 1);
self->printHandler(L);
return 0;
}
// Access the LEDs from Lua
static int luaInitLED(lua_State* L)
{
lua_Number numLEDs = lua_tonumber(L, -1);
lua_Number pin = lua_tonumber(L, -2);
lua_Number channel = lua_tonumber(L, -3);
System::initLED(channel, pin, numLEDs);
return 0;
}
static int luaSetLED(lua_State* L)
{
lua_Number b = lua_tonumber(L, -1);
lua_Number g = lua_tonumber(L, -2);
lua_Number r = lua_tonumber(L, -3);
lua_Number index = lua_tonumber(L, -4);
lua_Number channel = lua_tonumber(L, -5);
System::setLED(channel, index, r, g, b);
return 0;
}
static int luaSetLEDs(lua_State* L)
{
lua_Number b = lua_tonumber(L, -1);
lua_Number g = lua_tonumber(L, -2);
lua_Number r = lua_tonumber(L, -3);
lua_Number count = lua_tonumber(L, -4);
lua_Number startIndex = lua_tonumber(L, -5);
lua_Number channel = lua_tonumber(L, -6);
System::setLEDs(channel, startIndex, count, r, g, b);
return 0;
}
static int luaHSVToRGB(lua_State* L)
{
lua_Number v = std::max(0, std::min(255, int(lua_tonumber(L, -1))));
lua_Number s = std::max(0, std::min(255, int(lua_tonumber(L, -2))));
lua_Number h = std::max(0, std::min(255, int(lua_tonumber(L, -3))));
uint8_t r, g, b;
Graphics::hsvToRGB(r, g, b, h * 256, s, v);
lua_pushnumber(L, r);
lua_pushnumber(L, g);
lua_pushnumber(L, b);
return 3;
}
static int luaRefreshLEDs(lua_State* L)
{
lua_Number channel = lua_tonumber(L, -1);
System::refreshLEDs(channel);
return 0;
}
static int luaDelay(lua_State* L)
{
lua_Number ms = lua_tonumber(L, -1);
System::delay(ms);
return 0;
}
static int luaMillis(lua_State* L)
{
lua_pushnumber(L, System::millis());
return 1;
}
int8_t
LuaManager::execute(const std::string& filename, int cpl, std::vector<std::string> args,
std::function<void(const char*, size_t)> printCB)
{
std::shared_ptr<LuaManager> mgr = std::make_shared<LuaManager>(printCB);
mgr->_command = filename;
// Set the incoming cpl as a global
lua_pushinteger(mgr->_luaState, cpl);
lua_setglobal(mgr->_luaState, "__cpl__");
// Set the require path
std::string realRequirePath = WebFileSystem::realPath("/sys/?.lua");
luaL_dostring(mgr->_luaState, (std::string("package.path = \"") + realRequirePath + "\"").c_str());
// add a print method
lua_register(mgr->_luaState, "__print__", printLua);
// Set the LED and delay functions
lua_pushcfunction(mgr->_luaState, luaInitLED);
lua_setglobal(mgr->_luaState, "initLED");
lua_pushcfunction(mgr->_luaState, luaSetLED);
lua_setglobal(mgr->_luaState, "setLED");
lua_pushcfunction(mgr->_luaState, luaSetLEDs);
lua_setglobal(mgr->_luaState, "setLEDs");
lua_pushcfunction(mgr->_luaState, luaHSVToRGB);
lua_setglobal(mgr->_luaState, "hsvToRGB");
lua_pushcfunction(mgr->_luaState, luaRefreshLEDs);
lua_setglobal(mgr->_luaState, "refreshLEDs");
lua_pushcfunction(mgr->_luaState, luaDelay);
lua_setglobal(mgr->_luaState, "delay");
lua_pushcfunction(mgr->_luaState, luaMillis);
lua_setglobal(mgr->_luaState, "millis");
// Set an 'arg' global with the args
lua_createtable(mgr->_luaState, int(args.size()), 0);
int i = 1;
for (const auto& it : args) {
lua_pushnumber(mgr->_luaState, i);
lua_pushstring(mgr->_luaState, it.c_str());
lua_settable(mgr->_luaState, -3);
++i;
}
lua_setglobal(mgr->_luaState, "arg");
_managers.emplace(mgr->_id, mgr);
mgr->_thread = std::thread([mgr, filename]() { mgr->commandThread(filename); });
mgr->_thread.detach();
return mgr->_id;
}
void
LuaManager::terminate(int8_t id)
{
std::unique_lock<std::mutex> lk(_mutex);
const auto& it = _managers.find(id);
if (it == _managers.end()) {
// Uh oh. Manager is gone. For now just leave
System::logE(TAG, "termination failed, id %d does not exist", int(id));
return;
}
System::logI(TAG, "terminating Lua program with id %d", int(id));
lua_stop(it->second->_luaState);
}
std::shared_ptr<LuaManager>
LuaManager::getManager(uint8_t id)
{
const auto& it = _managers.find(id);
if (it == _managers.end()) {
// Uh oh. Manager is gone. For now just leave
return nullptr;
}
return it->second;
}
void
LuaManager::waitForFinish()
{
std::unique_lock<std::mutex> lk(_mutex);
if (_status != Status::Done) {
_statusCond.wait(lk);
}
}
void
LuaManager::commandThread(const std::string& filename)
{
{
std::unique_lock<std::mutex> lk(_mutex);
_status = Status::Running;
}
std::string realPath = WebFileSystem::realPath(filename.c_str());
if (luaL_dofile(_luaState, realPath.c_str()) != LUA_OK) {
std::unique_lock<std::mutex> lk(_mutex);
_status = Status::Done;
_errorString = lua_tostring(_luaState, -1);
std::string err = " Lua file '" + command() + "' failed to run: " + _errorString + "\n";
print(_printCB, err.c_str(), err.length());
} else {
std::unique_lock<std::mutex> lk(_mutex);
_status = Status::Done;
_errorString = "";
}
_statusCond.notify_all();
std::unique_lock<std::mutex> lk(_mutex);
clearId(_id);
lua_close(_luaState);
const auto& it = _managers.find(_id);
if (it != _managers.end()) {
_managers.erase(it);
}
}