aboutsummaryrefslogtreecommitdiff
path: root/altimeter.c
blob: b0148d929b1b97e59a3932e65c705ad86828d3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>


#include "data_manager.h"
#include "display.h"
#include "timer.h"
#include "ui.h"
#include "barometer.h"

#ifdef WDT_DISABLE
# warning "WDT disabled in this build"
# define WDT_SETUP_MAYBE(x)
# define WDT_PAT_MAYBE()
#else
# define WDT_SETUP_MAYBE(x) do{wdt_enable(x);}while(0)
# define WDT_PAT_MAYBE()    do{wdt_reset();}while(0)
#endif

static struct data_ctx dctx = {};
static struct ui_ctx ui_ctx = {};

/* ISR for collecting and displaying pressure, altitude data etc */
ISR(TIMER1_COMPA_vect)
{
	data_manager_tick(&dctx);
	ui_update(&ui_ctx, &dctx);
	WDT_PAT_MAYBE();
}

int main(void)
{
	struct barometer barometer;
	struct display display;
	struct timer timer;

	/* get descriptors for system default peripherals */
	get_system_barometer(&barometer);
	get_system_display(&display);
	get_system_timer(&timer);

	/* Initialise peripherals/modules before enabling interrupts below */
	barometer.init();
	timer.init();
	data_manager_init(&dctx, &timer, &barometer);
	ui_init(&ui_ctx, &display);

#ifdef USBCON
	/* Disable USB controller if one is present - this spams (latches?) USB_GEN
	 * interrupt which we're not handling for now */
	USBCON &= ~(1 << USBE);
#endif
	// FIXME measure current saving here if any
	//ACSR |= (1 << ACD);

	/* main body should take no longer than 1 second to run */
	WDT_SETUP_MAYBE(WDTO_2S);

	sei();
	while(1) {
		set_sleep_mode(SLEEP_MODE_IDLE);
		sleep_mode();
	}
}


/* FIXME remove these - handy for testing in gdb */
void up(void){ui_input_event(&ui_ctx, &dctx, UI_INPUT_EVENT_UP);ui_update(&ui_ctx, &dctx);}
void down(void){ui_input_event(&ui_ctx, &dctx, UI_INPUT_EVENT_DOWN);ui_update(&ui_ctx, &dctx);}
void press(void){ui_input_event(&ui_ctx, &dctx, UI_INPUT_EVENT_PRESS);ui_update(&ui_ctx, &dctx);}
void hold(void){ui_input_event(&ui_ctx, &dctx, UI_INPUT_EVENT_HOLD);ui_update(&ui_ctx, &dctx);}