My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

meatpack.cpp 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * MeatPack G-code Compression
  24. *
  25. * Algorithm & Implementation: Scott Mudge - mail@scottmudge.com
  26. * Date: Dec. 2020
  27. *
  28. * Character Frequencies from ~30 MB of comment-stripped gcode:
  29. * '1' -> 4451136 '4' -> 1353273 '\n' -> 1087683 '-' -> 90242
  30. * '0' -> 4253577 '9' -> 1352147 'G' -> 1075806 'Z' -> 34109
  31. * ' ' -> 3053297 '3' -> 1262929 'X' -> 975742 'M' -> 11879
  32. * '.' -> 3035310 '5' -> 1189871 'E' -> 965275 'S' -> 9910
  33. * '2' -> 1523296 '6' -> 1127900 'Y' -> 965274
  34. * '8' -> 1366812 '7' -> 1112908 'F' -> 99416
  35. *
  36. * When space is omitted the letter 'E' is used in its place
  37. */
  38. #include "../inc/MarlinConfig.h"
  39. #if HAS_MEATPACK
  40. #include "meatpack.h"
  41. MeatPack meatpack;
  42. #define MeatPack_ProtocolVersion "PV01"
  43. //#define MP_DEBUG
  44. #define DEBUG_OUT ENABLED(MP_DEBUG)
  45. #include "../core/debug_out.h"
  46. // The 15 most-common characters used in G-code, ~90-95% of all G-code uses these characters
  47. // Stored in SRAM for performance.
  48. uint8_t meatPackLookupTable[16] = {
  49. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  50. '.', ' ', '\n', 'G', 'X',
  51. '\0' // Unused. 0b1111 indicates a literal character
  52. };
  53. TERN_(MP_DEBUG, uint8_t chars_decoded = 0); // Log the first 64 bytes after each reset
  54. void MeatPack::reset_state() {
  55. state = 0;
  56. cmd_is_next = false;
  57. second_char = 0;
  58. cmd_count = full_char_count = char_out_count = 0;
  59. TERN_(MP_DEBUG, chars_decoded = 0);
  60. }
  61. /**
  62. * Unpack one or two characters from a packed byte into a buffer.
  63. * Return flags indicating whether any literal bytes follow.
  64. */
  65. uint8_t MeatPack::unpack_chars(const uint8_t pk, uint8_t* __restrict const chars_out) {
  66. uint8_t out = 0;
  67. // If lower nybble is 1111, the higher nybble is unused, and next char is full.
  68. if ((pk & kFirstNotPacked) == kFirstNotPacked)
  69. out = kFirstCharIsLiteral;
  70. else {
  71. const uint8_t chr = pk & 0x0F;
  72. chars_out[0] = meatPackLookupTable[chr]; // Set the first char
  73. }
  74. // Check if upper nybble is 1111... if so, we don't need the second char.
  75. if ((pk & kSecondNotPacked) == kSecondNotPacked)
  76. out |= kSecondCharIsLiteral;
  77. else {
  78. const uint8_t chr = (pk >> 4) & 0x0F;
  79. chars_out[1] = meatPackLookupTable[chr]; // Set the second char
  80. }
  81. return out;
  82. }
  83. /**
  84. * Interpret a single (non-command) character
  85. * according to the current MeatPack state.
  86. */
  87. void MeatPack::handle_rx_char_inner(const uint8_t c) {
  88. if (TEST(state, MPConfig_Bit_Active)) { // Is MeatPack active?
  89. if (!full_char_count) { // No literal characters to fetch?
  90. uint8_t buf[2] = { 0, 0 };
  91. const uint8_t res = unpack_chars(c, buf); // Decode the byte into one or two characters.
  92. if (res & kFirstCharIsLiteral) { // The 1st character couldn't be packed.
  93. ++full_char_count; // So the next stream byte is a full character.
  94. if (res & kSecondCharIsLiteral) ++full_char_count; // The 2nd character couldn't be packed. Another stream byte is a full character.
  95. else second_char = buf[1]; // Retain the unpacked second character.
  96. }
  97. else {
  98. handle_output_char(buf[0]); // Send the unpacked first character out.
  99. if (buf[0] != '\n') { // After a newline the next char won't be set
  100. if (res & kSecondCharIsLiteral) ++full_char_count; // The 2nd character couldn't be packed. The next stream byte is a full character.
  101. else handle_output_char(buf[1]); // Send the unpacked second character out.
  102. }
  103. }
  104. }
  105. else {
  106. handle_output_char(c); // Pass through the character that couldn't be packed...
  107. if (second_char) {
  108. handle_output_char(second_char); // ...and send an unpacked 2nd character, if set.
  109. second_char = 0;
  110. }
  111. --full_char_count; // One literal character was consumed
  112. }
  113. }
  114. else // Packing not enabled, just copy character to output
  115. handle_output_char(c);
  116. }
  117. /**
  118. * Buffer a single output character which will be picked up in
  119. * GCodeQueue::get_serial_commands via calls to get_result_char
  120. */
  121. void MeatPack::handle_output_char(const uint8_t c) {
  122. char_out_buf[char_out_count++] = c;
  123. #if ENABLED(MP_DEBUG)
  124. if (chars_decoded < 1024) {
  125. ++chars_decoded;
  126. DEBUG_ECHOLNPAIR("RB: ", AS_CHAR(c));
  127. }
  128. #endif
  129. }
  130. /**
  131. * Process a MeatPack command byte to update the state.
  132. * Report the new state to serial.
  133. */
  134. void MeatPack::handle_command(const MeatPack_Command c) {
  135. switch (c) {
  136. case MPCommand_QueryConfig: break;
  137. case MPCommand_EnablePacking: SBI(state, MPConfig_Bit_Active); DEBUG_ECHOLNPGM("[MPDBG] ENA REC"); break;
  138. case MPCommand_DisablePacking: CBI(state, MPConfig_Bit_Active); DEBUG_ECHOLNPGM("[MPDBG] DIS REC"); break;
  139. case MPCommand_ResetAll: reset_state(); DEBUG_ECHOLNPGM("[MPDBG] RESET REC"); break;
  140. case MPCommand_EnableNoSpaces:
  141. SBI(state, MPConfig_Bit_NoSpaces);
  142. meatPackLookupTable[kSpaceCharIdx] = kSpaceCharReplace; DEBUG_ECHOLNPGM("[MPDBG] ENA NSP"); break;
  143. case MPCommand_DisableNoSpaces:
  144. CBI(state, MPConfig_Bit_NoSpaces);
  145. meatPackLookupTable[kSpaceCharIdx] = ' '; DEBUG_ECHOLNPGM("[MPDBG] DIS NSP"); break;
  146. default: DEBUG_ECHOLNPGM("[MPDBG] UNK CMD REC");
  147. }
  148. report_state();
  149. }
  150. void MeatPack::report_state() {
  151. // NOTE: if any configuration vars are added below, the outgoing sync text for host plugin
  152. // should not contain the "PV' substring, as this is used to indicate protocol version
  153. SERIAL_ECHOPGM("[MP] ");
  154. SERIAL_ECHOPGM(MeatPack_ProtocolVersion " ");
  155. serialprint_onoff(TEST(state, MPConfig_Bit_Active));
  156. SERIAL_ECHOPGM_P(TEST(state, MPConfig_Bit_NoSpaces) ? PSTR(" NSP\n") : PSTR(" ESP\n"));
  157. }
  158. /**
  159. * Interpret a single character received from serial
  160. * according to the current meatpack state.
  161. */
  162. void MeatPack::handle_rx_char(const uint8_t c, const serial_index_t serial_ind) {
  163. if (c == kCommandByte) { // A command (0xFF) byte?
  164. if (cmd_count) { // In fact, two in a row?
  165. cmd_is_next = true; // Then a MeatPack command follows
  166. cmd_count = 0;
  167. }
  168. else
  169. ++cmd_count; // cmd_count = 1 // One command byte received so far...
  170. return;
  171. }
  172. if (cmd_is_next) { // Were two command bytes received?
  173. PORT_REDIRECT(SERIAL_PORTMASK(serial_ind));
  174. handle_command((MeatPack_Command)c); // Then the byte is a MeatPack command
  175. cmd_is_next = false;
  176. return;
  177. }
  178. if (cmd_count) { // Only a single 0xFF was received
  179. handle_rx_char_inner(kCommandByte); // A single 0xFF is passed on literally so it can be interpreted as kFirstNotPacked|kSecondNotPacked
  180. cmd_count = 0;
  181. }
  182. handle_rx_char_inner(c); // Other characters are passed on for MeatPack decoding
  183. }
  184. uint8_t MeatPack::get_result_char(char* const __restrict out) {
  185. uint8_t res = 0;
  186. if (char_out_count) {
  187. res = char_out_count;
  188. char_out_count = 0;
  189. for (uint8_t i = 0; i < res; ++i)
  190. out[i] = (char)char_out_buf[i];
  191. }
  192. return res;
  193. }
  194. #endif // HAS_MEATPACK