Some refactorings to incoporate CLI parsing
Some checks failed
Build ptprnt / build (push) Failing after 31s
Some checks failed
Build ptprnt / build (push) Failing after 31s
This commit is contained in:
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -9,7 +9,7 @@
|
|||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/builddir/ptprnt",
|
"program": "${workspaceFolder}/builddir/ptprnt",
|
||||||
"args": [],
|
"args": ["--verbose"],
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
"cwd": "${fileDirname}",
|
"cwd": "${fileDirname}",
|
||||||
"environment": [],
|
"environment": [],
|
||||||
|
@@ -14,12 +14,12 @@ This project requires:
|
|||||||
|
|
||||||
Install dependencies on Arch Linux
|
Install dependencies on Arch Linux
|
||||||
``` bash
|
``` bash
|
||||||
pacman -S libusb spdlog pango cairo meson gcovr
|
pacman -S libusb spdlog pango cairo meson gcovr cli11
|
||||||
```
|
```
|
||||||
|
|
||||||
Install dependencies on Debian/Ubuntu
|
Install dependencies on Debian/Ubuntu
|
||||||
``` bash
|
``` bash
|
||||||
apt-get install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev meson gcovr
|
apt-get install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev meson gcovr libcli11-dev
|
||||||
```
|
```
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
@@ -7,6 +7,7 @@ project('ptprnt', 'cpp',
|
|||||||
usb_dep = dependency('libusb-1.0')
|
usb_dep = dependency('libusb-1.0')
|
||||||
log_dep = dependency('spdlog')
|
log_dep = dependency('spdlog')
|
||||||
pangocairo_dep = dependency('pangocairo')
|
pangocairo_dep = dependency('pangocairo')
|
||||||
|
cli11_dep = dependency('CLI11')
|
||||||
|
|
||||||
incdir = include_directories('src')
|
incdir = include_directories('src')
|
||||||
|
|
||||||
@@ -48,7 +49,7 @@ ptprnt_exe = executable(
|
|||||||
'ptprnt',
|
'ptprnt',
|
||||||
'src/main.cpp',
|
'src/main.cpp',
|
||||||
install: true,
|
install: true,
|
||||||
dependencies : [usb_dep, log_dep, ptprnt_dep],
|
dependencies : [usb_dep, log_dep, ptprnt_dep, cli11_dep],
|
||||||
cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'],
|
cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@@ -72,13 +72,10 @@ const PrinterStatus P700Printer::getPrinterStatus() {
|
|||||||
int tx = 0;
|
int tx = 0;
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
std::vector<uint8_t> recvBuf(32);
|
std::vector<uint8_t> recvBuf(32);
|
||||||
do {
|
while (tries++ < MAX_TRIES_GET_STATUS) {
|
||||||
std::this_thread::sleep_for(100ms);
|
std::this_thread::sleep_for(100ms);
|
||||||
mUsbHndl->bulkTransfer(0x81, recvBuf, &tx, 0);
|
mUsbHndl->bulkTransfer(commands["printerinfo"][0], recvBuf, &tx, 0);
|
||||||
if (tries++ > 10) {
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
} while (tx == 0);
|
|
||||||
|
|
||||||
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
|
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
|
||||||
}
|
}
|
||||||
|
@@ -31,6 +31,8 @@
|
|||||||
|
|
||||||
namespace ptprnt::printer {
|
namespace ptprnt::printer {
|
||||||
|
|
||||||
|
constexpr uint8_t MAX_TRIES_GET_STATUS = 10;
|
||||||
|
|
||||||
class P700Printer : public ::ptprnt::IPrinterDriver {
|
class P700Printer : public ::ptprnt::IPrinterDriver {
|
||||||
public:
|
public:
|
||||||
P700Printer() = default;
|
P700Printer() = default;
|
||||||
@@ -61,11 +63,11 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
|
|||||||
|
|
||||||
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
|
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
|
||||||
|
|
||||||
PrinterInfo mInfo{.driverName = "P700",
|
static constexpr PrinterInfo mInfo{.driverName = "P700",
|
||||||
.name = "Brother P-touch P700",
|
.name = "Brother P-touch P700",
|
||||||
.version = "v1.0",
|
.version = "v1.0",
|
||||||
.vid = 0x04f9,
|
.vid = 0x04f9,
|
||||||
.pid = 0x2061};
|
.pid = 0x2061};
|
||||||
std::map<std::string, std::vector<uint8_t>> commands{
|
std::map<std::string, std::vector<uint8_t>> commands{
|
||||||
{"rasterstart",
|
{"rasterstart",
|
||||||
{0x1b, 0x69, 0x61,
|
{0x1b, 0x69, 0x61,
|
||||||
@@ -75,7 +77,7 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
|
|||||||
{"lf", {0x5a}},
|
{"lf", {0x5a}},
|
||||||
{"ff", {0x0c}},
|
{"ff", {0x0c}},
|
||||||
{"eject", {0x1a}},
|
{"eject", {0x1a}},
|
||||||
};
|
{"printerinfo", {0x81}}};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ptprnt::printer
|
} // namespace ptprnt::printer
|
@@ -19,30 +19,45 @@
|
|||||||
|
|
||||||
#include "PtouchPrint.hpp"
|
#include "PtouchPrint.hpp"
|
||||||
|
|
||||||
|
#include <CLI/App.hpp>
|
||||||
|
#include <spdlog/common.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
#include "P700Printer.hpp"
|
#include "P700Printer.hpp"
|
||||||
#include "graphics/Bitmap.hpp"
|
#include "graphics/Bitmap.hpp"
|
||||||
#include "libusbwrap/UsbDeviceFactory.hpp"
|
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||||
|
|
||||||
PtouchPrint::PtouchPrint() {}
|
PtouchPrint::PtouchPrint(const char* versionString) : mVersionString{versionString} {}
|
||||||
|
|
||||||
PtouchPrint::~PtouchPrint() {}
|
int PtouchPrint::init(int argc, char** argv) {
|
||||||
|
setupCliParser();
|
||||||
|
if (mVerboseFlag) {
|
||||||
|
setupLogger(spdlog::level::debug);
|
||||||
|
} else {
|
||||||
|
setupLogger(spdlog::level::critical);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
mApp.parse(argc, argv);
|
||||||
|
} catch (const CLI::ParseError& e) {
|
||||||
|
return mApp.exit(e);
|
||||||
|
}
|
||||||
|
|
||||||
void PtouchPrint::init() {
|
|
||||||
mUsbDeviceFactory.init();
|
mUsbDeviceFactory.init();
|
||||||
mCompatiblePrinters = {std::make_shared<ptprnt::printer::P700Printer>()};
|
mCompatiblePrinters = {std::make_shared<ptprnt::printer::P700Printer>()};
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PtouchPrint::run() {
|
int PtouchPrint::run() {
|
||||||
|
|
||||||
auto numFoundPrinters = getCompatiblePrinters();
|
auto numFoundPrinters = getCompatiblePrinters();
|
||||||
if (numFoundPrinters == 0) {
|
if (numFoundPrinters == 0) {
|
||||||
spdlog::error(
|
spdlog::error(
|
||||||
"No compatible printers found, please make sure if they are turned on and connected");
|
"No compatible printers found, please make sure if they are turned on and connected");
|
||||||
return;
|
return -1;
|
||||||
} else if (numFoundPrinters > 1) {
|
} else if (numFoundPrinters > 1) {
|
||||||
spdlog::warn("Found more than one compatible printer. Currently not supported.");
|
spdlog::warn("Found more than one compatible printer. Currently not supported.");
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto printer = mCompatiblePrinters[0];
|
auto printer = mCompatiblePrinters[0];
|
||||||
@@ -50,7 +65,7 @@ void PtouchPrint::run() {
|
|||||||
if (devices.size() != 1) {
|
if (devices.size() != 1) {
|
||||||
spdlog::warn(
|
spdlog::warn(
|
||||||
"Found more than one device of the same printer on bus. Currently not supported");
|
"Found more than one device of the same printer on bus. Currently not supported");
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
printer->attachUsbDevice(devices[0]);
|
printer->attachUsbDevice(devices[0]);
|
||||||
auto status = printer->getPrinterStatus();
|
auto status = printer->getPrinterStatus();
|
||||||
@@ -58,6 +73,8 @@ void PtouchPrint::run() {
|
|||||||
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(512, 128);
|
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(512, 128);
|
||||||
printer->printBitmap(bm);
|
printer->printBitmap(bm);
|
||||||
//printer->printText("wurst", 1);
|
//printer->printText("wurst", 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int PtouchPrint::getCompatiblePrinters() {
|
unsigned int PtouchPrint::getCompatiblePrinters() {
|
||||||
@@ -81,3 +98,12 @@ unsigned int PtouchPrint::getCompatiblePrinters() {
|
|||||||
mCompatiblePrinters.clear();
|
mCompatiblePrinters.clear();
|
||||||
return mDetectedPrinters.size();
|
return mDetectedPrinters.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
|
||||||
|
spdlog::set_level(lvl);
|
||||||
|
spdlog::debug("Starting ptprnt {}", mVersionString);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PtouchPrint::setupCliParser() {
|
||||||
|
mApp.add_option("-v,--verbose", mVerboseFlag, "Enable verbose output");
|
||||||
|
}
|
@@ -19,23 +19,35 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <CLI/CLI.hpp>
|
||||||
|
#include <spdlog/common.h>
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include "constants.hpp"
|
||||||
#include "interface/IPrinterDriver.hpp"
|
#include "interface/IPrinterDriver.hpp"
|
||||||
#include "libusbwrap/UsbDeviceFactory.hpp"
|
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||||
|
|
||||||
class PtouchPrint {
|
class PtouchPrint {
|
||||||
public:
|
public:
|
||||||
PtouchPrint();
|
PtouchPrint(const char* versionString);
|
||||||
~PtouchPrint();
|
~PtouchPrint() = default;
|
||||||
|
|
||||||
void init();
|
int init(int argc, char** argv);
|
||||||
void run();
|
int run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// methods
|
// methods
|
||||||
|
void setupLogger(spdlog::level::level_enum lvl);
|
||||||
|
void setupCliParser();
|
||||||
unsigned int getCompatiblePrinters();
|
unsigned int getCompatiblePrinters();
|
||||||
|
|
||||||
// member variables
|
// member variables
|
||||||
|
CLI::App mApp{ptprnt::APP_DESC};
|
||||||
libusbwrap::UsbDeviceFactory mUsbDeviceFactory;
|
libusbwrap::UsbDeviceFactory mUsbDeviceFactory;
|
||||||
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mCompatiblePrinters;
|
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mCompatiblePrinters;
|
||||||
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mDetectedPrinters;
|
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mDetectedPrinters;
|
||||||
|
std::string mVersionString = "";
|
||||||
|
|
||||||
|
// CLI flags
|
||||||
|
bool mVerboseFlag = false;
|
||||||
};
|
};
|
26
src/constants.hpp
Normal file
26
src/constants.hpp
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
ptrnt - print labels on linux
|
||||||
|
Copyright (C) 2023 Moritz Martinius
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace ptprnt {
|
||||||
|
constexpr const char* const APP_NAME = "ptprnt";
|
||||||
|
constexpr const char* const APP_DESC =
|
||||||
|
"ptprnt - a userspace driver for Brother P-touch label printer";
|
||||||
|
} // namespace ptprnt
|
@@ -32,7 +32,8 @@ namespace libusbwrap {
|
|||||||
UsbDeviceFactory::UsbDeviceFactory() {}
|
UsbDeviceFactory::UsbDeviceFactory() {}
|
||||||
|
|
||||||
UsbDeviceFactory::~UsbDeviceFactory() {
|
UsbDeviceFactory::~UsbDeviceFactory() {
|
||||||
libusb_free_device_list(mLibusbDeviceList, 1);
|
// ToDo: SEGV here
|
||||||
|
libusb_free_device_list(mLibusbDeviceList, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
||||||
|
20
src/main.cpp
20
src/main.cpp
@@ -17,21 +17,17 @@
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <CLI/CLI.hpp>
|
||||||
|
#include <spdlog/common.h>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
#include "PtouchPrint.hpp"
|
#include "PtouchPrint.hpp"
|
||||||
|
|
||||||
void setupLogger() {
|
|
||||||
spdlog::set_level(spdlog::level::debug);
|
|
||||||
spdlog::info("Starting ptprnt {}", PROJ_VERSION);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
setupLogger();
|
PtouchPrint ptouchprnt(PROJ_VERSION);
|
||||||
|
int ret = ptouchprnt.init(argc, argv);
|
||||||
PtouchPrint ptouchprnt;
|
if (ret != 0) {
|
||||||
ptouchprnt.init();
|
return ret;
|
||||||
ptouchprnt.run();
|
}
|
||||||
|
return ptouchprnt.run();
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user