1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#![no_std]
use embedded_hal::serial;
use core::fmt;
pub struct SerialIO<TX, RX> {
tx: TX,
rx: RX,
}
impl<TX, RX> SerialIO<TX, RX>
where TX: serial::Write<u8>,
RX: serial::Read<u8>
{
pub fn new(tx: TX, rx: RX) -> Self
{
Self {
tx,
rx,
}
}
pub fn release(self) -> (TX, RX) {
(self.tx, self.rx)
}
pub fn _fmt_write(&mut self, args: fmt::Arguments) -> Result<(), fmt::Error> {
use core::fmt::Write;
self.write_fmt(args)
}
}
#[macro_export]
macro_rules! sprint {
($serial:expr, $($arg:tt)*) => {
$serial._fmt_write(format_args!($($arg)*)).ok()
};
}
#[macro_export]
macro_rules! sprintln {
($serial:expr, $fmt:expr) => {
$crate::sprint!($serial, concat!($fmt, "\n"))
};
($serial:expr, $fmt:expr, $($arg:tt)*) => {
$crate::sprint!($serial, concat!($fmt, "\n"), $($arg)*)
};
}
impl<TX, RX> fmt::Write for SerialIO<TX, RX>
where TX: serial::Write<u8>,
RX: serial::Read<u8>
{
fn write_str(&mut self, s: &str) -> fmt::Result {
for byte in s.bytes() {
self.tx.write(byte).ok();
}
Ok(())
}
}