2010-09-07 04:24:53 +03:00
|
|
|
#include <jz47xx_gpio.h>
|
|
|
|
#include <jz47xx_mmap.h>
|
|
|
|
|
|
|
|
#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
|
2010-09-07 04:49:11 +03:00
|
|
|
int pin = luaL_checkint(L, 2);
|
|
|
|
lua_pushnumber(L, (lua_Number)jz_gpio_as_output(pio, pin));
|
2010-09-07 04:24:53 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int jz_gpio_set_pin_wrap(lua_State *L) {
|
|
|
|
JZ_PIO *pio = luaL_checkudata(L, 1, metaname); // check argument type
|
2010-09-07 04:49:11 +03:00
|
|
|
int pin = luaL_checkint(L, 2);
|
|
|
|
lua_pushnumber(L, (lua_Number)jz_gpio_set_pin(pio, pin));
|
2010-09-07 04:24:53 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int jz_gpio_clear_pin_wrap(lua_State *L) {
|
|
|
|
JZ_PIO *pio = luaL_checkudata(L, 1, metaname); // check argument type
|
2010-09-07 04:49:11 +03:00
|
|
|
int pin = luaL_checkint(L, 2);
|
|
|
|
lua_pushnumber(L, (lua_Number)jz_gpio_clear_pin(pio, pin));
|
2010-09-07 04:24:53 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int point_new_wrapper(lua_State *L) { // get Lua to allocate an initialize a Point*
|
2010-09-07 04:49:11 +03:00
|
|
|
int port = luaL_checkint(L, 1);
|
2010-09-07 04:24:53 +03:00
|
|
|
//create user data and associate metable with it
|
2010-09-07 04:49:11 +03:00
|
|
|
JZ_PIO *pio = jz_gpio_map (port);
|
2010-09-07 04:24:53 +03:00
|
|
|
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;
|
|
|
|
}
|