#include #include #include "lua.h" #include "lualib.h" #include "lauxlib.h" const char *metaname = "mine.JZ_PIO"; // associated with userdata of type Point* static int jz_gpio_as_output_wrap(lua_State *L) { JZ_PIO *pio = (JZ_PIO *)lua_touserdata(L,1); luaL_checkudata(L, 1, metaname); // check argument type int pin = luaL_checkint(L, 2); lua_pushnumber(L, (lua_Number)jz_gpio_as_output(pio, pin)); return 1; } static int jz_gpio_set_pin_wrap(lua_State *L) { JZ_PIO *pio = luaL_checkudata(L, 1, metaname); // check argument type int pin = luaL_checkint(L, 2); lua_pushnumber(L, (lua_Number)jz_gpio_set_pin(pio, pin)); return 1; } static int jz_gpio_clear_pin_wrap(lua_State *L) { JZ_PIO *pio = luaL_checkudata(L, 1, metaname); // check argument type int pin = luaL_checkint(L, 2); lua_pushnumber(L, (lua_Number)jz_gpio_clear_pin(pio, pin)); return 1; } static int point_new_wrapper(lua_State *L) { // get Lua to allocate an initialize a Point* int port = luaL_checkint(L, 1); //create user data and associate metable with it JZ_PIO *pio = jz_gpio_map (port); lua_pushlightuserdata(L,pio); luaL_getmetatable(L, metaname); lua_setmetatable(L, -2); return 1; } static const struct luaL_reg functions[] = { {"jz_gpio_as_output", jz_gpio_as_output_wrap}, {"jz_gpio_set_pin", jz_gpio_set_pin_wrap}, {"jz_gpio_clear_pin", jz_gpio_clear_pin_wrap}, {"point_new", point_new_wrapper}, { NULL, NULL} }; //This is the init function that will be called when you require 'mylib' int luaopen_sram_gpio_lib(lua_State *L) { luaL_newmetatable(L, metaname); //pop 1 elements from the statck .. why?? to pop the newmetatable that is useless. // //lua_pop(L, 1); //replace luaL_openlib luaL_register(L, "sram_gpio_lib", functions); return 1; }