Help with my homebrew - check if NO keys are released

Discussion in '3DS - Homebrew Development and Emulators' started by WhoAmI?, Aug 14, 2015.

  1. WhoAmI?
    OP

    WhoAmI? PASTA's dirty animal

    Member
    1,273
    1,009
    Mar 15, 2015
    Poké Ball
    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
     
  2. machinamentum

    machinamentum GBAtemp Regular

    Member
    163
    450
    Jul 5, 2015
    United States
    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
    }
    
     
    WhoAmI? likes this.
  3. WhoAmI?
    OP

    WhoAmI? PASTA's dirty animal

    Member
    1,273
    1,009
    Mar 15, 2015
    Poké Ball
    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
    }
     
  4. hippy dave

    hippy dave Butts Butts Megabutts

    Member
    2,584
    1,800
    Apr 30, 2012
    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).
     
    WhoAmI? likes this.
  5. WhoAmI?
    OP

    WhoAmI? PASTA's dirty animal

    Member
    1,273
    1,009
    Mar 15, 2015
    Poké Ball
    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.
     
  6. machinamentum

    machinamentum GBAtemp Regular

    Member
    163
    450
    Jul 5, 2015
    United States
    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.
     
    WhoAmI? likes this.
  7. WhoAmI?
    OP

    WhoAmI? PASTA's dirty animal

    Member
    1,273
    1,009
    Mar 15, 2015
    Poké Ball
    Thank you! :D