hid/read-controls: Updated for hid-refactor.

This commit is contained in:
yellows8 2020-12-08 13:52:19 -05:00
parent 40ec713452
commit ac702280bc
No known key found for this signature in database
GPG Key ID: 0AF90DA3F1E60E43

View File

@ -1,26 +1,41 @@
#include <string.h> // Include the most common headers from the C standard library
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Include the main libnx system header, for Switch development
#include <switch.h> #include <switch.h>
//See also libnx hid.h. //See also libnx hid.h.
int main(int argc, char **argv) // Main program entrypoint
int main(int argc, char* argv[])
{ {
//Matrix containing the name of each key. Useful for printing when a key is pressed // This example uses a text console, as a simple way to output text to the screen.
char keysNames[32][32] = { // If you want to write a software-rendered graphics application,
"KEY_A", "KEY_B", "KEY_X", "KEY_Y", // take a look at the graphics/simplegfx example, which uses the libnx Framebuffer API instead.
"KEY_LSTICK", "KEY_RSTICK", "KEY_L", "KEY_R", // If on the other hand you want to write an OpenGL based application,
"KEY_ZL", "KEY_ZR", "KEY_PLUS", "KEY_MINUS", // take a look at the graphics/opengl set of examples, which uses EGL instead.
"KEY_DLEFT", "KEY_DUP", "KEY_DRIGHT", "KEY_DDOWN",
"KEY_LSTICK_LEFT", "KEY_LSTICK_UP", "KEY_LSTICK_RIGHT", "KEY_LSTICK_DOWN",
"KEY_RSTICK_LEFT", "KEY_RSTICK_UP", "KEY_RSTICK_RIGHT", "KEY_RSTICK_DOWN",
"KEY_SL_LEFT", "KEY_SR_LEFT", "KEY_SL_RIGHT", "KEY_SR_RIGHT",
"KEY_TOUCH", "", "", ""
};
consoleInit(NULL); consoleInit(NULL);
// 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);
//Matrix containing the name of each key. Useful for printing when a key is pressed
char keysNames[28][32] = {
"A", "B", "X", "Y",
"StickL", "StickR", "L", "R",
"ZL", "ZR", "Plus", "Minus",
"Left", "Up", "Right", "Down",
"StickLLeft", "StickLUp", "StickLRight", "StickLDown",
"StickRLeft", "StickRUp", "StickRRight", "StickRDown",
"LeftSL", "LeftSR", "RightSL", "RightSR",
};
u32 kDownOld = 0, kHeldOld = 0, kUpOld = 0; //In these variables there will be information about keys detected in the previous frame u32 kDownOld = 0, kHeldOld = 0, kUpOld = 0; //In these variables there will be information about keys detected in the previous frame
printf("\x1b[1;1HPress PLUS to exit."); printf("\x1b[1;1HPress PLUS to exit.");
@ -30,34 +45,39 @@ int main(int argc, char **argv)
// Main loop // Main loop
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
//hidKeysHeld returns information about which buttons have are held down in this frame u64 kDown = padGetButtonsDown(&pad);
u64 kHeld = hidKeysHeld(CONTROLLER_P1_AUTO);
//hidKeysUp returns information about which buttons have been just released
u64 kUp = hidKeysUp(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu // padGetButtons returns the set of buttons that are currently pressed
u64 kHeld = padGetButtons(&pad);
//Do the keys printing only if keys have changed // padGetButtonsUp returns the set of buttons that have been
// newly released in this frame compared to the previous one
u64 kUp = padGetButtonsUp(&pad);
if (kDown & HidNpadButton_Plus)
break; // break in order to return to hbmenu
// Do the keys printing only if keys have changed
if (kDown != kDownOld || kHeld != kHeldOld || kUp != kUpOld) if (kDown != kDownOld || kHeld != kHeldOld || kUp != kUpOld)
{ {
//Clear console // Clear console
consoleClear(); consoleClear();
//These two lines must be rewritten because we cleared the whole console // These two lines must be rewritten because we cleared the whole console
printf("\x1b[1;1HPress PLUS to exit."); printf("\x1b[1;1HPress PLUS to exit.");
printf("\x1b[2;1HLeft joystick position:"); printf("\x1b[2;1HLeft stick position:");
printf("\x1b[4;1HRight joystick position:"); printf("\x1b[4;1HRight stick position:");
printf("\x1b[6;1H"); //Move the cursor to the sixth row because on the previous ones we'll write the joysticks' position printf("\x1b[6;1H"); //Move the cursor to the sixth row because on the previous ones we'll write the joysticks' position
//Check if some of the keys are down, held or up // Check if some of the keys are down, held or up
int i; int i;
for (i = 0; i < 32; i++) for (i = 0; i < 28; i++)
{ {
if (kDown & BIT(i)) printf("%s down\n", keysNames[i]); if (kDown & BIT(i)) printf("%s down\n", keysNames[i]);
if (kHeld & BIT(i)) printf("%s held\n", keysNames[i]); if (kHeld & BIT(i)) printf("%s held\n", keysNames[i]);
@ -65,24 +85,24 @@ int main(int argc, char **argv)
} }
} }
//Set keys old values for the next frame // Set keys old values for the next frame
kDownOld = kDown; kDownOld = kDown;
kHeldOld = kHeld; kHeldOld = kHeld;
kUpOld = kUp; kUpOld = kUp;
JoystickPosition pos_left, pos_right; // Read the sticks' position
HidAnalogStickState analog_stick_l = padGetStickPos(&pad, 0);
HidAnalogStickState analog_stick_r = padGetStickPos(&pad, 1);
//Read the joysticks' position // Print the sticks' position
hidJoystickRead(&pos_left, CONTROLLER_P1_AUTO, JOYSTICK_LEFT); printf("\x1b[3;1H%04d; %04d", analog_stick_l.x, analog_stick_l.y);
hidJoystickRead(&pos_right, CONTROLLER_P1_AUTO, JOYSTICK_RIGHT); printf("\x1b[5;1H%04d; %04d", analog_stick_r.x, analog_stick_r.y);
//Print the joysticks' position
printf("\x1b[3;1H%04d; %04d", pos_left.dx, pos_left.dy);
printf("\x1b[5;1H%04d; %04d", pos_right.dx, pos_right.dy);
// Update the console, sending a new frame to the display
consoleUpdate(NULL); consoleUpdate(NULL);
} }
// Deinitialize and clean up resources used by the console (important!)
consoleExit(NULL); consoleExit(NULL);
return 0; return 0;
} }