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 d9a3451..0ab0688 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 { @@ -71,15 +71,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]}; @@ -132,7 +131,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 @@ -159,7 +158,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap& bitmap) }; } - send(commands["eject"]); + send(p700::commands::EJECT); return true; } @@ -184,13 +183,13 @@ bool P700Printer::setVAlign(VAlignPosition vpos) { } bool P700Printer::print() { - 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."); @@ -210,8 +209,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 2d82448..9c1d06e 100644 --- a/src/P700Printer.hpp +++ b/src/P700Printer.hpp @@ -32,13 +32,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; @@ -67,14 +77,20 @@ class P700Printer : public ::ptprnt::IPrinterDriver { bool print() override; private: - bool send(std::vector& data); + bool send(const std::vector& data); bool init(); std::shared_ptr mUsbHndl{nullptr}; + + static constexpr PrinterInfo mInfo{.driverName = "P700", + .name = "Brother P-touch P700", + .version = "v1.0", + .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 + {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}}, diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp index 28369c7..e9d8fa2 100644 --- a/src/PtouchPrint.cpp +++ b/src/PtouchPrint.cpp @@ -75,8 +75,7 @@ int PtouchPrint::run() { mDetectedPrinters = getCompatiblePrinters(); auto numFoundPrinters = mDetectedPrinters.size(); if (numFoundPrinters == 0) { - spdlog::error( - "No compatible printers found, please make sure that they are turned on and connected"); + spdlog::error("No compatible printers found, please make sure that they are turned on and connected"); return -1; } else if (numFoundPrinters > 1) { spdlog::warn("Found more than one compatible printer. Currently not supported."); @@ -85,10 +84,9 @@ int PtouchPrint::run() { auto printer = mDetectedPrinters[0]; const auto printerUsbId = printer->getUsbId(); - auto devices = mUsbDeviceFactory.findDevices(printerUsbId.first, printerUsbId.second); + auto devices = mUsbDeviceFactory.findDevices(printerUsbId.first, printerUsbId.second); if (devices.size() != 1) { - spdlog::warn( - "Found more than one device of the same printer on bus. Currently not supported"); + spdlog::warn("Found more than one device of the same printer on bus. Currently not supported"); return -1; } printer->attachUsbDevice(std::move(devices[0])); @@ -115,12 +113,10 @@ int PtouchPrint::run() { printer->setFontSize(static_cast(std::atoi(cmd.second.c_str()))); break; case CliCmdType::HAlign:; - spdlog::debug("[Not implemented] Setting text horizontal alignment to {}", - cmd.second); + spdlog::debug("[Not implemented] Setting text horizontal alignment to {}", cmd.second); break; case CliCmdType::VAlign:; - spdlog::debug("[Not implemented] Setting text vertical alignment to {}", - cmd.second); + spdlog::debug("[Not implemented] Setting text vertical alignment to {}", cmd.second); break; case CliCmdType::None:; [[fallthrough]]; @@ -209,7 +205,6 @@ void PtouchPrint::setupCliParser() { ->each([this](std::string halign) { mCommands.emplace_back(CliCmdType::HAlign, halign); }); // Image options - mApp.add_option("-i,--image", "Image to print. Excludes all text printing ") - ->group("Image printing"); + mApp.add_option("-i,--image", "Image to print. Excludes all text printing ")->group("Image printing"); } } // namespace ptprnt \ No newline at end of file diff --git a/src/interface/IPrinterDriver.hpp b/src/interface/IPrinterDriver.hpp index 3b461bc..9655887 100644 --- a/src/interface/IPrinterDriver.hpp +++ b/src/interface/IPrinterDriver.hpp @@ -28,7 +28,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 1cce413..75d9c31 100644 --- a/src/interface/IPrinterTypes.hpp +++ b/src/interface/IPrinterTypes.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "libusbwrap/LibUsbTypes.hpp" @@ -40,6 +41,8 @@ struct PrinterStatus { unsigned int tapeWidthMm = 0.0; }; +using cmd_T = std::vector; + enum class HAlignPosition { UNKNOWN = 0, LEFT = 1,