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 d8b70e5d56
commit 1b52fb0458
13 changed files with 86 additions and 53 deletions

View File

@@ -41,6 +41,11 @@
namespace ptprnt::printer {
const PrinterInfo P700Printer::mInfo = {.driverName = "P700",
.name = "Brother P-touch P700",
.version = "v1.0",
.usbId{0x04f9, 0x2061}};
P700Printer::~P700Printer() {
detachUsbDevice();
if (mUsbHndl) {
@@ -80,12 +85,8 @@ const PrinterStatus P700Printer::getPrinterStatus() {
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
}
const uint16_t P700Printer::getVid() {
return mInfo.vid;
}
const uint16_t P700Printer::getPid() {
return mInfo.pid;
const libusbwrap::usbId P700Printer::getUsbId() {
return mInfo.usbId;
}
bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) {

View File

@@ -25,6 +25,7 @@
#include "interface/IPrinterDriver.hpp"
#include "interface/IPrinterTypes.hpp"
#include "libusbwrap/LibUsbTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
#pragma once
@@ -44,11 +45,13 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
P700Printer(P700Printer&&) = default;
P700Printer& operator=(P700Printer&&) = default;
// Printer info has to be static to be accessed without instantiation
static const PrinterInfo mInfo;
// IPrinterDriver
const std::string_view getDriverName() override;
const std::string_view getName() override;
const uint16_t getPid() override;
const uint16_t getVid() override;
const libusbwrap::usbId getUsbId() override;
const std::string_view getVersion() override;
const PrinterInfo getPrinterInfo() override;
const PrinterStatus getPrinterStatus() override;
@@ -62,12 +65,6 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
bool init();
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
static constexpr PrinterInfo mInfo{.driverName = "P700",
.name = "Brother P-touch P700",
.version = "v1.0",
.vid = 0x04f9,
.pid = 0x2061};
std::map<std::string, std::vector<uint8_t>> commands{
{"rasterstart",
{0x1b, 0x69, 0x61,

View File

@@ -0,0 +1,22 @@
#include "PrinterDriverFactory.hpp"
#include <memory>
#include "libusbwrap/LibUsbTypes.hpp"
#include "P700Printer.hpp"
#include <spdlog/spdlog.h>
namespace ptprnt {
std::shared_ptr<IPrinterDriver> PrinterDriverFactory::create(libusbwrap::usbId id) {
if(printer::P700Printer::mInfo.usbId == id) {
spdlog::debug("Printer is P700!");
return std::make_shared<printer::P700Printer>();
}
else {
spdlog::debug("{:04x}:{:04x} is not a compatible printer", id.first, id.second);
}
return nullptr;
}
}

View File

@@ -0,0 +1,22 @@
#include <memory>
#include "interface/IPrinterDriver.hpp"
#include "libusbwrap/LibUsbTypes.hpp"
namespace ptprnt {
class PrinterDriverFactory {
public:
PrinterDriverFactory() = default;
~PrinterDriverFactory() = default;
PrinterDriverFactory(const PrinterDriverFactory&) = delete;
PrinterDriverFactory& operator=(const PrinterDriverFactory&) = delete;
PrinterDriverFactory(PrinterDriverFactory&&) = delete;
PrinterDriverFactory& operator=(PrinterDriverFactory&&) = delete;
std::shared_ptr<IPrinterDriver> create(libusbwrap::usbId id);
private:
};
}

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) {

View File

@@ -49,13 +49,12 @@ class PtouchPrint {
// methods
void setupLogger(spdlog::level::level_enum lvl);
void setupCliParser();
unsigned int getCompatiblePrinters();
std::vector<std::shared_ptr<IPrinterDriver>> getCompatiblePrinters();
// member variables
CLI::App mApp{ptprnt::APP_DESC};
libusbwrap::UsbDeviceFactory mUsbDeviceFactory{};
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mCompatiblePrinters{};
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mDetectedPrinters{};
std::vector<std::shared_ptr<IPrinterDriver>> mDetectedPrinters{};
std::vector<CliCmd> mCommands{};
std::string mVersionString = "";

View File

@@ -34,8 +34,7 @@ class IPrinterDriver {
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 libusbwrap::usbId getUsbId() = 0;
virtual const PrinterInfo getPrinterInfo() = 0;
virtual const PrinterStatus getPrinterStatus() = 0;
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;

View File

@@ -21,6 +21,7 @@
#include <cstdint>
#include <string_view>
#include "libusbwrap/LibUsbTypes.hpp"
namespace ptprnt {
@@ -28,8 +29,7 @@ struct PrinterInfo {
std::string_view driverName = "";
std::string_view name = "";
std::string_view version = "";
uint16_t vid = 0x00;
uint16_t pid = 0x00;
libusbwrap::usbId usbId {0x00, 0x00};
};
struct PrinterStatus {

View File

@@ -19,7 +19,7 @@
#pragma once
#include <memory>
#include <utility>
#include "libusb.h"
@@ -40,4 +40,7 @@ enum class Error {
NOT_SUPPORTED = LIBUSB_ERROR_NOT_SUPPORTED,
OTHER = LIBUSB_ERROR_OTHER
};
}
using usbId = std::pair<uint16_t, uint16_t>;
} // namespace libusbwrap

View File

@@ -104,12 +104,8 @@ bool UsbDevice::bulkTransfer(uint8_t endpoint, std::vector<uint8_t>& data, int*
return true;
}
const uint16_t UsbDevice::getVid() {
return mLibusbDevDesc.idVendor;
}
const uint16_t UsbDevice::getPid() {
return mLibusbDevDesc.idProduct;
const usbId UsbDevice::getUsbId() {
return {mLibusbDevDesc.idVendor, mLibusbDevDesc.idProduct};
}
const device::Speed UsbDevice::getSpeed() {

View File

@@ -47,8 +47,7 @@ class UsbDevice : public IUsbDevice {
unsigned int timeout) override;
// getters
const uint16_t getVid() override;
const uint16_t getPid() override;
const usbId getUsbId() override;
const device::Speed getSpeed() override;
const uint8_t getBusNumber() override;
const uint8_t getPortNumber() override;

View File

@@ -36,8 +36,7 @@ class IUsbDevice {
unsigned int timeout) = 0;
// getters
virtual const uint16_t getVid() = 0;
virtual const uint16_t getPid() = 0;
virtual const usbId getUsbId() = 0;
virtual const device::Speed getSpeed() = 0;
virtual const uint8_t getBusNumber() = 0;
virtual const uint8_t getPortNumber() = 0;

View File

@@ -8,6 +8,7 @@ ptprnt_hpps = files (
'interface/IPrinterTypes.hpp',
'P700Printer.hpp',
'PtouchPrint.hpp',
'PrinterDriverFactory.hpp',
'graphics/Bitmap.hpp',
'graphics/Image.hpp',
'graphics/Monochrome.hpp'
@@ -15,6 +16,7 @@ ptprnt_hpps = files (
ptprnt_srcs = files (
'PtouchPrint.cpp',
'PrinterDriverFactory.cpp',
'P700Printer.cpp',
'graphics/Image.cpp',
'graphics/Bitmap.cpp',