Formatting...
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
#include "UsbTypes.hpp"
|
#include "UsbTypes.hpp"
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
@@ -10,8 +10,9 @@
|
|||||||
namespace ptprnt::driver {
|
namespace ptprnt::driver {
|
||||||
|
|
||||||
class P700Driver : public IDriver {
|
class P700Driver : public IDriver {
|
||||||
public:
|
public:
|
||||||
P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor = 0x04f9, uint16_t usbDevId = 0x2061);
|
P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor = 0x04f9,
|
||||||
|
uint16_t usbDevId = 0x2061);
|
||||||
~P700Driver() override;
|
~P700Driver() override;
|
||||||
|
|
||||||
driver::info getInfo() override;
|
driver::info getInfo() override;
|
||||||
@@ -19,7 +20,7 @@ class P700Driver : public IDriver {
|
|||||||
bool close() override;
|
bool close() override;
|
||||||
bool command() override;
|
bool command() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<Usb> mUsbDriver{0};
|
std::shared_ptr<Usb> mUsbDriver{0};
|
||||||
UsbDevice mUsbDev{0};
|
UsbDevice mUsbDev{0};
|
||||||
|
|
||||||
|
@@ -8,13 +8,13 @@
|
|||||||
namespace ptprnt::driver {
|
namespace ptprnt::driver {
|
||||||
|
|
||||||
class Usb : public IUsb {
|
class Usb : public IUsb {
|
||||||
public:
|
public:
|
||||||
Usb();
|
Usb();
|
||||||
~Usb() override;
|
~Usb() override;
|
||||||
|
|
||||||
std::optional<std::vector<UsbDevice>> getDevices() override;
|
std::optional<std::vector<UsbDevice>> getDevices() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<UsbDevice> mDevices{};
|
std::vector<UsbDevice> mDevices{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -7,16 +7,16 @@
|
|||||||
|
|
||||||
namespace ptprnt::driver {
|
namespace ptprnt::driver {
|
||||||
|
|
||||||
P700Driver::P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor, uint16_t usbProductId)
|
P700Driver::P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor,
|
||||||
: mUsbDriver{std::move(usbDriver)}
|
uint16_t usbProductId)
|
||||||
, mUsbDev{.vendorId = usbDevVendor, .productId = usbProductId}
|
: mUsbDriver{std::move(usbDriver)},
|
||||||
|
mUsbDev{.vendorId = usbDevVendor, .productId = usbProductId}
|
||||||
|
|
||||||
{
|
{
|
||||||
spdlog::debug("P700Driver constructing");
|
spdlog::debug("P700Driver constructing");
|
||||||
if(!init()) {
|
if (!init()) {
|
||||||
spdlog::error("Could not find any P700 Printers.");
|
spdlog::error("Could not find any P700 Printers.");
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
spdlog::info("Found P700 Printer :-).");
|
spdlog::info("Found P700 Printer :-).");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -44,17 +44,20 @@ bool P700Driver::command() {
|
|||||||
bool P700Driver::init() {
|
bool P700Driver::init() {
|
||||||
auto devs = mUsbDriver->getDevices();
|
auto devs = mUsbDriver->getDevices();
|
||||||
|
|
||||||
if(!devs.has_value()) {
|
if (!devs.has_value()) {
|
||||||
spdlog::error("No USB devices found");
|
spdlog::error("No USB devices found");
|
||||||
return false;
|
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()) {
|
auto devIt =
|
||||||
spdlog::error("No device with {0:04X}:{1:04X}", mUsbDev.vendorId, mUsbDev.productId);
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
23
src/Usb.cpp
23
src/Usb.cpp
@@ -1,9 +1,9 @@
|
|||||||
#include "Usb.hpp"
|
#include "Usb.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <iomanip>
|
|
||||||
#include <optional>
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <iostream>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
namespace ptprnt::driver {
|
namespace ptprnt::driver {
|
||||||
|
|
||||||
@@ -11,28 +11,29 @@ Usb::Usb() {
|
|||||||
spdlog::debug("Usb constructing");
|
spdlog::debug("Usb constructing");
|
||||||
libusb_init(NULL);
|
libusb_init(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
Usb::~Usb() {
|
Usb::~Usb() {
|
||||||
spdlog::debug("Usb destructing");
|
spdlog::debug("Usb destructing");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::vector<UsbDevice>> Usb::getDevices() {
|
std::optional<std::vector<UsbDevice>> Usb::getDevices() {
|
||||||
libusb_device ** devs;
|
libusb_device** devs;
|
||||||
libusb_device *dev;
|
libusb_device* dev;
|
||||||
struct libusb_device_descriptor desc;
|
struct libusb_device_descriptor desc;
|
||||||
int r,i=0;
|
int r, i = 0;
|
||||||
|
|
||||||
mDevices.clear();
|
mDevices.clear();
|
||||||
|
|
||||||
libusb_get_device_list(NULL, &devs);
|
libusb_get_device_list(NULL, &devs);
|
||||||
|
|
||||||
while ((dev=devs[i++]) != NULL) {
|
while ((dev = devs[i++]) != NULL) {
|
||||||
UsbDevice newDev;
|
UsbDevice newDev;
|
||||||
|
|
||||||
if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) {
|
if ((r = libusb_get_device_descriptor(dev, &desc)) < 0) {
|
||||||
spdlog::error("failed to open device");
|
spdlog::error("failed to open device");
|
||||||
libusb_free_device_list(devs, 1);
|
libusb_free_device_list(devs, 1);
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
newDev.vendorId = desc.idVendor;
|
newDev.vendorId = desc.idVendor;
|
||||||
newDev.productId = desc.idProduct;
|
newDev.productId = desc.idProduct;
|
||||||
|
@@ -11,9 +11,8 @@
|
|||||||
using namespace ptprnt;
|
using namespace ptprnt;
|
||||||
|
|
||||||
void setupLogger() {
|
void setupLogger() {
|
||||||
spdlog::set_level(spdlog::level::debug);
|
spdlog::set_level(spdlog::level::debug);
|
||||||
spdlog::info("Starting ptprnt {}", PROJ_VERSION);
|
spdlog::info("Starting ptprnt {}", PROJ_VERSION);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
@@ -21,7 +20,7 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
auto usb = std::make_shared<driver::Usb>();
|
auto usb = std::make_shared<driver::Usb>();
|
||||||
auto maybeDevs = usb->getDevices();
|
auto maybeDevs = usb->getDevices();
|
||||||
if(!maybeDevs.has_value()) {
|
if (!maybeDevs.has_value()) {
|
||||||
spdlog::error("No USB devices found");
|
spdlog::error("No USB devices found");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -32,6 +31,3 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user