switch-examples/hid/ring-con/source/main.c
2020-03-24 14:27:07 -04:00

76 lines
3.3 KiB
C

// Include the most common headers from the C standard library
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Include the main libnx system header, for Switch development
#include <switch.h>
// This example shows how to use the Ring-Con which attaches to a Joy-Con (from Ring Fit Adventure).
// See also libnx ringcon.h.
// Main program entrypoint
int main(int argc, char **argv)
{
// This example uses a text console, as a simple way to output text to the screen.
// If you want to write a software-rendered graphics application,
// take a look at the graphics/simplegfx example, which uses the libnx Framebuffer API instead.
// If on the other hand you want to write an OpenGL based application,
// take a look at the graphics/opengl set of examples, which uses EGL instead.
consoleInit(NULL);
printf("\x1b[1;1HPress PLUS to exit.");
Result rc=0;
RingCon ring={0};
RingConUserCal user_cal={0};
bool ready=0;
rc = ringconCreate(&ring, CONTROLLER_PLAYER_1); // Setup Ring-Con usage for the specified controller, if you want to use multiple controllers you can do so by calling this multiple times with multiple RingCon objects/controllers. The Ring-Con must be connected to the specified controller.
printf(CONSOLE_ESC(2;1H)"ringconCreate(): 0x%x, 0x%x", rc, ringconGetErrorFlags(&ring)); // You can also use ringconGetErrorFlag().
if (R_SUCCEEDED(rc)) {
ringconGetUserCal(&ring, &user_cal);
if (user_cal.data_valid == RingConDataValid_Cal)
printf(CONSOLE_ESC(3;1H)"The user-cal is not calibrated."); // When this happens the calibration needs set with ringconUpdateUserCal(). This example won't impl doing so - Ring Fit Adventure can handle setting this.
else {
ready = true;
printf(CONSOLE_ESC(3;1H)"User-calibration: os_max = %d, hk_max = %d, zero = %d", user_cal.os_max, user_cal.hk_max, user_cal.zero);
}
// See ringcon.h for more ringcon funcs.
}
// Main loop
while(appletMainLoop())
{
//Scan all the inputs. This should be done once for each frame
hidScanInput();
// hidKeysDown returns information about which buttons have been
// just pressed in this frame compared to the previous one
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS)
break; // break in order to return to hbmenu
// Get the sensor state data.
if (ready) {
s32 total_out=0;
RingConPollingData polldata[0x1]; // For this example we'll just read the latest entry, however if you want the rest use 0x9.
rc = ringconGetPollingData(&ring, polldata, 0x1, &total_out); // See libnx ringcon.h regarding ringconGetPollingData().
printf(CONSOLE_ESC(4;1H)"ringconGetPollingData(): 0x%x (total_out=%d)\n", rc, total_out);
for (s32 polli=0; polli<total_out; polli++) printf("[%d]: data = %d\n", polli, polldata[polli].data);
// In an actual app you'd want to compare the above data with the user_cal fields.
}
// Update the console, sending a new frame to the display
consoleUpdate(NULL);
}
ringconClose(&ring);
// Deinitialize and clean up resources used by the console (important!)
consoleExit(NULL);
return 0;
}