Added Usb Detection using libUsb

This commit is contained in:
2022-11-12 18:29:04 +01:00
parent b20fa5ff13
commit 5a6a268c93
8 changed files with 171 additions and 106 deletions

View File

@@ -3,12 +3,26 @@
#include <iostream>
#include <stdexcept>
#include <spdlog/spdlog.h>
namespace ptprnt::driver {
P700Driver::P700Driver(uint16_t UsbDevVendor, uint16_t UsbDevId) {}
P700Driver::P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor, uint16_t usbProductId)
: mUsbDriver{std::move(usbDriver)}
, mUsbDev{.vendorId = usbDevVendor, .productId = usbProductId}
{
spdlog::debug("P700Driver constructing");
if(!init()) {
spdlog::error("Could not find any P700 Printers.");
}
else {
spdlog::info("Found P700 Printer :-).");
}
}
P700Driver::~P700Driver() {
spdlog::debug("P700Driver destructing");
}
driver::info P700Driver::getInfo() {
@@ -27,4 +41,26 @@ bool P700Driver::command() {
return false;
}
bool P700Driver::init() {
auto devs = mUsbDriver->getDevices();
if(!devs.has_value()) {
spdlog::error("No USB devices found");
return false;
}
auto devIt = std::find_if(devs.value().begin(), devs.value().end(),
[=] (auto dev) { return mUsbDev.vendorId == dev.vendorId && mUsbDev.productId == dev.productId;}
);
if(devIt == devs.value().end()) {
spdlog::error("No device with {0:04X}:{1:04X}", mUsbDev.vendorId, mUsbDev.productId);
return false;
}
mUsbDev = *devIt;
return true;
}
} // namespace ptprnt::driver