colors/notification-led: Updated for hid-refactor.

This commit is contained in:
yellows8 2020-12-07 23:44:31 -05:00
parent 1e59bd0699
commit 2b0d5062a5
No known key found for this signature in database
GPG Key ID: 0AF90DA3F1E60E43
2 changed files with 61 additions and 45 deletions

View File

@ -1,53 +1,64 @@
#include <string.h>
// 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>
//See also libnx hid.h.
int main(int argc, char **argv)
// 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);
// 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);
Result rc=0;
printf(CONSOLE_ESC(1;1H) "Press PLUS to exit.");
// Main loop
while(appletMainLoop())
{
//Scan all the inputs. This should be done once for each frame
hidScanInput();
// Scan the gamepad. This should be done once for each frame
padUpdate(&pad);
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
// padGetButtonsDown returns the set of buttons that have been
// newly pressed in this frame compared to the previous one
u64 kDown = padGetButtonsDown(&pad);
if (kDown & KEY_PLUS) break; // break in order to return to hbmenu
if (kDown & HidNpadButton_Plus)
break; // break in order to return to hbmenu
HidControllerColors colors;
HidNpadControllerColor colors={0};
hidGetControllerColors(CONTROLLER_P1_AUTO, &colors);
printf(CONSOLE_ESC(2;1H) "singleSet = %d, splitSet = %d.\n", colors.singleSet, colors.splitSet);
//Note that if a color field is zero that indicates that the color field isn't set, regardless of the *Set fields.
if (colors.singleSet) {
printf(CONSOLE_ESC(3;1H) "singleColorBody = 0x%08x, singleColorButtons = 0x%08x.\n", colors.singleColorBody, colors.singleColorButtons);
// Note that if a color field is zero that indicates that the color field isn't set.
// You might want to also use hidGetNpadControllerColorSplit.
rc = hidGetNpadControllerColorSingle(padIsHandheld(&pad) ? HidNpadIdType_Handheld : HidNpadIdType_No1, &colors);
if (R_SUCCEEDED(rc)) {
printf(CONSOLE_ESC(3;1H) "main = 0x%08x, sub = 0x%08x.\n", colors.main, colors.sub);
}
else {
printf(CONSOLE_ESC(3;1H) CONSOLE_ESC(2K));
}
if (colors.splitSet) {
printf(CONSOLE_ESC(4;1H) "leftColorBody = 0x%08x, leftColorButtons = 0x%08x.\n", colors.leftColorBody, colors.leftColorButtons);
printf(CONSOLE_ESC(5;1H) "rightColorBody = 0x%08x, rightColorButtons = 0x%08x.\n", colors.rightColorBody, colors.rightColorButtons);
}
else {
printf(CONSOLE_ESC(4;1H) CONSOLE_ESC(2K));
printf(CONSOLE_ESC(5;1H) CONSOLE_ESC(2K));
}
// Update the console, sending a new frame to the display
consoleUpdate(NULL);
}
// Deinitialize and clean up resources used by the console (important!)
consoleExit(NULL);
return 0;
}

View File

