From 551e8e121e691fca5ee4c38ba8a994073cc9810e Mon Sep 17 00:00:00 2001 From: Firew0lf Date: Thu, 20 Aug 2015 18:33:49 +0200 Subject: [PATCH] Added the "ctr.ir" library. Maybe it's working. --- source/ctr.c | 2 ++ source/ir.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 source/ir.c diff --git a/source/ctr.c b/source/ctr.c index 72b666e..8837b3a 100644 --- a/source/ctr.c +++ b/source/ctr.c @@ -9,6 +9,7 @@ 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); +int load_ir_lib(lua_State *L); static int ctr_run(lua_State *L) { lua_pushboolean(L, aptMainLoop()); @@ -35,6 +36,7 @@ struct { char *name; int (*load)(lua_State *L); } ctr_libs[] = { { "news", load_news_lib }, { "ptm", load_ptm_lib }, { "hid", load_hid_lib }, + { "ir", load_ir_lib }, { NULL, NULL } }; diff --git a/source/ir.c b/source/ir.c new file mode 100644 index 0000000..14a1ec7 --- /dev/null +++ b/source/ir.c @@ -0,0 +1,91 @@ +#include <3ds/types.h> +#include <3ds/services/ir.h> +#include <3ds/linear.h> + +#include +#include + +u32 bufferSize = 0; +u32 *buffer; + +static int ir_init(lua_State *L) { + u8 bitrate = luaL_checkinteger(L, 1); + bufferSize = luaL_optinteger(L, 2, 2048); //default: 2Kio + buffer = linearAlloc(bufferSize); + + Result ret = IRU_Initialize(buffer, bufferSize); + if (ret) { + lua_pushboolean(L, false); + lua_pushinteger(L, ret); + return 2; + } + IRU_SetBitRate(bitrate); + + lua_pushboolean(L, true); + return 1; +} + +static int ir_shutdown(lua_State *L) { + IRU_Shutdown(); + + return 0; +} + +static int ir_send(lua_State *L) { + u8 *data = (u8*)luaL_checkstring(L, 1); + u32 wait = lua_toboolean(L, 2); + + IRU_SendData(data, sizeof(data), wait); + + return 0; +} + +static int ir_receive(lua_State *L) { + u32 size = luaL_optinteger(L, 1, bufferSize); + u32 wait = lua_toboolean(L, 2); + u8 *data = 0; + u32 *transfercount = 0; + + IRU_RecvData(data, size, 0x00, transfercount, wait); + + lua_pushstring(L, (const char *)data); + + return 1; +} + +static int ir_setBitRate(lua_State *L) { + u8 bitrate = luaL_checkinteger(L, 1); + + IRU_SetBitRate(bitrate); + + return 0; +} + +static int ir_getBitRate(lua_State *L) { + u8 bitrate = 0; + + IRU_GetBitRate(&bitrate); + + lua_pushinteger(L, bitrate); + + return 1; +} + +static const struct luaL_Reg ir_lib[] = { + {"init", ir_init }, + {"shutdown", ir_shutdown }, + {"send", ir_send }, + {"receive", ir_receive }, + {"setBitRate", ir_setBitRate}, + {"getBitRate", ir_getBitRate}, + {NULL, NULL} +}; + +int luaopen_ir_lib(lua_State *L) { + luaL_newlib(L, ir_lib); + return 1; +} + +void load_ir_lib(lua_State *L) { + luaL_requiref(L, "ctr.ir", luaopen_ir_lib, 0); +}