blob: d8f5b1d19c08d131340c236b35c7bb175eb683c5 [file] [log] [blame]
Guillaume Endignouxf3879d22019-12-18 16:45:02 +01001#![no_std]
Alistair Francise5f74c32020-09-11 15:21:14 -07002use libtock::println;
Guillaume Endignouxf3879d22019-12-18 16:45:02 +01003/**
4 * This example shows a repeated timer combined with reading and displaying the current time in
5 * clock ticks.
6 **/
Guillaume Endignouxf3879d22019-12-18 16:45:02 +01007use libtock::result::TockResult;
torfmaster86a9af52020-01-11 12:26:58 +01008use libtock::timer::DriverContext;
Guillaume Endignouxf3879d22019-12-18 16:45:02 +01009use libtock::timer::Duration;
10
Johnathan Van Why074fa7d2020-10-14 17:06:50 -070011libtock_core::stack_size! {0x800}
12
Guillaume Endignoux6cbac752019-12-18 18:15:40 +010013const DELAY_MS: usize = 500;
14
Guillaume Endignouxf3879d22019-12-18 16:45:02 +010015#[libtock::main]
16async fn main() -> TockResult<()> {
Woytenb1226202020-01-13 22:47:48 +010017 let mut drivers = libtock::retrieve_drivers()?;
Woyten8d517b02020-01-13 20:58:20 +010018
Alistair Francise5f74c32020-09-11 15:21:14 -070019 drivers.console.create_console();
torfmaster86a9af52020-01-11 12:26:58 +010020
Guillaume Endignouxdda94992019-12-18 16:53:12 +010021 let mut previous_ticks = None;
22
Guillaume Endignouxf3879d22019-12-18 16:45:02 +010023 for i in 0.. {
Alistair Francise5f74c32020-09-11 15:21:14 -070024 print_now(&mut drivers.timer, &mut previous_ticks, i)?;
Woytenb1226202020-01-13 22:47:48 +010025 let mut timer_driver = drivers.timer.create_timer_driver();
26 let timer_driver = timer_driver.activate()?;
torfmaster86a9af52020-01-11 12:26:58 +010027
28 timer_driver.sleep(Duration::from_ms(DELAY_MS)).await?;
Guillaume Endignoux6cbac752019-12-18 18:15:40 +010029 }
30
31 Ok(())
32}
33
34fn print_now(
torfmaster86a9af52020-01-11 12:26:58 +010035 timer_context: &mut DriverContext,
Guillaume Endignoux6cbac752019-12-18 18:15:40 +010036 previous_ticks: &mut Option<isize>,
37 i: usize,
38) -> TockResult<()> {
torfmaster86a9af52020-01-11 12:26:58 +010039 let mut timer_with_callback = timer_context.with_callback(|_, _| {});
Guillaume Endignoux6cbac752019-12-18 18:15:40 +010040 let timer = timer_with_callback.init()?;
41 let current_clock = timer.get_current_clock()?;
42 let ticks = current_clock.num_ticks();
43 let frequency = timer.clock_frequency().hz();
Alistair Francise5f74c32020-09-11 15:21:14 -070044 println!(
torfmaster86a9af52020-01-11 12:26:58 +010045 "[{}] Waited roughly {}. Now is {} = {:#010x} ticks ({:?} ticks since last time at {} Hz)",
46 i,
47 PrettyTime::from_ms(i * DELAY_MS),
48 PrettyTime::from_ms(current_clock.ms_f64() as usize),
49 ticks,
50 previous_ticks.map(|previous| ticks - previous),
51 frequency
Alistair Francise5f74c32020-09-11 15:21:14 -070052 );
Guillaume Endignoux6cbac752019-12-18 18:15:40 +010053 *previous_ticks = Some(ticks);
Guillaume Endignouxf3879d22019-12-18 16:45:02 +010054 Ok(())
55}
Guillaume Endignoux7798b1e2019-12-18 17:03:28 +010056
57struct PrettyTime {
58 mins: usize,
59 secs: usize,
60 ms: usize,
61}
62
63impl PrettyTime {
64 fn from_ms(ms: usize) -> PrettyTime {
65 PrettyTime {
66 ms: ms % 1000,
67 secs: (ms / 1000) % 60,
68 mins: ms / (60 * 1000),
69 }
70 }
71}
72
torfmaster86a9af52020-01-11 12:26:58 +010073impl core::fmt::Display for PrettyTime {
Guillaume Endignoux7798b1e2019-12-18 17:03:28 +010074 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
75 if self.mins != 0 {
76 write!(f, "{}m", self.mins)?
77 }
78 write!(f, "{}.{:03}s", self.secs, self.ms)
79 }
80}