xous-core/services/ticktimer-server/src/lib.rs

40 lines
1.2 KiB
Rust
Raw Normal View History

2020-10-12 11:08:16 +00:00
#![cfg_attr(target_os = "none", no_std)]
pub mod api;
use xous::{send_message, Error, CID};
2021-03-28 10:14:41 +00:00
use num_traits::ToPrimitive;
2020-10-12 11:08:16 +00:00
2021-03-28 10:14:41 +00:00
pub struct Ticktimer {
conn: CID,
}
2021-03-28 10:14:41 +00:00
impl Ticktimer {
pub fn new() -> Result<Self, Error> {
let conn = xous::connect(xous::SID::from_bytes(b"ticktimer-server").unwrap())?;
Ok(Ticktimer {
conn,
})
}
2020-10-12 11:08:16 +00:00
2021-03-28 20:35:11 +00:00
/// note special case for elapsed_ms() is "infalliable". it really should never fail so get rid of the Error
pub fn elapsed_ms(&self) -> u64 {
2021-03-28 10:14:41 +00:00
let response = send_message(self.conn,
xous::Message::new_blocking_scalar(api::Opcode::ElapsedMs.to_usize().unwrap(),
0, 0, 0, 0,
)).expect("Ticktimer: failure to send message to Ticktimer");
2021-03-28 10:14:41 +00:00
if let xous::Result::Scalar2(upper, lower) = response {
2021-03-28 20:35:11 +00:00
upper as u64 | ((lower as u64) << 32)
2021-03-28 10:14:41 +00:00
} else {
2021-03-28 20:35:11 +00:00
panic!("Ticktimer elapsed_ms(): unexpected return value: {:#?}", response);
2021-03-28 10:14:41 +00:00
}
}
2021-03-28 10:14:41 +00:00
pub fn sleep_ms(&self, ms: usize) -> Result<(), Error> {
send_message(self.conn,
xous::Message::new_blocking_scalar(api::Opcode::SleepMs.to_usize().unwrap(),
ms,
0, 0, 0)
2021-03-28 10:14:41 +00:00
).map(|_| ())
}
2020-10-12 11:08:16 +00:00
}