gba-trycatch

diff src/main.c @ 1:b7130fe3f073

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 13 Jun 2014 19:10:11 +0300
parents 0d2602a1b851
children 5143908d0220
line diff
     1.1 --- a/src/main.c	Thu Jun 12 05:37:18 2014 +0300
     1.2 +++ b/src/main.c	Fri Jun 13 19:10:11 2014 +0300
     1.3 @@ -1,21 +1,34 @@
     1.4  #include "gbasys.h"
     1.5 +#include "logger.h"
     1.6  
     1.7  static void draw(void);
     1.8 -static void keyb_handler(void);
     1.9 +static void keyb(int key, int pressed);
    1.10 +static void keyb_intr(void);
    1.11  
    1.12 -static unsigned int keystate;
    1.13 +static unsigned short keystate;
    1.14 +
    1.15 +#define EVQ_SIZE	8
    1.16 +volatile static unsigned short evhead, evtail;
    1.17 +static unsigned short events[EVQ_SIZE];
    1.18  
    1.19  int main(void)
    1.20  {
    1.21  	gba_init();
    1.22 -	//interrupt(INTR_KEY, keyb_handler);
    1.23 +	interrupt(INTR_KEY, keyb_intr);
    1.24  
    1.25  	set_video_mode(VMODE_LFB_160x128_16, 1);
    1.26  
    1.27  	clear_buffer(front_buffer, 0);
    1.28 -	draw_string("please wait...", 0, 0, front_buffer);
    1.29 +	set_text_writebg(1);
    1.30 +	logmsg(LOG_ALL, "please wait...\n");
    1.31  
    1.32  	for(;;) {
    1.33 +		/* process events */
    1.34 +		while(evhead != evtail) {
    1.35 +			unsigned short ev = events[evhead];
    1.36 +			evhead = (evhead + 1) % EVQ_SIZE;
    1.37 +			keyb(ev, 1);
    1.38 +		}
    1.39  		draw();
    1.40  	}
    1.41  	return 0;
    1.42 @@ -25,6 +38,37 @@
    1.43  {
    1.44  }
    1.45  
    1.46 -static void keyb_handler(void)
    1.47 +static void keyb(int key, int pressed)
    1.48  {
    1.49 +	static const char *keyname[] = {
    1.50 +		"A", "B", "select", "start", "right", "left", "up", "down", "RT", "LT"
    1.51 +	};
    1.52 +	int i;
    1.53 +	for(i=0; i<32; i++) {
    1.54 +		if(key & 1) {
    1.55 +			break;
    1.56 +		}
    1.57 +		key >>= 1;
    1.58 +	}
    1.59 +	if(i < sizeof keyname / sizeof *keyname) {
    1.60 +		logmsg(LOG_ALL, "key %s %s\n", keyname[i], pressed ? "pressed" : "released");
    1.61 +	}
    1.62  }
    1.63 +
    1.64 +static void keyb_intr(void)
    1.65 +{
    1.66 +	int i;
    1.67 +	unsigned short prev_keystate = keystate;
    1.68 +	unsigned short diff;
    1.69 +	keystate = get_key_state(KEY_ALL);
    1.70 +	diff = keystate ^ prev_keystate;
    1.71 +
    1.72 +	for(i=0; i<32; i++) {
    1.73 +		int key = 1 << i;
    1.74 +
    1.75 +		if(diff & key) {
    1.76 +			events[evtail] = key;
    1.77 +			evtail = (evtail + 1) % EVQ_SIZE;
    1.78 +		}
    1.79 +	}
    1.80 +}