gbacycle

view src/main.c @ 0:858dd5fbfef2

gbacycle import
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 Mar 2012 14:38:21 +0200
parents
children
line source
1 #include "gbasys.h"
2 #include "dashboard.h"
3 #include "gbacycle.h"
5 /* 1800 2sec intervals per hour, \times perimeter, \times x, \over 100000 cm in a kilometer */
6 #define RP2S_TO_KMH(x) ((18 * WHEEL_PERIMETER * (x)) / 1000)
8 /* wheel perimeter in centimeters */
9 #define WHEEL_PERIMETER 194
11 static void init_input(void);
12 static void comm_handler(int s);
13 static void timer_sig(int s);
15 int main(void)
16 {
17 gba_init();
18 set_video_mode(VMODE_LFB_240x160_16, 1);
20 init_input();
22 init_dash();
23 start_dash();
25 signal(SIGALRM, timer_sig);
26 alarm(2);
28 for(;;) {
29 if(get_key_state(KEY_SELECT)) {
30 dbg = 1;
31 }
32 if(get_key_state(KEY_START)) {
33 dbg = 0;
34 }
35 draw();
36 }
37 return 0;
38 }
40 static void init_input(void)
41 {
42 comm_setup(COMM_GPIO);
43 gpio_dir_mask(0);
45 signal(SIGIO, comm_handler);
46 }
48 static void comm_handler(int s)
49 {
50 static volatile unsigned long last_upd;
51 unsigned long msec;
53 msec = get_millisec();
56 if(msec - last_upd >= 80) {
57 nrot++;
58 dist_cm = nrot * WHEEL_PERIMETER;
59 last_upd = msec;
60 }
61 }
63 static void timer_sig(int s)
64 {
65 volatile static unsigned long sec;
66 volatile static unsigned long last_rot[4];
67 int idx;
69 alarm(2);
71 idx = ++sec & 3;
73 speed_rp2s = nrot - last_rot[idx];
74 speed_kmh = RP2S_TO_KMH(speed_rp2s);
76 last_rot[idx] = nrot;
77 }