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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use alloc::string::String;
use cntrlr_macros::board_fn;
use core::{fmt::Debug, future::Future, ops::DerefMut};
#[derive(Debug)]
pub enum LineError<E> {
Read(E),
Utf8(core::str::Utf8Error),
}
pub trait Read {
type Error: Debug;
type Future<'a>: Future<Output = Result<usize, Self::Error>> + 'a;
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::Future<'a>;
}
pub trait Write {
type Error: Debug;
type Future<'a>: Future<Output = Result<usize, Self::Error>> + 'a;
type FlushFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a;
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::Future<'a>;
fn flush<'a>(&'a mut self) -> Self::FlushFuture<'a>;
}
pub trait ReadExt: Read {
type ExactFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a;
type LineFuture<'a>: Future<Output = Result<(), LineError<Self::Error>>> + 'a;
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ExactFuture<'a>;
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> Self::LineFuture<'a>;
}
impl<T: Read + 'static> ReadExt for T {
type ExactFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a;
type LineFuture<'a> = impl Future<Output = Result<(), LineError<Self::Error>>> + 'a;
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ExactFuture<'a> {
async move {
let mut buf = buf;
while buf.len() > 0 {
let read = self.read(buf).await?;
buf = &mut buf[read..];
}
Ok(())
}
}
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> Self::LineFuture<'a> {
async move {
let buf = unsafe { buf.as_mut_vec() };
loop {
let mut byte = [0];
self.read(&mut byte).await.map_err(|e| LineError::Read(e))?;
buf.push(byte[0]);
if byte[0] == b'\n' {
break;
}
}
let _ = core::str::from_utf8(&buf).map_err(|e| LineError::Utf8(e))?;
Ok(())
}
}
}
pub trait WriteExt: Write {
type AllFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a;
type FmtFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a;
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> Self::AllFuture<'a>;
fn write_fmt<'a>(&'a mut self, fmt: core::fmt::Arguments<'a>) -> Self::FmtFuture<'a>;
}
impl<T: Write + 'static> WriteExt for T {
type AllFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a;
type FmtFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a;
fn write_all<'a>(&'a mut self, buf: &'a [u8]) -> Self::AllFuture<'a> {
async move {
let mut buf = buf;
while buf.len() > 0 {
let written = self.write(buf).await?;
buf = &buf[written..];
}
Ok(())
}
}
fn write_fmt<'a>(&'a mut self, fmt: core::fmt::Arguments<'a>) -> Self::FmtFuture<'a> {
async move {
use alloc::format;
let formatted = format!("{}", fmt);
self.write_all(formatted.as_bytes()).await
}
}
}
pub trait Serial: Read + Write {
type Error: Debug;
fn enable(&mut self, baud: usize) -> Result<(), <Self as Serial>::Error>;
fn disable(&mut self) -> Result<(), <Self as Serial>::Error>;
}
#[board_fn(io, red_v)]
pub fn pc_serial() -> impl DerefMut<Target = impl Serial> {}
#[board_fn(io, red_v, teensy_30, teensy_32, teensy_35, teensy_36, teensy_lc)]
pub fn serial_1() -> impl DerefMut<Target = impl Serial> {}
#[board_fn(io, red_v, teensy_30, teensy_32, teensy_35, teensy_36, teensy_lc)]
pub fn serial_2() -> impl DerefMut<Target = impl Serial> {}
#[board_fn(io, teensy_30, teensy_32, teensy_35, teensy_36, teensy_lc)]
pub fn serial_3() -> impl DerefMut<Target = impl Serial> {}
#[board_fn(io, teensy_35, teensy_36)]
pub fn serial_4() -> impl DerefMut<Target = impl Serial> {}
#[board_fn(io, teensy_35, teensy_36)]
pub fn serial_5() -> impl DerefMut<Target = impl Serial> {}
#[board_fn(io, teensy_35)]
pub fn serial_6() -> impl DerefMut<Target = impl Serial> {}