My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

meatpack.cpp 8.1KB

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. #define MeatPack_ProtocolVersion "PV01"
  42. //#define MP_DEBUG
  43. #define DEBUG_OUT ENABLED(MP_DEBUG)
  44. #include "../core/debug_out.h"
  45. // The 15 most-common characters used in G-code, ~90-95% of all G-code uses these characters
  46. // Stored in SRAM for performance.
  47. uint8_t meatPackLookupTable[16] = {
  48. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  49. '.', ' ', '\n', 'G', 'X',
  50. '\0' // Unused. 0b1111 indicates a literal character
  51. };
  52. #if ENABLED(MP_DEBUG)
  53. uint8_t chars_decoded = 0; // Log the first 64 bytes after each reset
  54. #endif
  55. void MeatPack::reset_state() {
  56. state = 0;
  57. cmd_is_next = false;
  58. second_char = 0;
  59. cmd_count = full_char_count = char_out_count = 0;
  60. TERN_(MP_DEBUG, chars_decoded = 0);
  61. }
  62. /**
  63. * Unpack one or two characters from a packed byte into a buffer.
  64. * Return flags indicating whether any literal bytes follow.
  65. */
  66. uint8_t MeatPack::unpack_chars(const uint8_t pk, uint8_t* __restrict const chars_out) {
  67. uint8_t out = 0;
  68. // If lower nybble is 1111, the higher nybble is unused, and next char is full.
  69. if ((pk & kFirstNotPacked) == kFirstNotPacked)
  70. out = kFirstCharIsLiteral;
  71. else {
  72. const uint8_t chr = pk & 0x0F;
  73. chars_out[0] = meatPackLookupTable[chr]; // Set the first char
  74. }
  75. // Check if upper nybble is 1111... if so, we don't need the second char.
  76. if ((pk & kSecondNotPacked) == kSecondNotPacked)
  77. out |= kSecondCharIsLiteral;
  78. else {
  79. const uint8_t chr = (pk >> 4) & 0x0F;
  80. chars_out[1] = meatPackLookupTable[chr]; // Set the second char
  81. }
  82. return out;
  83. }
  84. /**
  85. * Interpret a single (non-command) character
  86. * according to the current MeatPack state.
  87. */
  88. void MeatPack::handle_rx_char_inner(const uint8_t c) {
  89. if (TEST(state, MPConfig_Bit_Active)) { // Is MeatPack active?
  90. if (!full_char_count) { // No literal characters to fetch?
  91. uint8_t buf[2] = { 0, 0 };
  92. const uint8_t res = unpack_chars(c, buf); // Decode the byte into one or two characters.
  93. if (res & kFirstCharIsLiteral) { // The 1st character couldn't be packed.
  94. ++full_char_count; // So the next stream byte is a full character.
  95. if (res & kSecondCharIsLiteral) ++full_char_count; // The 2nd character couldn't be packed. Another stream byte is a full character.
  96. else second_char = buf[1]; // Retain the unpacked second character.
  97. }
  98. else {
  99. handle_output_char(buf[0]); // Send the unpacked first character out.
  100. if (buf[0] != '\n') { // After a newline the next char won't be set
  101. if (res & kSecondCharIsLiteral) ++full_char_count; // The 2nd character couldn't be packed. The next stream byte is a full character.
  102. else handle_output_char(buf[1]); // Send the unpacked second character out.
  103. }
  104. }
  105. }
  106. else {
  107. handle_output_char(c); // Pass through the character that couldn't be packed...
  108. if (second_char) {
  109. handle_output_char(second_char); // ...and send an unpacked 2nd character, if set.
  110. second_char = 0;
  111. }
  112. --full_char_count; // One literal character was consumed
  113. }
  114. }
  115. else // Packing not enabled, just copy character to output
  116. handle_output_char(c);
  117. }
  118. /**
  119. * Buffer a single output character which will be picked up in
  120. * GCodeQueue::get_serial_commands via calls to get_result_char
  121. */
  122. void MeatPack::handle_output_char(const uint8_t c) {
  123. char_out_buf[char_out_count++] = c;
  124. #if ENABLED(MP_DEBUG)
  125. if (chars_decoded < 1024) {
  126. ++chars_decoded;
  127. DEBUG_ECHOLNPGM("RB: ", AS_CHAR(c));
  128. }
  129. #endif
  130. }
  131. /**
  132. * Process a MeatPack command byte to update the state.
  133. * Report the new state to serial.
  134. */
  135. void MeatPack::handle_command(const MeatPack_Command c) {
  136. switch (c) {
  137. case MPCommand_QueryConfig: break;
  138. case MPCommand_EnablePacking: SBI(state, MPConfig_Bit_Active); DEBUG_ECHOLNPGM("[MPDBG] ENA REC"); break;
  139. case MPCommand_DisablePacking: CBI(state, MPConfig_Bit_Active); DEBUG_ECHOLNPGM("[MPDBG] DIS REC"); break;
  140. case MPCommand_ResetAll: reset_state(); DEBUG_ECHOLNPGM("[MPDBG] RESET REC"); break;
  141. case MPCommand_EnableNoSpaces:
  142. SBI(state, MPConfig_Bit_NoSpaces);
  143. meatPackLookupTable[kSpaceCharIdx] = kSpaceCharReplace; DEBUG_ECHOLNPGM("[MPDBG] ENA NSP"); break;
  144. case MPCommand_DisableNoSpaces:
  145. CBI(state, MPConfig_Bit_NoSpaces);
  146. meatPackLookupTable[kSpaceCharIdx] = ' '; DEBUG_ECHOLNPGM("[MPDBG] DIS NSP"); break;
  147. default: DEBUG_ECHOLNPGM("[MPDBG] UNK CMD REC");
  148. }
  149. report_state();
  150. }
  151. void MeatPack::report_state() {
  152. // NOTE: if any configuration vars are added below, the outgoing sync text for host plugin
  153. // should not contain the "PV' substring, as this is used to indicate protocol version
  154. SERIAL_ECHOPGM("[MP] " MeatPack_ProtocolVersion " ");
  155. serialprint_onoff(TEST(state, MPConfig_Bit_Active));
  156. SERIAL_ECHOF(TEST(state, MPConfig_Bit_NoSpaces) ? F(" NSP\n") : F(" 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