-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgpio.c
80 lines (61 loc) · 1.74 KB
/
gpio.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
#include "Arduino.h"
#include "wiring_digital.h"
#include "lua.h"
#include "lauxlib.h"
int gpio_mode(lua_State *L)
{
int pin = luaL_checkinteger(L, 1);
int mode = luaL_checkinteger(L, 2);
pinMode(pin, mode);
return 0;
}
int gpio_read(lua_State *L)
{
int pin = luaL_checkinteger(L, 1);
lua_pushnumber(L, digitalRead(pin));
return 1;
}
int gpio_write(lua_State *L)
{
int pin = luaL_checkinteger(L, 1);
int value = luaL_checkinteger(L, 2);
digitalWrite(pin, value);
return 0;
}
#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 0
#include "lrodefs.h"
#define MOD_REG_NUMBER(L, name, value) lua_pushnumber(L, value); \
lua_setfield(L, -2, name);
const LUA_REG_TYPE gpio_map[] =
{
{LSTRKEY("mode"), LFUNCVAL(gpio_mode)},
{LSTRKEY("read"), LFUNCVAL(gpio_read)},
{LSTRKEY("write"), LFUNCVAL(gpio_write)},
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "OUTPUT" ), LNUMVAL( OUTPUT ) },
{ LSTRKEY( "INPUT" ), LNUMVAL( INPUT ) },
{ LSTRKEY( "HIGH" ), LNUMVAL( HIGH ) },
{ LSTRKEY( "LOW" ), LNUMVAL( LOW ) },
{ LSTRKEY( "INPUT_PULLUP" ), LNUMVAL( INPUT_PULLUP ) },
#endif
{LNILKEY, LNILVAL}
};
LUALIB_API int luaopen_gpio(lua_State *L)
{
lua_register(L, "pinMode", gpio_mode);
lua_register(L, "digitalRead", gpio_read);
lua_register(L, "digitalWrite", gpio_write);
#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else // #if LUA_OPTIMIZE_MEMORY > 0
luaL_register(L, "gpio", gpio_map);
// Add constants
MOD_REG_NUMBER( L, "OUTPUT", OUTPUT );
MOD_REG_NUMBER( L, "INPUT", INPUT );
MOD_REG_NUMBER( L, "HIGH", HIGH );
MOD_REG_NUMBER( L, "LOW", LOW );
MOD_REG_NUMBER( L, "INPUT_PULLUP", INPUT_PULLUP );
return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}