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

71 lines
1.9 KiB
Rust
Raw Normal View History

2020-10-12 11:08:16 +00:00
use xous::{Message, ScalarMessage};
#[derive(Debug)]
pub enum Opcode {
/// Reset the timer
2021-01-04 20:40:34 +00:00
// Reset, // This is a bad idea, in retrospect.
2020-10-12 11:08:16 +00:00
/// Get the elapsed time in milliseconds
ElapsedMs,
/// Sleep for the specified numer of milliseconds
SleepMs(usize),
/// Recalculate the sleep time
RecalculateSleep,
2020-10-12 11:08:16 +00:00
}
impl<'a> core::convert::TryFrom<&'a Message> for Opcode {
type Error = &'static str;
fn try_from(message: &'a Message) -> Result<Self, Self::Error> {
match message {
Message::Scalar(m) => match m.id {
2021-01-04 20:40:34 +00:00
// 1 => Ok(Opcode::Reset),
131072 => Ok(Opcode::RecalculateSleep),
2020-10-12 11:08:16 +00:00
_ => Err("unrecognized opcode"),
},
Message::BlockingScalar(m) => match m.id {
4919 => Ok(Opcode::ElapsedMs),
3 => Ok(Opcode::SleepMs(m.arg1)),
_ => Err("unrecognized opcode"),
},
2020-10-12 11:08:16 +00:00
_ => Err("unhandled message type"),
}
}
}
impl Into<Message> for Opcode {
fn into(self) -> Message {
match self {
2021-01-04 20:40:34 +00:00
/* Opcode::Reset => Message::Scalar(ScalarMessage {
2020-10-12 11:08:16 +00:00
id: 1,
arg1: 0,
arg2: 0,
arg3: 0,
arg4: 0,
2021-01-04 20:40:34 +00:00
}),*/
Opcode::RecalculateSleep => Message::Scalar(ScalarMessage {
id: 131072,
arg1: 0,
arg2: 0,
arg3: 0,
arg4: 0,
}),
2020-10-13 14:49:12 +00:00
Opcode::ElapsedMs => Message::BlockingScalar(ScalarMessage {
id: 4919,
2020-10-12 11:08:16 +00:00
arg1: 0,
arg2: 0,
arg3: 0,
arg4: 0,
}),
Opcode::SleepMs(ms) => Message::BlockingScalar(ScalarMessage {
id: 3,
arg1: ms,
arg2: 0,
arg3: 0,
arg4: 0,
}),
2020-10-12 11:08:16 +00:00
}
}
}