pub struct Packer<const S: usize> { /* private fields */ }Expand description
A struct for that packs bytes and emits lines of meatpacked gcode. Stripping comments is on by default and empty lines are omitted.
Implementations§
Source§impl<const S: usize> Packer<S>
impl<const S: usize> Packer<S>
Sourcepub fn new(strip_comments: bool, strip_whitespace: bool) -> Self
pub fn new(strip_comments: bool, strip_whitespace: bool) -> Self
Create a new instance of the packer
Sourcepub fn pack(&mut self, b: &u8) -> Result<MeatPackResult<'_>, MeatPackError>
pub fn pack(&mut self, b: &u8) -> Result<MeatPackResult<'_>, MeatPackError>
Pack a byte into the current line.
Examples found in repository?
examples/pack.rs (line 20)
3fn main() {
4 // Some example gcode.
5 let gcode = "M73 P0 R3
6M73 Q0 S3 ; Hello
7M201 X4000 Y4000 Z200 E2500
8M203 X300 Y300 Z40 E100
9M204 P4000 R1200 T4000
10";
11
12 // Initiliase the packer with buffer size depending
13 // on your application
14 let mut packer = Packer::<64>::default();
15
16 // Feed in the bytes as you receive them and
17 // the packer will return completed lines of
18 // meatpacked gcode to send onwards.
19 for byte in gcode.as_bytes() {
20 let packed = packer.pack(byte);
21 match packed {
22 Ok(MeatPackResult::Line(line)) => {
23 println!("{:?}", line);
24 }
25 Ok(MeatPackResult::WaitingForNextByte) => {}
26 Err(e) => {
27 println!("{:?}", e);
28 panic!()
29 }
30 }
31 }
32}More examples
examples/pack_unpack.rs (line 30)
5fn main() {
6 let gcode = "M73 P0 R3
7M73 Q0 S3 ; Hello
8M201 X4000 Y4000 Z200 E2500
9M203 X300 Y300 Z40 E100
10M204 P4000 R1200 T4000
11";
12
13 println!("## IN ##");
14 println!("{}", gcode);
15 println!("####");
16
17 // Initiliase the packer with buffer size depending
18 // on your application
19 let mut packer = Packer::<64>::default();
20 let mut out: Vec<u8> = vec![];
21
22 // This will store the entire packed version of the gcode.
23 // Don't forget the header.
24 out.extend(&MEATPACK_HEADER);
25
26 // Feed in the bytes as you receive them and
27 // the packer will return completed lines of
28 // meatpacked gcode.
29 for byte in gcode.as_bytes() {
30 let packed = packer.pack(byte);
31 match packed {
32 Ok(MeatPackResult::Line(line)) => {
33 println!("{:?}", line);
34 out.extend(line);
35 }
36 Ok(MeatPackResult::WaitingForNextByte) => {}
37 Err(e) => println!("{:?}", e),
38 }
39 }
40
41 println!("{:?}", out);
42
43 println!("## OUT ##");
44
45 // Now we create an unpacker to unpack the meatpacked data.
46 let mut unpacker = Unpacker::<64>::default();
47
48 // Imagine receiving the bytes from some I/O and we want
49 // to construct gcode lines and deal with them as we form them.
50 for byte in out.iter() {
51 let res = unpacker.unpack(byte);
52 match res {
53 Ok(MeatPackResult::WaitingForNextByte) => {}
54 Ok(MeatPackResult::Line(line)) => {
55 // If in std.
56 for byte in line {
57 let c = char::from(*byte);
58 print!("{}", c);
59 }
60 }
61 Err(e) => {
62 println!("{:?}", e);
63 process::exit(0)
64 }
65 }
66 }
67
68 println!("####");
69}Sourcepub fn data_remains(&self) -> bool
pub fn data_remains(&self) -> bool
A utility function to check if any data remains in the internal inner. We expect all meatpack lines to newline end.
Trait Implementations§
Auto Trait Implementations§
impl<const S: usize> Freeze for Packer<S>
impl<const S: usize> RefUnwindSafe for Packer<S>
impl<const S: usize> Send for Packer<S>
impl<const S: usize> Sync for Packer<S>
impl<const S: usize> Unpin for Packer<S>
impl<const S: usize> UnwindSafe for Packer<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more