diff --git a/meson.build b/meson.build index 00d09ad..1298279 100644 --- a/meson.build +++ b/meson.build @@ -11,6 +11,7 @@ incdir = include_directories('src') srcs = [ 'src/main.cpp', + 'src/PtouchPrint.cpp', 'src/P700Printer.cpp', 'src/libusbwrap/UsbDeviceFactory.cpp', 'src/libusbwrap/UsbDevice.cpp' diff --git a/src/AutoDetect.hpp b/src/AutoDetect.hpp deleted file mode 100644 index c4a4505..0000000 --- a/src/AutoDetect.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include - -#include "interface/IPrinterDriver.hpp" -#include "interface/IPrinterInfo.hpp" -#include "libusbwrap/interface/IUsbDevice.hpp" - -#pragma once - -namespace ptprnt { - -class AutoDetect { - public: - AutoDetect(std::vector> printers, - std::vector> devices); - ~AutoDetect(); - - std::pair, typename std::shared_ptr> - getCompatiblePrinters(); - - private: -}; - -} // namespace ptprnt \ No newline at end of file diff --git a/src/P700Printer.cpp b/src/P700Printer.cpp index 285492c..41b1bf0 100644 --- a/src/P700Printer.cpp +++ b/src/P700Printer.cpp @@ -4,23 +4,27 @@ namespace ptprnt::printer { const std::string_view P700Printer::getDriverName() { - return info.driverName; + return mInfo.driverName; } const std::string_view P700Printer::getName() { - return info.name; + return mInfo.name; } const std::string_view P700Printer::getVersion() { - return info.version; + return mInfo.version; +} + +const PrinterInfo P700Printer::getPrinterInfo() { + return mInfo; } const uint16_t P700Printer::getVid() { - return info.vid; + return mInfo.vid; } const uint16_t P700Printer::getPid() { - return info.pid; + return mInfo.pid; } bool P700Printer::attachUsbDevice(std::shared_ptr usbHndl) { diff --git a/src/P700Printer.hpp b/src/P700Printer.hpp index 02b24de..e18497d 100644 --- a/src/P700Printer.hpp +++ b/src/P700Printer.hpp @@ -1,14 +1,14 @@ #include #include "interface/IPrinterDriver.hpp" -#include "interface/IPrinterInfo.hpp" +#include "interface/IPrinterTypes.hpp" #include "libusbwrap/interface/IUsbDevice.hpp" #pragma once namespace ptprnt::printer { -class P700Printer : public ::ptprnt::IPrinterInfo, public ::ptprnt::IPrinterDriver { +class P700Printer : public ::ptprnt::IPrinterDriver { public: P700Printer() = default; ~P700Printer() = default; @@ -17,14 +17,13 @@ class P700Printer : public ::ptprnt::IPrinterInfo, public ::ptprnt::IPrinterDriv P700Printer(const P700Printer&) = delete; P700Printer& operator=(P700Printer&) = delete; - // IPrinterInfo + // IPrinterDriver const std::string_view getDriverName() override; const std::string_view getName() override; const uint16_t getPid() override; const uint16_t getVid() override; const std::string_view getVersion() override; - - // IPrinterDriver + const PrinterInfo getPrinterInfo() override; bool attachUsbDevice(std::shared_ptr usbHndl) override; bool detachUsbDevice() override; bool printBitmap(const Bitmap& bitmap) override; @@ -33,11 +32,11 @@ class P700Printer : public ::ptprnt::IPrinterInfo, public ::ptprnt::IPrinterDriv private: std::shared_ptr mUsbHndl{nullptr}; - PrinterInfo info{.driverName = "P700", - .name = "Brother P-touch P700", - .version = "v1.0", - .vid = 0x04f9, - .pid = 0x2061}; + PrinterInfo mInfo{.driverName = "P700", + .name = "Brother P-touch P700", + .version = "v1.0", + .vid = 0x04f9, + .pid = 0x2061}; }; } // namespace ptprnt::printer \ No newline at end of file diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp new file mode 100644 index 0000000..037e444 --- /dev/null +++ b/src/PtouchPrint.cpp @@ -0,0 +1,44 @@ +#include "PtouchPrint.hpp" + +#include + +#include "P700Printer.hpp" +#include "libusbwrap/UsbDeviceFactory.hpp" + +PtouchPrint::PtouchPrint() {} + +PtouchPrint::~PtouchPrint() {} + +void PtouchPrint::init() { + mUsbDeviceFactory.init(); + mCompatiblePrinters = {std::make_shared()}; +} + +void PtouchPrint::run() { + if (getCompatiblePrinters() == 0) { + spdlog::error( + "No compatible printers found, please make sure if they are turned on and connected"); + } +} + +unsigned int PtouchPrint::getCompatiblePrinters() { + auto usbDevs = mUsbDeviceFactory.findAllDevices(); + + 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 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(); +} diff --git a/src/PtouchPrint.hpp b/src/PtouchPrint.hpp new file mode 100644 index 0000000..141ef27 --- /dev/null +++ b/src/PtouchPrint.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include "interface/IPrinterDriver.hpp" +#include "libusbwrap/UsbDeviceFactory.hpp" + +class PtouchPrint { + public: + PtouchPrint(); + ~PtouchPrint(); + + void init(); + void run(); + + private: + // methods + unsigned int getCompatiblePrinters(); + + // member variables + libusbwrap::UsbDeviceFactory mUsbDeviceFactory; + std::vector> mCompatiblePrinters; + std::vector> mDetectedPrinters; +}; \ No newline at end of file diff --git a/src/interface/IPrinterDriver.hpp b/src/interface/IPrinterDriver.hpp index 36036b4..b91c8b2 100644 --- a/src/interface/IPrinterDriver.hpp +++ b/src/interface/IPrinterDriver.hpp @@ -1,14 +1,22 @@ #pragma once #include +#include #include "Bitmap.hpp" +#include "interface/IPrinterTypes.hpp" #include "libusbwrap/interface/IUsbDevice.hpp" namespace ptprnt { class IPrinterDriver { public: + 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 PrinterInfo getPrinterInfo() = 0; virtual bool attachUsbDevice(std::shared_ptr usbHndl) = 0; virtual bool detachUsbDevice() = 0; virtual bool printBitmap(const Bitmap& bitmap) = 0; diff --git a/src/interface/IPrinterInfo.hpp b/src/interface/IPrinterTypes.hpp similarity index 50% rename from src/interface/IPrinterInfo.hpp rename to src/interface/IPrinterTypes.hpp index ab28aea..e4d184a 100644 --- a/src/interface/IPrinterInfo.hpp +++ b/src/interface/IPrinterTypes.hpp @@ -14,13 +14,4 @@ struct PrinterInfo { uint16_t pid = 0x00; }; -class IPrinterInfo { - public: - 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; -}; - } // namespace ptprnt \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7d20249..e4e4f83 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,6 @@ -#include #include -#include - -#include "AutoDetect.hpp" -#include "P700Printer.hpp" -#include "interface/IPrinterInfo.hpp" -#include "libusbwrap/UsbDeviceFactory.hpp" +#include "PtouchPrint.hpp" void setupLogger() { spdlog::set_level(spdlog::level::debug); @@ -16,28 +10,9 @@ void setupLogger() { int main(int argc, char** argv) { setupLogger(); - std::vector> compatiblePrinters = { - std::make_shared()}; - - std::vector> detectedPrinters{}; - auto usbFactory = libusbwrap::UsbDeviceFactory(); - usbFactory.init(); - auto usbDevs = usbFactory.findAllDevices(); - - for (auto usbDev : usbDevs) { - auto foundPrinterIt = - std::find_if(compatiblePrinters.begin(), compatiblePrinters.end(), - [usbDev](const std::shared_ptr& printer) { - return printer->getPid() == usbDev->getPid() && - printer->getVid() == usbDev->getVid(); - }); - if (foundPrinterIt == compatiblePrinters.end()) { - continue; - } - auto foundPrinter = *foundPrinterIt; - spdlog::info("Found Printer {}", foundPrinter->getName()); - compatiblePrinters.push_back(foundPrinter); - } + PtouchPrint ptouchprnt; + ptouchprnt.init(); + ptouchprnt.run(); return 0; }