Hardware Design: SIE
Sign in or create your account | Project List | Help
Hardware Design: SIE Git Source Tree
Root/
1 | #include <jz47xx_gpio.h> |
2 | #include <jz47xx_mmap.h> |
3 | |
4 | #include "lua.h" |
5 | #include "lualib.h" |
6 | #include "lauxlib.h" |
7 | |
8 | const char *metaname = "mine.JZ_PIO"; // associated with userdata of type Point* |
9 | |
10 | static int jz_gpio_as_output_wrap(lua_State *L) { |
11 | JZ_PIO *pio = (JZ_PIO *)lua_touserdata(L,1); |
12 | luaL_checkudata(L, 1, metaname); // check argument type |
13 | int pin = luaL_checkint(L, 2); |
14 | lua_pushnumber(L, (lua_Number)jz_gpio_as_output(pio, pin)); |
15 | return 1; |
16 | } |
17 | |
18 | static int jz_gpio_set_pin_wrap(lua_State *L) { |
19 | JZ_PIO *pio = luaL_checkudata(L, 1, metaname); // check argument type |
20 | int pin = luaL_checkint(L, 2); |
21 | lua_pushnumber(L, (lua_Number)jz_gpio_set_pin(pio, pin)); |
22 | return 1; |
23 | } |
24 | |
25 | static int jz_gpio_clear_pin_wrap(lua_State *L) { |
26 | JZ_PIO *pio = luaL_checkudata(L, 1, metaname); // check argument type |
27 | int pin = luaL_checkint(L, 2); |
28 | lua_pushnumber(L, (lua_Number)jz_gpio_clear_pin(pio, pin)); |
29 | return 1; |
30 | } |
31 | |
32 | |
33 | |
34 | static int open_port_wrapper(lua_State *L) { // get Lua to allocate an initialize a Point* |
35 | int port = luaL_checkint(L, 1); |
36 | //create user data and associate metable with it |
37 | JZ_PIO *pio = jz_gpio_map (port); |
38 | lua_pushlightuserdata(L,pio); |
39 | luaL_getmetatable(L, metaname); |
40 | lua_setmetatable(L, -2); |
41 | return 1; |
42 | } |
43 | |
44 | static const struct luaL_reg functions[] = { |
45 | {"gpio_as_output", jz_gpio_as_output_wrap}, |
46 | {"set_pin", jz_gpio_set_pin_wrap}, |
47 | {"clear_pin", jz_gpio_clear_pin_wrap}, |
48 | {"open_port", open_port_wrapper}, |
49 | { NULL, NULL} |
50 | }; |
51 | |
52 | //This is the init function that will be called when you require 'gpio' |
53 | |
54 | int luaopen_gpio(lua_State *L) { |
55 | luaL_newmetatable(L, metaname); |
56 | //pop 1 elements from the statck .. why?? to pop the newmetatable that is useless. |
57 | // |
58 | //lua_pop(L, 1); |
59 | //replace luaL_openlib |
60 | luaL_register(L, "gpio", functions); |
61 | return 1; |
62 | } |
63 |
Branches:
master