@ -18,11 +18,17 @@ int main(int argc, char* argv[])
// take a look at the graphics/opengl set of examples, which uses EGL instead.
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);
Result rc=0;
bool initflag=0;
s32 i;
s32 total_entries;
u64 UniquePadIds[2];
HidsysUniquePadId unique_pad_ids[2]={0};
HidsysNotificationLedPattern pattern;
printf("notification-led example\n");
@ -33,7 +39,6 @@ int main(int argc, char* argv[])
printf("Init failed, press + to exit.\n");
}
else {
initflag = 1;
printf("Press A to set a Breathing effect notification-LED pattern.\n");
printf("Press B to set a Heartbeat effect notification-LED pattern.\n");
printf("Press + to disable notification-LED and exit.\n");
@ -42,18 +47,18 @@ int main(int argc, char* argv[])
// Main loop
while (appletMainLoop())
{
// Scan all the inputs. This should be done once for each frame
hidScanInput();
// Scan the gamepad. This should be done once for each frame
padUpdate(&pad);
// hidKeysDown returns information about which buttons have been
// just pressed in this frame compared to the previous one
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
// padGetButtonsDown returns the set of buttons that have been
// newly pressed in this frame compared to the previous one
u64 kDown = padGetButtonsDown(&pad);
if (kDown & KEY_PLUS) {
if (kDown & HidNpadButton_Plus) {
// Disable notification led. Only needed with hidsysSetNotificationLedPattern, with hidsysSetNotificationLedPatternWithTimeout the LED will be automatically disabled via the timeout.
memset(&pattern, 0, sizeof(pattern));
}
else if (kDown & KEY_A) {
else if (kDown & HidNpadButton_A) {
memset(&pattern, 0, sizeof(pattern));
// Setup Breathing effect pattern data.
@ -69,7 +74,7 @@ int main(int argc, char* argv[])
pattern.miniCycles[1].transitionSteps = 0xF; // 15 steps. Transition time 1.5s.
pattern.miniCycles[1].finalStepDuration = 0x0; // Forced 12.5ms.
}
else if (kDown & KEY_B) {
else if (kDown & HidNpadButton_B) {
memset(&pattern, 0, sizeof(pattern));
// Setup Heartbeat effect pattern data.
@ -102,40 +107,40 @@ int main(int argc, char* argv[])
}
}
if (kDown & (KEY_A | KEY_B | KEY_PLUS)) {
if (kDown & (HidNpadButton_A | HidNpadButton_B | HidNpadButton_Plus)) {
total_entries = 0;
memset(UniquePadIds, 0, sizeof(UniquePadIds));
memset(unique_pad_ids, 0, sizeof(unique_pad_ids));
// Get the UniquePadIds for the specified controller, which will then be used with hidsysSetNotificationLedPattern*.
// If you want to get the UniquePadIds for all controllers, you can use hidsysGetUniquePadIds instead.
rc = hidsysGetUniquePadsFromNpad(hidGetHandheldMode() ? CONTROLLER_HANDHELD : CONTROLLER_PLAYER_1, UniquePadIds, 2, &total_entries);
rc = hidsysGetUniquePadsFromNpad(padIsHandheld(&pad) ? HidNpadIdType_Handheld : HidNpadIdType_No1, unique_pad_ids, 2, &total_entries);
printf("hidsysGetUniquePadsFromNpad(): 0x%x", rc);
if (R_SUCCEEDED(rc)) printf(", %d", total_entries);
printf("\n");
if (R_SUCCEEDED(rc)) {
for(i=0; i<total_entries; i++) { // System will skip sending the subcommand to controllers where this isn't available.
printf("[%d] = 0x%lx ", i, UniquePadIds[i]);
printf("[%d] = 0x%lx ", i, unique_pad_ids[i].id);
// Attempt to use hidsysSetNotificationLedPatternWithTimeout first with a 2 second timeout, then fallback to hidsysSetNotificationLedPattern on failure. See hidsys.h for the requirements for using these.
rc = hidsysSetNotificationLedPatternWithTimeout(&pattern, UniquePadIds[i], 2000000000ULL);
rc = hidsysSetNotificationLedPatternWithTimeout(&pattern, unique_pad_ids[i], 2000000000ULL);
printf("hidsysSetNotificationLedPatternWithTimeout(): 0x%x\n", rc);
if (R_FAILED(rc)) {
rc = hidsysSetNotificationLedPattern(&pattern, UniquePadIds[i]);
rc = hidsysSetNotificationLedPattern(&pattern, unique_pad_ids[i]);
printf("hidsysSetNotificationLedPattern(): 0x%x\n", rc);
}
}
}
}
if (kDown & HidNpadButton_Plus)
break; // break in order to return to hbmenu
// Update the console, sending a new frame to the display
consoleUpdate(NULL);
if (kDown & KEY_PLUS)
break; // break in order to return to hbmenu
}
if (initflag) hidsysExit();
hidsysExit();
// Deinitialize and clean up resources used by the console (important!)
consoleExit(NULL);