From 848fe4209664c346e21245637ec2a7fc0885a22b Mon Sep 17 00:00:00 2001 From: Firew0lf Date: Sat, 12 Sep 2015 00:15:51 +0200 Subject: [PATCH] Finished the QTM library, Added some text in ctr.c The QTM (Headtracking) is not tested on real hardware, and doesn't work in citra. But should work, maybe. Please update your boot.3dsx to use it without errors. --- source/ctr.c | 2 ++ source/qtm.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/source/ctr.c b/source/ctr.c index 0a226f1..0fe153e 100644 --- a/source/ctr.c +++ b/source/ctr.c @@ -13,6 +13,7 @@ void load_ir_lib(lua_State *L); void load_fs_lib(lua_State *L); void load_httpc_lib(lua_State *L); void load_qtm_lib(lua_State *L); +//void load_cam_lib(lua_State *L); void unload_gfx_lib(lua_State *L); void unload_hid_lib(lua_State *L); @@ -48,6 +49,7 @@ struct { char *name; void (*load)(lua_State *L); void (*unload)(lua_State *L); } { "fs", load_fs_lib, unload_fs_lib }, { "httpc", load_httpc_lib, unload_httpc_lib }, { "qtm", load_qtm_lib, NULL }, +// { "cam", load_cam_lib, NULL }, { NULL, NULL } }; diff --git a/source/qtm.c b/source/qtm.c index 3e0599b..948789e 100644 --- a/source/qtm.c +++ b/source/qtm.c @@ -5,10 +5,20 @@ #include #include -static int qtm_init(lua_State *L) { - qtmInit(); +typedef struct { + qtmHeadtrackingInfo *info; +} qtm_userdata; - return 0; +static int qtm_init(lua_State *L) { + Result ret = qtmInit(); + if (ret) { + lua_pushboolean(L, false); + lua_pushinteger(L, ret); + return 2; + } + + lua_pushboolean(L, true); + return 1; } static int qtm_shutdown(lua_State *L) { @@ -25,18 +35,69 @@ static int qtm_checkInitialized(lua_State *L) { } static int qtm_getHeadtrackingInfo(lua_State *L) { - return 0; + qtm_userdata *data = lua_newuserdata(L, sizeof(*data)); + luaL_getmetatable(L, "LQTM"); + lua_setmetatable(L, -2); + Result ret = qtmGetHeadtrackingInfo(0, data->info); + if (ret) { + lua_pushnil(L); + return 1; + } + return 1; } static int qtm_checkHeadFullyDetected(lua_State *L) { - return 0; + qtm_userdata *info = luaL_checkudata(L, 1, "LQTM"); + lua_pushboolean(L, qtmCheckHeadFullyDetected(info->info)); + return 1; +} + +static int qtm___index(lua_State *L) { + qtm_userdata *info = luaL_checkudata(L, 1, "LQTM"); + lua_Integer index = luaL_checkinteger(L, 2); + index = index - 1; // Lua index begins at 1 + if (index > 3 || index < 0) { + lua_pushnil(L); + lua_pushnil(L); + return 2; + } + + lua_pushnumber(L, info->info->coords0[index].x); + lua_pushnumber(L, info->info->coords0[index].y); + + return 2; } static int qtm_convertCoordToScreen(lua_State *L) { - return 0; + qtm_userdata *info = luaL_checkudata(L, 1, "LQTM"); + lua_Integer index = luaL_checkinteger(L, 2); + index = index - 1; // Lua index begins at 1 + if (index > 3 || index < 0) { + lua_pushnil(L); + lua_pushnil(L); + return 2; + } + float screenWidth = luaL_optnumber(L, 3, 400.0f); + float screenHeight = luaL_optnumber(L, 4, 320.0f); + + u32 x, y = 0; + qtmConvertCoordToScreen(&info->info->coords0[index], &screenWidth, &screenHeight, &x, &y); + + lua_pushinteger(L, x); + lua_pushinteger(L, y); + + return 2; } -// module +// object +static const struct luaL_Reg qtm_methods[] = { + {"checkHeadFullyDetected", qtm_checkHeadFullyDetected}, + {"convertCoordToScreen", qtm_convertCoordToScreen }, + {"__index", qtm___index }, + {NULL, NULL} +}; + +// module functions static const struct luaL_Reg qtm_functions[] = { {"init", qtm_init }, {"shutdown", qtm_shutdown }, @@ -48,6 +109,11 @@ static const struct luaL_Reg qtm_functions[] = { }; int luaopen_qtm_lib(lua_State *L) { + luaL_newmetatable(L, "LQTM"); + lua_pushvalue(L, -1); + lua_setfield(L, -2, "__index"); + luaL_setfuncs(L, qtm_methods, 0); + luaL_newlib(L, qtm_functions); return 1; }