GP32 Single Keypresses


Tealion

Still Fresh
Joined
Mar 15, 2004
Messages
33
Age
37
Location
Exeter Devon UK
Website
Visit site
I believe that the solution to this problem is quite simple but after some coding I am still unable to get this to work.

Basically I want to make it so that the user presses say the start button and then it turns something on like display a message then the message should dissapear again when the button is pressed again.

I have tried to do this but it keeps turning on and off as the keypresses are detected all the time. I have tried a button already pressed variable but I couldn't get that to work either.

I hope that made sense.

Pls help :(

TIA
 
Try this:

Code:
#include "gpdef.h" 
#include "gpstdlib.h" 
#include "gpgraphic.h" 
#include "gpfont.h" 
#include "gpmm.h" 
#include "gpstdio.h" 


/* global variable */ 
GPDRAWSURFACE gpDraw[2]; 
int nflip; 

void GpMain(void * arg) 
{ 
  int pressedKeys, newKeys, oldKeys;
  int select=FALSE; 

  GpLcdSurfaceGet(&gpDraw[0], 0); 
  GpLcdSurfaceGet(&gpDraw[1], 1); 
  GpSurfaceSet(&gpDraw[0]); 
  nflip = 1; 

  while (TRUE) 
  { 
    GpRectFill(NULL, &gpDraw[nflip], 0, 0 ,320, 240, 0xFF); 
    GpTextOut(NULL, &gpDraw[nflip], 10, 10, (char*) "press SELECT", 0xe0); 
    if (select) 
      GpTextOut(NULL, &gpDraw[nflip], 50, 50, (char*) "select", 0xe0); 
    else
      GpTextOut(NULL, &gpDraw[nflip], 50, 50, (char*) "no select", 0xe0); 

    GpSurfaceFlip(&gpDraw[nflip++]); 
    nflip &= 0x01; 

    // Get buttons state
    oldKeys = pressedKeys;
    newKeys = pressedKeys = GpKeyGet();

    // Filter buttons
    if ((newKeys & GPC_VK_SELECT) && (oldKeys & GPC_VK_SELECT))
      newKeys = newKeys & ~GPC_VK_SELECT;

    // Manage buttons
    if (newKeys & GPC_VK_SELECT)
      select = !select; 
  } 
}
 
Remember to also include a wait statement while polling, you don't want to be polling at the full speed of the GP32. Otherwise the instructions fire off too fast and cause the problem you are describing.
 
// Wait for a new key
while(GpKeyGet()!=GPC_VK_NONE);
while(GpKeyGet()==GPC_VK_NONE);

// Get the new key
pressedKeys = GpKeyGet();

I hope it works for you,
Aiken
 
Back
Top