Skip to content

Commit 86a9af5

Browse files
committed
Rebase on current master, replace subscribe example, use display instead of debug
1 parent 6cbac75 commit 86a9af5

File tree

2 files changed

+26
-44
lines changed

2 files changed

+26
-44
lines changed

examples/now.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,55 @@
66
use core::fmt::Write;
77
use libtock::console::Console;
88
use libtock::result::TockResult;
9-
use libtock::timer;
9+
use libtock::timer::DriverContext;
1010
use libtock::timer::Duration;
11+
use libtock::Drivers;
1112

1213
const DELAY_MS: usize = 500;
1314

1415
#[libtock::main]
1516
async fn main() -> TockResult<()> {
16-
let mut console = Console::new();
17+
let Drivers {
18+
mut timer_context,
19+
console_driver,
20+
..
21+
} = libtock::retrieve_drivers()?;
22+
let mut console = console_driver.create_console();
23+
1724
let mut previous_ticks = None;
1825

1926
for i in 0.. {
20-
print_now(&mut console, &mut previous_ticks, i)?;
21-
timer::sleep(Duration::from_ms(DELAY_MS as isize)).await?;
27+
print_now(&mut console, &mut timer_context, &mut previous_ticks, i)?;
28+
let mut driver = timer_context.create_timer_driver();
29+
let timer_driver = driver.activate()?;
30+
31+
timer_driver.sleep(Duration::from_ms(DELAY_MS)).await?;
2232
}
2333

2434
Ok(())
2535
}
2636

2737
fn print_now(
2838
console: &mut Console,
39+
timer_context: &mut DriverContext,
2940
previous_ticks: &mut Option<isize>,
3041
i: usize,
3142
) -> TockResult<()> {
32-
let mut timer_with_callback = timer::with_callback(|_, _| {});
43+
let mut timer_with_callback = timer_context.with_callback(|_, _| {});
3344
let timer = timer_with_callback.init()?;
3445
let current_clock = timer.get_current_clock()?;
3546
let ticks = current_clock.num_ticks();
3647
let frequency = timer.clock_frequency().hz();
3748
writeln!(
38-
console,
39-
"[{}] Waited roughly {:?}. Now is {:?} = {:#010x} ticks ({:?} ticks since last time at {} Hz)",
40-
i,
41-
PrettyTime::from_ms(i * DELAY_MS),
42-
PrettyTime::from_ms(current_clock.ms_f64() as usize),
43-
ticks,
44-
previous_ticks.map(|previous| ticks - previous),
45-
frequency
46-
)?;
49+
console,
50+
"[{}] Waited roughly {}. Now is {} = {:#010x} ticks ({:?} ticks since last time at {} Hz)",
51+
i,
52+
PrettyTime::from_ms(i * DELAY_MS),
53+
PrettyTime::from_ms(current_clock.ms_f64() as usize),
54+
ticks,
55+
previous_ticks.map(|previous| ticks - previous),
56+
frequency
57+
)?;
4758
*previous_ticks = Some(ticks);
4859
Ok(())
4960
}
@@ -64,7 +75,7 @@ impl PrettyTime {
6475
}
6576
}
6677

67-
impl core::fmt::Debug for PrettyTime {
78+
impl core::fmt::Display for PrettyTime {
6879
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
6980
if self.mins != 0 {
7081
write!(f, "{}m", self.mins)?

examples/timer_subscribe.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)