-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlua_api.cpp
370 lines (308 loc) · 6.89 KB
/
lua_api.cpp
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "lua_api.h"
#include "config.h"
bool hold = false;
#ifdef NO_INITIAL_DELAY
bool first_key = false;
#else
bool first_key = true;
#endif
#define FIRST_KEY_CHECK { \
if (first_key) { \
delay(TYPING_DELAY); \
first_key = false; \
} \
while (hold) delay(1); \
}
int l_wait(lua_State *L) {
while (hold) delay(1);
delay(luaL_checknumber(L, 1));
return 0;
}
// KEYBOARD
int l_type(lua_State *L) {
FIRST_KEY_CHECK;
Keyboard.print(luaL_checkstring(L, 1));
return 0;
}
int l_typeln(lua_State *L) {
FIRST_KEY_CHECK;
Keyboard.println(luaL_checkstring(L, 1));
return 0;
}
int l_press(lua_State *L) {
FIRST_KEY_CHECK;
int key = luaL_checkint(L, 1);
if (key < 0)
return 0;
Keyboard.press(key);
return 0;
}
int l_release(lua_State *L) {
FIRST_KEY_CHECK;
int n = lua_gettop(L);
if (n == 0) {
Keyboard.releaseAll();
return 0;
}
int key = luaL_checkint(L, 1);
if (key < 0)
return 0;
Keyboard.release(key);
return 0;
}
int l_write(lua_State *L) {
FIRST_KEY_CHECK;
Keyboard.write(luaL_checkstring(L, 1)[0]);
return 0;
}
int l_set_speed(lua_State *L) {
double speed = luaL_checknumber(L, 1);
if (speed <= 0) {
Keyboard.m_delay = 0;
return 0;
}
Keyboard.m_delay = speed / 1000;
return 0;
}
void l_load_keys(lua_State *L) {
luaL_dostring(L,
"keys = {\n"
" ctrl = 0x80,\n"
" shift = 0x81,\n"
" alt = 0x82,\n"
" gui = 0x83,\n"
" menu = 0xed,\n"
"\n"
" left_ctrl = 0x80,\n"
" left_shift = 0x81,\n"
" left_alt = 0x82,\n"
" left_gui = 0x83,\n"
" right_ctrl = 0x84,\n"
" right_shift = 0x85,\n"
" right_alt = 0x86,\n"
" right_gui = 0x87,\n"
" \n"
" up_arrow = 0xda,\n"
" down_arrow = 0xd9,\n"
" left_arrow = 0xd8,\n"
" right_arrow = 0xd7,\n"
" up = 0xda,\n"
" down = 0xd9,\n"
" left = 0xd8,\n"
" right = 0xd7,\n"
"\n"
" backspace = 0xb2,\n"
" tab = 0xb3,\n"
" return = 0xb0,\n"
" enter = 0xb0,\n"
" esc = 0xb1,\n"
" escape = 0xb1,\n"
" insert = 0xd1,\n"
" delete = 0xd4,\n"
" page_up = 0xd3,\n"
" page_down = 0xd6,\n"
" home = 0xd2,\n"
" end = 0xd5,\n"
" caps_lock = 0xc1,\n"
"\n"
" f1 = 0xc2,\n"
" f2 = 0xc3,\n"
" f3 = 0xc4,\n"
" f4 = 0xc5,\n"
" f5 = 0xc6,\n"
" f6 = 0xc7,\n"
" f7 = 0xc8,\n"
" f8 = 0xc9,\n"
" f9 = 0xca,\n"
" f10 = 0xcb,\n"
" f11 = 0xcc,\n"
" f12 = 0xcd,\n"
" f13 = 0xf0,\n"
" f14 = 0xf1,\n"
" f15 = 0xf2,\n"
" f16 = 0xf3,\n"
" f17 = 0xf4,\n"
" f18 = 0xf5,\n"
" f19 = 0xf6,\n"
" f20 = 0xf7,\n"
" f21 = 0xf8,\n"
" f22 = 0xf9,\n"
" f23 = 0xfa,\n"
" f24 = 0xfb,\n"
"}\n"
"");
}
// MOUSE
int l_mouse_move(lua_State *L) {
FIRST_KEY_CHECK;
int n = lua_gettop(L);
int x, y, wheel=0;
x = luaL_checknumber(L, 1);
y = luaL_checknumber(L, 2);
if (n >= 3)
wheel = luaL_checknumber(L, 3);
Mouse.move(x, y, wheel);
return 0;
}
int l_mouse_click(lua_State *L) {
FIRST_KEY_CHECK;
int n = lua_gettop(L);
uint8_t button = MOUSE_LEFT;
if (n >= 1)
button = luaL_checknumber(L, 1);
Mouse.click(button);
return 0;
}
int l_mouse_press(lua_State *L) {
FIRST_KEY_CHECK;
int n = lua_gettop(L);
uint8_t button = MOUSE_LEFT;
if (n >= 1)
button = luaL_checknumber(L, 1);
Mouse.press(button);
return 0;
}
int l_mouse_release(lua_State *L) {
FIRST_KEY_CHECK;
int n = lua_gettop(L);
uint8_t button = MOUSE_LEFT;
if (n >= 1)
button = luaL_checknumber(L, 1);
Mouse.release(button);
return 0;
}
// I/O
int l_set_led(lua_State *L) {
while (hold) delay(1);
digitalWrite(BUILT_IN_LED_PIN, lua_toboolean(L, 1) ? HIGH : LOW);
return 0;
}
int l_set_led_external(lua_State *L) {
while (hold) delay(1);
digitalWrite(EXTERNAL_LED_PIN, lua_toboolean(L, 1) ? HIGH : LOW);
return 0;
}
int l_pin_mode(lua_State *L) {
while (hold) delay(1);
if (lua_gettop(L) < 2)
return 0;
pinMode(luaL_checknumber(L, 1), luaL_checknumber(L, 2));
return 0;
}
int l_analog_write(lua_State *L) {
while (hold) delay(1);
if (lua_gettop(L) < 2)
return 0;
analogWrite(luaL_checknumber(L, 1), luaL_checknumber(L, 2));
return 0;
}
int l_digital_write(lua_State *L) {
while (hold) delay(1);
if (lua_gettop(L) < 2)
return 0;
digitalWrite(luaL_checknumber(L, 1), luaL_checknumber(L, 2));
return 0;
}
int l_digital_read(lua_State *L) {
while (hold) delay(1);
if (lua_gettop(L) < 1)
return 0;
lua_pushboolean(L, digitalRead(luaL_checknumber(L, 1)));
return 1;
}
int l_analog_read(lua_State *L) {
while (hold) delay(1);
if (lua_gettop(L) < 1)
return 0;
lua_pushnumber(L, analogRead(luaL_checknumber(L, 1)));
return 1;
}
void l_init(lua_State *L) {
lua_pushcfunction(L, l_set_led);
lua_setglobal(L, "setLed");
lua_pushcfunction(L, l_set_led_external);
lua_setglobal(L, "setLedExternal");
lua_pushcfunction(L, l_wait);
lua_setglobal(L, "wait");
lua_pushcfunction(L, l_type);
lua_setglobal(L, "type");
lua_pushcfunction(L, l_typeln);
lua_setglobal(L, "typeln");
lua_pushcfunction(L, l_write);
lua_setglobal(L, "write");
lua_pushcfunction(L, l_press);
lua_setglobal(L, "press");
lua_pushcfunction(L, l_release);
lua_setglobal(L, "release");
lua_pushcfunction(L, l_mouse_move);
lua_setglobal(L, "mouseMove");
lua_pushcfunction(L, l_mouse_click);
lua_setglobal(L, "mouseClick");
lua_pushcfunction(L, l_mouse_press);
lua_setglobal(L, "mousePress");
lua_pushcfunction(L, l_mouse_release);
lua_setglobal(L, "mouseRelease");
lua_pushcfunction(L, l_set_speed);
lua_setglobal(L, "setSpeed");
// Aliases
lua_getglobal(L, "type");
lua_setglobal(L, "string");
lua_getglobal(L, "wait");
lua_setglobal(L, "sleep");
lua_getglobal(L, "wait");
lua_setglobal(L, "delay");
l_load_keys(L);
lua_pushcfunction(L, l_pin_mode);
lua_setglobal(L, "pinMode");
lua_pushcfunction(L, l_analog_write);
lua_setglobal(L, "analogWrite");
lua_pushcfunction(L, l_digital_write);
lua_setglobal(L, "digitalWrite");
lua_pushcfunction(L, l_digital_read);
lua_setglobal(L, "digitalRead");
lua_pushcfunction(L, l_analog_read);
lua_setglobal(L, "analogRead");
luaL_dostring(L, "\
INPUT = 0\n\
OUTPUT = 1\n\
INPUT_PULLUP = 2\n\
\n");
luaL_dostring(L, "\
LOW = 0\n\
HIGH = 1\n\
\n");
luaL_dostring(L, "\
MOUSE_LEFT = 1\n\
MOUSE_RIGHT = 2\n\
MOUSE_MIDDLE = 4\n\
MOUSE_ALL = 7\n\
\n");
luaL_dostring(L, "function gui(char)\n\
press(131)\n\
write(char)\n\
release(131)\n\
end");
lua_getglobal(L, "gui");
lua_setglobal(L, "win");
lua_getglobal(L, "gui");
lua_setglobal(L, "meta");
luaL_dostring(L, "function alt(char)\n\
press(130)\n\
write(char)\n\
release(130)\n\
end");
luaL_dostring(L, "function ctrl(char)\n\
press(128)\n\
write(char)\n\
release(128)\n\
end");
luaL_dostring(L, "function blinkLed(ratem)\n\
rate = rate or 1000\n\
while true do\n\
setLed(true)\n\
wait(rate)\n\
setLed(false)\n\
wait(rate)\n\
end\n\
end\n");
}