Prusa3d Marlin fork
meatpack.h
1 /*
2 * MeatPack G-Code Compression
3 *
4 * Algorithm & Implementation: Scott Mudge - mail@scottmudge.com
5 * Date: Dec. 2020
6 *
7 * Specifically optimized for 3D printing G-Code, this is a zero-cost data compression method
8 * which packs ~180-190% more data into the same amount of bytes going to the CNC controller.
9 * As a majority of G-Code can be represented by a restricted alphabet, I performed histogram
10 * analysis on a wide variety of 3D printing gcode samples, and found ~93% of all gcode could
11 * be represented by the same 15-character alphabet.
12 *
13 * This allowed me to design a system of packing 2 8-bit characters into a single byte, assuming
14 * they fall within this limited 15-character alphabet. Using a 4-bit lookup table, these 8-bit
15 * characters can be represented by a 4-bit index.
16 *
17 * Combined with some logic to allow commingling of full-width characters outside of this 15-
18 * character alphabet (at the cost of an extra 8-bits per full-width character), and by stripping
19 * out unnecessary comments, the end result is gcode which is roughly half the original size.
20 *
21 * Why did I do this? I noticed micro-stuttering and other data-bottleneck issues while printing
22 * objects with high curvature, especially at high speeds. There is also the issue of the limited
23 * baud rate provided by Prusa's Atmega2560-based boards, over the USB serial connection. So soft-
24 * ware like OctoPrint would also suffer this same micro-stuttering and poor print quality issue.
25 *
26 */
27 #include <stdint.h>
28 #include "Configuration.h"
29 
30 #ifndef MEATPACK_H_
31 #define MEATPACK_H_
32 
33 #ifdef ENABLE_MEATPACK
34 
35 #define MeatPack_SecondNotPacked 0b11110000
36 #define MeatPack_FirstNotPacked 0b00001111
37 
38 // These are commands sent to MeatPack to control its behavior.
39 // They are sent by first sending 2x MeatPack_CommandByte (0xFF) in sequence,
40 // followed by one of the command bytes below.
41 // Provided that 0xFF is an exceedingly rare character that is virtually never
42 // present in g-code naturally, it is safe to assume 2 in sequence should never
43 // happen naturally, and so it is used as a signal here.
44 //
45 // 0xFF *IS* used in "packed" g-code (used to denote that the next 2 characters are
46 // full-width), however 2 in a row will never occur, as the next 2 bytes will always
47 // some non-0xFF character.
48 enum MeatPack_Command {
49  MPCommand_None = 0U,
50  // MPCommand_TogglePacking = 253U, -- Unused, byte 253 can be re-used later.
51  MPCommand_EnablePacking = 251U,
52  MPCommand_DisablePacking = 250U,
53  MPCommand_ResetAll = 249U,
54  MPCommand_QueryConfig = 248U,
55  MPCommand_EnableNoSpaces = 247U,
56  MPCommand_DisableNoSpaces = 246U
57 };
58 
59 // Pass in a character rx'd by SD card or serial. Automatically parses command/ctrl sequences,
60 // and will control state internally.
61 extern void mp_handle_rx_char(const uint8_t c);
62 
63 // After passing in rx'd char using above method, call this to get characters out. Can return
64 // from 0 to 2 characters at once.
65 // @param out [in] Output pointer for unpacked/processed data.
66 // @return Number of characters returned. Range from 0 to 2.
67 extern uint8_t mp_get_result_char(char* const __restrict out);
68 #endif
69 
70 #endif // MEATPACK_H_