diff --git a/sdcard/ctruLua/main.lua b/sdcard/ctruLua/main.lua index aff3589..f7d60b6 100644 --- a/sdcard/ctruLua/main.lua +++ b/sdcard/ctruLua/main.lua @@ -1,7 +1,20 @@ local gfx = require("ctr.gfx") +local hid = require("ctr.hid") + +local x = 0 +local y = 0 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.render() diff --git a/source/ctr.c b/source/ctr.c index 9ceff6f..55c31b1 100644 --- a/source/ctr.c +++ b/source/ctr.c @@ -5,15 +5,17 @@ int load_os_lib(lua_State *L); int load_gfx_lib(lua_State *L); int load_news_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[] = { { NULL, NULL } }; struct { char *name; int (*load)(lua_State *L); } ctr_libs[] = { - { "gfx", load_gfx_lib }, + { "gfx", load_gfx_lib }, { "news", load_news_lib }, - { "ptm", load_ptm_lib }, + { "ptm", load_ptm_lib }, + { "hid", load_hid_lib }, { NULL, NULL } }; diff --git a/source/hid.c b/source/hid.c new file mode 100644 index 0000000..e9df181 --- /dev/null +++ b/source/hid.c @@ -0,0 +1,89 @@ +#include <3ds/types.h> +#include <3ds/services/hid.h> + +#include +#include + +// 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); +} \ No newline at end of file diff --git a/source/os.c b/source/os.c index 700537e..6f84e48 100644 --- a/source/os.c +++ b/source/os.c @@ -1,4 +1,5 @@ -#include <3ds.h> +#include <3ds/types.h> +#include <3ds/services/apt.h> #include