gba-trycatch

view 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 source
1 #include "gbasys.h"
2 #include "logger.h"
4 static void draw(void);
5 static void keyb(int key, int pressed);
6 static void keyb_intr(void);
8 static unsigned short keystate;
10 #define EVQ_SIZE 8
11 volatile static unsigned short evhead, evtail;
12 static unsigned short events[EVQ_SIZE];
14 int main(void)
15 {
16 gba_init();
17 interrupt(INTR_KEY, keyb_intr);
19 set_video_mode(VMODE_LFB_160x128_16, 1);
21 clear_buffer(front_buffer, 0);
22 set_text_writebg(1);
23 logmsg(LOG_ALL, "please wait...\n");
25 for(;;) {
26 /* process events */
27 while(evhead != evtail) {
28 unsigned short ev = events[evhead];
29 evhead = (evhead + 1) % EVQ_SIZE;
30 keyb(ev, 1);
31 }
32 draw();
33 }
34 return 0;
35 }
37 static void draw(void)
38 {
39 }
41 static void keyb(int key, int pressed)
42 {
43 static const char *keyname[] = {
44 "A", "B", "select", "start", "right", "left", "up", "down", "RT", "LT"
45 };
46 int i;
47 for(i=0; i<32; i++) {
48 if(key & 1) {
49 break;
50 }
51 key >>= 1;
52 }
53 if(i < sizeof keyname / sizeof *keyname) {
54 logmsg(LOG_ALL, "key %s %s\n", keyname[i], pressed ? "pressed" : "released");
55 }
56 }
58 static void keyb_intr(void)
59 {
60 int i;
61 unsigned short prev_keystate = keystate;
62 unsigned short diff;
63 keystate = get_key_state(KEY_ALL);
64 diff = keystate ^ prev_keystate;
66 for(i=0; i<32; i++) {
67 int key = 1 << i;
69 if(diff & key) {
70 events[evtail] = key;
71 evtail = (evtail + 1) % EVQ_SIZE;
72 }
73 }
74 }