Skip to content

Commit

Permalink
Merge pull request #3 from prusa3d/ascii_to_binary
Browse files Browse the repository at this point in the history
Ascii to binary
  • Loading branch information
enricoturri1966 authored Aug 2, 2023
2 parents 0319434 + e64445e commit 6703b19
Show file tree
Hide file tree
Showing 6 changed files with 23,630 additions and 12,625 deletions.
119 changes: 119 additions & 0 deletions src/base/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1198,4 +1198,123 @@ EResult SlicerMetadataBlock::read_data(FILE& file, const FileHeader& file_header
return EResult::Success;
}

bool Binarizer::is_enabled() const { return m_enabled; }
void Binarizer::set_enabled(bool enable) { m_enabled = enable; }
BinaryData& Binarizer::get_binary_data() { return m_binary_data; }
const BinaryData& Binarizer::get_binary_data() const { return m_binary_data; }
size_t Binarizer::get_max_gcode_cache_size() const { return m_gcode_cache_size; }
void Binarizer::set_max_gcode_cache_size(size_t size) { m_gcode_cache_size = size; }

EResult Binarizer::initialize(FILE& file, const BinarizerConfig& config)
{
if (!m_enabled)
return EResult::Success;

m_file = &file;
m_config = config;

// save header
FileHeader file_header;
file_header.checksum_type = (uint16_t)m_config.checksum;
EResult res = file_header.write(*m_file);
if (res != EResult::Success)
// propagate error
return res;

// save file metadata block
res = m_binary_data.file_metadata.write(*m_file, m_config.compression.file_metadata, m_config.checksum);
if (res != EResult::Success)
// propagate error
return res;

// save printer metadata block
res = m_binary_data.printer_metadata.write(*m_file, m_config.compression.printer_metadata, m_config.checksum);
if (res != EResult::Success)
// propagate error
return res;

// save thumbnail blocks
for (const ThumbnailBlock& block : m_binary_data.thumbnails) {
res = block.write(*m_file, m_config.checksum);
if (res != EResult::Success)
// propagate error
return res;
}

// save print metadata block
res = m_binary_data.print_metadata.write(*m_file, m_config.compression.print_metadata, m_config.checksum);
if (res != EResult::Success)
// propagate error
return res;

// save slicer metadata block
res = m_binary_data.slicer_metadata.write(*m_file, m_config.compression.slicer_metadata, m_config.checksum);
if (res != EResult::Success)
// propagate error
return res;

return EResult::Success;
}

static EResult write_gcode_block(FILE& file, const std::string& raw_data, const BinarizerConfig& config)
{
GCodeBlock block;
block.encoding_type = (uint16_t)config.gcode_encoding;
block.raw_data = raw_data;
return block.write(file, config.compression.gcode, config.checksum);
}

EResult Binarizer::append_gcode(const std::string& gcode)
{
if (gcode.empty())
return EResult::Success;

assert(m_file != nullptr);
if (m_file == nullptr)
return EResult::WriteError;

auto it_begin = gcode.begin();
do {
const size_t begin_pos = std::distance(gcode.begin(), it_begin);
const size_t end_line_pos = gcode.find_first_of('\n', begin_pos);
if (end_line_pos == std::string::npos)
return EResult::WriteError;

const size_t line_size = 1 + end_line_pos - begin_pos;
if (line_size + m_gcode_cache.length() > m_gcode_cache_size) {
if (!m_gcode_cache.empty()) {
const EResult res = write_gcode_block(*m_file, m_gcode_cache, m_config);
if (res != EResult::Success)
// propagate error
return res;
m_gcode_cache.clear();
}
}

if (line_size > m_gcode_cache_size)
return EResult::WriteError;

m_gcode_cache.insert(m_gcode_cache.end(), it_begin, it_begin + line_size);
it_begin += line_size;
} while (it_begin != gcode.end());

return EResult::Success;
}

EResult Binarizer::finalize()
{
if (!m_enabled)
return EResult::Success;

// save gcode cache, if not empty
if (!m_gcode_cache.empty()) {
const EResult res = write_gcode_block(*m_file, m_gcode_cache, m_config);
if (res != EResult::Success)
// propagate error
return res;
}

return EResult::Success;
}

}} // namespace bgcode
50 changes: 50 additions & 0 deletions src/base/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,56 @@ struct SlicerMetadataBlock : public BaseMetadataBlock
core::EResult read_data(FILE& file, const core::FileHeader& file_header, const core::BlockHeader& block_header);
};

struct BinarizerConfig
{
struct Compression
{
core::ECompressionType file_metadata{ core::ECompressionType::None };
core::ECompressionType printer_metadata{ core::ECompressionType::None };
core::ECompressionType print_metadata{ core::ECompressionType::None };
core::ECompressionType slicer_metadata{ core::ECompressionType::None };
core::ECompressionType gcode{ core::ECompressionType::None };
};
Compression compression;
core::EGCodeEncodingType gcode_encoding{ core::EGCodeEncodingType::None };
core::EMetadataEncodingType metadata_encoding{ core::EMetadataEncodingType::INI };
core::EChecksumType checksum{ core::EChecksumType::CRC32 };
};

struct BinaryData
{
FileMetadataBlock file_metadata;
PrinterMetadataBlock printer_metadata;
std::vector<ThumbnailBlock> thumbnails;
SlicerMetadataBlock slicer_metadata;
PrintMetadataBlock print_metadata;
};

class Binarizer
{
public:
bool is_enabled() const;
void set_enabled(bool enable);

BinaryData& get_binary_data();
const BinaryData& get_binary_data() const;

size_t get_max_gcode_cache_size() const;
void set_max_gcode_cache_size(size_t size);

core::EResult initialize(FILE& file, const BinarizerConfig& config);
core::EResult append_gcode(const std::string& gcode);
core::EResult finalize();

private:
FILE* m_file{ nullptr };
bool m_enabled{ false };
BinarizerConfig m_config;
BinaryData m_binary_data;
std::string m_gcode_cache;
size_t m_gcode_cache_size{ 65536 };
};

}} // bgcode::core

#endif // BGCODE_BASE_HPP
Loading

0 comments on commit 6703b19

Please sign in to comment.