Add a printer factory to simplify construction of printer drivers (#11)
All checks were successful
Build ptprnt / build (push) Successful in 1m1s

Co-authored-by: Moritz Martinius <mm@cloudprinters.de>
Reviewed-on: #11
This commit is contained in:
2024-04-20 11:10:30 +00:00
parent 5b3148cb65
commit ad0b2c91ae
17 changed files with 231 additions and 87 deletions

View File

@@ -16,12 +16,11 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdint>
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
#define SPDLOG_DEBUG_ON
#define SPDLOG_TRACE_ON
#include "PtouchPrint.hpp"
#include <CLI/App.hpp>
#include <fmt/core.h>
#include <spdlog/common.h>
@@ -37,7 +36,8 @@
#include <vector>
#include "CLI/Option.hpp"
#include "P700Printer.hpp"
#include "PrinterDriverFactory.hpp"
#include "PtouchPrint.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::info("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");
@@ -97,32 +98,54 @@ int PtouchPrint::run() {
//printer->printText("wurst", 1);
for (auto& cmd : mCommands) {
spdlog::debug("Command: {}", cmd.second);
switch (cmd.first) {
case CliCmdType::Text:
spdlog::debug("Setting text to {}", cmd.second);
printer->setText(cmd.second);
break;
case CliCmdType::Font:;
spdlog::debug("Setting font to {}", cmd.second);
printer->setFont(cmd.second);
break;
case CliCmdType::FontSize:;
spdlog::debug("Setting font size to {}", cmd.second);
printer->setFontSize(static_cast<uint8_t>(std::atoi(cmd.second.c_str())));
break;
case CliCmdType::HAlign:;
spdlog::debug("[Not implemented] Setting text horizontal alignment to {}",
cmd.second);
break;
case CliCmdType::VAlign:;
spdlog::debug("[Not implemented] Setting text vertical alignment to {}",
cmd.second);
break;
case CliCmdType::None:;
[[fallthrough]];
default:
spdlog::warn("This command is currently not supported.");
break;
}
}
if (!printer->print()) {
spdlog::error("An error occured while printing");
return -1;
}
return 0;
}
unsigned int PtouchPrint::getCompatiblePrinters() {
auto usbDevs = mUsbDeviceFactory.findAllDevices();
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) {