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

View File

View File

@@ -1,11 +0,0 @@
#include <vector>
#pragma once
namespace ptprnt::bitmap {
struct Bitmap {
std::vector<std::vector<int>> map;
};
} // namespace ptprnt::bitmap

View File

@@ -1,14 +0,0 @@
#include <string>
#pragma once
namespace ptprnt::driver {
struct info {
std::string name{""};
std::string version{""};
uint32_t busNo{0};
uint32_t flags{0};
};
} // namespace ptprnt::driver

View File

@@ -1,44 +0,0 @@
#include "DriverTypes.hpp"
#pragma once
namespace ptprnt::driver {
class IDriver {
public:
virtual ~IDriver(){};
/**
* @brief Get Information struct about this driver
*
* @return driver::info
*/
virtual driver::info getInfo() = 0;
/**
* @brief opens up the device specified
*
* @return true successfully open up device
* @return false failed to open device
*/
virtual bool open() = 0;
/**
* @brief close the device
*
* @return true successfully closed device
* @return false failed to close device
*/
virtual bool close() = 0;
/**
* @brief Send a command to device
*
* @return true successfully sent command to device
* @return false error sending command
*/
virtual bool command() = 0;
};
} // namespace ptprnt::driver

View File

@@ -1,43 +0,0 @@
#include <memory>
#include <string_view>
#include "Bitmap.hpp"
#include "PrinterTypes.hpp"
#pragma once
namespace ptprnt::printer {
class IPrinter {
public:
virtual ~IPrinter(){};
/**
* @brief Get Information struct about the printer
*
* @return driver::info
*/
virtual printer::info getInfo() = 0;
/**
* @brief Prints text immediatly
*
* @param text Text to print
* @param fontSize Size of the text to print
* @return true Printing succeeded
* @return false Printing failed
*/
virtual bool printText(std::string_view text, uint32_t fontSize) = 0;
/**
* @brief Prints supplied bitmap immediatly
*
* @param bm Bitmap to print
* @return true Printing succeeded
* @return false Printing failed
*/
virtual bool printBitmap(std::shared_ptr<bitmap::Bitmap> bm) = 0;
};
} // namespace ptprnt::printer

View File

@@ -1,19 +0,0 @@
#include <optional>
#include <vector>
#include "UsbTypes.hpp"
#pragma once
namespace ptprnt::driver {
class IUsb {
public:
virtual ~IUsb() = default;
virtual std::optional<std::vector<UsbDevice>> getDevices() = 0;
virtual std::optional<UsbDevice> open(UsbDevice) = 0;
virtual bool close(UsbDevice) = 0;
};
} // namespace ptprnt::driver

View File

@@ -1,29 +0,0 @@
#include <cstdint>
#include <memory>
#include "IDriver.hpp"
#include "Usb.hpp"
#pragma once
namespace ptprnt::driver {
class P700Driver : public IDriver {
public:
P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor = 0x04f9, uint16_t usbDevId = 0x2061);
~P700Driver() override;
driver::info getInfo() override;
bool open() override;
bool close() override;
bool command() override;
private:
std::shared_ptr<Usb> mUsbDriver{0};
UsbDevice mUsbDev{0};
driver::info mInfo{};
bool init();
};
} // namespace ptprnt::driver

View File

@@ -1,24 +0,0 @@
#include <memory>
#include "IPrinter.hpp"
#include "P700Driver.hpp"
#pragma once
namespace ptprnt::printer {
class P700Printer : public IPrinter {
public:
P700Printer(std::shared_ptr<driver::P700Driver> driver);
~P700Printer() override;
printer::info getInfo() override;
bool printText(std::string_view text, uint32_t fontSize) override;
bool printBitmap(std::shared_ptr<bitmap::Bitmap> bm) override;
private:
static info mPrinterInfo;
std::shared_ptr<driver::P700Driver> mDriver;
};
} // namespace ptprnt::printer

View File

@@ -1,21 +0,0 @@
#include <string>
#pragma once
namespace ptprnt::printer {
enum class colorMode {
monochrome = 0,
color = 0,
};
struct info {
std::string name{""};
std::string revision{""};
uint32_t xres = {0};
uint32_t yres = {0};
colorMode color = {colorMode::monochrome};
bool cutter = {false};
};
} // namespace ptprnt::printer

View File

@@ -1,24 +0,0 @@
#include <memory>
#include <libusb-1.0/libusb.h>
#pragma once
namespace ptprnt::driver {
struct libusb_device_list_ptr_deleter {
void operator()(libusb_device** usbdevicelistptr) {
libusb_free_device_list(usbdevicelistptr, 1);
}
};
typedef std::unique_ptr<libusb_device**, libusb_device_list_ptr_deleter> libusb_device_list_ptr;
struct UsbDevice {
uint16_t vendorId{0};
uint16_t productId{0};
libusb_device* device{nullptr};
libusb_device_handle* hndl{nullptr};
};
} // namespace ptprnt::driver

View File

@@ -7,11 +7,13 @@ project('ptprnt', 'cpp',
usbdep = dependency('libusb-1.0')
logdep = dependency('spdlog')
incdir = include_directories('inc')
incdir = include_directories('src')
srcs = [
'src/main.cpp',
'src/UsbDeviceFactory.cpp'
'src/P700Printer.cpp',
'src/libusbwrap/UsbDeviceFactory.cpp',
'src/libusbwrap/UsbDevice.cpp'
]
executable(

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,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

@@ -7,6 +7,11 @@ 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

View File

@@ -25,10 +25,10 @@ class IUsbDevice {
virtual void close() = 0;
// getters
virtual uint16_t getVid() = 0;
virtual uint16_t getPid() = 0;
virtual device::Speed getSpeed() = 0;
virtual uint8_t getBusNumber() = 0;
virtual uint8_t getPortNumber() = 0;
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

@@ -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;
}