USB updates, remove iostream usage

This commit is contained in:
2023-08-01 18:19:50 +02:00
parent c3f200fee4
commit 458806c6af
8 changed files with 75 additions and 67 deletions

View File

@@ -13,7 +13,7 @@ AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
@@ -41,11 +41,11 @@ BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 120
ColumnLimit: 100
CompactNamespaces: false
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DerivePointerAlignment: false
EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true
IndentCaseLabels: true
@@ -58,7 +58,7 @@ ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PointerAlignment: Left
ReflowComments: false
SeparateDefinitionBlocks: Always
SeparateDefinitionBlocks: Always
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
@@ -85,11 +85,11 @@ IncludeBlocks: Regroup
IncludeCategories:
# Classes own header is always priority 0
# System C/C++ headers in <> with extension.
- Regex: '<([A-Za-z0-9.\Q/-_\E])+.h.*>'
Priority: 1
- Regex: '<([A-Za-z0-9.\Q/-_\E])+.h.*>'
Priority: 1
# System Headers in <> without extension.
- Regex: '<([A-Za-z0-9.\Q/-_\E])+>'
Priority: 2
- Regex: '<([A-Za-z0-9.\Q/-_\E])+>'
Priority: 2
# c Headers in "" with extension.
- Regex: '"([A-Za-z0-9.\Q/-_\E])+"'
Priority: 3
- Regex: '"([A-Za-z0-9.\Q/-_\E])+"'
Priority: 3

View File

@@ -1,15 +1,15 @@
{
"configurations": [
{
"name": "Linux",
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"compileCommands": "${workspaceFolder}/builddir/compile_commands.json",
"browse": {
"path": ["${workspaceFolder}"]
}
"configurations": [
{
"name": "Linux",
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"compileCommands": "${workspaceFolder}/builddir/compile_commands.json",
"browse": {
"path": ["${workspaceFolder}"]
}
],
"version": 4
}
}
],
"version": 4
}

48
.vscode/launch.json vendored
View File

@@ -1,27 +1,27 @@
{
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Starten",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/builddir/ptprnt",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"name": "(gdb) Starten",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/builddir/ptprnt",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
]
}

View File

@@ -1,6 +1,5 @@
{
"clangd.arguments": ["-background-index", "-compile-commands-dir=builddir"],
"C_Cpp.codeAnalysis.clangTidy.args": ["-extra-arg=-std=c++17"],
"clangd.arguments": ["-background-index", "-compile-commands-dir=builddir/"],
"editor.formatOnType": false,
"editor.formatOnSave": true,
"files.associations": {
@@ -84,5 +83,7 @@
"charconv": "cpp",
"*.ipp": "cpp"
},
"clang-tidy.buildPath": "builddir/"
"clang-tidy.buildPath": "builddir/",
"clangd.onConfigChanged": "restart",
"C_Cpp.default.compileCommands": "builddir/compile_commands.json"
}

View File

@@ -15,6 +15,7 @@ class Usb : public IUsb {
bool close(UsbDevice) override;
private:
libusb_device** mLibUsbDevs;
// TODO: This should be a std::map with handles & locking on access
std::vector<UsbDevice> mDevices{};
};

View File

@@ -1,3 +1,4 @@
#include <memory>
#include <libusb-1.0/libusb.h>
@@ -5,6 +6,14 @@
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};

View File

@@ -2,10 +2,13 @@
#include <spdlog/spdlog.h>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <optional>
#include "UsbTypes.hpp"
namespace ptprnt::driver {
Usb::Usb() {
@@ -18,37 +21,31 @@ Usb::~Usb() {
}
std::optional<std::vector<UsbDevice>> Usb::getDevices() {
// TODO: wrap libUsbDevs in a smart pointer with custom destructor
// calling libusb_free_device_list
libusb_device** libUsbDevs;
libusb_device* libUsbDev;
struct libusb_device_descriptor libUsbDesc;
int ret, i = 0;
mDevices.clear();
if (0 == (ret = libusb_get_device_list(NULL, &libUsbDevs))) {
if (0 == (ret = libusb_get_device_list(NULL, &mLibUsbDevs))) {
spdlog::error("Could not find any USB devices: {}", libusb_error_name(ret));
return std::nullopt;
}
while ((libUsbDev = libUsbDevs[i++]) != NULL) {
UsbDevice newDev;
while ((libUsbDev = mLibUsbDevs[i++]) != NULL) {
if ((ret = libusb_get_device_descriptor(libUsbDev, &libUsbDesc)) < 0) {
spdlog::error("failed to open device");
libusb_free_device_list(libUsbDevs, 1);
spdlog::error("failed to get device");
return std::nullopt;
}
newDev.vendorId = libUsbDesc.idVendor;
newDev.productId = libUsbDesc.idProduct;
newDev.device = libUsbDev;
newDev.hndl = nullptr; // handle is only available after we opened the dev
UsbDevice newDev{.vendorId = libUsbDesc.idVendor,
.productId = libUsbDesc.idProduct,
.device = libUsbDev,
.hndl = nullptr}; // handle is only available after we opened the dev
mDevices.push_back(newDev);
}
libusb_free_device_list(libUsbDevs, 1);
return mDevices;
}
@@ -95,7 +92,7 @@ bool Usb::close(UsbDevice dev) {
dev.vendorId, dev.productId, libusb_error_name(ret));
}
std::cout << "test" << std::endl;
spdlog::debug("test");
libusb_close(dev.hndl);
return true;
}

View File

@@ -26,7 +26,7 @@ int main(int argc, char** argv) {
}
auto driver = std::make_shared<driver::P700Driver>(usb);
auto printer = std::make_unique<printer::P700Printer>(std::move(driver));
auto printer = std::make_shared<printer::P700Printer>(std::move(driver));
//printer::info info = printer->getInfo();
return 0;