hid/irsensor: Updated for hid-refactor.

This commit is contained in:
yellows8 2020-12-08 01:03:19 -05:00
parent 00a0bb3899
commit a6ea828801
No known key found for this signature in database
GPG Key ID: 0AF90DA3F1E60E43

View File

@ -1,8 +1,9 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h> #include <stdarg.h>
// Include the main libnx system header, for Switch development
#include <switch.h> #include <switch.h>
// Joy-Con IR-sensor example, displays the image from the IR camera. See also libnx irs.h. // Joy-Con IR-sensor example, displays the image from the IR camera. See also libnx irs.h.
@ -25,8 +26,8 @@ void userAppExit(void)
irsExit(); irsExit();
} }
__attribute__((format(printf, 1, 2))) __attribute__((format(printf, 2, 3)))
static int error_screen(const char* fmt, ...) static int error_screen(PadState *pad, const char* fmt, ...)
{ {
consoleInit(NULL); consoleInit(NULL);
va_list va; va_list va;
@ -36,8 +37,8 @@ static int error_screen(const char* fmt, ...)
printf("Press PLUS to exit\n"); printf("Press PLUS to exit\n");
while (appletMainLoop()) while (appletMainLoop())
{ {
hidScanInput(); padUpdate(pad);
if (hidKeysDown(CONTROLLER_P1_AUTO) & KEY_PLUS) if (padGetButtonsDown(pad) & HidNpadButton_Plus)
break; break;
consoleUpdate(NULL); consoleUpdate(NULL);
} }
@ -45,26 +46,34 @@ static int error_screen(const char* fmt, ...)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
int main(int argc, char **argv) // Main program entrypoint
int main(int argc, char* argv[])
{ {
Result rc=0; Result rc=0;
// Configure our supported input layout: a single player with standard controller styles
padConfigureInput(1, HidNpadStyleSet_NpadStandard);
// Initialize the default gamepad (which reads handheld mode inputs as well as the first connected controller)
PadState pad;
padInitializeDefault(&pad);
const size_t ir_buffer_size = 0x12c00; // Size for the max IrsImageTransferProcessorFormat. const size_t ir_buffer_size = 0x12c00; // Size for the max IrsImageTransferProcessorFormat.
u8 *ir_buffer = NULL; u8 *ir_buffer = NULL;
ir_buffer = (u8*)malloc(ir_buffer_size); ir_buffer = (u8*)malloc(ir_buffer_size);
if (!ir_buffer) { if (!ir_buffer) {
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory); rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
return error_screen("Failed to allocate memory for ir_buffer.\n"); return error_screen(&pad, "Failed to allocate memory for ir_buffer.\n");
} }
memset(ir_buffer, 0, ir_buffer_size); memset(ir_buffer, 0, ir_buffer_size);
// Get the handle for the specified controller. // Get the handle for the specified controller.
padUpdate(&pad); // Only needed because this wasn't used yet, and we're using padIsHandheld.
IrsIrCameraHandle irhandle; IrsIrCameraHandle irhandle;
hidScanInput(); // Only needed because hidScanInput() was not used yet since this is before the main-loop, and we're using hidGetHandheldMode(). rc = irsGetIrCameraHandle(&irhandle, padIsHandheld(&pad) ? HidNpadIdType_Handheld : HidNpadIdType_No1);
rc = irsGetIrCameraHandle(&irhandle, hidGetHandheldMode() ? CONTROLLER_HANDHELD : CONTROLLER_PLAYER_1);
if (R_FAILED(rc)) if (R_FAILED(rc))
return error_screen("irsGetIrCameraHandle() returned 0x%x\n", rc); return error_screen(&pad, "irsGetIrCameraHandle() returned 0x%x\n", rc);
// If a controller update is needed, force an update. // If a controller update is needed, force an update.
bool updateflag=0; bool updateflag=0;
@ -81,7 +90,7 @@ int main(int argc, char **argv)
irsGetDefaultImageTransferProcessorConfig(&config); irsGetDefaultImageTransferProcessorConfig(&config);
rc = irsRunImageTransferProcessor(irhandle, &config, 0x100000); rc = irsRunImageTransferProcessor(irhandle, &config, 0x100000);
if (R_FAILED(rc)) if (R_FAILED(rc))
return error_screen("irsRunImageTransferProcessor() returned 0x%x\n", rc); return error_screen(&pad, "irsRunImageTransferProcessor() returned 0x%x\n", rc);
Framebuffer fb; Framebuffer fb;
framebufferCreate(&fb, nwindowGetDefault(), FB_WIDTH, FB_HEIGHT, PIXEL_FORMAT_RGBA_8888, 2); framebufferCreate(&fb, nwindowGetDefault(), FB_WIDTH, FB_HEIGHT, PIXEL_FORMAT_RGBA_8888, 2);
@ -91,13 +100,15 @@ int main(int argc, char **argv)
while (appletMainLoop()) while (appletMainLoop())
{ {
// Scan all the inputs. This should be done once for each frame // Scan the gamepad. This should be done once for each frame
hidScanInput(); padUpdate(&pad);
// hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame) // padGetButtonsDown returns the set of buttons that have been
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO); // newly pressed in this frame compared to the previous one
u64 kDown = padGetButtonsDown(&pad);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu if (kDown & HidNpadButton_Plus)
break; // break in order to return to hbmenu
// With the default config the image is updated every few seconds (see above). Likewise, it takes a few seconds for the initial image to become available. // With the default config the image is updated every few seconds (see above). Likewise, it takes a few seconds for the initial image to become available.
// This will return an error when no image is available yet. // This will return an error when no image is available yet.