You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
atmega-geiger/src/main.c

85 lines
2.0 KiB

#include "sevenseg.h"
#include <avr/interrupt.h>
#include <util/atomic.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include "time_base.h"
#include "high_voltage.h"
#include "radiation.h"
#include "user_interface.h"
#include "ssd1306.h"
/* globals */
volatile bool req_update_display = false;
volatile bool status_weak_battery = false;
volatile uint8_t disp_show_tick_mark = 0;
static void shutdown_due_to_weak_battery();
void __attribute__((noreturn)) main()
{
init_user_interface();
show_loading_screen(0, true);
init_timebase();
init_radiation();
// --- let's go ---
sei();
// this needs interrupts enabled
init_high_voltage();
for (;;) {
// one tick makes the marker lit for 10 ms (more in practice, plus display ghosting latency)
for (uint8_t i = 0; i < 10; i++) {
_delay_ms(10);
if (disp_show_tick_mark == 1) {
ssd1306_setColor(0xFFFF);
ssd1306_fillRect(124, 0, 127, 3);
disp_show_tick_mark = 2;
} else if (disp_show_tick_mark == 2) {
disp_show_tick_mark = 0;
ssd1306_setColor(0x0000);
ssd1306_fillRect(124, 0, 127, 3);
ssd1306_setColor(0xFFFF);
}
}
// check if redraw is needed
// bool update_display = true;
// ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// update_display = req_update_display;
// req_update_display = false;
// }
// if (update_display) {
show_current_radiation();
// }
if (status_weak_battery) {
shutdown_due_to_weak_battery();
}
}
}
void __attribute__((noreturn)) shutdown_due_to_weak_battery()
{
cli();
hv_disable();
show_empty_battery();
// try to preserve power; but the display will still drain the battery.
// we should probably shut it off too.
_delay_ms(2000);
turn_off_display();
for (;;) {
sleep_enable();
sleep_cpu();
}
}