| 1 | #ifndef _BGCODE_BINARIZE_MEATPACK_HPP_ |
| 2 | #define _BGCODE_BINARIZE_MEATPACK_HPP_ |
| 3 | |
| 4 | #include <cstdint> |
| 5 | #include <vector> |
| 6 | #include <string> |
| 7 | #include <array> |
| 8 | |
| 9 | // |
| 10 | // Adaptation of MeatPack G-Code Compression taken from: |
| 11 | // https://github.com/scottmudge/OctoPrint-MeatPack/blob/master/OctoPrint_MeatPack/meatpack.py |
| 12 | // https://github.com/bubnikv/OctoPrint-MeatPack/blob/master/OctoPrint_MeatPack/meatpack.py |
| 13 | // https://github.com/scottmudge/Prusa-Firmware-MeatPack/blob/MK3_sm_MeatPack/Firmware/meatpack.h |
| 14 | // https://github.com/scottmudge/Prusa-Firmware-MeatPack/blob/MK3_sm_MeatPack/Firmware/meatpack.cpp |
| 15 | // |
| 16 | |
| 17 | namespace MeatPack { |
| 18 | |
| 19 | static constexpr const uint8_t Flag_OmitWhitespaces{ 0x01 }; |
| 20 | static constexpr const uint8_t Flag_RemoveComments{ 0x02 }; |
| 21 | |
| 22 | class MPBinarizer |
| 23 | { |
| 24 | public: |
| 25 | explicit MPBinarizer(uint8_t flags = 0); |
| 26 | |
| 27 | void initialize(std::vector<uint8_t>& dst); |
| 28 | void finalize(std::vector<uint8_t>& dst); |
| 29 | |
| 30 | void binarize_line(const std::string& line, std::vector<uint8_t>& dst); |
| 31 | |
| 32 | private: |
| 33 | unsigned char m_flags{ 0 }; |
| 34 | bool m_binarizing{ false }; |
| 35 | |
| 36 | struct LookupTables |
| 37 | { |
| 38 | std::array<uint8_t, 256> packable; |
| 39 | std::array<uint8_t, 256> value; |
| 40 | bool initialized; |
| 41 | unsigned char flags; |
| 42 | }; |
| 43 | |
| 44 | static LookupTables s_lookup_tables; |
| 45 | |
| 46 | void append_command(unsigned char cmd, std::vector<uint8_t>& dst); |
| 47 | void initialize_lookup_tables(); |
| 48 | }; |
| 49 | |
| 50 | extern void unbinarize(const std::vector<uint8_t>& src, std::string& dst); |
| 51 | |
| 52 | } // namespace MeatPack |
| 53 | |
| 54 | #endif // _BGCODE_BINARIZE_MEATPACK_HPP_ |