From 0462ba4d34808d073513583077dfdf1de03adced Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Sat, 23 Mar 2024 14:35:43 +0100 Subject: [PATCH] This Commit adds a rudimentary printer factory to greatly simplify the creation of printers --- src/P700Printer.cpp | 13 +++++----- src/P700Printer.hpp | 13 ++++------ src/PrinterDriverFactory.cpp | 22 ++++++++++++++++ src/PrinterDriverFactory.hpp | 22 ++++++++++++++++ src/PtouchPrint.cpp | 34 ++++++++++--------------- src/PtouchPrint.hpp | 5 ++-- src/interface/IPrinterDriver.hpp | 3 +-- src/interface/IPrinterTypes.hpp | 4 +-- src/libusbwrap/LibUsbTypes.hpp | 7 +++-- src/libusbwrap/UsbDevice.cpp | 8 ++---- src/libusbwrap/UsbDevice.hpp | 3 +-- src/libusbwrap/interface/IUsbDevice.hpp | 3 +-- src/meson.build | 2 ++ 13 files changed, 86 insertions(+), 53 deletions(-) create mode 100644 src/PrinterDriverFactory.cpp create mode 100644 src/PrinterDriverFactory.hpp diff --git a/src/P700Printer.cpp b/src/P700Printer.cpp index 98ed2ed..68ea725 100644 --- a/src/P700Printer.cpp +++ b/src/P700Printer.cpp @@ -41,6 +41,11 @@ namespace ptprnt::printer { +const PrinterInfo P700Printer::mInfo = {.driverName = "P700", + .name = "Brother P-touch P700", + .version = "v1.0", + .usbId{0x04f9, 0x2061}}; + P700Printer::~P700Printer() { detachUsbDevice(); if (mUsbHndl) { @@ -80,12 +85,8 @@ const PrinterStatus P700Printer::getPrinterStatus() { return PrinterStatus{.tapeWidthMm = recvBuf[10]}; } -const uint16_t P700Printer::getVid() { - return mInfo.vid; -} - -const uint16_t P700Printer::getPid() { - return mInfo.pid; +const libusbwrap::usbId P700Printer::getUsbId() { + return mInfo.usbId; } bool P700Printer::attachUsbDevice(std::shared_ptr usbHndl) { diff --git a/src/P700Printer.hpp b/src/P700Printer.hpp index a21fc55..6b2243d 100644 --- a/src/P700Printer.hpp +++ b/src/P700Printer.hpp @@ -25,6 +25,7 @@ #include "interface/IPrinterDriver.hpp" #include "interface/IPrinterTypes.hpp" +#include "libusbwrap/LibUsbTypes.hpp" #include "libusbwrap/interface/IUsbDevice.hpp" #pragma once @@ -44,11 +45,13 @@ class P700Printer : public ::ptprnt::IPrinterDriver { P700Printer(P700Printer&&) = default; P700Printer& operator=(P700Printer&&) = default; + // Printer info has to be static to be accessed without instantiation + static const PrinterInfo mInfo; + // IPrinterDriver const std::string_view getDriverName() override; const std::string_view getName() override; - const uint16_t getPid() override; - const uint16_t getVid() override; + const libusbwrap::usbId getUsbId() override; const std::string_view getVersion() override; const PrinterInfo getPrinterInfo() override; const PrinterStatus getPrinterStatus() override; @@ -62,12 +65,6 @@ class P700Printer : public ::ptprnt::IPrinterDriver { 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}; std::map> commands{ {"rasterstart", {0x1b, 0x69, 0x61, diff --git a/src/PrinterDriverFactory.cpp b/src/PrinterDriverFactory.cpp new file mode 100644 index 0000000..86f4ea9 --- /dev/null +++ b/src/PrinterDriverFactory.cpp @@ -0,0 +1,22 @@ +#include "PrinterDriverFactory.hpp" +#include +#include "libusbwrap/LibUsbTypes.hpp" +#include "P700Printer.hpp" +#include + +namespace ptprnt { + +std::shared_ptr PrinterDriverFactory::create(libusbwrap::usbId id) { + + if(printer::P700Printer::mInfo.usbId == id) { + spdlog::debug("Printer is P700!"); + return std::make_shared(); + } + else { + spdlog::debug("{:04x}:{:04x} is not a compatible printer", id.first, id.second); + } + return nullptr; +} + + +} \ No newline at end of file diff --git a/src/PrinterDriverFactory.hpp b/src/PrinterDriverFactory.hpp new file mode 100644 index 0000000..2d75586 --- /dev/null +++ b/src/PrinterDriverFactory.hpp @@ -0,0 +1,22 @@ +#include +#include "interface/IPrinterDriver.hpp" +#include "libusbwrap/LibUsbTypes.hpp" + +namespace ptprnt { + +class PrinterDriverFactory { + public: + PrinterDriverFactory() = default; + ~PrinterDriverFactory() = default; + + PrinterDriverFactory(const PrinterDriverFactory&) = delete; + PrinterDriverFactory& operator=(const PrinterDriverFactory&) = delete; + PrinterDriverFactory(PrinterDriverFactory&&) = delete; + PrinterDriverFactory& operator=(PrinterDriverFactory&&) = delete; + + std::shared_ptr create(libusbwrap::usbId id); + + private: +}; + +} \ No newline at end of file diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp index 60caab3..88e0efe 100644 --- a/src/PtouchPrint.cpp +++ b/src/PtouchPrint.cpp @@ -37,7 +37,7 @@ #include #include "CLI/Option.hpp" -#include "P700Printer.hpp" +#include "PrinterDriverFactory.hpp" #include "graphics/Bitmap.hpp" #include "libusbwrap/UsbDeviceFactory.hpp" @@ -65,14 +65,14 @@ int PtouchPrint::init(int argc, char** argv) { spdlog::error("Could not initialize libusb"); return -1; } - mCompatiblePrinters = {std::make_shared()}; return 0; } int PtouchPrint::run() { spdlog::debug("ptprnt version {}", mVersionString); SPDLOG_TRACE("testing trace"); - auto numFoundPrinters = getCompatiblePrinters(); + 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"); @@ -82,8 +82,9 @@ int PtouchPrint::run() { return -1; } - auto printer = mCompatiblePrinters[0]; - auto devices = mUsbDeviceFactory.findDevices(printer->getVid(), printer->getPid()); + auto printer = mDetectedPrinters[0]; + const auto printerUsbId = printer->getUsbId(); + 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"); @@ -103,26 +104,19 @@ int PtouchPrint::run() { return 0; } -unsigned int PtouchPrint::getCompatiblePrinters() { +std::vector> PtouchPrint::getCompatiblePrinters() { + auto usbDevs = mUsbDeviceFactory.findAllDevices(); + auto driverFactory = std::make_unique(); + std::vector> foundPrinterDrivers {}; for (auto usbDev : usbDevs) { - auto foundPrinterIt = - std::find_if(mCompatiblePrinters.begin(), mCompatiblePrinters.end(), - [usbDev](const std::shared_ptr& printer) { - return printer->getPid() == usbDev->getPid() && - printer->getVid() == usbDev->getVid(); - }); - if (foundPrinterIt == mCompatiblePrinters.end()) { - continue; + auto driver = driverFactory->create(usbDev->getUsbId()); + if(driver != nullptr) { + foundPrinterDrivers.push_back(driver); } - auto foundPrinter = *foundPrinterIt; - spdlog::info("Found Printer {}", foundPrinter->getName()); - mDetectedPrinters.push_back(foundPrinter); } - // we can delete all instantiated printers that are not compatible - mCompatiblePrinters.clear(); - return mDetectedPrinters.size(); + return foundPrinterDrivers; } void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) { diff --git a/src/PtouchPrint.hpp b/src/PtouchPrint.hpp index 303d78c..75b04b3 100644 --- a/src/PtouchPrint.hpp +++ b/src/PtouchPrint.hpp @@ -49,13 +49,12 @@ class PtouchPrint { // methods void setupLogger(spdlog::level::level_enum lvl); void setupCliParser(); - unsigned int getCompatiblePrinters(); + std::vector> getCompatiblePrinters(); // member variables CLI::App mApp{ptprnt::APP_DESC}; libusbwrap::UsbDeviceFactory mUsbDeviceFactory{}; - std::vector> mCompatiblePrinters{}; - std::vector> mDetectedPrinters{}; + std::vector> mDetectedPrinters{}; std::vector mCommands{}; std::string mVersionString = ""; diff --git a/src/interface/IPrinterDriver.hpp b/src/interface/IPrinterDriver.hpp index e90dec1..7f39ba8 100644 --- a/src/interface/IPrinterDriver.hpp +++ b/src/interface/IPrinterDriver.hpp @@ -34,8 +34,7 @@ class IPrinterDriver { virtual const std::string_view getDriverName() = 0; virtual const std::string_view getName() = 0; virtual const std::string_view getVersion() = 0; - virtual const uint16_t getVid() = 0; - virtual const uint16_t getPid() = 0; + virtual const libusbwrap::usbId getUsbId() = 0; virtual const PrinterInfo getPrinterInfo() = 0; virtual const PrinterStatus getPrinterStatus() = 0; virtual bool attachUsbDevice(std::shared_ptr usbHndl) = 0; diff --git a/src/interface/IPrinterTypes.hpp b/src/interface/IPrinterTypes.hpp index 9e6e66d..effda94 100644 --- a/src/interface/IPrinterTypes.hpp +++ b/src/interface/IPrinterTypes.hpp @@ -21,6 +21,7 @@ #include #include +#include "libusbwrap/LibUsbTypes.hpp" namespace ptprnt { @@ -28,8 +29,7 @@ struct PrinterInfo { std::string_view driverName = ""; std::string_view name = ""; std::string_view version = ""; - uint16_t vid = 0x00; - uint16_t pid = 0x00; + libusbwrap::usbId usbId {0x00, 0x00}; }; struct PrinterStatus { diff --git a/src/libusbwrap/LibUsbTypes.hpp b/src/libusbwrap/LibUsbTypes.hpp index 43977c0..054210f 100644 --- a/src/libusbwrap/LibUsbTypes.hpp +++ b/src/libusbwrap/LibUsbTypes.hpp @@ -19,7 +19,7 @@ #pragma once -#include +#include #include "libusb.h" @@ -40,4 +40,7 @@ enum class Error { NOT_SUPPORTED = LIBUSB_ERROR_NOT_SUPPORTED, OTHER = LIBUSB_ERROR_OTHER }; -} \ No newline at end of file + +using usbId = std::pair; + +} // namespace libusbwrap \ No newline at end of file diff --git a/src/libusbwrap/UsbDevice.cpp b/src/libusbwrap/UsbDevice.cpp index 8841561..dc073d8 100644 --- a/src/libusbwrap/UsbDevice.cpp +++ b/src/libusbwrap/UsbDevice.cpp @@ -104,12 +104,8 @@ bool UsbDevice::bulkTransfer(uint8_t endpoint, std::vector& data, int* return true; } -const uint16_t UsbDevice::getVid() { - return mLibusbDevDesc.idVendor; -} - -const uint16_t UsbDevice::getPid() { - return mLibusbDevDesc.idProduct; +const usbId UsbDevice::getUsbId() { + return {mLibusbDevDesc.idVendor, mLibusbDevDesc.idProduct}; } const device::Speed UsbDevice::getSpeed() { diff --git a/src/libusbwrap/UsbDevice.hpp b/src/libusbwrap/UsbDevice.hpp index 7e32ebc..712e3a7 100644 --- a/src/libusbwrap/UsbDevice.hpp +++ b/src/libusbwrap/UsbDevice.hpp @@ -47,8 +47,7 @@ class UsbDevice : public IUsbDevice { unsigned int timeout) override; // getters - const uint16_t getVid() override; - const uint16_t getPid() override; + const usbId getUsbId() override; const device::Speed getSpeed() override; const uint8_t getBusNumber() override; const uint8_t getPortNumber() override; diff --git a/src/libusbwrap/interface/IUsbDevice.hpp b/src/libusbwrap/interface/IUsbDevice.hpp index 5025c59..e547beb 100644 --- a/src/libusbwrap/interface/IUsbDevice.hpp +++ b/src/libusbwrap/interface/IUsbDevice.hpp @@ -36,8 +36,7 @@ class IUsbDevice { unsigned int timeout) = 0; // getters - virtual const uint16_t getVid() = 0; - virtual const uint16_t getPid() = 0; + virtual const usbId getUsbId() = 0; virtual const device::Speed getSpeed() = 0; virtual const uint8_t getBusNumber() = 0; virtual const uint8_t getPortNumber() = 0; diff --git a/src/meson.build b/src/meson.build index a5fb667..9061231 100644 --- a/src/meson.build +++ b/src/meson.build @@ -8,6 +8,7 @@ ptprnt_hpps = files ( 'interface/IPrinterTypes.hpp', 'P700Printer.hpp', 'PtouchPrint.hpp', + 'PrinterDriverFactory.hpp', 'graphics/Bitmap.hpp', 'graphics/Image.hpp', 'graphics/Monochrome.hpp' @@ -15,6 +16,7 @@ ptprnt_hpps = files ( ptprnt_srcs = files ( 'PtouchPrint.cpp', + 'PrinterDriverFactory.cpp', 'P700Printer.cpp', 'graphics/Image.cpp', 'graphics/Bitmap.cpp',