Skip to content

Commit

Permalink
Updated the hid/abstracted-pad and hid/hdls examples for latest libnx…
Browse files Browse the repository at this point in the history
…, this includes hdls support for [9.0.0+]. Adjusted buttons handling in hdls example, and print NpadInterfaceType/PowerInfo.
  • Loading branch information
yellows8 committed Sep 14, 2019
1 parent c24895c commit 9d11f70
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
10 changes: 5 additions & 5 deletions hid/abstracted-pad/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Include the main libnx system header, for Switch development
#include <switch.h>

// This example shows how to use AbstractedPad, see also libnx hiddbg.h. Depending on state type2, either a new virtual controller can be registered, or the state can be merged with an existing controller.
// This example shows how to use AbstractedPad, see also libnx hiddbg.h. Depending on state npadInterfaceType, either a new virtual controller can be registered, or the state can be merged with an existing controller.
// This is deprecated, use Hdls instead when running on compatible system-versions.

// Main program entrypoint
Expand All @@ -27,7 +27,7 @@ int main(int argc, char* argv[])

hidScanInput();

// When hiddbgSetAutoPilotVirtualPadState runs a new controller may become available, depending on the specified type2 field. If CONTROLLER_HANDHELD is being internally, CONTROLLER_P1_AUTO would use the new controller. Check which controller we're currently using and don't use CONTROLLER_P1_AUTO, so it doesn't switch to using the new controller later.
// When hiddbgSetAutoPilotVirtualPadState runs a new controller may become available, depending on the specified npadInterfaceType field. If CONTROLLER_HANDHELD is being internally, CONTROLLER_P1_AUTO would use the new controller. Check which controller we're currently using and don't use CONTROLLER_P1_AUTO, so it doesn't switch to using the new controller later.
HidControllerID conID = hidGetHandheldMode() ? CONTROLLER_HANDHELD : CONTROLLER_PLAYER_1;

printf("Connected controllers: ");
Expand Down Expand Up @@ -86,7 +86,7 @@ int main(int argc, char* argv[])
if (R_SUCCEEDED(rc) && tmpout>=1) {
for (u32 i=0; i<tmpout; i++) {
printf("0x%x: \n", i);
printf("type = 0x%x, flags = 0x%x, colors = 0x%x 0x%x, type2 = 0x%x, buttons = 0x%x, stickL.dx = 0x%x, stickL.dy = 0x%x, stickR.dx = 0x%x, stickR.dy = 0x%x\n", states[i].type, states[i].flags, states[i].singleColorBody, states[i].singleColorButtons, states[i].type2, states[i].state.buttons, states[i].state.joysticks[0].dx, states[i].state.joysticks[0].dy, states[i].state.joysticks[1].dx, states[i].state.joysticks[1].dy);
printf("type = 0x%x, flags = 0x%x, colors = 0x%x 0x%x, npadInterfaceType = 0x%x, buttons = 0x%x, stickL.dx = 0x%x, stickL.dy = 0x%x, stickR.dx = 0x%x, stickR.dy = 0x%x\n", states[i].type, states[i].flags, states[i].singleColorBody, states[i].singleColorButtons, states[i].npadInterfaceType, states[i].state.buttons, states[i].state.joysticks[0].dx, states[i].state.joysticks[0].dy, states[i].state.joysticks[1].dx, states[i].state.joysticks[1].dy);
}
}

Expand All @@ -98,8 +98,8 @@ int main(int argc, char* argv[])
// Set type to one that's usable with state-merge. Note that this is also available with Hdls.
states[0].type = BIT(1);
// Use state-merge for the above controller, the state will be merged with an existing controller.
// For a plain virtual controller, use type2 value 0x0, and update the above type value.
states[0].type2 = 0x2;
// For a plain virtual controller, use NpadInterfaceType_Bluetooth, and update the above type value.
states[0].npadInterfaceType = NpadInterfaceType_Rail;

states[0].state.buttons |= KEY_ZL;

Expand Down
37 changes: 31 additions & 6 deletions hid/hdls/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ int main(int argc, char* argv[])
HiddbgHdlsDeviceInfo device = {0};
HiddbgHdlsState state={0};

