use std::sync::{Mutex, PoisonError};
use std::time::Duration;
use crate::board::{self, BootRecord};
use crate::error::record;
pub const LIVENESS_TIMEOUT_S: u32 = 30;
static BOOT: Mutex<Option<BootRecord>> = Mutex::new(None);
fn begin_run() {
if !board::installed() {
return;
}
if let Ok(Some(r)) = board::with(|b| b.boot_record()) {
*BOOT.lock().unwrap_or_else(PoisonError::into_inner) = Some(r);
}
if let Err(e) = board::with(|b| b.liveness_begin(LIVENESS_TIMEOUT_S)) {
record(e);
}
}
fn end_pass() {
if board::installed() {
let _ = board::with(|b| b.liveness_feed());
}
}
pub fn run(setup: fn(), loop_once: fn()) -> ! {
begin_run();
setup();
loop {
loop_once();
end_pass();
}
}
pub fn run_for(setup: fn(), loop_once: fn(), iterations: u64) {
begin_run();
setup();
for _ in 0..iterations {
loop_once();
end_pass();
}
}
#[must_use]
pub fn boot_record() -> Option<BootRecord> {
*BOOT.lock().unwrap_or_else(PoisonError::into_inner)
}
pub fn delay(ms: u64) {
std::thread::sleep(Duration::from_millis(ms));
}
pub fn restart() -> bool {
crate::error::ok(crate::board::with(|b| b.restart()))
}
#[must_use]
pub fn millis() -> u64 {
crate::board::millis()
}