Joystick

Simple sample to show you how to read all the joystick inputs.

Using library

GP2X minimal library by rlyeh, 2005

License

  • Free for non-commercial projects (it would be nice receiving a mail from you).  Other cases, ask me first.
  • GamePark Holdings is not allowed to use this library and/or use parts from it.

Source code

#include "minimal.h"

int main(int argc, char *argv[])
{
gp2x_init(1000, 16, 11025,16,1,60, 1);

while(1)
{
unsigned long pad=gp2x_joystick_read();

gp2x_printf(NULL,(320-28*6)/2,(240-15*10)/2,
"UP : %s\n"
"DOWN : %s\n"
"LEFT : %s\n"
"RIGHT : %s\n"
"PUSH BUTTON : %s\n"
"L TRIGGER : %s\n"
"R TRIGGER : %s\n"
"A BUTTON : %s\n"
"B BUTTON : %s\n"
"X BUTTON : %s\n"
"Y BUTTON : %s\n"
"SELECT BUTTON : %s\n"
"START BUTTON : %s\n"
"VOLUME - BUTTON : %s\n"
"VOLUME + BUTTON : %s\n",
(pad & GP2X_UP ? "pressed " : "not pressed" ),
(pad & GP2X_DOWN ? "pressed " : "not pressed" ),
(pad & GP2X_LEFT ? "pressed " : "not pressed" ),
(pad & GP2X_RIGHT ? "pressed " : "not pressed" ),
(pad & GP2X_PUSH ? "pressed " : "not pressed" ),
(pad & GP2X_L ? "pressed " : "not pressed" ),
(pad & GP2X_R ? "pressed " : "not pressed" ),
(pad & GP2X_A ? "pressed " : "not pressed" ),
(pad & GP2X_B ? "pressed " : "not pressed" ),
(pad & GP2X_X ? "pressed " : "not pressed" ),
(pad & GP2X_Y ? "pressed " : "not pressed" ),
(pad & GP2X_SELECT ? "pressed " : "not pressed" ),
(pad & GP2X_START ? "pressed " : "not pressed" ),
(pad & GP2X_VOL_DOWN ? "pressed " : "not pressed" ),
(pad & GP2X_VOL_UP ? "pressed " : "not pressed" )
);

if(pad & GP2X_VOL_DOWN) if(pad & GP2X_START) exit(0);

gp2x_video_RGB_flip(0);
}
}

void gp2x_sound_frame(void *blah, void *buff, int samples) {}

Explanation

  • Our gp2x_init line inits the whole library.  We set 1000 ticks per second, 16 bits video mode, 11025/16bits/stereo/60Hz sound mode, and solid font.
  • Our gp2x_joystick_read line reads the joystick.  In this case we do a loop until any press is found.
  • Our gp2x_printf line writes all joystick inputs by checking the pad variable.  It writes the string at center of screen and using default font.
  • Our gp2x_video_RGB_flip line updates the whole screen and sets up the next hidden screen area.
  • Our gp2x_sound_frame line fills our sound buffer.  Note that this function will not generate any sound since we have written a void function.

C Compilation

arm-linux-gcc minimal.c joystick.c -static -lpthread -lm

C++ Compilation

arm-linux-g++ minimal.c joystick.c -static -lpthread -lm

Output

  • this program prints all the joystick inputs.
  • press volume down and start buttons to exit.
void gp2x_init(int ticks_per_second,
int bpp,
int rate,
int bits,
int stereo,
int Hz,
int solid_font)
This function sets up the whole library.
unsigned long gp2x_joystick_read(void)
This function returns the active GP2X joystick values.
void gp2x_printf(gp2x_font *f,
int x,
int y,
const char *format,
 ...)
This function printfs a string into the RGB layer.
void gp2x_video_RGB_flip(int layer)
This function flips video buffers.
extern void gp2x_sound_frame(void *blah,
void *buffer,
int samples)
This function is automatically called by the library, and expects a sound buffer to be filled in.