gbacycle

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.c	Thu Mar 08 14:38:21 2012 +0200
     1.3 @@ -0,0 +1,77 @@
     1.4 +#include "gbasys.h"
     1.5 +#include "dashboard.h"
     1.6 +#include "gbacycle.h"
     1.7 +
     1.8 +/* 1800 2sec intervals per hour, \times perimeter, \times x, \over 100000 cm in a kilometer */
     1.9 +#define RP2S_TO_KMH(x)	((18 * WHEEL_PERIMETER * (x)) / 1000)
    1.10 +
    1.11 +/* wheel perimeter in centimeters */
    1.12 +#define WHEEL_PERIMETER		194
    1.13 +
    1.14 +static void init_input(void);
    1.15 +static void comm_handler(int s);
    1.16 +static void timer_sig(int s);
    1.17 +
    1.18 +int main(void)
    1.19 +{
    1.20 +	gba_init();
    1.21 +	set_video_mode(VMODE_LFB_240x160_16, 1);
    1.22 +
    1.23 +	init_input();
    1.24 +
    1.25 +	init_dash();
    1.26 +	start_dash();
    1.27 +
    1.28 +	signal(SIGALRM, timer_sig);
    1.29 +	alarm(2);
    1.30 +
    1.31 +	for(;;) {
    1.32 +		if(get_key_state(KEY_SELECT)) {
    1.33 +			dbg = 1;
    1.34 +		}
    1.35 +		if(get_key_state(KEY_START)) {
    1.36 +			dbg = 0;
    1.37 +		}
    1.38 +		draw();
    1.39 +	}
    1.40 +	return 0;
    1.41 +}
    1.42 +
    1.43 +static void init_input(void)
    1.44 +{
    1.45 +	comm_setup(COMM_GPIO);
    1.46 +	gpio_dir_mask(0);
    1.47 +
    1.48 +	signal(SIGIO, comm_handler);
    1.49 +}
    1.50 +
    1.51 +static void comm_handler(int s)
    1.52 +{
    1.53 +	static volatile unsigned long last_upd;
    1.54 +	unsigned long msec;
    1.55 +
    1.56 +	msec = get_millisec();
    1.57 +
    1.58 +
    1.59 +	if(msec - last_upd >= 80) {
    1.60 +		nrot++;
    1.61 +		dist_cm = nrot * WHEEL_PERIMETER;
    1.62 +		last_upd = msec;
    1.63 +	}
    1.64 +}
    1.65 +
    1.66 +static void timer_sig(int s)
    1.67 +{
    1.68 +	volatile static unsigned long sec;
    1.69 +	volatile static unsigned long last_rot[4];
    1.70 +	int idx;
    1.71 +
    1.72 +	alarm(2);
    1.73 +
    1.74 +	idx = ++sec & 3;
    1.75 +
    1.76 +	speed_rp2s = nrot - last_rot[idx];
    1.77 +	speed_kmh = RP2S_TO_KMH(speed_rp2s);
    1.78 +
    1.79 +	last_rot[idx] = nrot;
    1.80 +}