diff --git a/inc/.gitkeep b/inc/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/inc/Bitmap.hpp b/inc/Bitmap.hpp deleted file mode 100644 index 1f2fd7b..0000000 --- a/inc/Bitmap.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#include - -#pragma once - -namespace ptprnt::bitmap { - -struct Bitmap { - std::vector> map; -}; - -} // namespace ptprnt::bitmap \ No newline at end of file diff --git a/inc/DriverTypes.hpp b/inc/DriverTypes.hpp deleted file mode 100644 index abb4ecd..0000000 --- a/inc/DriverTypes.hpp +++ /dev/null @@ -1,14 +0,0 @@ -#include - -#pragma once - -namespace ptprnt::driver { - -struct info { - std::string name{""}; - std::string version{""}; - uint32_t busNo{0}; - uint32_t flags{0}; -}; - -} // namespace ptprnt::driver \ No newline at end of file diff --git a/inc/IDriver.hpp b/inc/IDriver.hpp deleted file mode 100644 index 937d8ff..0000000 --- a/inc/IDriver.hpp +++ /dev/null @@ -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 \ No newline at end of file diff --git a/inc/IPrinter.hpp b/inc/IPrinter.hpp deleted file mode 100644 index 267fe90..0000000 --- a/inc/IPrinter.hpp +++ /dev/null @@ -1,43 +0,0 @@ - -#include -#include - -#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 bm) = 0; -}; - -} // namespace ptprnt::printer \ No newline at end of file diff --git a/inc/IUsb.hpp b/inc/IUsb.hpp deleted file mode 100644 index 5254b93..0000000 --- a/inc/IUsb.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include - -#include "UsbTypes.hpp" - -#pragma once - -namespace ptprnt::driver { - -class IUsb { - public: - virtual ~IUsb() = default; - - virtual std::optional> getDevices() = 0; - virtual std::optional open(UsbDevice) = 0; - virtual bool close(UsbDevice) = 0; -}; - -} // namespace ptprnt::driver \ No newline at end of file diff --git a/inc/P700Driver.hpp b/inc/P700Driver.hpp deleted file mode 100644 index 320fb3c..0000000 --- a/inc/P700Driver.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -#include - -#include "IDriver.hpp" -#include "Usb.hpp" - -#pragma once - -namespace ptprnt::driver { - -class P700Driver : public IDriver { - public: - P700Driver(std::shared_ptr 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 mUsbDriver{0}; - UsbDevice mUsbDev{0}; - driver::info mInfo{}; - - bool init(); -}; - -} // namespace ptprnt::driver \ No newline at end of file diff --git a/inc/P700Printer.hpp b/inc/P700Printer.hpp deleted file mode 100644 index 84a32d2..0000000 --- a/inc/P700Printer.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#include - -#include "IPrinter.hpp" -#include "P700Driver.hpp" - -#pragma once - -namespace ptprnt::printer { - -class P700Printer : public IPrinter { - public: - P700Printer(std::shared_ptr driver); - ~P700Printer() override; - - printer::info getInfo() override; - bool printText(std::string_view text, uint32_t fontSize) override; - bool printBitmap(std::shared_ptr bm) override; - - private: - static info mPrinterInfo; - std::shared_ptr mDriver; -}; - -} // namespace ptprnt::printer \ No newline at end of file diff --git a/inc/PrinterTypes.hpp b/inc/PrinterTypes.hpp deleted file mode 100644 index 6d5f6e3..0000000 --- a/inc/PrinterTypes.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#include - -#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 \ No newline at end of file diff --git a/inc/UsbTypes.hpp b/inc/UsbTypes.hpp deleted file mode 100644 index e7e28a9..0000000 --- a/inc/UsbTypes.hpp +++ /dev/null @@ -1,24 +0,0 @@ -#include - -#include - -#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_list_ptr; - -struct UsbDevice { - uint16_t vendorId{0}; - uint16_t productId{0}; - libusb_device* device{nullptr}; - libusb_device_handle* hndl{nullptr}; -}; - -} // namespace ptprnt::driver \ No newline at end of file diff --git a/meson.build b/meson.build index be43404..00d09ad 100644 --- a/meson.build +++ b/meson.build @@ -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( diff --git a/src/AutoDetect.hpp b/src/AutoDetect.hpp new file mode 100644 index 0000000..c4a4505 --- /dev/null +++ b/src/AutoDetect.hpp @@ -0,0 +1,25 @@ +#include +#include +#include + +#include "interface/IPrinterDriver.hpp" +#include "interface/IPrinterInfo.hpp" +#include "libusbwrap/interface/IUsbDevice.hpp" + +#pragma once + +namespace ptprnt { + +class AutoDetect { + public: + AutoDetect(std::vector> printers, + std::vector> devices); + ~AutoDetect(); + + std::pair, typename std::shared_ptr> + getCompatiblePrinters(); + + private: +}; + +} // namespace ptprnt \ No newline at end of file diff --git a/src/Bitmap.hpp b/src/Bitmap.hpp new file mode 100644 index 0000000..9a960ad --- /dev/null +++ b/src/Bitmap.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +#include + +namespace ptprnt { + +struct Bitmap { + uint16_t width; + uint16_t height; + uint16_t bitsPerPixel; + std::vector data; +}; + +} // namespace ptprnt \ No newline at end of file diff --git a/src/P700Printer.cpp b/src/P700Printer.cpp index 22914a6..285492c 100644 --- a/src/P700Printer.cpp +++ b/src/P700Printer.cpp @@ -1,33 +1,41 @@ #include "P700Printer.hpp" #include -#include namespace ptprnt::printer { - -P700Printer::P700Printer(std::shared_ptr 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 bm) { - return false; +const uint16_t P700Printer::getPid() { + return info.pid; } +bool P700Printer::attachUsbDevice(std::shared_ptr 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 \ No newline at end of file diff --git a/src/P700Printer.hpp b/src/P700Printer.hpp new file mode 100644 index 0000000..02b24de --- /dev/null +++ b/src/P700Printer.hpp @@ -0,0 +1,43 @@ +#include + +#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 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 mUsbHndl{nullptr}; + + PrinterInfo info{.driverName = "P700", + .name = "Brother P-touch P700", + .version = "v1.0", + .vid = 0x04f9, + .pid = 0x2061}; +}; + +} // namespace ptprnt::printer \ No newline at end of file diff --git a/src/interface/IPrinterDriver.hpp b/src/interface/IPrinterDriver.hpp new file mode 100644 index 0000000..36036b4 --- /dev/null +++ b/src/interface/IPrinterDriver.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +#include "Bitmap.hpp" +#include "libusbwrap/interface/IUsbDevice.hpp" + +namespace ptprnt { + +class IPrinterDriver { + public: + virtual bool attachUsbDevice(std::shared_ptr 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 \ No newline at end of file diff --git a/src/interface/IPrinterInfo.hpp b/src/interface/IPrinterInfo.hpp new file mode 100644 index 0000000..ab28aea --- /dev/null +++ b/src/interface/IPrinterInfo.hpp @@ -0,0 +1,26 @@ + +#pragma once + +#include +#include + +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 \ No newline at end of file diff --git a/inc/libusbwrap/LibUsbTypes.hpp b/src/libusbwrap/LibUsbTypes.hpp similarity index 100% rename from inc/libusbwrap/LibUsbTypes.hpp rename to src/libusbwrap/LibUsbTypes.hpp diff --git a/src/libusbwrap/UsbDevice.cpp b/src/libusbwrap/UsbDevice.cpp new file mode 100644 index 0000000..b66958c --- /dev/null +++ b/src/libusbwrap/UsbDevice.cpp @@ -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 \ No newline at end of file diff --git a/src/libusbwrap/UsbDevice.hpp b/src/libusbwrap/UsbDevice.hpp new file mode 100644 index 0000000..8b763c7 --- /dev/null +++ b/src/libusbwrap/UsbDevice.hpp @@ -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 \ No newline at end of file diff --git a/src/UsbDeviceFactory.cpp b/src/libusbwrap/UsbDeviceFactory.cpp similarity index 84% rename from src/UsbDeviceFactory.cpp rename to src/libusbwrap/UsbDeviceFactory.cpp index 5e17612..74e7dc5 100644 --- a/src/UsbDeviceFactory.cpp +++ b/src/libusbwrap/UsbDeviceFactory.cpp @@ -6,6 +6,7 @@ #include #include "libusb.h" +#include "libusbwrap/UsbDevice.hpp" #include "libusbwrap/interface/IUsbDevice.hpp" namespace libusbwrap { @@ -17,12 +18,12 @@ UsbDeviceFactory::~UsbDeviceFactory() { std::vector> UsbDeviceFactory::findAllDevices() { refreshDeviceList(); - return std::vector>(); + return std::vector>({std::make_shared()}); } std::vector> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) { refreshDeviceList(); - return std::vector>(); + return std::vector>({std::make_shared()}); } int UsbDeviceFactory::refreshDeviceList() { diff --git a/inc/libusbwrap/UsbDeviceFactory.hpp b/src/libusbwrap/UsbDeviceFactory.hpp similarity index 87% rename from inc/libusbwrap/UsbDeviceFactory.hpp rename to src/libusbwrap/UsbDeviceFactory.hpp index 678e29c..282a06c 100644 --- a/inc/libusbwrap/UsbDeviceFactory.hpp +++ b/src/libusbwrap/UsbDeviceFactory.hpp @@ -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 diff --git a/inc/libusbwrap/interface/IUsbDevice.hpp b/src/libusbwrap/interface/IUsbDevice.hpp similarity index 70% rename from inc/libusbwrap/interface/IUsbDevice.hpp rename to src/libusbwrap/interface/IUsbDevice.hpp index 0deb8af..0f9d238 100644 --- a/inc/libusbwrap/interface/IUsbDevice.hpp +++ b/src/libusbwrap/interface/IUsbDevice.hpp @@ -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 \ No newline at end of file diff --git a/inc/libusbwrap/interface/IUsbDeviceFactory.hpp b/src/libusbwrap/interface/IUsbDeviceFactory.hpp similarity index 100% rename from inc/libusbwrap/interface/IUsbDeviceFactory.hpp rename to src/libusbwrap/interface/IUsbDeviceFactory.hpp diff --git a/src/main.cpp b/src/main.cpp index 0123f56..1cbaff1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,12 @@ +#include #include #include +#include "AutoDetect.hpp" +#include "P700Printer.hpp" +#include "interface/IPrinterInfo.hpp" #include "libusbwrap/UsbDeviceFactory.hpp" -#include void setupLogger() { spdlog::set_level(spdlog::level::debug); @@ -13,9 +16,31 @@ void setupLogger() { int main(int argc, char** argv) { setupLogger(); + std::vector> compatiblePrinters = { + std::make_shared()}; + + std::vector> 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& 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; }