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.

This commit is contained in:
yellows8 2019-09-14 11:17:58 -04:00
parent c24895c52f
commit 9d11f70f3c
No known key found for this signature in database
GPG Key ID: 0AF90DA3F1E60E43
2 changed files with 36 additions and 11 deletions

View File

@ -6,7 +6,7 @@
// Include the main libnx system header, for Switch development // Include the main libnx system header, for Switch development
#include <switch.h> #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. // This is deprecated, use Hdls instead when running on compatible system-versions.
// Main program entrypoint // Main program entrypoint
@ -27,7 +27,7 @@ int main(int argc, char* argv[])
hidScanInput(); 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; HidControllerID conID = hidGetHandheldMode() ? CONTROLLER_HANDHELD : CONTROLLER_PLAYER_1;
printf("Connected controllers: "); printf("Connected controllers: ");
@ -86,7 +86,7 @@ int main(int argc, char* argv[])
if (R_SUCCEEDED(rc) && tmpout>=1) { if (R_SUCCEEDED(rc) && tmpout>=1) {
for (u32 i=0; i<tmpout; i++) { for (u32 i=0; i<tmpout; i++) {
printf("0x%x: \n", 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);
} }
} }
@ -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. // Set type to one that's usable with state-merge. Note that this is also available with Hdls.
states[0].type = BIT(1); states[0].type = BIT(1);
// Use state-merge for the above controller, the state will be merged with an existing controller. // 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. // For a plain virtual controller, use NpadInterfaceType_Bluetooth, and update the above type value.
states[0].type2 = 0x2; states[0].npadInterfaceType = NpadInterfaceType_Rail;
states[0].state.buttons |= KEY_ZL; states[0].state.buttons |= KEY_ZL;

View File

@ -48,15 +48,17 @@ int main(int argc, char* argv[])
HiddbgHdlsDeviceInfo device = {0}; HiddbgHdlsDeviceInfo device = {0};
HiddbgHdlsState state={0}; HiddbgHdlsState state={0};
// Set the controller type to Pro-Controller. // Set the controller type to Pro-Controller, and set the npadInterfaceType.
device.type = BIT(0); device.deviceType = HidDeviceType_FullKey3;
// Set the controller colors. 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.singleColorBody = RGBA8_MAXALPHA(255,255,255);
device.singleColorButtons = RGBA8_MAXALPHA(0,0,0); 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. // Setup example controller state.
state.batteryCharge = 4; // Set battery charge to full. state.batteryCharge = 4; // Set battery charge to full.
state.buttons = KEY_A | KEY_ZR;
state.joysticks[JOYSTICK_LEFT].dx = 0x1234; state.joysticks[JOYSTICK_LEFT].dx = 0x1234;
state.joysticks[JOYSTICK_LEFT].dy = -0x1234; state.joysticks[JOYSTICK_LEFT].dy = -0x1234;
state.joysticks[JOYSTICK_RIGHT].dx = 0x5678; state.joysticks[JOYSTICK_RIGHT].dx = 0x5678;
@ -91,20 +93,43 @@ int main(int argc, char* argv[])
rc2 = hiddbgSetHdlsState(HdlsHandle, &state); rc2 = hiddbgSetHdlsState(HdlsHandle, &state);
if (R_FAILED(rc2)) printf("hiddbgSetHdlsState(): 0x%x\n", rc2); 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; state.joysticks[JOYSTICK_LEFT].dx += 0x10;
if (state.joysticks[JOYSTICK_LEFT].dx > JOYSTICK_MAX) state.joysticks[JOYSTICK_LEFT].dx = JOYSTICK_MIN; if (state.joysticks[JOYSTICK_LEFT].dx > JOYSTICK_MAX) state.joysticks[JOYSTICK_LEFT].dx = JOYSTICK_MIN;
state.joysticks[JOYSTICK_RIGHT].dy -= 0x10; state.joysticks[JOYSTICK_RIGHT].dy -= 0x10;
if (state.joysticks[JOYSTICK_LEFT].dy < JOYSTICK_MIN) state.joysticks[JOYSTICK_LEFT].dy = JOYSTICK_MAX; 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"); printf("Connected controllers:\n");
for(i=0; i<10; i++) { for(i=0; i<10; i++) {
if (hidIsControllerConnected(i)) { if (hidIsControllerConnected(i)) {
JoystickPosition tmpjoy[2]; JoystickPosition tmpjoy[2];
hidJoystickRead(&tmpjoy[0], i, JOYSTICK_LEFT); hidJoystickRead(&tmpjoy[0], i, JOYSTICK_LEFT);
hidJoystickRead(&tmpjoy[1], i, JOYSTICK_RIGHT); 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"); printf("\n");