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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use core::num::NonZeroU32;
use core::slice;
use std::io::{Result, Write};
use super::consts::*;
const SYNC_PULSE_TOLERANCE: u32 = (SYNC_PULSE2_LENGTH.get() - SYNC_PULSE1_LENGTH.get())/2;
const SYNC_PULSE1_MIN: u32 = SYNC_PULSE1_LENGTH.get() - SYNC_PULSE_TOLERANCE;
const SYNC_PULSE1_MAX: u32 = SYNC_PULSE1_LENGTH.get() + SYNC_PULSE_TOLERANCE - 1;
const SYNC_PULSE2_MIN: u32 = SYNC_PULSE2_LENGTH.get() - SYNC_PULSE_TOLERANCE;
const SYNC_PULSE2_MAX: u32 = SYNC_PULSE2_LENGTH.get() + SYNC_PULSE_TOLERANCE - 1;
const LEAD_PULSE_TOLERANCE: u32 = 250;
const LEAD_PULSE_MIN: u32 = LEAD_PULSE_LENGTH.get() - LEAD_PULSE_TOLERANCE;
const LEAD_PULSE_MAX: u32 = LEAD_PULSE_LENGTH.get() + LEAD_PULSE_TOLERANCE - 1;
const DATA_PULSE_TOLERANCE: u32 = 250;
const DATA_PULSE_MIN: u32 = ZERO_PULSE_LENGTH.get() - DATA_PULSE_TOLERANCE;
const DATA_PULSE_MAX: u32 = ONE_PULSE_LENGTH.get() + DATA_PULSE_TOLERANCE - 1;
const DATA_PULSE_THRESHOLD: u32 = (ZERO_PULSE_LENGTH.get() + ONE_PULSE_LENGTH.get())/2;
const MIN_LEAD_COUNT: u32 = 16;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PulseDecodeState {
Idle,
Lead {
counter: u32
},
Sync1,
Sync2,
Data{
current: u8,
pulse: u8,
written: u32
},
}
#[derive(Debug)]
pub struct PulseDecodeWriter<W> {
state: PulseDecodeState,
wr: W,
}
impl PulseDecodeState {
pub fn is_idle(&self) -> bool {
matches!(self, PulseDecodeState::Idle)
}
pub fn is_lead(&self) -> bool {
matches!(self, PulseDecodeState::Lead {..})
}
pub fn is_data(&self) -> bool {
matches!(self, PulseDecodeState::Data {..})
}
pub fn is_sync1(&self) -> bool {
matches!(self, PulseDecodeState::Sync1)
}
pub fn is_sync2(&self) -> bool {
matches!(self, PulseDecodeState::Sync2)
}
}
impl<W> PulseDecodeWriter<W> {
pub fn reset(&mut self) {
self.state = PulseDecodeState::Idle;
}
pub fn is_idle(&self) -> bool {
self.state.is_idle()
}
pub fn into_inner(self) -> W {
self.wr
}
pub fn state(&self) -> PulseDecodeState {
self.state
}
pub fn get_mut(&mut self) -> &mut W {
&mut self.wr
}
pub fn get_ref(&self) -> &W {
&self.wr
}
pub fn data_size(&self) -> Option<NonZeroU32> {
NonZeroU32::new(if let PulseDecodeState::Data { written, .. } = self.state {
written
}
else {
0
})
}
pub fn with_state(mut self, state: PulseDecodeState) -> Self {
self.state = state;
self
}
}
impl<W: Write> PulseDecodeWriter<W> {
pub fn new(wr: W) -> Self {
PulseDecodeWriter { state: PulseDecodeState::Idle, wr }
}
pub fn end(&mut self) -> Result<Option<NonZeroU32>> {
let res = if let PulseDecodeState::Data { mut current, pulse, written } = self.state {
NonZeroU32::new(if pulse <= 1 {
written
}
else {
if pulse & 1 == 1 {
current &= !1;
}
current <<= (16 - (pulse & 15)) >> 1;
self.write_byte(current)?;
written + 1
})
}
else {
None
};
self.state = PulseDecodeState::Idle;
Ok(res)
}
pub fn write_decoded_pulses<I>(&mut self, iter: I) -> Result<Option<NonZeroU32>>
where I: Iterator<Item=NonZeroU32>
{
let mut iter = iter.peekable();
if iter.peek().is_none() {
return self.end(); }
for delta in iter {
match self.state {
PulseDecodeState::Idle => {
if let LEAD_PULSE_MIN..=LEAD_PULSE_MAX = delta.get() {
self.state = PulseDecodeState::Lead { counter: 1 };
}
}
PulseDecodeState::Lead { counter } => match delta.get() {
SYNC_PULSE1_MIN..=SYNC_PULSE1_MAX if counter >= MIN_LEAD_COUNT => {
self.state = PulseDecodeState::Sync1;
}
LEAD_PULSE_MIN..=LEAD_PULSE_MAX => {
self.state = PulseDecodeState::Lead { counter: counter.saturating_add(1) };
}
_ => { self.state = PulseDecodeState::Idle },
}
PulseDecodeState::Sync1 => match delta.get() {
SYNC_PULSE2_MIN..=SYNC_PULSE2_MAX => {
self.state = PulseDecodeState::Sync2;
}
_ => { self.state = PulseDecodeState::Idle },
}
PulseDecodeState::Sync2 => match delta.get() {
delta @ DATA_PULSE_MIN..=DATA_PULSE_MAX => {
let current = (delta > DATA_PULSE_THRESHOLD) as u8;
self.state = PulseDecodeState::Data { current, pulse: 1, written: 0 }
}
_ => { self.state = PulseDecodeState::Idle },
}
PulseDecodeState::Data { current, pulse, written } => match delta.get() {
delta @ DATA_PULSE_MIN..=DATA_PULSE_MAX => {
let bit = (delta > DATA_PULSE_THRESHOLD) as u8;
let current = if pulse & 1 == 1 {
if (current ^ bit) & 1 == 1 {
return self.end();
}
if pulse == 15 {
self.state = PulseDecodeState::Data { current: 0, pulse: 0, written: written + 1 };
self.write_byte(current)?;
continue;
}
current
}
else {
(current << 1) | bit
};
self.state = PulseDecodeState::Data { current, pulse: pulse + 1, written }
}
_ => return self.end()
}
}
}
Ok(None)
}
#[inline]
fn write_byte(&mut self, byte: u8) -> Result<()> {
if let Err(e) = self.wr.write_all(slice::from_ref(&byte)) {
self.state = PulseDecodeState::Idle;
return Err(e);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;
#[test]
fn write_mic_works() {
let wr = Cursor::new(Vec::new());
let mut mpw = PulseDecodeWriter::new(wr);
let pulses = [];
assert_eq!(PulseDecodeState::Idle, mpw.state());
assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.iter().copied()).unwrap());
assert_eq!(PulseDecodeState::Idle, mpw.state());
assert_eq!(None, mpw.data_size());
let pulses = [PAUSE_PULSE_LENGTH, LEAD_PULSE_LENGTH];
assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.iter().copied()).unwrap());
assert_eq!(PulseDecodeState::Lead {counter:1} , mpw.state());
assert_eq!(None, mpw.data_size());
let pulses = vec![LEAD_PULSE_LENGTH;LEAD_PULSES_HEAD as usize];
assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.into_iter()).unwrap());
assert_eq!(PulseDecodeState::Lead {counter:LEAD_PULSES_HEAD as u32 + 1} , mpw.state());
assert_eq!(None, mpw.data_size());
let pulses = [SYNC_PULSE1_LENGTH, SYNC_PULSE2_LENGTH,
ZERO_PULSE_LENGTH, ZERO_PULSE_LENGTH,
ONE_PULSE_LENGTH, ONE_PULSE_LENGTH];
assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.iter().copied()).unwrap());
assert_eq!(PulseDecodeState::Data {current: 0b0000_0001, pulse: 4, written: 0} , mpw.state());
assert_eq!(None, mpw.data_size());
assert_eq!(0, mpw.get_ref().position());
let pulses = [ZERO_PULSE_LENGTH, ZERO_PULSE_LENGTH,
ONE_PULSE_LENGTH, ONE_PULSE_LENGTH,
ZERO_PULSE_LENGTH, ZERO_PULSE_LENGTH,
ONE_PULSE_LENGTH, ONE_PULSE_LENGTH,
ZERO_PULSE_LENGTH, ZERO_PULSE_LENGTH,
ONE_PULSE_LENGTH, ONE_PULSE_LENGTH];
assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.iter().copied()).unwrap());
assert_eq!(PulseDecodeState::Data {current: 0, pulse: 0, written: 1} , mpw.state());
assert_eq!(NonZeroU32::new(1), mpw.data_size());
assert_eq!(1, mpw.get_ref().position());
assert_eq!(&[0b0101_0101], &mpw.get_ref().get_ref()[..]);
assert_eq!(NonZeroU32::new(1), mpw.end().unwrap());
assert_eq!(PulseDecodeState::Idle, mpw.state());
assert_eq!(None, mpw.data_size());
}
#[test]
fn write_mic_missing_bits_works() {
let wr = Cursor::new(Vec::new());
let mut mpw = PulseDecodeWriter::new(wr);
let pulses = vec![LEAD_PULSE_LENGTH;LEAD_PULSES_DATA as usize];
assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.into_iter()).unwrap());
assert_eq!(PulseDecodeState::Lead {counter:LEAD_PULSES_DATA as u32} , mpw.state());
assert_eq!(None, mpw.data_size());
let pulses = [SYNC_PULSE1_LENGTH, SYNC_PULSE2_LENGTH,
ONE_PULSE_LENGTH, ONE_PULSE_LENGTH,
ZERO_PULSE_LENGTH, ZERO_PULSE_LENGTH,
ONE_PULSE_LENGTH];
assert_eq!(None, mpw.write_decoded_pulses(&mut pulses.iter().copied()).unwrap());
assert_eq!(PulseDecodeState::Data {current: 0b0000_0101, pulse: 5, written: 0} , mpw.state());
assert_eq!(None, mpw.data_size());
assert_eq!(NonZeroU32::new(1), mpw.end().unwrap());
assert_eq!(1, mpw.get_ref().position());
assert_eq!(&[0b1000_0000], &mpw.get_ref().get_ref()[..]);
assert_eq!(PulseDecodeState::Idle, mpw.state());
assert_eq!(None, mpw.data_size());
}
}