// Set the controller type to Pro-Controller.
device.type = BIT(0);
// Set the controller colors.
// Set the controller type to Pro-Controller, and set the npadInterfaceType.
device.deviceType = HidDeviceType_FullKey3;
device.npadInterfaceType = NpadInterfaceType_Bluetooth;
// Set the controller colors. The grip colors are for Pro-Controller on [9.0.0+].
device.singleColorBody = RGBA8_MAXALPHA(255,255,255);
device.singleColorButtons = RGBA8_MAXALPHA(0,0,0);
device.colorLeftGrip = RGBA8_MAXALPHA(230,255,0);
device.colorRightGrip = RGBA8_MAXALPHA(0,40,20);

// Setup example controller state.
state.batteryCharge = 4; // Set battery charge to full.
state.buttons = KEY_A | KEY_ZR;
state.joysticks[JOYSTICK_LEFT].dx = 0x1234;
state.joysticks[JOYSTICK_LEFT].dy = -0x1234;
state.joysticks[JOYSTICK_RIGHT].dx = 0x5678;
Expand Down Expand Up @@ -91,20 +93,43 @@ int main(int argc, char* argv[])
rc2 = hiddbgSetHdlsState(HdlsHandle, &state);
if (R_FAILED(rc2)) printf("hiddbgSetHdlsState(): 0x%x\n", rc2);

state.buttons = 0;

if (hidKeysHeld(conID) & KEY_R)
state.buttons |= KEY_HOME;

if (hidKeysHeld(conID) & KEY_L)
state.buttons |= KEY_CAPTURE;

if (hidKeysHeld(conID) & KEY_DUP)
state.buttons |= KEY_ZR;

state.joysticks[JOYSTICK_LEFT].dx += 0x10;
if (state.joysticks[JOYSTICK_LEFT].dx > JOYSTICK_MAX) state.joysticks[JOYSTICK_LEFT].dx = JOYSTICK_MIN;
state.joysticks[JOYSTICK_RIGHT].dy -= 0x10;
if (state.joysticks[JOYSTICK_LEFT].dy < JOYSTICK_MIN) state.joysticks[JOYSTICK_LEFT].dy = JOYSTICK_MAX;
}

if (R_SUCCEEDED(rc) && (kDown & KEY_A)) {
if (R_SUCCEEDED(rc) && (kDown & (KEY_A | KEY_X))) {
printf("Connected controllers:\n");
for(i=0; i<10; i++) {
if (hidIsControllerConnected(i)) {
JoystickPosition tmpjoy[2];
hidJoystickRead(&tmpjoy[0], i, JOYSTICK_LEFT);
hidJoystickRead(&tmpjoy[1], i, JOYSTICK_RIGHT);
printf("%d: type = 0x%x, buttons = 0x%lx, stickL.dx = 0x%x, stickL.dy = 0x%x, stickR.dx = 0x%x, stickR.dy = 0x%x\n", i, hidGetControllerType(i), hidKeysHeld(i), tmpjoy[0].dx, tmpjoy[0].dy, tmpjoy[1].dx, tmpjoy[1].dy);

u8 interfacetype=0;
rc2 = hidGetNpadInterfaceType(i, &interfacetype);
if (R_FAILED(rc2)) printf("hidGetNpadInterfaceType(): 0x%x\n", rc2);

HidPowerInfo powerinfo[3]={0};
hidGetControllerPowerInfo(i, &powerinfo[0], 1);
hidGetControllerPowerInfo(i, &powerinfo[1], 2);

printf("%d: type = 0x%x, devicetype = 0x%x, buttons = 0x%lx, stickL.dx = 0x%x, stickL.dy = 0x%x, stickR.dx = 0x%x, stickR.dy = 0x%x, interface = %d\n", i, hidGetControllerType(i), hidGetControllerDeviceType(i), hidKeysHeld(i), tmpjoy[0].dx, tmpjoy[0].dy, tmpjoy[1].dx, tmpjoy[1].dy, interfacetype);

for (u32 poweri=0; poweri<3; poweri++)
printf("%d powerinfo[%d]: powerConnected = %d, isCharging = %d, batteryCharge = %d\n", i, poweri, powerinfo[poweri].powerConnected, powerinfo[poweri].isCharging, powerinfo[poweri].batteryCharge);
}
}
printf("\n");
Expand Down

0 comments on commit 9d11f70

Please sign in to comment.