gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Please help a noob programmer out :>

#153382 - DLightning - Sat Mar 29, 2008 1:51 pm

Hey there, I'm pretty new to DS programming, and I don't know what I've done wrong.

I've made a button checker program, if e.g. the A button is pressed, it should say:
The A button is pressed.

But when I run the .nds file in DeSmuME, it keeps saying: (e.g.) A button is released when I press it!

Here's the code:

main.c

Code:
#include <nds.h>
#include <stdio.h>
 
int main(void)
{
   consoleDemoInit();
 
   while(1)
   {
      scanKeys();
      int held = keysHeld();
 
        if( held & KEY_A)
       printf("Key A is pressed\n");
   else
       printf("Key A is released\n");

        if( held & KEY_B)
            printf("Key B is pressed\n");
        else
            printf("Key B is released\n");
 
   if( held & KEY_X)
       printf("Key X is pressed\n");
   else
       printf("Key X is released\n");

        if( held & KEY_Y)
       printf("Key Y is pressed\n");
        else   
            printf("Key Y is released\n");
 
   if( held & KEY_TOUCH)
       printf("Touch pad is touched\n");
   else
       printf("Touch pad is not touched\n");

      swiWaitForVBlank();
 
      consoleClear();
   }
 
   return 0;

}


Nds.h from libnds.

Contents of stdio.h:

Code:

mod note: removed, we all have the same file, not sure what it adds to the thread.


Please help me out!

#153401 - eKid - Sat Mar 29, 2008 4:42 pm

What's happening is your program is freezing when it hits the swiWaitForVBlank(). That function waits until a vblank interrupt occurs (thus a new frame) before it returns. You need to add
Code:
irqInit();
irqEnable( IRQ_VBLANK );

to the beginning of your code before using swiWaitForVBlank. Those two lines will initialize/enable libnds's interrupt handler and enable the vblank interrupt.