Simplified driver interface and moved application logic to a seperate application class

This commit is contained in:
2023-08-26 13:45:26 +02:00
parent 779911e97e
commit 5644c84c83
9 changed files with 97 additions and 78 deletions

View File

@@ -11,6 +11,7 @@ incdir = include_directories('src')
srcs = [
'src/main.cpp',
'src/PtouchPrint.cpp',
'src/P700Printer.cpp',
'src/libusbwrap/UsbDeviceFactory.cpp',
'src/libusbwrap/UsbDevice.cpp'

View File

@@ -1,25 +0,0 @@
#include <memory>
#include <utility>
#include <vector>
#include "interface/IPrinterDriver.hpp"
#include "interface/IPrinterInfo.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
#pragma once
namespace ptprnt {
class AutoDetect {
public:
AutoDetect(std::vector<std::shared_ptr<IPrinterInfo>> printers,
std::vector<std::shared_ptr<libusbwrap::IUsbDevice>> devices);
~AutoDetect();
std::pair<std::shared_ptr<IPrinterInfo>, typename std::shared_ptr<libusbwrap::IUsbDevice>>
getCompatiblePrinters();
private:
};
} // namespace ptprnt

View File

@@ -4,23 +4,27 @@
namespace ptprnt::printer {
const std::string_view P700Printer::getDriverName() {
return info.driverName;
return mInfo.driverName;
}
const std::string_view P700Printer::getName() {
return info.name;
return mInfo.name;
}
const std::string_view P700Printer::getVersion() {
return info.version;
return mInfo.version;
}
const PrinterInfo P700Printer::getPrinterInfo() {
return mInfo;
}
const uint16_t P700Printer::getVid() {
return info.vid;
return mInfo.vid;
}
const uint16_t P700Printer::getPid() {
return info.pid;
return mInfo.pid;
}
bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) {

View File

@@ -1,14 +1,14 @@
#include <memory>
#include "interface/IPrinterDriver.hpp"
#include "interface/IPrinterInfo.hpp"
#include "interface/IPrinterTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
#pragma once
namespace ptprnt::printer {
class P700Printer : public ::ptprnt::IPrinterInfo, public ::ptprnt::IPrinterDriver {
class P700Printer : public ::ptprnt::IPrinterDriver {
public:
P700Printer() = default;
~P700Printer() = default;
@@ -17,14 +17,13 @@ class P700Printer : public ::ptprnt::IPrinterInfo, public ::ptprnt::IPrinterDriv
P700Printer(const P700Printer&) = delete;
P700Printer& operator=(P700Printer&) = delete;
// IPrinterInfo
// IPrinterDriver
const std::string_view getDriverName() override;
const std::string_view getName() override;
const uint16_t getPid() override;
const uint16_t getVid() override;
const std::string_view getVersion() override;
// IPrinterDriver
const PrinterInfo getPrinterInfo() override;
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
bool detachUsbDevice() override;
bool printBitmap(const Bitmap& bitmap) override;
@@ -33,11 +32,11 @@ class P700Printer : public ::ptprnt::IPrinterInfo, public ::ptprnt::IPrinterDriv
private:
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
PrinterInfo info{.driverName = "P700",
.name = "Brother P-touch P700",
.version = "v1.0",
.vid = 0x04f9,
.pid = 0x2061};
PrinterInfo mInfo{.driverName = "P700",
.name = "Brother P-touch P700",
.version = "v1.0",
.vid = 0x04f9,
.pid = 0x2061};
};
} // namespace ptprnt::printer

44
src/PtouchPrint.cpp Normal file
View File

@@ -0,0 +1,44 @@
#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() {
if (getCompatiblePrinters() == 0) {
spdlog::error(
"No compatible printers found, please make sure if they are turned on and connected");
}
}
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();
}

22
src/PtouchPrint.hpp Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include "interface/IPrinterDriver.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp"
class PtouchPrint {
public:
PtouchPrint();
~PtouchPrint();
void init();
void run();
private:
// methods
unsigned int getCompatiblePrinters();
// member variables
libusbwrap::UsbDeviceFactory mUsbDeviceFactory;
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mCompatiblePrinters;
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mDetectedPrinters;
};

View File

@@ -1,14 +1,22 @@
#pragma once
#include <memory>
#include <string_view>
#include "Bitmap.hpp"
#include "interface/IPrinterTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace ptprnt {
class IPrinterDriver {
public:
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 PrinterInfo getPrinterInfo() = 0;
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
virtual bool detachUsbDevice() = 0;
virtual bool printBitmap(const Bitmap& bitmap) = 0;

View File

@@ -14,13 +14,4 @@ struct PrinterInfo {
uint16_t pid = 0x00;
};
class IPrinterInfo {
public:
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;
};
} // namespace ptprnt

View File

@@ -1,12 +1,6 @@
#include <algorithm>
#include <spdlog/spdlog.h>
#include <memory>
#include "AutoDetect.hpp"
#include "P700Printer.hpp"
#include "interface/IPrinterInfo.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp"
#include "PtouchPrint.hpp"
void setupLogger() {
spdlog::set_level(spdlog::level::debug);
@@ -16,28 +10,9 @@ void setupLogger() {
int main(int argc, char** argv) {
setupLogger();
std::vector<std::shared_ptr<ptprnt::IPrinterInfo>> compatiblePrinters = {
std::make_shared<ptprnt::printer::P700Printer>()};
std::vector<std::shared_ptr<ptprnt::IPrinterInfo>> detectedPrinters{};
auto usbFactory = libusbwrap::UsbDeviceFactory();
usbFactory.init();
auto usbDevs = usbFactory.findAllDevices();
for (auto usbDev : usbDevs) {
auto foundPrinterIt =
std::find_if(compatiblePrinters.begin(), compatiblePrinters.end(),
[usbDev](const std::shared_ptr<ptprnt::IPrinterInfo>& printer) {
return printer->getPid() == usbDev->getPid() &&
printer->getVid() == usbDev->getVid();
});
if (foundPrinterIt == compatiblePrinters.end()) {
continue;
}
auto foundPrinter = *foundPrinterIt;
spdlog::info("Found Printer {}", foundPrinter->getName());
compatiblePrinters.push_back(foundPrinter);
}
PtouchPrint ptouchprnt;
ptouchprnt.init();
ptouchprnt.run();
return 0;
}