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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#![no_std]
#![deny(missing_docs)]
pub mod prelude;
use embedded_hal::serial::Write;
use heapless::LinearMap;
use nb;
pub struct OnlyFront(pub bool);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum ValueType {
Bool = 0,
Int = 1,
Float = 2,
}
#[derive(Clone)]
pub struct ValueRec<const P: usize> {
is_only_front: bool,
vtype: ValueType,
vals: [i32; P],
}
impl<const P: usize> ValueRec<P> {
pub const fn new(vtype: ValueType) -> Self {
Self {
is_only_front: false,
vtype,
vals: [0; P],
}
}
}
pub enum AddError {
MapOverflow,
}
pub trait NextValue {
fn next<F>(&mut self, f: F)
where
F: FnOnce(&Self);
}
#[derive(Clone, Copy)]
pub struct SVStruct<M> {
current: usize,
map: M,
}
impl<M> core::ops::Deref for SVStruct<M> {
type Target = M;
fn deref(&self) -> &Self::Target {
&self.map
}
}
impl<M> core::ops::DerefMut for SVStruct<M> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.map
}
}
pub type SVMap<const N: usize, const P: usize> = SVStruct<LinearMap<&'static str, ValueRec<P>, N>>;
impl<const N: usize, const P: usize> SVMap<N, P> {
pub const fn new() -> Self {
Self {
current: 0,
map: LinearMap::new(),
}
}
pub fn is_first(&self) -> bool {
self.current == 0
}
pub fn is_last(&self) -> bool {
self.current == P - 1
}
fn set_value(
&mut self,
name: &'static str,
vtype: ValueType,
val: i32,
only_pos_front: bool,
) -> Result<(), AddError> {
if !self.map.contains_key(&name) {
if self.map.insert(name, ValueRec::new(vtype)).is_err() {
return Err(AddError::MapOverflow);
}
}
let vr = self.map.get_mut(name).unwrap();
vr.vals[self.current] = val;
vr.is_only_front = only_pos_front;
Ok(())
}
pub fn set<T: Value>(&mut self, name: &'static Name, value: T) -> Result<(), AddError> {
self.set_value(name, T::TYPE, value.to_i32(), T::ONLY_FRONT)
}
}
pub trait Value {
const TYPE: ValueType;
const ONLY_FRONT: bool;
fn to_i32(self) -> i32;
}
impl Value for i32 {
const TYPE: ValueType = ValueType::Int;
const ONLY_FRONT: bool = false;
fn to_i32(self) -> i32 {
self
}
}
impl Value for f32 {
const TYPE: ValueType = ValueType::Float;
const ONLY_FRONT: bool = false;
fn to_i32(self) -> i32 {
self.to_bits() as i32
}
}
impl Value for bool {
const TYPE: ValueType = ValueType::Bool;
const ONLY_FRONT: bool = false;
fn to_i32(self) -> i32 {
self as i32
}
}
impl Value for OnlyFront {
const TYPE: ValueType = ValueType::Bool;
const ONLY_FRONT: bool = true;
fn to_i32(self) -> i32 {
self.0 as i32
}
}
impl<const N: usize, const P: usize> NextValue for SVMap<N, P> {
fn next<F>(&mut self, f: F)
where
F: FnOnce(&Self),
{
let previous = self.current;
self.current += 1;
if self.current >= P {
self.current -= P;
f(self);
}
for (_, v) in self.map.iter_mut() {
v.vals[self.current] = if v.is_only_front { 0 } else { v.vals[previous] };
}
}
}
pub trait SendPackage<V> {
type Error;
fn send_package(&mut self, module: &'static Name, values: &V) -> Result<(), Self::Error>;
}
impl<Tx, const N: usize, const P: usize> SendPackage<SVMap<N, P>> for Tx
where
Tx: WriteIter,
{
type Error = <Tx as WriteIter>::Error;
fn send_package(
&mut self,
module: &'static Name,
values: &SVMap<N, P>,
) -> Result<(), Self::Error> {
use core::iter::repeat;
let vl_size = Name::MAX_SIZE + 4 + P * 4;
let full_size = (Name::MAX_SIZE + vl_size * values.map.len()) as u32;
self.bwrite_iter(
"=begin="
.bytes()
.chain(full_size.to_le_bytes().iter().cloned())
.chain(module.bytes())
.chain(repeat(0).take(Name::MAX_SIZE - module.len())),
)?;
self.bflush()?;
for (&name, v) in values.map.iter() {
self.bwrite_iter(
name.bytes()
.chain(repeat(0).take(Name::MAX_SIZE - name.len()))
.chain((v.vtype as i32).to_le_bytes().iter().cloned())
.chain(v.vals.iter().flat_map(|val| val.to_le_bytes())),
)?;
self.bflush()?;
}
self.bwrite_iter("=end=".bytes())?;
self.bflush()?;
Ok(())
}
}
pub struct Name(&'static str);
impl core::ops::Deref for Name {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0
}
}
impl Name {
const MAX_SIZE: usize = 24;
pub const fn new(name: &'static str) -> Self {
assert!(!name.is_empty());
assert!(name.len() < Self::MAX_SIZE);
assert!(!equal(name, "=end="));
assert!(!equal(name, "=begin="));
Self(name)
}
}
const fn equal(first: &'static str, second: &'static str) -> bool {
if first.len() != second.len() {
return false;
}
let fb = first.as_bytes();
let sb = second.as_bytes();
let mut i = 0;
while i < first.len() {
if fb[i] != sb[i] {
return false;
}
i += 1;
}
true
}
pub trait WriteIter {
type Error;
fn bwrite_iter<WI>(&mut self, bytes: WI) -> Result<(), Self::Error>
where
WI: Iterator<Item = u8>;
fn bflush(&mut self) -> Result<(), Self::Error>;
}
impl<Tx> WriteIter for Tx
where
Tx: Write<u8>,
{
type Error = <Tx as Write<u8>>::Error;
fn bwrite_iter<WI>(&mut self, mut bytes: WI) -> Result<(), Self::Error>
where
WI: Iterator<Item = u8>,
{
bytes.try_for_each(|c| nb::block!(self.write(c)))
}
fn bflush(&mut self) -> Result<(), Self::Error> {
nb::block!(self.flush())
}
}