Skip to content

Commit 9d11f70

Browse files
committed
Updated the hid/abstracted-pad and hid/hdls examples for latest libnx, this includes hdls support for [9.0.0+]. Adjusted buttons handling in hdls example, and print NpadInterfaceType/PowerInfo.
1 parent c24895c commit 9d11f70

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

hid/abstracted-pad/source/main.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// Include the main libnx system header, for Switch development
77
#include <switch.h>
88

9-
// 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.
9+
// 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.
1010
// This is deprecated, use Hdls instead when running on compatible system-versions.
1111

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

2828
hidScanInput();
2929

30-
// 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.
30+
// 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.
3131
HidControllerID conID = hidGetHandheldMode() ? CONTROLLER_HANDHELD : CONTROLLER_PLAYER_1;
3232

3333
printf("Connected controllers: ");
@@ -86,7 +86,7 @@ int main(int argc, char* argv[])
8686
if (R_SUCCEEDED(rc) && tmpout>=1) {
8787
for (u32 i=0; i<tmpout; i++) {
8888
printf("0x%x: \n", i);
89-
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);
89+
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);
9090
}
9191
}
9292

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

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

hid/hdls/source/main.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ int main(int argc, char* argv[])
4848
HiddbgHdlsDeviceInfo device = {0};
4949
HiddbgHdlsState state={0};
5050

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

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

96+
state.buttons = 0;
97+
98+
if (hidKeysHeld(conID) & KEY_R)
99+
state.buttons |= KEY_HOME;
100+
101+
if (hidKeysHeld(conID) & KEY_L)
102+
state.buttons |= KEY_CAPTURE;
103+
104+
if (hidKeysHeld(conID) & KEY_DUP)
105+
state.buttons |= KEY_ZR;
106+
94107
state.joysticks[JOYSTICK_LEFT].dx += 0x10;
95108
if (state.joysticks[JOYSTICK_LEFT].dx > JOYSTICK_MAX) state.joysticks[JOYSTICK_LEFT].dx = JOYSTICK_MIN;
96109
state.joysticks[JOYSTICK_RIGHT].dy -= 0x10;
97110
if (state.joysticks[JOYSTICK_LEFT].dy < JOYSTICK_MIN) state.joysticks[JOYSTICK_LEFT].dy = JOYSTICK_MAX;
98111
}
99112

100-
if (R_SUCCEEDED(rc) && (kDown & KEY_A)) {
113+
if (R_SUCCEEDED(rc) && (kDown & (KEY_A | KEY_X))) {
101114
printf("Connected controllers:\n");
102115
for(i=0; i<10; i++) {
103116
if (hidIsControllerConnected(i)) {
104117
JoystickPosition tmpjoy[2];
105118
hidJoystickRead(&tmpjoy[0], i, JOYSTICK_LEFT);
106119
hidJoystickRead(&tmpjoy[1], i, JOYSTICK_RIGHT);
107-
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);
120+
121+
u8 interfacetype=0;
122+
rc2 = hidGetNpadInterfaceType(i, &interfacetype);
123+
if (R_FAILED(rc2)) printf("hidGetNpadInterfaceType(): 0x%x\n", rc2);
124+
125+
HidPowerInfo powerinfo[3]={0};
126+
hidGetControllerPowerInfo(i, &powerinfo[0], 1);
127+
hidGetControllerPowerInfo(i, &powerinfo[1], 2);
128+
129+
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);
130+
131+
for (u32 poweri=0; poweri<3; poweri++)
132+
printf("%d powerinfo[%d]: powerConnected = %d, isCharging = %d, batteryCharge = %d\n", i, poweri, powerinfo[poweri].powerConnected, powerinfo[poweri].isCharging, powerinfo[poweri].batteryCharge);
108133
}
109134
}
110135
printf("\n");

0 commit comments

Comments
 (0)