mirror of
https://github.com/switchbrew/switch-examples.git
synced 2025-06-21 13:22:40 +02:00
Updated the irsensor example for latest libnx, and various improvements.
This commit is contained in:
parent
2e7d0c198e
commit
948ce48e58
@ -49,27 +49,34 @@ int main(int argc, char **argv)
|
||||
{
|
||||
Result rc=0;
|
||||
|
||||
const size_t ir_buffer_size = 0x12c00;
|
||||
const size_t ir_buffer_size = 0x12c00; // Size for the max IrsImageTransferProcessorFormat.
|
||||
u8 *ir_buffer = NULL;
|
||||
ir_buffer = (u8*)malloc(ir_buffer_size);
|
||||
if (!ir_buffer)
|
||||
{
|
||||
if (!ir_buffer) {
|
||||
rc = MAKERESULT(Module_Libnx, LibnxError_OutOfMemory);
|
||||
return error_screen("Failed to allocate memory for ir_buffer.\n");
|
||||
}
|
||||
|
||||
memset(ir_buffer, 0, ir_buffer_size);
|
||||
rc = irsActivateIrsensor(1);
|
||||
if (R_FAILED(rc))
|
||||
return error_screen("irsActivateIrsensor() returned 0x%x\n", rc);
|
||||
|
||||
// Get the handle for the specified controller.
|
||||
u32 irhandle=0;
|
||||
hidScanInput();
|
||||
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, hidGetHandheldMode() ? CONTROLLER_HANDHELD : CONTROLLER_PLAYER_1);
|
||||
if (R_FAILED(rc))
|
||||
return error_screen("irsGetIrCameraHandle() returned 0x%x\n", rc);
|
||||
|
||||
// If a controller update is needed, force an update.
|
||||
bool updateflag=0;
|
||||
rc = irsCheckFirmwareUpdateNecessity(irhandle, &updateflag);
|
||||
if (R_SUCCEEDED(rc) && updateflag) {
|
||||
HidLaControllerFirmwareUpdateArg updatearg;
|
||||
hidLaCreateControllerFirmwareUpdateArg(&updatearg);
|
||||
updatearg.enable_force_update = 1;
|
||||
hidLaShowControllerFirmwareUpdate(&updatearg);
|
||||
}
|
||||
|
||||
// Run the ImageTransferProcessor with the default config. The default uses the max IrsImageTransferProcessorFormat which has the slowest update-rate with irsGetImageTransferProcessorState. Hence, you may want to use a different IrsImageTransferProcessorFormat, or trim the image with irsRunImageTransferExProcessor.
|
||||
IrsImageTransferProcessorConfig config;
|
||||
irsGetDefaultImageTransferProcessorConfig(&config);
|
||||
rc = irsRunImageTransferProcessor(irhandle, &config, 0x100000);
|
||||
@ -80,6 +87,8 @@ int main(int argc, char **argv)
|
||||
framebufferCreate(&fb, nwindowGetDefault(), FB_WIDTH, FB_HEIGHT, PIXEL_FORMAT_RGBA_8888, 2);
|
||||
framebufferMakeLinear(&fb);
|
||||
|
||||
u64 sampling_number=0;
|
||||
|
||||
while (appletMainLoop())
|
||||
{
|
||||
// Scan all the inputs. This should be done once for each frame
|
||||
@ -90,7 +99,7 @@ int main(int argc, char **argv)
|
||||
|
||||
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
|
||||
|
||||
//Note that the image is updated every few seconds. 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.
|
||||
IrsImageTransferProcessorState state;
|
||||
rc = irsGetImageTransferProcessorState(irhandle, ir_buffer, ir_buffer_size, &state);
|
||||
@ -98,22 +107,20 @@ int main(int argc, char **argv)
|
||||
u32 stride;
|
||||
u32* framebuf = (u32*)framebufferBegin(&fb, &stride);
|
||||
|
||||
if (R_SUCCEEDED(rc))
|
||||
{
|
||||
if (R_SUCCEEDED(rc) && state.sampling_number != sampling_number) { // Only update framebuf when irsGetImageTransferProcessorState() is successful, where sampling_number changed.
|
||||
sampling_number = state.sampling_number;
|
||||
memset(framebuf, 0, stride*FB_HEIGHT);
|
||||
|
||||
// IR image width/height with the default config.
|
||||
// The image is grayscale (1 byte per pixel / 8bits, with 1 color-component).
|
||||
const u32 ir_width = 240;
|
||||
const u32 ir_height = 320;
|
||||
const u32 ir_width = 320;
|
||||
const u32 ir_height = 240;
|
||||
|
||||
u32 x, y;
|
||||
for (y=0; y<ir_height; y++)//Access the buffer linearly.
|
||||
{
|
||||
for (x=0; x<ir_width; x++)
|
||||
{
|
||||
for (y=0; y<ir_height; y++) { // Access the buffer linearly.
|
||||
for (x=0; x<ir_width; x++) {
|
||||
u32 pos = y * stride/sizeof(u32) + x;
|
||||
u32 pos2 = x * ir_height + y;//The IR image/camera is sideways with the joycon held flat.
|
||||
u32 pos2 = y * ir_width + x;//The IR image/camera is sideways with the joycon held flat. We won't rotate it here - you can do so yourself if you want.
|
||||
framebuf[pos] = RGBA8_MAXALPHA(/*ir_buffer[pos2]*/0, ir_buffer[pos2], /*ir_buffer[pos2]*/0);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user