aboutsummaryrefslogtreecommitdiff
path: root/altimeter.c
blob: 3133d2af9d1a427f50d8bf755fd89eaec3b74094 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

#include "barometer.h"
#include "display.h"
#include "util.h"

/* ISR for collecting and displaying pressure, altitude data etc */
ISR(TIMER1_COMPA_vect)
{
	PORTD ^= 1 << 5;
	int tcnt1_start = TCNT1;
	static volatile float setting = 1040.21; /* FIXME volatile for gdb hacks */
	static float last_height = 0;
	float pressure = 0;
	float height_m = 0;
	float rate_m_s;
	char rate_sym;
	pressure = barometer_read();
	height_m = pressure_to_metres_asl(pressure, setting);
	rate_m_s = height_m - last_height;
	last_height = height_m;

	if (rate_m_s < 0) {
		rate_sym = C_DESC;
	} else if (rate_m_s == 0) {
		rate_sym = C_IDLE;
	} else {
		rate_sym = C_ASC;
	}

	char line[LCD_WIDTH+1];
	/* line 1 */
	int alt_m_int;
	float alt_m_float;
	split_float(height_m, &alt_m_int, &alt_m_float);
	snprintf(line, sizeof(line), "%d.%d m %c%d m/s", alt_m_int, (int)(round(alt_m_float*10)), rate_sym, abs(rate_m_s));
	blank_to_eol(line, sizeof(line));
	display_set_cursor(0, 0);
	display_write(line);


	/* line 2 */
	int pressure_hpa_int;
	float pressure_hpa_float;
	split_float(pressure, &pressure_hpa_int, &pressure_hpa_float);
	snprintf(line, sizeof(line), "%d.%d hPa", pressure_hpa_int, (int)(round(pressure_hpa_float*100)));
	blank_to_eol(line, sizeof(line));
	display_set_cursor(0, 1);
	display_write(line);

	/* line 3 */
	int setting_hpa_int;
	float setting_hpa_float;
	split_float(setting, &setting_hpa_int, &setting_hpa_float);
	snprintf(line, sizeof(line), "SET %d.%d hPa", setting_hpa_int, (int)(round(setting_hpa_float*100)));
	blank_to_eol(line, sizeof(line));
	display_set_cursor(0, 2);
	display_write(line);

	int tcnt1_end = TCNT1;
	/* line 4 */
	snprintf(line, sizeof(line), "TCNT1 delta %u", tcnt1_end - tcnt1_start);
	blank_to_eol(line, sizeof(line));
	display_set_cursor(0, 3);
	display_write(line);
	return;
}

int main(void)
{
	/* Initialise display before enabling interrupts */
	display_init();
	display_clear();

	DDRB |= 1 << 0;
	DDRD |= 1 << 5;

	/* Initialise timers for /1024 prescaler, 1 Hz comparator val */
	TCCR1B |= (1 << CS10) | (1 << CS12) | (1 << WGM12);
	TIMSK1 |= (1 << OCIE1A);
	OCR1A = F_CPU / 1024;

#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

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