Formatting...

This commit is contained in:
2022-11-12 18:37:24 +01:00
parent d7dfdc4739
commit f09e4ab5c8
6 changed files with 37 additions and 36 deletions

View File

@@ -1,7 +1,7 @@
#include "UsbTypes.hpp"
#include <vector>
#include <optional>
#include <vector>
#pragma once

View File

@@ -10,8 +10,9 @@
namespace ptprnt::driver {
class P700Driver : public IDriver {
public:
P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor = 0x04f9, uint16_t usbDevId = 0x2061);
public:
P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor = 0x04f9,
uint16_t usbDevId = 0x2061);
~P700Driver() override;
driver::info getInfo() override;
@@ -19,7 +20,7 @@ class P700Driver : public IDriver {
bool close() override;
bool command() override;
private:
private:
std::shared_ptr<Usb> mUsbDriver{0};
UsbDevice mUsbDev{0};

View File

@@ -8,13 +8,13 @@
namespace ptprnt::driver {
class Usb : public IUsb {
public:
public:
Usb();
~Usb() override;
std::optional<std::vector<UsbDevice>> getDevices() override;
private:
private:
std::vector<UsbDevice> mDevices{};
};

View File

@@ -7,16 +7,16 @@
namespace ptprnt::driver {
P700Driver::P700Driver(std::shared_ptr<Usb> usbDriver, uint16_t usbDevVendor, uint16_t usbProductId)
: mUsbDriver{std::move(usbDriver)}
, mUsbDev{.vendorId = usbDevVendor, .productId = usbProductId}
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()) {
if (!init()) {
spdlog::error("Could not find any P700 Printers.");
}
else {
} else {
spdlog::info("Found P700 Printer :-).");
}
}
@@ -44,17 +44,20 @@ bool P700Driver::command() {
bool P700Driver::init() {
auto devs = mUsbDriver->getDevices();
if(!devs.has_value()) {
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);
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;
}

View File

@@ -1,9 +1,9 @@
#include "Usb.hpp"
#include <iostream>
#include <iomanip>
#include <optional>
#include <spdlog/spdlog.h>
#include <iomanip>
#include <iostream>
#include <optional>
namespace ptprnt::driver {
@@ -11,28 +11,29 @@ Usb::Usb() {
spdlog::debug("Usb constructing");
libusb_init(NULL);
}
Usb::~Usb() {
spdlog::debug("Usb destructing");
}
std::optional<std::vector<UsbDevice>> Usb::getDevices() {
libusb_device ** devs;
libusb_device *dev;
libusb_device** devs;
libusb_device* dev;
struct libusb_device_descriptor desc;
int r,i=0;
int r, i = 0;
mDevices.clear();
libusb_get_device_list(NULL, &devs);
while ((dev=devs[i++]) != NULL) {
while ((dev = devs[i++]) != NULL) {
UsbDevice newDev;
if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) {
spdlog::error("failed to open device");
libusb_free_device_list(devs, 1);
if ((r = libusb_get_device_descriptor(dev, &desc)) < 0) {
spdlog::error("failed to open device");
libusb_free_device_list(devs, 1);
return std::nullopt;
}
}
newDev.vendorId = desc.idVendor;
newDev.productId = desc.idProduct;

View File

@@ -11,9 +11,8 @@
using namespace ptprnt;
void setupLogger() {
spdlog::set_level(spdlog::level::debug);
spdlog::set_level(spdlog::level::debug);
spdlog::info("Starting ptprnt {}", PROJ_VERSION);
}
int main(int argc, char** argv) {
@@ -21,7 +20,7 @@ int main(int argc, char** argv) {
auto usb = std::make_shared<driver::Usb>();
auto maybeDevs = usb->getDevices();
if(!maybeDevs.has_value()) {
if (!maybeDevs.has_value()) {
spdlog::error("No USB devices found");
return -1;
}
@@ -32,6 +31,3 @@ int main(int argc, char** argv) {
return 0;
}