-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathlua_periphery.c
109 lines (79 loc) · 2.48 KB
/
lua_periphery.c
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
/*
* lua-periphery by vsergeev
* https://github.com/vsergeev/lua-periphery
* License: MIT
*/
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <time.h>
#include <unistd.h>
#include "lua_periphery.h"
#include "lua_compat.h"
LUALIB_API int luaopen_periphery_gpio(lua_State *L);
LUALIB_API int luaopen_periphery_led(lua_State *L);
LUALIB_API int luaopen_periphery_pwm(lua_State *L);
LUALIB_API int luaopen_periphery_spi(lua_State *L);
LUALIB_API int luaopen_periphery_mmio(lua_State *L);
LUALIB_API int luaopen_periphery_i2c(lua_State *L);
LUALIB_API int luaopen_periphery_serial(lua_State *L);
static int periphery_error_tostring(lua_State *L) {
lua_getfield(L, -1, "message");
return 1;
}
static int periphery_sleep(lua_State *L) {
unsigned int duration;
duration = luaL_checkunsigned(L, 1);
sleep(duration);
return 0;
}
static int periphery_sleep_ms(lua_State *L) {
unsigned int duration;
struct timespec ts;
duration = luaL_checkunsigned(L, 1);
ts.tv_sec = duration / 1000;
ts.tv_nsec = (duration - ts.tv_sec*1000)*1000000;
nanosleep(&ts, NULL);
return 0;
}
static int periphery_sleep_us(lua_State *L) {
unsigned int duration;
struct timespec ts;
duration = luaL_checkunsigned(L, 1);
ts.tv_sec = duration / 1000000;
ts.tv_nsec = (duration - ts.tv_sec*1000000)*1000;
nanosleep(&ts, NULL);
return 0;
}
LUALIB_API int luaopen_periphery(lua_State *L) {
/* Create error metatable with __tostring */
luaL_newmetatable(L, "periphery.error");
lua_pushcclosure(L, periphery_error_tostring, 0);
lua_setfield(L, -2, "__tostring");
lua_pop(L, 1);
/* Create table of sub-modules */
lua_newtable(L);
luaopen_periphery_gpio(L);
lua_setfield(L, -2, "GPIO");
luaopen_periphery_led(L);
lua_setfield(L, -2, "LED");
luaopen_periphery_pwm(L);
lua_setfield(L, -2, "PWM");
luaopen_periphery_spi(L);
lua_setfield(L, -2, "SPI");
luaopen_periphery_i2c(L);
lua_setfield(L, -2, "I2C");
luaopen_periphery_serial(L);
lua_setfield(L, -2, "Serial");
luaopen_periphery_mmio(L);
lua_setfield(L, -2, "MMIO");
lua_pushstring(L, LUA_PERIPHERY_VERSION);
lua_setfield(L, -2, "version");
lua_pushcclosure(L, periphery_sleep, 0);
lua_setfield(L, -2, "sleep");
lua_pushcclosure(L, periphery_sleep_ms, 0);
lua_setfield(L, -2, "sleep_ms");
lua_pushcclosure(L, periphery_sleep_us, 0);
lua_setfield(L, -2, "sleep_us");
return 1;
}