/* ptrnt - print labels on linux Copyright (C) 2023 Moritz Martinius This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "PtouchPrint.hpp" #include #include "P700Printer.hpp" #include "graphics/Bitmap.hpp" #include "libusbwrap/UsbDeviceFactory.hpp" PtouchPrint::PtouchPrint() {} PtouchPrint::~PtouchPrint() {} void PtouchPrint::init() { mUsbDeviceFactory.init(); mCompatiblePrinters = {std::make_shared()}; } 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); auto bm = ptprnt::graphics::Bitmap(512, 128); printer->printBitmap(bm); //printer->printText("wurst", 1); } 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(); }