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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#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 "barometer.h"
#include "display.h"
#include "util.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
#define DEBUG_ALL_VAL ((1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6))
#define DEBUG_INIT do{DDRB |= DEBUG_ALL_VAL;}while(0)
#define DEBUG_MARK_SET_ALL do{PORTB |= DEBUG_ALL_VAL;}while(0)
#define DEBUG_MARK_1_SET do{PORTB |= 1 << 1;}while(0)
#define DEBUG_MARK_1_CLEAR do{PORTB &= ~(1 << 1);}while(0)
#define DEBUG_MARK_2_SET do{PORTB |= 1 << 3;}while(0)
#define DEBUG_MARK_2_CLEAR do{PORTB &= ~(1 << 3);}while(0)
#define DEBUG_MARK_3_SET do{PORTB |= 1 << 2;}while(0)
#define DEBUG_MARK_3_CLEAR do{PORTB &= ~(1 << 2);}while(0)
#define DEBUG_MARK_4_SET do{PORTB |= 1 << 6;}while(0)
#define DEBUG_MARK_4_CLEAR do{PORTB &= ~(1 << 6);}while(0)
#define DEBUG_MARK_5_SET do{PORTB |= 1 << 5;}while(0)
#define DEBUG_MARK_5_CLEAR do{PORTB &= ~(1 << 5);}while(0)
#define DEBUG_MARK_6_SET do{PORTB |= 1 << 4;}while(0)
#define DEBUG_MARK_6_CLEAR do{PORTB &= ~(1 << 4);}while(0)
/* ISR for collecting and displaying pressure, altitude data etc */
ISR(TIMER1_COMPA_vect)
{
DEBUG_MARK_SET_ALL;
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;
DEBUG_MARK_1_CLEAR;
pressure = barometer_read();
DEBUG_MARK_2_CLEAR;
height_m = pressure_to_metres_asl(pressure, setting);
DEBUG_MARK_3_CLEAR;
rate_m_s = height_m - last_height;
last_height = height_m;
DEBUG_MARK_4_CLEAR;
if (rate_m_s < 0) {
rate_sym = C_DESC;
} else if (rate_m_s == 0) {
rate_sym = C_IDLE;
} else {
rate_sym = C_ASC;
}
DEBUG_MARK_5_CLEAR;
char line[LCD_WIDTH+1];
/* line 1 */
snprintf(line, sizeof(line), "%.1f m %c%d m/s", height_m, rate_sym, abs(rate_m_s));
blank_to_eol(line, sizeof(line));
display_set_cursor(0, 0);
display_write(line);
/* line 2 */
snprintf(line, sizeof(line), "%.2f hPa", pressure);
blank_to_eol(line, sizeof(line));
display_set_cursor(0, 1);
display_write(line);
/* line 3 */
snprintf(line, sizeof(line), "SET %.2f hPa", setting);
blank_to_eol(line, sizeof(line));
display_set_cursor(0, 2);
display_write(line);
/* line 4 */
snprintf(line, sizeof(line), "%.1f ""\xdf""C", 12.3f);
blank_to_eol(line, sizeof(line));
display_set_cursor(0, 3);
display_write(line);
DEBUG_MARK_6_CLEAR;
WDT_PAT_MAYBE();
}
int main(void)
{
/* Initialise display before enabling interrupts */
display_init();
display_clear();
DEBUG_INIT;
/* 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
/* 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();
}
}
|