Add USB stub, add logging using spdlog

This commit is contained in:
2022-11-12 15:28:21 +01:00
parent 4809f60d2c
commit 815e67bdfb
8 changed files with 106 additions and 7 deletions

View File

@@ -5,7 +5,10 @@
"compilerPath": "/usr/bin/clang", "compilerPath": "/usr/bin/clang",
"cStandard": "c11", "cStandard": "c11",
"cppStandard": "c++17", "cppStandard": "c++17",
"compileCommands": "/builddir/compile_commands.json" "compileCommands": "${workspaceFolder}/builddir/compile_commands.json",
"browse": {
"path": ["${workspaceFolder}"]
}
} }
], ],
"version": 4 "version": 4

View File

@@ -78,6 +78,10 @@
"typeindex": "cpp", "typeindex": "cpp",
"typeinfo": "cpp", "typeinfo": "cpp",
"valarray": "cpp", "valarray": "cpp",
"variant": "cpp" "variant": "cpp",
"csignal": "cpp",
"any": "cpp",
"regex": "cpp",
"future": "cpp"
} }
} }

16
inc/IUsb.hpp Normal file
View File

@@ -0,0 +1,16 @@
#include "UsbTypes.hpp"
#include <vector>
#pragma once
namespace ptprnt::driver {
class IUsb {
public:
virtual ~IUsb(){};
virtual std::vector<UsbDevice> listDevices() = 0;
};
} // namespace ptprnt::driver

20
inc/Usb.hpp Normal file
View File

@@ -0,0 +1,20 @@
#include "IUsb.hpp"
#include <libusb-1.0/libusb.h>
#pragma once
namespace ptprnt::driver {
class Usb : public IUsb {
public:
Usb();
~Usb() override;
std::vector<UsbDevice> listDevices() override;
private:
};
} // namespace ptprnt::driver

13
inc/UsbTypes.hpp Normal file
View File

@@ -0,0 +1,13 @@
#include <libusb-1.0/libusb.h>
#pragma once
namespace ptprnt::driver {
struct UsbDevice {
libusb_device* dev{nullptr};
libusb_device_handle* hndl{nullptr};
};
} // namespace ptprnt::driver

View File

@@ -1,10 +1,11 @@
project('ptprnt', 'cpp', project('ptprnt', 'cpp',
version: '0.1.0', version: 'v0.1.0-'+run_command('git', 'rev-parse', '--short', 'HEAD').stdout().strip(),
license: 'GPLv3', license: 'GPLv3',
default_options : ['c_std=c11', 'cpp_std=c++17'] default_options : ['c_std=c11', 'cpp_std=c++17']
) )
usbdep = dependency('libusb-1.0') usbdep = dependency('libusb-1.0')
logdep = dependency('spdlog')
incdir = include_directories('inc') incdir = include_directories('inc')
@@ -16,7 +17,9 @@ srcs = [
] ]
executable( executable(
'ptprnt', srcs, 'ptprnt',
srcs,
include_directories : incdir, include_directories : incdir,
dependencies : usbdep dependencies : [usbdep, logdep],
cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"']
) )

37
src/Usb.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "Usb.hpp"
#include <iostream>
#include <iomanip>
namespace ptprnt::driver {
Usb::Usb() {
std::cout << "Usb starting" << std::endl;
libusb_init(NULL);
}
Usb::~Usb() {
std::cout << "Usb stopping" << std::endl;
}
std::vector<UsbDevice> Usb::listDevices() {
libusb_device ** devs;
libusb_device *dev;
struct libusb_device_descriptor desc;
int r,i=0;
libusb_get_device_list(NULL, &devs);
while ((dev=devs[i++]) != NULL) {
if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) {
std::cerr << "failed to open device" << std::endl;
libusb_free_device_list(devs, 1);
}
std::cout << std::hex << std::setw(4) << desc.idVendor << ":" << desc.idProduct;
std::cout << std::endl;
}
return std::vector<UsbDevice>();
}
} // namespace ptprnt::driver

View File

@@ -6,11 +6,13 @@
#include "Usb.hpp" #include "Usb.hpp"
#include <libusb-1.0/libusb.h> #include <libusb-1.0/libusb.h>
#include <spdlog/spdlog.h>
using namespace ptprnt; using namespace ptprnt;
int main(int argc, char** argv) { int main(int argc, char** argv) {
std::cout << "Hello ptprnt!" << std::endl; spdlog::info("Starting ptprnt {}", PROJ_VERSION);
auto usb = std::make_unique<driver::Usb>(); auto usb = std::make_unique<driver::Usb>();
auto devs = usb->listDevices(); auto devs = usb->listDevices();
@@ -24,4 +26,5 @@ int main(int argc, char** argv) {
printer::info info = printer->getInfo();*/ printer::info info = printer->getInfo();*/
return 0; return 0;
} }