This Commit adds a rudimentary printer factory to greatly simplify the creation of printers
All checks were successful
Build ptprnt / build (push) Successful in 1m36s

This commit is contained in:
Moritz Martinius
2024-03-23 14:35:43 +01:00
parent e0e158ca90
commit 0462ba4d34
13 changed files with 86 additions and 53 deletions

View File

@@ -37,7 +37,7 @@
#include <vector>
#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<ptprnt::printer::P700Printer>()};
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<std::shared_ptr<IPrinterDriver>> PtouchPrint::getCompatiblePrinters() {
auto usbDevs = mUsbDeviceFactory.findAllDevices();
auto driverFactory = std::make_unique<PrinterDriverFactory>();
std::vector<std::shared_ptr<IPrinterDriver>> foundPrinterDrivers {};
for (auto usbDev : usbDevs) {
auto foundPrinterIt =
std::find_if(mCompatiblePrinters.begin(), mCompatiblePrinters.end(),
[usbDev](const std::shared_ptr<ptprnt::IPrinterDriver>& 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) {