1 /* $NetBSD: gpio.c,v 1.9 2014/07/19 18:38:34 lneto Exp $ */
4 * Copyright (c) 2011 Marc Balmer <marc@msys.ch>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 /* GPIO interface for Lua */
44 #include <sys/ioctl.h>
46 #define GPIO_METATABLE "GPIO object methods"
48 static __printflike(2, 3) void
49 gpio_error(lua_State
*L
, const char *fmt
, ...)
56 len
= vasprintf(&msg
, fmt
, ap
);
60 lua_pushstring(L
, msg
);
63 lua_pushstring(L
, "vasprintf failed");
68 gpio_open(lua_State
*L
)
72 fd
= lua_newuserdata(L
, sizeof(int));
73 *fd
= open(luaL_checkstring(L
, -2), O_RDWR
);
75 gpio_error(L
, "%s", strerror(errno
));
79 luaL_getmetatable(L
, GPIO_METATABLE
);
80 lua_setmetatable(L
, -2);
85 gpio_close(lua_State
*L
)
89 fd
= luaL_checkudata(L
, 1, GPIO_METATABLE
);
98 gpio_info(lua_State
*L
)
100 struct gpio_info info
;
103 fd
= luaL_checkudata(L
, 1, GPIO_METATABLE
);
104 if (ioctl(*fd
, GPIOINFO
, &info
) == -1)
105 gpio_error(L
, "GPIOINFO");
106 lua_pushinteger(L
, info
.gpio_npins
);
111 gpio_get_pin(lua_State
*L
, int n
, struct gpio_req
*req
)
113 switch (lua_type(L
, n
)) {
115 req
->gp_pin
= (int)lua_tointeger(L
, n
); /* not 1 based! */
118 strlcpy(req
->gp_name
, lua_tostring(L
, n
), sizeof(req
->gp_name
));
121 luaL_argerror(L
, n
, "expected string or integer");
127 gpio_set(lua_State
*L
)
132 fd
= luaL_checkudata(L
, 1, GPIO_METATABLE
);
133 memset(&set
, 0, sizeof(set
));
134 gpio_get_pin(L
, 2, (void *)&set
);
135 set
.gp_flags
= (int)luaL_checkinteger(L
, 3);
136 if (ioctl(*fd
, GPIOSET
, &set
) == -1)
137 gpio_error(L
, "GPIOSET");
142 gpio_unset(lua_State
*L
)
147 fd
= luaL_checkudata(L
, 1, GPIO_METATABLE
);
148 memset(&set
, 0, sizeof(set
));
149 gpio_get_pin(L
, 2, (void *)&set
);
150 if (ioctl(*fd
, GPIOUNSET
, &set
) == -1)
151 gpio_error(L
, "GPIOUNSET");
156 gpio_read(lua_State
*L
)
161 fd
= luaL_checkudata(L
, 1, GPIO_METATABLE
);
162 memset(&req
, 0, sizeof(req
));
163 gpio_get_pin(L
, 2, &req
);
164 if (ioctl(*fd
, GPIOREAD
, &req
) == -1)
165 gpio_error(L
, "GPIOREAD");
166 lua_pushinteger(L
, req
.gp_value
);
172 gpio_write(lua_State
*L
)
177 fd
= luaL_checkudata(L
, 1, GPIO_METATABLE
);
178 val
= (int)luaL_checkinteger(L
, 3);
179 if (val
!= GPIO_PIN_HIGH
&& val
!= GPIO_PIN_LOW
)
180 gpio_error(L
, "%d: invalid value", val
);
181 memset(&req
, 0, sizeof(req
));
182 gpio_get_pin(L
, 2, &req
);
184 if (ioctl(*fd
, GPIOWRITE
, &req
) == -1)
185 gpio_error(L
, "GPIOWRITE");
186 lua_pushinteger(L
, req
.gp_value
);
191 gpio_toggle(lua_State
*L
)
196 fd
= luaL_checkudata(L
, 1, GPIO_METATABLE
);
197 memset(&req
, 0, sizeof(req
));
198 gpio_get_pin(L
, 2, &req
);
199 if (ioctl(*fd
, GPIOTOGGLE
, &req
) == -1)
200 gpio_error(L
, "GPIOTOGGLE");
201 lua_pushinteger(L
, req
.gp_value
);
206 gpio_attach(lua_State
*L
)
208 struct gpio_attach attach
;
211 fd
= luaL_checkudata(L
, 1, GPIO_METATABLE
);
212 memset(&attach
, 0, sizeof(attach
));
213 strlcpy(attach
.ga_dvname
, luaL_checkstring(L
, 2),
214 sizeof(attach
.ga_dvname
));
215 attach
.ga_offset
= (int)luaL_checkinteger(L
, 3);
216 attach
.ga_mask
= (int)luaL_checkinteger(L
, 4);
217 if (lua_gettop(L
) > 4)
218 attach
.ga_flags
= (int)luaL_checkinteger(L
, 5);
222 if (ioctl(*fd
, GPIOATTACH
, &attach
) == -1)
223 gpio_error(L
, "GPIOATTACH");
232 static const struct constant gpio_constant
[] = {
233 /* GPIO pin states */
234 { "PIN_LOW", GPIO_PIN_LOW
},
235 { "PIN_HIGH", GPIO_PIN_HIGH
},
237 /* GPIO pin configuration flags */
238 { "PIN_INPUT", GPIO_PIN_INPUT
},
239 { "PIN_OUTPUT", GPIO_PIN_OUTPUT
},
240 { "PIN_INOUT", GPIO_PIN_INOUT
},
241 { "PIN_OPENDRAIN", GPIO_PIN_OPENDRAIN
},
242 { "PIN_PUSHPULL", GPIO_PIN_PUSHPULL
},
243 { "PIN_TRISTATE", GPIO_PIN_TRISTATE
},
244 { "PIN_PULLUP", GPIO_PIN_PULLUP
},
245 { "PIN_PULLDOWN", GPIO_PIN_PULLDOWN
},
246 { "PIN_INVIN", GPIO_PIN_INVIN
},
247 { "PIN_INVOUT", GPIO_PIN_INVOUT
},
248 { "PIN_USER", GPIO_PIN_USER
},
249 { "PIN_PULSATE", GPIO_PIN_PULSATE
},
250 { "PIN_SET", GPIO_PIN_SET
},
255 gpio_set_info(lua_State
*L
)
257 lua_pushliteral(L
, "_COPYRIGHT");
258 lua_pushliteral(L
, "Copyright (C) 2011, 2013 Marc Balmer "
261 lua_pushliteral(L
, "_DESCRIPTION");
262 lua_pushliteral(L
, "GPIO interface for Lua");
264 lua_pushliteral(L
, "_VERSION");
265 lua_pushliteral(L
, "gpio 1.0.2");
269 int luaopen_gpio(lua_State
*);
272 luaopen_gpio(lua_State
* L
)
274 static const struct luaL_Reg methods
[] = {
275 { "open", gpio_open
},
278 static const struct luaL_Reg gpio_methods
[] = {
279 { "info", gpio_info
},
280 { "close", gpio_close
},
282 { "unset", gpio_unset
},
283 { "read", gpio_read
},
284 { "write", gpio_write
},
285 { "toggle", gpio_toggle
},
286 { "attach", gpio_attach
},
291 luaL_newlib(L
, methods
);
292 luaL_setfuncs(L
, gpio_methods
, 0);
295 /* The gpio metatable */
296 if (luaL_newmetatable(L
, GPIO_METATABLE
)) {
297 luaL_setfuncs(L
, gpio_methods
, 0);
299 lua_pushliteral(L
, "__gc");
300 lua_pushcfunction(L
, gpio_close
);
303 lua_pushliteral(L
, "__index");
304 lua_pushvalue(L
, -2);
307 lua_pushliteral(L
, "__metatable");
308 lua_pushliteral(L
, "must not access this metatable");
313 for (n
= 0; gpio_constant
[n
].name
!= NULL
; n
++) {
314 lua_pushinteger(L
, gpio_constant
[n
].value
);
315 lua_setfield(L
, -2, gpio_constant
[n
].name
);