From 2b0d5062a5884d9be66b2288b8ca000687d77af2 Mon Sep 17 00:00:00 2001 From: yellows8 Date: Mon, 7 Dec 2020 23:44:31 -0500 Subject: [PATCH] colors/notification-led: Updated for hid-refactor. --- hid/colors/source/main.c | 59 ++++++++++++++++++------------ hid/notification-led/source/main.c | 47 +++++++++++++----------- 2 files changed, 61 insertions(+), 45 deletions(-) diff --git a/hid/colors/source/main.c b/hid/colors/source/main.c index 1f169b9..b940ebc 100644 --- a/hid/colors/source/main.c +++ b/hid/colors/source/main.c @@ -1,53 +1,64 @@ -#include +// Include the most common headers from the C standard library #include +#include +#include +// Include the main libnx system header, for Switch development #include //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; } diff --git a/hid/notification-led/source/main.c b/hid/notification-led/source/main.c index 0e045f7..4e07f50 100644 --- a/hid/notification-led/source/main.c +++ b/hid/notification-led/source/main.c @@ -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