Homebrew Help with my homebrew - check if NO keys are released

WhoAmI?

PASTA's dirty animal
OP
Member
Joined
Mar 15, 2015
Messages
1,276
Trophies
0
Location
Poké Ball
Website
lavanoid.github.io
XP
1,279
Country
So I've been working on a homebrew app for my robotics project but I don't know how to check if NO keys are pressed so it can send the "Stop" command to a web server. Here's the code:

Code:
int main()
{
    gfxInitDefault();
    consoleInit(GFX_TOP,NULL);
    printf("Start pressing buttons\n");
    u32 actionOld = 0;
    // Main loop
    while (aptMainLoop())
    {
        gspWaitForVBlank();
        hidScanInput();
        u32 kDown = hidKeysDown();
        u32 kUp = hidKeysUp();
        u32 kHeld = hidKeysHeld();
        u32 kHeldOld = 0;
        u32 action = 0;
       
        //consoleClear();
       
        if (kHeld != kHeldOld) {
            if (kHeld & KEY_UP) {
                if (kHeld & KEY_X) {
                    action = 1;
                    if (action != actionOld) {
                        printf("Moving forward...\n");
                    }
                } else {
                    if (kHeld & KEY_B) {
                        action = 2;
                        if (action != actionOld) {
                            printf("Turning right...\n");
                        }
                    } else {
                        action = 3;
                        if (action != actionOld) {
                            printf("Left wheel moving forward...\n");
                        }
                    }
                }
            } else {
                if (kHeld & KEY_DOWN) {
                    if (kHeld & KEY_B) {
                        action = 4;
                        if (action != actionOld) {
                            printf("Moving backwards...\n");
                        }
                    } else {
                        if (kHeld & KEY_X) {
                            action = 5;
                            if (action != actionOld) {
                                printf("Turning left...\n");
                            }
                        } else {
                            action = 6;
                            if (action != actionOld) {
                                printf("Left wheel moving backwards...\n");
                            }
                        }
                    }
                } else {
                    if (kHeld & KEY_X) {
                        if (kHeld & KEY_UP) {
                            action = 1;
                            if (action != actionOld) {
                                printf("Moving forward...\n");
                            }
                        } else {
                            if (kHeld & KEY_DOWN) {
                                action = 5;
                                if (action != actionOld) {
                                    printf("Turning left...\n");
                                }
                            } else {
                                action = 7;
                                if (action != actionOld) {
                                    printf("Right wheel moving forward...\n");
                                }
                            }
                        }
                    } else {
                        if (kHeld & KEY_B) {
                            if (kHeld & KEY_DOWN) {
                                action = 4;
                                if (action != actionOld) {
                                    printf("Moving backwards...\n");
                                }
                            } else {
                                if (kHeld & KEY_UP) {
                                    action = 2;
                                    if (action != actionOld) {
                                        printf("Turning right...\n");
                                    }
                                } else {
                                    action = 8;
                                    if (action != actionOld) {
                                        printf("Right wheel moving backwards...\n");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
       
        kHeldOld = kHeld;
        actionOld = action;
       
        if(kDown & KEY_START)
            break;

        // Flush and swap framebuffers
        gfxFlushBuffers();
        gfxSwapBuffers();
    }

    // Exit services
    gfxExit();
    return 0;
}

It needs to be designed so that it doesn't spam my robot with the same command. Anyone got any tips or example code? Yes, I referred to ctrulibs example code but it didn't solve my question.

Thanks! :D
 

machinamentum

Well-Known Member
Member
Joined
Jul 5, 2015
Messages
163
Trophies
0
XP
549
Country
United States
So I've been working on a homebrew app for my robotics project but I don't know how to check if NO keys are pressed so it can send the "Stop" command to a web server. Here's the code:
...
It needs to be designed so that it doesn't spam my robot with the same command. Anyone got any tips or example code? Yes, I referred to ctrulibs example code but it didn't solve my question.

Thanks! :D
If you just want to see if no keys are being held just check if the bitfield is zero.
Code:
if (hidKeysDown() == 0)
{
     // do stuff
}

if (hidKeysPressed() == 0)
{
     // do other stuff
}
 
  • Like
Reactions: WhoAmI?

WhoAmI?

PASTA's dirty animal
OP
Member
Joined
Mar 15, 2015
Messages
1,276
Trophies
0
Location
Poké Ball
Website
lavanoid.github.io
XP
1,279
Country
If you just want to see if no keys are being held just check if the bitfield is zero.
Code:
if (hidKeysDown() == 0)
{
     // do stuff
}

if (hidKeysPressed() == 0)
{
     // do other stuff
}
THANK YOU! :D

Do you think I could read the value of a specific value? Such as something like this:

if (X = 1) {
// X is pressed
}
 

hippy dave

BBMB
Member
Joined
Apr 30, 2012
Messages
9,866
Trophies
2
XP
29,012
Country
United Kingdom
From your code,

Code:
if (kHeld & KEY_X)

will tell you if X is pressed,

Code:
if (kHeld == KEY_X)

should tell you if ONLY X is pressed, and no other buttons are (Haven't tested the second one, but should work unless there's something weird going on).
 
  • Like
Reactions: WhoAmI?

WhoAmI?

PASTA's dirty animal
OP
Member
Joined
Mar 15, 2015
Messages
1,276
Trophies
0
Location
Poké Ball
Website
lavanoid.github.io
XP
1,279
Country
From your code,

Code:
if (kHeld & KEY_X)

will tell you if X is pressed,

Code:
if (kHeld == KEY_X)

should tell you if ONLY X is pressed, and no other buttons are (Haven't tested the second one, but should work unless there's something weird going on).

Yeah, I know it does but unfortunately it doesn't quite work with my use scenario. Seems that I can only use that in the event that the key is pressed but it doesn't work so well in loops, especially when the key is held or multiple keys are pressed/released.

Was hoping to have something more "arduino like" where you can always read the values of a button wether it's pressed or not. I can onky read the values of buttons when an event is triggered, which sucks IMO.
 

machinamentum

Well-Known Member
Member
Joined
Jul 5, 2015
Messages
163
Trophies
0
XP
549
Country
United States
Yeah, I know it does but unfortunately it doesn't quite work with my use scenario. Seems that I can only use that in the event that the key is pressed but it doesn't work so well in loops, especially when the key is held or multiple keys are pressed/released.

Was hoping to have something more "arduino like" where you can always read the values of a button wether it's pressed or not. I can onky read the values of buttons when an event is triggered, which sucks IMO.
hidKeysHeld() effectively returns whether the button is being pressed or not. You can also separate the buttons out using bit shifts with masks like so
Code:
u8 XState = (hidKeysHeld() & KEY_X) >> 10; // 10 is the bit position in the bitfield, you can get this value form hid.h in ctrulib
if (XState == 1)
{
     // do stuff
}
Note that separating the bits like this wastes CPU time as it performs the same intent as just doing this
Code:
if (hidKeysHeld() & KEY_X)
{
     // do stuff
}
As hippy dave pointed out, your code is already doing this, so it is not very clear to me as to what you're trying to achieve.
 
  • Like
Reactions: WhoAmI?

WhoAmI?

PASTA's dirty animal
OP
Member
Joined
Mar 15, 2015
Messages
1,276
Trophies
0
Location
Poké Ball
Website
lavanoid.github.io
XP
1,279
Country
hidKeysHeld() effectively returns whether the button is being pressed or not. You can also separate the buttons out using bit shifts with masks like so
Code:
u8 XState = (hidKeysHeld() & KEY_X) >> 10; // 10 is the bit position in the bitfield, you can get this value form hid.h in ctrulib
if (XState == 1)
{
     // do stuff
}
Note that separating the bits like this wastes CPU time as it performs the same intent as just doing this
Code:
if (hidKeysHeld() & KEY_X)
{
     // do stuff
}
As hippy dave pointed out, your code is already doing this, so it is not very clear to me as to what you're trying to achieve.
Thank you! :D
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    NinStar @ NinStar: It will actually make it worse