Re-structured project

This commit is contained in:
2023-08-26 11:53:44 +02:00
parent c3915336dd
commit 61e2352a3c
25 changed files with 257 additions and 257 deletions

25
src/AutoDetect.hpp Normal file
View File

@@ -0,0 +1,25 @@
#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

16
src/Bitmap.hpp Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
#include <stdint.h>
#include <vector>
namespace ptprnt {
struct Bitmap {
uint16_t width;
uint16_t height;
uint16_t bitsPerPixel;
std::vector<uint8_t> data;
};
} // namespace ptprnt

View File

@@ -1,33 +1,41 @@
#include "P700Printer.hpp"
#include <iostream>
#include <stdexcept>
namespace ptprnt::printer {
P700Printer::P700Printer(std::shared_ptr<driver::P700Driver> driver) {
mDriver = std::move(driver);
if (!mDriver->open()) {
throw std::invalid_argument("Could not open driver!");
}
const std::string_view P700Printer::getDriverName() {
return info.driverName;
}
P700Printer::~P700Printer() {
if (!mDriver->close()) {
std::cerr << "Could not close driver properly!" << std::endl;
}
const std::string_view P700Printer::getName() {
return info.name;
}
printer::info P700Printer::getInfo() {
return printer::info{};
const std::string_view P700Printer::getVersion() {
return info.version;
}
bool P700Printer::printText(std::string_view text, uint32_t fontSize) {
return false;
const uint16_t P700Printer::getVid() {
return info.vid;
}
bool P700Printer::printBitmap(std::shared_ptr<bitmap::Bitmap> bm) {
return false;
const uint16_t P700Printer::getPid() {
return info.pid;
}
bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) {
return true;
}
bool P700Printer::detachUsbDevice() {
return true;
}
bool P700Printer::printBitmap(const Bitmap& bitmap) {
return true;
}
bool P700Printer::printText(const std::string& text, uint16_t fontSize) {
return true;
}
} // namespace ptprnt::printer

43
src/P700Printer.hpp Normal file
View File

@@ -0,0 +1,43 @@
#include <memory>
#include "interface/IPrinterDriver.hpp"
#include "interface/IPrinterInfo.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
#pragma once
namespace ptprnt::printer {
class P700Printer : public ::ptprnt::IPrinterInfo, public ::ptprnt::IPrinterDriver {
public:
P700Printer() = default;
~P700Printer() = default;
// delete copy ctor and assignment
P700Printer(const P700Printer&) = delete;
P700Printer& operator=(P700Printer&) = delete;
// IPrinterInfo
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
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
bool detachUsbDevice() override;
bool printBitmap(const Bitmap& bitmap) override;
bool printText(const std::string& text, uint16_t fontSize) override;
private:
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
PrinterInfo info{.driverName = "P700",
.name = "Brother P-touch P700",
.version = "v1.0",
.vid = 0x04f9,
.pid = 0x2061};
};
} // namespace ptprnt::printer

View File

@@ -0,0 +1,18 @@
#pragma once
#include <memory>
#include "Bitmap.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace ptprnt {
class IPrinterDriver {
public:
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
virtual bool detachUsbDevice() = 0;
virtual bool printBitmap(const Bitmap& bitmap) = 0;
virtual bool printText(const std::string& text, uint16_t fontSize) = 0;
};
} // namespace ptprnt

View File

@@ -0,0 +1,26 @@
#pragma once
#include <cstdint>
#include <string_view>
namespace ptprnt {
struct PrinterInfo {
std::string_view driverName = "";
std::string_view name = "";
std::string_view version = "";
uint16_t vid = 0x00;
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

@@ -0,0 +1,22 @@
#pragma once
#include "libusb.h"
namespace libusbwrap {
enum class Error {
SUCCESS = LIBUSB_SUCCESS,
IO = LIBUSB_ERROR_IO,
INVALID_PARAM = LIBUSB_ERROR_INVALID_PARAM,
ACCESS = LIBUSB_ERROR_ACCESS,
NO_DEVICE = LIBUSB_ERROR_NO_DEVICE,
NOT_FOUND = LIBUSB_ERROR_NOT_FOUND,
BUSY = LIBUSB_ERROR_BUSY,
TIMEOUT = LIBUSB_ERROR_TIMEOUT,
OVERFLOW = LIBUSB_ERROR_OVERFLOW,
PIPE = LIBUSB_ERROR_PIPE,
INTERRUPTED = LIBUSB_ERROR_INTERRUPTED,
NO_MEM = LIBUSB_ERROR_NO_MEM,
NOT_SUPPORTED = LIBUSB_ERROR_NOT_SUPPORTED,
OTHER = LIBUSB_ERROR_OTHER
};
}

View File

@@ -0,0 +1,35 @@
#include "libusbwrap/UsbDevice.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
UsbDevice::UsbDevice() {}
UsbDevice::~UsbDevice() {}
Error UsbDevice::open() {
return Error::SUCCESS;
}
void UsbDevice::close() {}
const uint16_t UsbDevice::getVid() {
return 0x0000;
}
const uint16_t UsbDevice::getPid() {
return 0x0000;
}
const device::Speed UsbDevice::getSpeed() {
return device::Speed::FULL;
}
const uint8_t UsbDevice::getBusNumber() {
return 0;
}
const uint8_t UsbDevice::getPortNumber() {
return 0;
}
} // namespace libusbwrap

View File

@@ -0,0 +1,25 @@
#pragma once
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
class UsbDevice : public IUsbDevice {
public:
UsbDevice();
~UsbDevice();
// delete copy ctor and assignment
UsbDevice(const UsbDevice&) = delete;
UsbDevice& operator=(UsbDevice&) = delete;
Error open() override;
void close() override;
// getters
const uint16_t getVid() override;
const uint16_t getPid() override;
const device::Speed getSpeed() override;
const uint8_t getBusNumber() override;
const uint8_t getPortNumber() override;
};
} // namespace libusbwrap

View File

@@ -6,6 +6,7 @@
#include <memory>
#include "libusb.h"
#include "libusbwrap/UsbDevice.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
@@ -17,12 +18,12 @@ UsbDeviceFactory::~UsbDeviceFactory() {
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
refreshDeviceList();
return std::vector<std::shared_ptr<IUsbDevice>>();
return std::vector<std::shared_ptr<IUsbDevice>>({std::make_shared<UsbDevice>()});
}
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
refreshDeviceList();
return std::vector<std::shared_ptr<IUsbDevice>>();
return std::vector<std::shared_ptr<IUsbDevice>>({std::make_shared<UsbDevice>()});
}
int UsbDeviceFactory::refreshDeviceList() {

View File

@@ -0,0 +1,39 @@
#pragma once
#include "libusbwrap/interface/IUsbDeviceFactory.hpp"
namespace libusbwrap {
class UsbDeviceFactory : public IUsbDeviceFactory {
public:
UsbDeviceFactory();
~UsbDeviceFactory();
// delete copy ctor and assignment
UsbDeviceFactory(const UsbDeviceFactory&) = delete;
UsbDeviceFactory& operator=(UsbDeviceFactory&) = delete;
bool init();
/**
* @brief Gets all devices that are detected by libusb. Will allocate a shared_ptr for each Device
*
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of all detected USB devices
*/
std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() override;
/**
* @brief Gets all devices with certain vid/pid combination.
* If only one device of certain type is connected, vector is usually only one element
*
* @param vid VendorId of the devices to find
* @param pid ProductId of the devices to find
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of detected USB devices based on vid/pid
*/
std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) override;
private:
// methods
int refreshDeviceList();
// members
libusb_context* mLibusbCtx{nullptr};
libusb_device** mLibusbDeviceList;
};
} // namespace libusbwrap

View File

@@ -0,0 +1,34 @@
#pragma once
#include <sys/types.h>
#include <cstdint>
#include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp"
namespace libusbwrap {
namespace device {
enum class Speed {
UNKNOWN = LIBUSB_SPEED_UNKNOWN,
LOW = LIBUSB_SPEED_LOW,
FULL = LIBUSB_SPEED_FULL,
HIGH = LIBUSB_SPEED_HIGH,
SUPER = LIBUSB_SPEED_SUPER,
SUPER_PLUS = LIBUSB_SPEED_SUPER_PLUS
};
} // namespace device
class IUsbDevice {
public:
virtual Error open() = 0;
virtual void close() = 0;
// getters
const virtual uint16_t getVid() = 0;
const virtual uint16_t getPid() = 0;
const virtual device::Speed getSpeed() = 0;
const virtual uint8_t getBusNumber() = 0;
const virtual uint8_t getPortNumber() = 0;
};
} // namespace libusbwrap

View File

@@ -0,0 +1,15 @@
#pragma once
#include <cstdint>
#include <memory>
#include <vector>
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap {
class IUsbDeviceFactory {
public:
virtual std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() = 0;
virtual std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
};
} // namespace libusbwrap

View File

@@ -1,9 +1,12 @@
#include <algorithm>
#include <spdlog/spdlog.h>
#include <memory>
#include "AutoDetect.hpp"
#include "P700Printer.hpp"
#include "interface/IPrinterInfo.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp"
#include <libusb-1.0/libusb.h>
void setupLogger() {
spdlog::set_level(spdlog::level::debug);
@@ -13,9 +16,31 @@ 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();
usbFactory.findAllDevices();
auto usbDevs = usbFactory.findAllDevices();
for (auto usbDev : usbDevs) {
auto vid = usbDev->getVid();
auto pid = usbDev->getPid();
spdlog::info("UsbDev {}:{}", vid, pid);
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);
}
return 0;
}