Files
ptouch-prnt/src/PtouchPrint.cpp

61 lines
2.1 KiB
C++

#include "PtouchPrint.hpp"
#include <spdlog/spdlog.h>
#include "P700Printer.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp"
PtouchPrint::PtouchPrint() {}
PtouchPrint::~PtouchPrint() {}
void PtouchPrint::init() {
mUsbDeviceFactory.init();
mCompatiblePrinters = {std::make_shared<ptprnt::printer::P700Printer>()};
}
void PtouchPrint::run() {
auto numFoundPrinters = getCompatiblePrinters();
if (numFoundPrinters == 0) {
spdlog::error(
"No compatible printers found, please make sure if they are turned on and connected");
return;
} else if (numFoundPrinters > 1) {
spdlog::warn("Found more than one compatible printer. Currently not supported.");
return;
}
auto printer = mCompatiblePrinters[0];
auto devices = mUsbDeviceFactory.findDevices(printer->getVid(), printer->getPid());
if (devices.size() != 1) {
spdlog::warn(
"Found more than one device of the same printer on bus. Currently not supported");
return;
}
printer->attachUsbDevice(devices[0]);
auto status = printer->getPrinterStatus();
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
}
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<ptprnt::IPrinterDriver>& 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();
}