#include #include #include #include #include #include #include #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(); } }