diff --git a/.clang-format b/.clang-format index a043404..08a6e94 100644 --- a/.clang-format +++ b/.clang-format @@ -41,7 +41,7 @@ BreakBeforeBinaryOperators: None BreakBeforeTernaryOperators: true BreakConstructorInitializers: BeforeColon BreakInheritanceList: BeforeColon -ColumnLimit: 100 +ColumnLimit: 120 CompactNamespaces: false ContinuationIndentWidth: 4 Cpp11BracedListStyle: true diff --git a/.vscode/settings.json b/.vscode/settings.json index d24ff8e..eef27fa 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -85,6 +85,9 @@ }, "clang-tidy.buildPath": "builddir/", "clangd.onConfigChanged": "restart", - "C_Cpp.default.compileCommands": "/home/moritz/Projekte/ptouch-prnt/builddir/compile_commands.json", - "gcovViewer.buildDirectories": ["/home/moritz/Projekte/ptouch-prnt/builddir"] + "C_Cpp.default.compileCommands": "/home/moritz/src/ptouch-prnt/builddir/compile_commands.json", + "gcovViewer.buildDirectories": [ + "/home/moritz/Projekte/ptouch-prnt/builddir" + ], + "C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild" } diff --git a/meson.build b/meson.build index 2ad425a..2c23677 100644 --- a/meson.build +++ b/meson.build @@ -1,7 +1,7 @@ project('ptprnt', 'cpp', version: 'v0.1.0-'+run_command('git', 'rev-parse', '--short', 'HEAD', check: true).stdout().strip(), license: 'GPLv3', - default_options : ['c_std=c11', 'cpp_std=c++17', 'b_sanitize=none', 'b_lto=true', 'b_lto_mode=thin', 'b_thinlto_cache=true'] + default_options : ['c_std=c11', 'cpp_std=c++20', 'b_sanitize=none', 'b_lto=true', 'b_lto_mode=thin', 'b_thinlto_cache=true'] ) usb_dep = dependency('libusb-1.0') diff --git a/src/P700Printer.cpp b/src/P700Printer.cpp index 6632294..7d60878 100644 --- a/src/P700Printer.cpp +++ b/src/P700Printer.cpp @@ -37,7 +37,7 @@ #include "spdlog/fmt/bin_to_hex.h" // as long as DRYRUN is defined, no data is actually send to the printer, we need to save some tape ;) -//#define DRYRUN +#define DRYRUN namespace ptprnt::printer { @@ -66,15 +66,14 @@ const PrinterInfo P700Printer::getPrinterInfo() { const PrinterStatus P700Printer::getPrinterStatus() { using namespace std::chrono_literals; - std::vector getStatusCmd({0x1b, 0x69, 0x53}); // status info request - send(getStatusCmd); + send(p700::commands::GET_STATUS); int tx = 0; int tries = 0; std::vector recvBuf(32); while (tries++ < MAX_TRIES_GET_STATUS) { std::this_thread::sleep_for(100ms); - mUsbHndl->bulkTransfer(commands["printerinfo"][0], recvBuf, &tx, 0); + mUsbHndl->bulkTransfer(p700::commands::INFO[0], recvBuf, &tx, 0); } return PrinterStatus{.tapeWidthMm = recvBuf[10]}; @@ -131,7 +130,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap& bitmap) } #endif - send(commands["rasterstart"]); + send(p700::commands::RASTER_START); std::vector rastercmd(4); rastercmd[0] = 0x47; rastercmd[1] = 0x00; // size +1 @@ -158,18 +157,18 @@ bool P700Printer::printBitmap(const graphics::Bitmap& bitmap) }; } - send(commands["eject"]); + send(p700::commands::EJECT); return true; } bool P700Printer::printText(const std::string& text, uint16_t fontSize) { - send(commands["lf"]); - send(commands["ff"]); - send(commands["eject"]); + send(p700::commands::LF); + send(p700::commands::FF); + send(p700::commands::EJECT); return true; } -bool P700Printer::send(std::vector& data) { +bool P700Printer::send(const std::vector& data) { if (mUsbHndl == nullptr || data.size() > 128) { spdlog::error("Invalid device handle or invalid data."); @@ -189,8 +188,7 @@ bool P700Printer::send(std::vector& data) { #endif if (tx != static_cast(data.size())) { - spdlog::error("Could not transfer all data via USB bulk transfer. Only sent {} of {} bytes", - tx, data.size()); + spdlog::error("Could not transfer all data via USB bulk transfer. Only sent {} of {} bytes", tx, data.size()); return false; } diff --git a/src/P700Printer.hpp b/src/P700Printer.hpp index 0c26562..f410a73 100644 --- a/src/P700Printer.hpp +++ b/src/P700Printer.hpp @@ -31,13 +31,23 @@ #pragma once namespace ptprnt::printer { +namespace p700::commands { +const cmd_T GET_STATUS{0x1b, 0x69, 0x53}; +const cmd_T RASTER_START{0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +const cmd_T INFO{0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; +const cmd_T PACKBITSON{0x02}; +const cmd_T LF{0x5a}; +const cmd_T FF{0x0c}; +const cmd_T EJECT{0x1a}; +const cmd_T PRINTER_INFO{0x81}; +} // namespace p700::commands constexpr uint8_t MAX_TRIES_GET_STATUS = 10; class P700Printer : public ::ptprnt::IPrinterDriver { public: P700Printer() = default; - ~P700Printer(); + ~P700Printer() override; // delete copy ctor and assignment P700Printer(const P700Printer&) = default; @@ -59,7 +69,7 @@ class P700Printer : public ::ptprnt::IPrinterDriver { bool printText(const std::string& text, uint16_t fontSize) override; private: - bool send(std::vector& data); + bool send(const std::vector& data); bool init(); std::shared_ptr mUsbHndl{nullptr}; @@ -70,16 +80,6 @@ class P700Printer : public ::ptprnt::IPrinterDriver { .vid = 0x04f9, .pid = 0x2061, .pixelLines = 128}; - std::map> commands{ - {"rasterstart", - {0x1b, 0x69, 0x61, - 0x01}}, // unique for P700, other printers have the 2 byte set to 0x52 instead of 0x61 - {"info", {0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, - {"packbitson", {0x02}}, - {"lf", {0x5a}}, - {"ff", {0x0c}}, - {"eject", {0x1a}}, - {"printerinfo", {0x81}}}; }; } // namespace ptprnt::printer \ No newline at end of file diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp index 3f27ce6..e6d6d73 100644 --- a/src/PtouchPrint.cpp +++ b/src/PtouchPrint.cpp @@ -113,7 +113,7 @@ int PtouchPrint::run() { return -1; } label.writeToPng("salat.png"); - printer->printBitmap(bm); + //printer->printBitmap(bm); } } diff --git a/src/interface/IPrinterDriver.hpp b/src/interface/IPrinterDriver.hpp index e90dec1..4b5376b 100644 --- a/src/interface/IPrinterDriver.hpp +++ b/src/interface/IPrinterDriver.hpp @@ -27,7 +27,6 @@ #include "libusbwrap/interface/IUsbDevice.hpp" namespace ptprnt { - class IPrinterDriver { public: virtual ~IPrinterDriver() = default; diff --git a/src/interface/IPrinterTypes.hpp b/src/interface/IPrinterTypes.hpp index dfcdfbc..ff75fa4 100644 --- a/src/interface/IPrinterTypes.hpp +++ b/src/interface/IPrinterTypes.hpp @@ -21,6 +21,7 @@ #include #include +#include namespace ptprnt { @@ -38,4 +39,6 @@ struct PrinterStatus { unsigned int tapeWidthMm = 0.0; }; +using cmd_T = std::vector; + } // namespace ptprnt \ No newline at end of file