Timer

Simple sample to show you how to init and use timer.

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)
{
static unsigned char color=0x00;
unsigned long pad=gp2x_joystick_read();

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

//wait one second
gp2x_timer_delay(1000);

//invert the screen
memset(gp2x_video_RGB[0].screen16, color^=0xFF, 320*240*2);

//output the elapsed ticks
gp2x_printf(NULL, 0,0, "one second passed away... %lu\n",gp2x_timer_read());

//update changes
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, 8 bits video mode, 11025/16bits/stereo/60Hz sound mode, and solid font.
  • Our gp2x_timer_delay line reads the joystick.  In this case we do a loop until any press is found.
  • Our gp2x_printf and gp2x_timer_read line displays the elapsed time in gp2x_ticks since the last gp2x_init call.
  • 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 1second.c -static -lpthread -lm

C++ Compilation

arm-linux-g++ minimal.c 1second.c -static -lpthread -lm

Output

  • this program changes the screen color every second, and writes a text line to the active linux console.
  • 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.
void gp2x_timer_delay(unsigned long ticks)
This function halts the program for a given delay in gp2x_ticks units.
void gp2x_printf(gp2x_font *f,
int x,
int y,
const char *format,
 ...)
This function printfs a string into the RGB layer.
unsigned long gp2x_timer_read(void)
This function returns elapsed ticks in gp2x_ticks units since last gp2x_init call.
Internal variable which holds current conversion between desired ticks and GP2X ticks.
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.