|
@@ -0,0 +1,254 @@
|
|
1
|
+/**
|
|
2
|
+ * Marlin 3D Printer Firmware
|
|
3
|
+ * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
|
4
|
+ *
|
|
5
|
+ * Based on Sprinter and grbl.
|
|
6
|
+ * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
|
7
|
+ *
|
|
8
|
+ * This program is free software: you can redistribute it and/or modify
|
|
9
|
+ * it under the terms of the GNU General Public License as published by
|
|
10
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+ * (at your option) any later version.
|
|
12
|
+ *
|
|
13
|
+ * This program is distributed in the hope that it will be useful,
|
|
14
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+ * GNU General Public License for more details.
|
|
17
|
+ *
|
|
18
|
+ * You should have received a copy of the GNU General Public License
|
|
19
|
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
20
|
+ *
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+/**
|
|
24
|
+ * MeatPack G-code Compression
|
|
25
|
+ *
|
|
26
|
+ * Algorithm & Implementation: Scott Mudge - mail@scottmudge.com
|
|
27
|
+ * Date: Dec. 2020
|
|
28
|
+ *
|
|
29
|
+ * Character Frequencies from ~30 MB of comment-stripped gcode:
|
|
30
|
+ * '1' -> 4451136 '4' -> 1353273 '\n' -> 1087683 '-' -> 90242
|
|
31
|
+ * '0' -> 4253577 '9' -> 1352147 'G' -> 1075806 'Z' -> 34109
|
|
32
|
+ * ' ' -> 3053297 '3' -> 1262929 'X' -> 975742 'M' -> 11879
|
|
33
|
+ * '.' -> 3035310 '5' -> 1189871 'E' -> 965275 'S' -> 9910
|
|
34
|
+ * '2' -> 1523296 '6' -> 1127900 'Y' -> 965274
|
|
35
|
+ * '8' -> 1366812 '7' -> 1112908 'F' -> 99416
|
|
36
|
+ *
|
|
37
|
+ * When space is omitted the letter 'E' is used in its place
|
|
38
|
+ */
|
|
39
|
+
|
|
40
|
+#include "../inc/MarlinConfig.h"
|
|
41
|
+
|
|
42
|
+#if ENABLED(MEATPACK)
|
|
43
|
+
|
|
44
|
+#include "meatpack.h"
|
|
45
|
+MeatPack meatpack;
|
|
46
|
+
|
|
47
|
+#define MeatPack_ProtocolVersion "PV01"
|
|
48
|
+//#define MEATPACK_LOOKUP_TABLE
|
|
49
|
+//#define MP_DEBUG
|
|
50
|
+
|
|
51
|
+#define DEBUG_OUT ENABLED(MP_DEBUG)
|
|
52
|
+#include "../core/debug_out.h"
|
|
53
|
+
|
|
54
|
+bool MeatPack::cmd_is_next = false; // A command is pending
|
|
55
|
+uint8_t MeatPack::state = 0; // Configuration state OFF
|
|
56
|
+uint8_t MeatPack::second_char = 0; // The unpacked 2nd character from an out-of-sequence packed pair
|
|
57
|
+uint8_t MeatPack::cmd_count = 0, // Counts how many command bytes are received (need 2)
|
|
58
|
+ MeatPack::full_char_count = 0, // Counts how many full-width characters are to be received
|
|
59
|
+ MeatPack::char_out_count = 0; // Stores number of characters to be read out.
|
|
60
|
+uint8_t MeatPack::char_out_buf[2]; // Output buffer for caching up to 2 characters
|
|
61
|
+
|
|
62
|
+#if ENABLED(MEATPACK_LOOKUP_TABLE)
|
|
63
|
+ // The 15 most-common characters used in G-code, ~90-95% of all G-code uses these characters
|
|
64
|
+ // Stored in SRAM for performance.
|
|
65
|
+ static const uint8_t meatPackLookupTable[16] = {
|
|
66
|
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
|
67
|
+ '.', ' ', '\n', 'G', 'X',
|
|
68
|
+ '\0' // Unused. 0b1111 indicates a literal character
|
|
69
|
+ };
|
|
70
|
+#endif
|
|
71
|
+
|
|
72
|
+uint8_t MeatPack::unpacked_char(register const uint8_t in) {
|
|
73
|
+ #if ENABLED(MEATPACK_LOOKUP_TABLE)
|
|
74
|
+
|
|
75
|
+ return meatPackLookupTable[in];
|
|
76
|
+
|
|
77
|
+ #else
|
|
78
|
+
|
|
79
|
+ switch (in) {
|
|
80
|
+ case 0b0000 ... 0b1001: return '0' + in;
|
|
81
|
+ case 0b1010: return '.';
|
|
82
|
+ case 0b1011: return (state & MPConfig_Bit_NoSpaces) ? kSpaceCharReplace : ' ';
|
|
83
|
+ case 0b1100: return '\n';
|
|
84
|
+ case 0b1101: return 'G';
|
|
85
|
+ case 0b1110: return 'X';
|
|
86
|
+ }
|
|
87
|
+ return 0;
|
|
88
|
+
|
|
89
|
+ #endif
|
|
90
|
+}
|
|
91
|
+
|
|
92
|
+TERN_(MP_DEBUG, uint8_t chars_decoded = 0); // Log the first 64 bytes after each reset
|
|
93
|
+
|
|
94
|
+void MeatPack::reset_state() {
|
|
95
|
+ state = 0;
|
|
96
|
+ cmd_is_next = false;
|
|
97
|
+ second_char = 0;
|
|
98
|
+ cmd_count = full_char_count = char_out_count = 0;
|
|
99
|
+ TERN_(MP_DEBUG, chars_decoded = 0);
|
|
100
|
+ report_state();
|
|
101
|
+}
|
|
102
|
+
|
|
103
|
+/**
|
|
104
|
+ * Unpack one or two characters from a packed byte into a buffer.
|
|
105
|
+ * Return flags indicating whether any literal bytes follow.
|
|
106
|
+ */
|
|
107
|
+uint8_t MeatPack::unpack_chars(const uint8_t pk, uint8_t* __restrict const chars_out) {
|
|
108
|
+ uint8_t out = 0;
|
|
109
|
+
|
|
110
|
+ // If lower nybble is 1111, the higher nybble is unused, and next char is full.
|
|
111
|
+ if ((pk & kFirstNotPacked) == kFirstNotPacked)
|
|
112
|
+ out = kFirstCharIsLiteral;
|
|
113
|
+ else {
|
|
114
|
+ const uint8_t chr = pk & 0x0F;
|
|
115
|
+ chars_out[0] = unpacked_char(chr); // Set the first char
|
|
116
|
+ }
|
|
117
|
+
|
|
118
|
+ // Check if upper nybble is 1111... if so, we don't need the second char.
|
|
119
|
+ if ((pk & kSecondNotPacked) == kSecondNotPacked)
|
|
120
|
+ out |= kSecondCharIsLiteral;
|
|
121
|
+ else {
|
|
122
|
+ const uint8_t chr = (pk >> 4) & 0x0F;
|
|
123
|
+ chars_out[1] = unpacked_char(chr); // Set the second char
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ return out;
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+/**
|
|
130
|
+ * Interpret a single (non-command) character
|
|
131
|
+ * according to the current MeatPack state.
|
|
132
|
+ */
|
|
133
|
+void MeatPack::handle_rx_char_inner(const uint8_t c) {
|
|
134
|
+ if (TEST(state, MPConfig_Bit_Active)) { // Is MeatPack active?
|
|
135
|
+ if (!full_char_count) { // No literal characters to fetch?
|
|
136
|
+ uint8_t buf[2] = { 0, 0 };
|
|
137
|
+ register const uint8_t res = unpack_chars(c, buf); // Decode the byte into one or two characters.
|
|
138
|
+ if (res & kFirstCharIsLiteral) { // The 1st character couldn't be packed.
|
|
139
|
+ ++full_char_count; // So the next stream byte is a full character.
|
|
140
|
+ if (res & kSecondCharIsLiteral) ++full_char_count; // The 2nd character couldn't be packed. Another stream byte is a full character.
|
|
141
|
+ else second_char = buf[1]; // Retain the unpacked second character.
|
|
142
|
+ }
|
|
143
|
+ else {
|
|
144
|
+ handle_output_char(buf[0]); // Send the unpacked first character out.
|
|
145
|
+ if (buf[0] != '\n') { // After a newline the next char won't be set
|
|
146
|
+ if (res & kSecondCharIsLiteral) ++full_char_count; // The 2nd character couldn't be packed. The next stream byte is a full character.
|
|
147
|
+ else handle_output_char(buf[1]); // Send the unpacked second character out.
|
|
148
|
+ }
|
|
149
|
+ }
|
|
150
|
+ }
|
|
151
|
+ else {
|
|
152
|
+ handle_output_char(c); // Pass through the character that couldn't be packed...
|
|
153
|
+ if (second_char) {
|
|
154
|
+ handle_output_char(second_char); // ...and send an unpacked 2nd character, if set.
|
|
155
|
+ second_char = 0;
|
|
156
|
+ }
|
|
157
|
+ --full_char_count; // One literal character was consumed
|
|
158
|
+ }
|
|
159
|
+ }
|
|
160
|
+ else // Packing not enabled, just copy character to output
|
|
161
|
+ handle_output_char(c);
|
|
162
|
+}
|
|
163
|
+
|
|
164
|
+/**
|
|
165
|
+ * Buffer a single output character which will be picked up in
|
|
166
|
+ * GCodeQueue::get_serial_commands via calls to get_result_char
|
|
167
|
+ */
|
|
168
|
+void MeatPack::handle_output_char(const uint8_t c) {
|
|
169
|
+ char_out_buf[char_out_count++] = c;
|
|
170
|
+
|
|
171
|
+ #if ENABLED(MP_DEBUG)
|
|
172
|
+ if (chars_decoded < 1024) {
|
|
173
|
+ ++chars_decoded;
|
|
174
|
+ DEBUG_ECHOPGM("RB: ");
|
|
175
|
+ MYSERIAL.print((char)c);
|
|
176
|
+ DEBUG_EOL();
|
|
177
|
+ }
|
|
178
|
+ #endif
|
|
179
|
+}
|
|
180
|
+
|
|
181
|
+/**
|
|
182
|
+ * Process a MeatPack command byte to update the state.
|
|
183
|
+ * Report the new state to serial.
|
|
184
|
+ */
|
|
185
|
+void MeatPack::handle_command(const MeatPack_Command c) {
|
|
186
|
+ switch (c) {
|
|
187
|
+ case MPCommand_EnablePacking: SBI(state, MPConfig_Bit_Active); DEBUG_ECHOLNPGM("[MPDBG] ENA REC"); break;
|
|
188
|
+ case MPCommand_DisablePacking: CBI(state, MPConfig_Bit_Active); DEBUG_ECHOLNPGM("[MPDBG] DIS REC"); break;
|
|
189
|
+ case MPCommand_TogglePacking: TBI(state, MPConfig_Bit_Active); DEBUG_ECHOLNPGM("[MPDBG] TGL REC"); break;
|
|
190
|
+ case MPCommand_ResetAll: reset_state(); DEBUG_ECHOLNPGM("[MPDBG] RESET REC"); break;
|
|
191
|
+ case MPCommand_EnableNoSpaces: SBI(state, MPConfig_Bit_NoSpaces); DEBUG_ECHOLNPGM("[MPDBG] ENA NSP");
|
|
192
|
+ TERN_(USE_LOOKUP_TABLE, MeatPackLookupTbl[kSpaceCharIdx] = kSpaceCharReplace);
|
|
193
|
+ break;
|
|
194
|
+ case MPCommand_DisableNoSpaces: CBI(state, MPConfig_Bit_NoSpaces); DEBUG_ECHOLNPGM("[MPDBG] DIS NSP");
|
|
195
|
+ TERN_(USE_LOOKUP_TABLE, MeatPackLookupTbl[kSpaceCharIdx] = ' ');
|
|
196
|
+ break;
|
|
197
|
+ default: DEBUG_ECHOLNPGM("[MPDBG] UNK CMD REC");
|
|
198
|
+ case MPCommand_QueryConfig: break;
|
|
199
|
+ }
|
|
200
|
+ report_state();
|
|
201
|
+}
|
|
202
|
+
|
|
203
|
+void MeatPack::report_state() {
|
|
204
|
+ // NOTE: if any configuration vars are added below, the outgoing sync text for host plugin
|
|
205
|
+ // should not contain the "PV' substring, as this is used to indicate protocol version
|
|
206
|
+ SERIAL_ECHOPGM("[MP] ");
|
|
207
|
+ SERIAL_ECHOPGM(MeatPack_ProtocolVersion);
|
|
208
|
+ serialprint_onoff(TEST(state, MPConfig_Bit_Active));
|
|
209
|
+ SERIAL_CHAR(' ');
|
|
210
|
+ serialprintPGM(TEST(state, MPConfig_Bit_NoSpaces) ? PSTR("NSP") : PSTR("ESP"));
|
|
211
|
+ SERIAL_EOL();
|
|
212
|
+}
|
|
213
|
+
|
|
214
|
+/**
|
|
215
|
+ * Interpret a single character received from serial
|
|
216
|
+ * according to the current meatpack state.
|
|
217
|
+ */
|
|
218
|
+void MeatPack::handle_rx_char(const uint8_t c) {
|
|
219
|
+ if (c == kCommandByte) { // A command (0xFF) byte?
|
|
220
|
+ if (cmd_count) { // In fact, two in a row?
|
|
221
|
+ cmd_is_next = true; // Then a MeatPack command follows
|
|
222
|
+ cmd_count = 0;
|
|
223
|
+ }
|
|
224
|
+ else
|
|
225
|
+ ++cmd_count; // cmd_count = 1 // One command byte received so far...
|
|
226
|
+ return;
|
|
227
|
+ }
|
|
228
|
+
|
|
229
|
+ if (cmd_is_next) { // Were two command bytes received?
|
|
230
|
+ handle_command((MeatPack_Command)c); // Then the byte is a MeatPack command
|
|
231
|
+ cmd_is_next = false;
|
|
232
|
+ return;
|
|
233
|
+ }
|
|
234
|
+
|
|
235
|
+ if (cmd_count) { // Only a single 0xFF was received
|
|
236
|
+ handle_rx_char_inner(kCommandByte); // A single 0xFF is passed on literally so it can be interpreted as kFirstNotPacked|kSecondNotPacked
|
|
237
|
+ cmd_count = 0;
|
|
238
|
+ }
|
|
239
|
+
|
|
240
|
+ handle_rx_char_inner(c); // Other characters are passed on for MeatPack decoding
|
|
241
|
+}
|
|
242
|
+
|
|
243
|
+uint8_t MeatPack::get_result_char(char* const __restrict out) {
|
|
244
|
+ uint8_t res = 0;
|
|
245
|
+ if (char_out_count) {
|
|
246
|
+ res = char_out_count;
|
|
247
|
+ char_out_count = 0;
|
|
248
|
+ for (register uint8_t i = 0; i < res; ++i)
|
|
249
|
+ out[i] = (char)char_out_buf[i];
|
|
250
|
+ }
|
|
251
|
+ return res;
|
|
252
|
+}
|
|
253
|
+
|
|
254
|
+#endif // MEATPACK
|