mirror of
https://github.com/ctruLua/ctruLua.git
synced 2025-10-27 16:39:29 +00:00
Added ctr.hid lib, hid.read() working.
This commit is contained in:
parent
2a85112e92
commit
0e42f25b8b
4 changed files with 109 additions and 4 deletions
|
|
@ -1,7 +1,20 @@
|
||||||
local gfx = require("ctr.gfx")
|
local gfx = require("ctr.gfx")
|
||||||
|
local hid = require("ctr.hid")
|
||||||
|
|
||||||
|
local x = 0
|
||||||
|
local y = 0
|
||||||
|
|
||||||
while os.run() do
|
while os.run() do
|
||||||
gfx.rectangle(10, 10, 56, 120)
|
local keys = hid.read()
|
||||||
|
|
||||||
|
if keys.down.start then return end
|
||||||
|
|
||||||
|
if keys.held.right then x = x + 1 end
|
||||||
|
if keys.held.left then x = x - 1 end
|
||||||
|
if keys.held.up then y = y - 1 end
|
||||||
|
if keys.held.down then y = y + 1 end
|
||||||
|
|
||||||
|
gfx.rectangle(x, y, 10, 10)
|
||||||
gfx.rectangle(240, 150, 120, 10)
|
gfx.rectangle(240, 150, 120, 10)
|
||||||
|
|
||||||
gfx.render()
|
gfx.render()
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ int load_os_lib(lua_State *L);
|
||||||
int load_gfx_lib(lua_State *L);
|
int load_gfx_lib(lua_State *L);
|
||||||
int load_news_lib(lua_State *L);
|
int load_news_lib(lua_State *L);
|
||||||
int load_ptm_lib(lua_State *L);
|
int load_ptm_lib(lua_State *L);
|
||||||
|
int load_hid_lib(lua_State *L);
|
||||||
|
|
||||||
static const struct luaL_Reg ctr_lib[] = {
|
static const struct luaL_Reg ctr_lib[] = {
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
|
|
@ -14,6 +15,7 @@ struct { char *name; int (*load)(lua_State *L); } ctr_libs[] = {
|
||||||
{ "gfx", load_gfx_lib },
|
{ "gfx", load_gfx_lib },
|
||||||
{ "news", load_news_lib },
|
{ "news", load_news_lib },
|
||||||
{ "ptm", load_ptm_lib },
|
{ "ptm", load_ptm_lib },
|
||||||
|
{ "hid", load_hid_lib },
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
89
source/hid.c
Normal file
89
source/hid.c
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
#include <3ds/types.h>
|
||||||
|
#include <3ds/services/hid.h>
|
||||||
|
|
||||||
|
#include <lua.h>
|
||||||
|
#include <lauxlib.h>
|
||||||
|
|
||||||
|
// key list based on hid.h from the ctrulib by smealum
|
||||||
|
struct { PAD_KEY key; char *name; } hid_keys[] = {
|
||||||
|
{ KEY_A , "a" },
|
||||||
|
{ KEY_B , "b" },
|
||||||
|
{ KEY_SELECT , "select" },
|
||||||
|
{ KEY_START , "start" },
|
||||||
|
{ KEY_DRIGHT , "dRight" },
|
||||||
|
{ KEY_DLEFT , "dLeft" },
|
||||||
|
{ KEY_DUP , "dUp" },
|
||||||
|
{ KEY_DDOWN , "dDown" },
|
||||||
|
{ KEY_R , "r" },
|
||||||
|
{ KEY_L , "l" },
|
||||||
|
{ KEY_X , "x" },
|
||||||
|
{ KEY_Y , "y" },
|
||||||
|
{ KEY_ZL , "zl" }, // (new 3DS only)
|
||||||
|
{ KEY_ZR , "zr" }, // (new 3DS only)
|
||||||
|
{ KEY_TOUCH , "touch" }, // Not actually provided by HID
|
||||||
|
{ KEY_CSTICK_RIGHT , "cstickRight"}, // c-stick (new 3DS only)
|
||||||
|
{ KEY_CSTICK_LEFT , "cstickLeft" }, // c-stick (new 3DS only)
|
||||||
|
{ KEY_CSTICK_UP , "cstickUp" }, // c-stick (new 3DS only)
|
||||||
|
{ KEY_CSTICK_DOWN , "cstickDown" }, // c-stick (new 3DS only)
|
||||||
|
{ KEY_CPAD_RIGHT , "cpadRight" }, // circle pad
|
||||||
|
{ KEY_CPAD_LEFT , "cpadLeft" }, // circle pad
|
||||||
|
{ KEY_CPAD_UP , "cpadUp" }, // circle pad
|
||||||
|
{ KEY_CPAD_DOWN , "cpadDown" }, // circle pad
|
||||||
|
|
||||||
|
// Generic catch-all directions
|
||||||
|
{ KEY_UP , "up" },
|
||||||
|
{ KEY_DOWN , "down" },
|
||||||
|
{ KEY_LEFT , "left" },
|
||||||
|
{ KEY_RIGHT , "right" }
|
||||||
|
};
|
||||||
|
|
||||||
|
static int hid_read(lua_State *L) {
|
||||||
|
hidScanInput();
|
||||||
|
|
||||||
|
u32 kDown = hidKeysDown();
|
||||||
|
u32 kHeld = hidKeysHeld();
|
||||||
|
u32 kUp = hidKeysUp();
|
||||||
|
|
||||||
|
lua_createtable(L, 0, 3);
|
||||||
|
lua_newtable(L); // down table
|
||||||
|
lua_newtable(L); // held table
|
||||||
|
lua_newtable(L); // up table
|
||||||
|
|
||||||
|
for (int i = 0; hid_keys[i].key; i++) {
|
||||||
|
PAD_KEY key = hid_keys[i].key;
|
||||||
|
char *name = hid_keys[i].name;
|
||||||
|
|
||||||
|
if (kDown & key) {
|
||||||
|
lua_pushboolean(L, true);
|
||||||
|
lua_setfield(L, -4, name);
|
||||||
|
}
|
||||||
|
if (kHeld & key) {
|
||||||
|
lua_pushboolean(L, true);
|
||||||
|
lua_setfield(L, -3, name);
|
||||||
|
}
|
||||||
|
if (kUp & key) {
|
||||||
|
lua_pushboolean(L, true);
|
||||||
|
lua_setfield(L, -2, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_setfield(L, -4, "up");
|
||||||
|
lua_setfield(L, -3, "held");
|
||||||
|
lua_setfield(L, -2, "down");
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const struct luaL_Reg hid_lib[] = {
|
||||||
|
{ "read", hid_read },
|
||||||
|
{ NULL, NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
int luaopen_hid_lib(lua_State *L) {
|
||||||
|
luaL_newlib(L, hid_lib);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void load_hid_lib(lua_State *L) {
|
||||||
|
luaL_requiref(L, "ctr.hid", luaopen_hid_lib, false);
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include <3ds.h>
|
#include <3ds/types.h>
|
||||||
|
#include <3ds/services/apt.h>
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue