unpack/
unpack.rs

1use core::str;
2
3use meatpack::{MeatPackResult, Unpacker};
4
5fn main() {
6    // Some meatpacked gcode
7    let packed: [u8; 93] = [
8        255, 255, 251, 255, 255, 247, 255, 255, 250, 59, 32, 10, 255, 255, 251, 127, 77, 243, 32,
9        15, 80, 255, 32, 82, 195, 127, 77, 243, 32, 15, 81, 255, 32, 83, 195, 47, 77, 16, 239, 32,
10        4, 0, 255, 32, 89, 4, 0, 255, 32, 90, 2, 240, 32, 43, 5, 192, 47, 77, 48, 239, 32, 3, 240,
11        32, 63, 89, 0, 255, 32, 90, 4, 191, 32, 1, 192, 47, 77, 64, 255, 32, 80, 4, 0, 255, 32, 82,
12        33, 0, 255, 32, 84, 4, 0,
13    ];
14
15    // Initiliase the packer with buffer size depending
16    // on your application
17    let mut unpacker = Unpacker::<64>::default();
18
19    // Imagine receiving the bytes from some I/O and we want
20    // to construct gcode lines and deal with them as we form them.
21    for b in packed.iter() {
22        let res = unpacker.unpack(b);
23        match res {
24            Ok(MeatPackResult::WaitingForNextByte) => {
25                //println!("Waiting for next byte");
26            }
27            Ok(MeatPackResult::Line(line)) => {
28                let line = str::from_utf8(line).unwrap();
29                println!("{:?}", line);
30            }
31            Err(e) => {
32                println!("{:?}", e);
33                panic!();
34            }
35        }
36    }
37}