playdate_device/interface/
async.rs

1use std::future::Future;
2
3use crate::device::command::Command;
4use crate::error::Error;
5
6
7pub trait Out: In {
8	fn send(&self, data: &[u8]) -> impl Future<Output = Result<usize, Error>>;
9
10	fn send_cmd(&self, cmd: Command) -> impl Future<Output = Result<usize, Error>> {
11		async move {
12			let mut pre = 0;
13			if !matches!(cmd, Command::Echo { .. }) {
14				use crate::device::command::Switch;
15
16				trace!("send cmd: echo off");
17				let echo = Command::Echo { value: Switch::Off };
18				pre = self.send(echo.with_break().as_bytes()).await?;
19			}
20
21			trace!("send cmd: {cmd}");
22			let sent = self.send(cmd.with_break().as_bytes()).await?;
23			Ok(pre + sent)
24		}
25	}
26}
27
28pub trait In {}
29
30pub trait Interface: Out {}
31impl<T: In + Out> Interface for T {}