From 3431cc222502b1c960819612038c1f9e54475f30 Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Fri, 13 Oct 2023 00:02:44 +0200 Subject: [PATCH 1/8] Some refactorings to incoporate CLI parsing --- .vscode/launch.json | 2 +- README.md | 4 +-- meson.build | 3 ++- src/P700Printer.cpp | 9 +++---- src/P700Printer.hpp | 14 +++++----- src/PtouchPrint.cpp | 40 ++++++++++++++++++++++++----- src/PtouchPrint.hpp | 20 ++++++++++++--- src/constants.hpp | 26 +++++++++++++++++++ src/libusbwrap/UsbDeviceFactory.cpp | 3 ++- src/main.cpp | 20 ++++++--------- 10 files changed, 101 insertions(+), 40 deletions(-) create mode 100644 src/constants.hpp diff --git a/.vscode/launch.json b/.vscode/launch.json index 33a2921..9f29407 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/builddir/ptprnt", - "args": [], + "args": ["--verbose"], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], diff --git a/README.md b/README.md index b0bfc7c..90221ed 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,12 @@ This project requires: Install dependencies on Arch Linux ``` bash -pacman -S libusb spdlog pango cairo meson gcovr +pacman -S libusb spdlog pango cairo meson gcovr cli11 ``` Install dependencies on Debian/Ubuntu ``` 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 diff --git a/meson.build b/meson.build index 2323591..96cf2c7 100644 --- a/meson.build +++ b/meson.build @@ -7,6 +7,7 @@ project('ptprnt', 'cpp', usb_dep = dependency('libusb-1.0') log_dep = dependency('spdlog') pangocairo_dep = dependency('pangocairo') +cli11_dep = dependency('CLI11') incdir = include_directories('src') @@ -48,7 +49,7 @@ ptprnt_exe = executable( 'ptprnt', 'src/main.cpp', 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()+'"'], ) diff --git a/src/P700Printer.cpp b/src/P700Printer.cpp index 7944375..98ed2ed 100644 --- a/src/P700Printer.cpp +++ b/src/P700Printer.cpp @@ -72,13 +72,10 @@ const PrinterStatus P700Printer::getPrinterStatus() { int tx = 0; int tries = 0; std::vector recvBuf(32); - do { + while (tries++ < MAX_TRIES_GET_STATUS) { std::this_thread::sleep_for(100ms); - mUsbHndl->bulkTransfer(0x81, recvBuf, &tx, 0); - if (tries++ > 10) { - break; - } - } while (tx == 0); + mUsbHndl->bulkTransfer(commands["printerinfo"][0], recvBuf, &tx, 0); + } return PrinterStatus{.tapeWidthMm = recvBuf[10]}; } diff --git a/src/P700Printer.hpp b/src/P700Printer.hpp index b7d74d2..a21fc55 100644 --- a/src/P700Printer.hpp +++ b/src/P700Printer.hpp @@ -31,6 +31,8 @@ namespace ptprnt::printer { +constexpr uint8_t MAX_TRIES_GET_STATUS = 10; + class P700Printer : public ::ptprnt::IPrinterDriver { public: P700Printer() = default; @@ -61,11 +63,11 @@ class P700Printer : public ::ptprnt::IPrinterDriver { std::shared_ptr mUsbHndl{nullptr}; - PrinterInfo mInfo{.driverName = "P700", - .name = "Brother P-touch P700", - .version = "v1.0", - .vid = 0x04f9, - .pid = 0x2061}; + static constexpr PrinterInfo mInfo{.driverName = "P700", + .name = "Brother P-touch P700", + .version = "v1.0", + .vid = 0x04f9, + .pid = 0x2061}; std::map> commands{ {"rasterstart", {0x1b, 0x69, 0x61, @@ -75,7 +77,7 @@ class P700Printer : public ::ptprnt::IPrinterDriver { {"lf", {0x5a}}, {"ff", {0x0c}}, {"eject", {0x1a}}, - }; + {"printerinfo", {0x81}}}; }; } // namespace ptprnt::printer \ No newline at end of file diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp index 0fd7162..feecc42 100644 --- a/src/PtouchPrint.cpp +++ b/src/PtouchPrint.cpp @@ -19,30 +19,45 @@ #include "PtouchPrint.hpp" +#include +#include #include #include "P700Printer.hpp" #include "graphics/Bitmap.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(); mCompatiblePrinters = {std::make_shared()}; + return 0; } -void PtouchPrint::run() { +int PtouchPrint::run() { + auto numFoundPrinters = getCompatiblePrinters(); if (numFoundPrinters == 0) { spdlog::error( "No compatible printers found, please make sure if they are turned on and connected"); - return; + return -1; } else if (numFoundPrinters > 1) { spdlog::warn("Found more than one compatible printer. Currently not supported."); - return; + return -1; } auto printer = mCompatiblePrinters[0]; @@ -50,7 +65,7 @@ void PtouchPrint::run() { if (devices.size() != 1) { spdlog::warn( "Found more than one device of the same printer on bus. Currently not supported"); - return; + return -1; } printer->attachUsbDevice(devices[0]); auto status = printer->getPrinterStatus(); @@ -58,6 +73,8 @@ void PtouchPrint::run() { auto bm = ptprnt::graphics::Bitmap(512, 128); printer->printBitmap(bm); //printer->printText("wurst", 1); + + return 0; } unsigned int PtouchPrint::getCompatiblePrinters() { @@ -81,3 +98,12 @@ unsigned int PtouchPrint::getCompatiblePrinters() { mCompatiblePrinters.clear(); 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"); +} \ No newline at end of file diff --git a/src/PtouchPrint.hpp b/src/PtouchPrint.hpp index f8b5533..9643c84 100644 --- a/src/PtouchPrint.hpp +++ b/src/PtouchPrint.hpp @@ -19,23 +19,35 @@ #pragma once +#include +#include +#include + +#include "constants.hpp" #include "interface/IPrinterDriver.hpp" #include "libusbwrap/UsbDeviceFactory.hpp" class PtouchPrint { public: - PtouchPrint(); - ~PtouchPrint(); + PtouchPrint(const char* versionString); + ~PtouchPrint() = default; - void init(); - void run(); + int init(int argc, char** argv); + int run(); private: // methods + void setupLogger(spdlog::level::level_enum lvl); + void setupCliParser(); unsigned int getCompatiblePrinters(); // member variables + CLI::App mApp{ptprnt::APP_DESC}; libusbwrap::UsbDeviceFactory mUsbDeviceFactory; std::vector> mCompatiblePrinters; std::vector> mDetectedPrinters; + std::string mVersionString = ""; + + // CLI flags + bool mVerboseFlag = false; }; \ No newline at end of file diff --git a/src/constants.hpp b/src/constants.hpp new file mode 100644 index 0000000..027b488 --- /dev/null +++ b/src/constants.hpp @@ -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 . + + */ + +#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 \ No newline at end of file diff --git a/src/libusbwrap/UsbDeviceFactory.cpp b/src/libusbwrap/UsbDeviceFactory.cpp index 53a8695..dfc8c8a 100644 --- a/src/libusbwrap/UsbDeviceFactory.cpp +++ b/src/libusbwrap/UsbDeviceFactory.cpp @@ -32,7 +32,8 @@ namespace libusbwrap { UsbDeviceFactory::UsbDeviceFactory() {} UsbDeviceFactory::~UsbDeviceFactory() { - libusb_free_device_list(mLibusbDeviceList, 1); + // ToDo: SEGV here + libusb_free_device_list(mLibusbDeviceList, 0); } std::vector> UsbDeviceFactory::findAllDevices() { diff --git a/src/main.cpp b/src/main.cpp index 525fe8a..3a57c4d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,21 +17,17 @@ */ +#include +#include #include #include "PtouchPrint.hpp" -void setupLogger() { - spdlog::set_level(spdlog::level::debug); - spdlog::info("Starting ptprnt {}", PROJ_VERSION); -} - int main(int argc, char** argv) { - setupLogger(); - - PtouchPrint ptouchprnt; - ptouchprnt.init(); - ptouchprnt.run(); - - return 0; + PtouchPrint ptouchprnt(PROJ_VERSION); + int ret = ptouchprnt.init(argc, argv); + if (ret != 0) { + return ret; + } + return ptouchprnt.run(); } -- 2.49.1 From 226bb1398bbaa25fdfeaa30a86e7ff717f50ad34 Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Fri, 13 Oct 2023 00:05:24 +0200 Subject: [PATCH 2/8] Add cli11 dependency also to git workflow --- .gitea/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index d935825..274419a 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -14,7 +14,7 @@ jobs: - name: install meson run: apt-get -yq install meson - name: Install build dependencies - run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev gcovr + run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev gcovr libcli11-dev - name: setup builddir run: meson setup builddir -Db_coverage=true - name: build all targets -- 2.49.1 From 1f012e51842c2d6b501972036d478f287cba924d Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Fri, 13 Oct 2023 00:20:25 +0200 Subject: [PATCH 3/8] moved cli11 dependency to meson wrap package --- .gitea/workflows/build.yaml | 2 +- README.md | 4 ++-- meson.build | 13 ++++++++++--- subprojects/cli11.wrap | 9 +++++++++ 4 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 subprojects/cli11.wrap diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index 274419a..d935825 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -14,7 +14,7 @@ jobs: - name: install meson run: apt-get -yq install meson - name: Install build dependencies - run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev gcovr libcli11-dev + run: apt-get -yq install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev gcovr - name: setup builddir run: meson setup builddir -Db_coverage=true - name: build all targets diff --git a/README.md b/README.md index 90221ed..b0bfc7c 100644 --- a/README.md +++ b/README.md @@ -14,12 +14,12 @@ This project requires: Install dependencies on Arch Linux ``` bash -pacman -S libusb spdlog pango cairo meson gcovr cli11 +pacman -S libusb spdlog pango cairo meson gcovr ``` Install dependencies on Debian/Ubuntu ``` bash -apt-get install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev meson gcovr libcli11-dev +apt-get install libusb-1.0-0-dev libspdlog-dev libpango1.0-dev libcairo2-dev meson gcovr ``` ## Build diff --git a/meson.build b/meson.build index 96cf2c7..82da49b 100644 --- a/meson.build +++ b/meson.build @@ -7,7 +7,6 @@ project('ptprnt', 'cpp', usb_dep = dependency('libusb-1.0') log_dep = dependency('spdlog') pangocairo_dep = dependency('pangocairo') -cli11_dep = dependency('CLI11') incdir = include_directories('src') @@ -45,17 +44,25 @@ ptprnt_lib = library('ptprnt', ptprnt_dep = declare_dependency(include_directories: incdir, link_with: ptprnt_lib) +# CLI11 +cli11_proj = subproject('cli11') +cli11_dep = cli11_proj.get_variable('CLI11_dep') +if not cli11_dep.found() + error('CLI11 not found, can not proceed') +endif + ptprnt_exe = executable( 'ptprnt', 'src/main.cpp', install: true, - dependencies : [usb_dep, log_dep, ptprnt_dep, cli11_dep], + dependencies : [usb_dep, log_dep, ptprnt_dep, ], cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'], ) +### Unit tests -#cmake = import('cmake') +# GTest gtest_proj = subproject('gtest') gtest_dep = gtest_proj.get_variable('gtest_main_dep') if not gtest_dep.found() diff --git a/subprojects/cli11.wrap b/subprojects/cli11.wrap new file mode 100644 index 0000000..c2f92b2 --- /dev/null +++ b/subprojects/cli11.wrap @@ -0,0 +1,9 @@ +[wrap-file] +directory = CLI11-2.3.2 +source_url = https://github.com/CLIUtils/CLI11/archive/refs/tags/v2.3.2.tar.gz +source_filename = CLI11-2.3.2.tar.gz +source_hash = aac0ab42108131ac5d3344a9db0fdf25c4db652296641955720a4fbe52334e22 +wrapdb_version = 2.3.2-1 + +[provide] +cli11 = CLI11_dep \ No newline at end of file -- 2.49.1 From 4a48183da404d44ef67bd6df96fe25125db4e2b3 Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Fri, 13 Oct 2023 00:22:13 +0200 Subject: [PATCH 4/8] forgot adding the dependency to exe --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 82da49b..131fb4e 100644 --- a/meson.build +++ b/meson.build @@ -55,7 +55,7 @@ ptprnt_exe = executable( 'ptprnt', 'src/main.cpp', 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()+'"'], ) -- 2.49.1 From 115b297c88c5d4885624688e105c9d569daf1ea6 Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Fri, 13 Oct 2023 00:41:22 +0200 Subject: [PATCH 5/8] linking order matters --- meson.build | 11 +++-------- src/main.cpp | 1 - 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/meson.build b/meson.build index 131fb4e..48dbf36 100644 --- a/meson.build +++ b/meson.build @@ -7,6 +7,7 @@ project('ptprnt', 'cpp', usb_dep = dependency('libusb-1.0') log_dep = dependency('spdlog') pangocairo_dep = dependency('pangocairo') +cli11_dep = dependency('CLI11') incdir = include_directories('src') @@ -38,24 +39,18 @@ ptprnt_srcs = [ ptprnt_lib = library('ptprnt', include_directories: incdir, install: true, - dependencies: [usb_dep, log_dep, pangocairo_dep], + dependencies: [usb_dep, log_dep, pangocairo_dep, cli11_dep], sources: [ptprnt_hpps, ptprnt_srcs]) ptprnt_dep = declare_dependency(include_directories: incdir, link_with: ptprnt_lib) -# CLI11 -cli11_proj = subproject('cli11') -cli11_dep = cli11_proj.get_variable('CLI11_dep') -if not cli11_dep.found() - error('CLI11 not found, can not proceed') -endif ptprnt_exe = executable( 'ptprnt', 'src/main.cpp', install: true, - dependencies : [usb_dep, log_dep, ptprnt_dep, cli11_dep], + dependencies : [usb_dep, log_dep, cli11_dep, ptprnt_dep], cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'], ) diff --git a/src/main.cpp b/src/main.cpp index 3a57c4d..e57eb15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,6 @@ */ -#include #include #include -- 2.49.1 From 4b96fcfac1f4fe72c60c73a228d063e23824db85 Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Fri, 13 Oct 2023 00:44:22 +0200 Subject: [PATCH 6/8] ancient meson version on ubuntu 20-04 --- meson.build | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/meson.build b/meson.build index 48dbf36..888c1e7 100644 --- a/meson.build +++ b/meson.build @@ -7,7 +7,13 @@ project('ptprnt', 'cpp', usb_dep = dependency('libusb-1.0') log_dep = dependency('spdlog') pangocairo_dep = dependency('pangocairo') -cli11_dep = dependency('CLI11') + +# CLI11 +cli11_proj = subproject('cli11') +cli11_dep = cli11_proj.get_variable('CLI11_dep') +if not cli11_dep.found() + error('CLI11 not found, can not proceed') +endif incdir = include_directories('src') @@ -45,7 +51,6 @@ ptprnt_lib = library('ptprnt', ptprnt_dep = declare_dependency(include_directories: incdir, link_with: ptprnt_lib) - ptprnt_exe = executable( 'ptprnt', 'src/main.cpp', -- 2.49.1 From 368d8f35992b88b9d111ea34f8371327005d80e3 Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Thu, 19 Oct 2023 21:29:09 +0200 Subject: [PATCH 7/8] Add cli options for formatting --- .vscode/launch.json | 2 +- src/PtouchPrint.cpp | 69 ++++++++++++++++++++++++----- src/PtouchPrint.hpp | 20 +++++++-- src/libusbwrap/UsbDeviceFactory.cpp | 8 ++-- src/libusbwrap/UsbDeviceFactory.hpp | 13 +++--- src/main.cpp | 2 +- 6 files changed, 88 insertions(+), 26 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9f29407..5ccd544 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/builddir/ptprnt", - "args": ["--verbose"], + "args": ["-t Hello"], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp index feecc42..a9a43bc 100644 --- a/src/PtouchPrint.cpp +++ b/src/PtouchPrint.cpp @@ -23,33 +23,42 @@ #include #include +#include + +#include "CLI/Option.hpp" #include "P700Printer.hpp" #include "graphics/Bitmap.hpp" #include "libusbwrap/UsbDeviceFactory.hpp" +namespace ptprnt { + PtouchPrint::PtouchPrint(const char* versionString) : mVersionString{versionString} {} 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); + mApp.exit(e); + return -1; } - mUsbDeviceFactory.init(); + if (mVerboseFlag) { + setupLogger(spdlog::level::trace); + } else { + setupLogger(spdlog::level::critical); + } + + if (!mUsbDeviceFactory.init()) { + spdlog::error("Could not initialize libusb"); + return -1; + } mCompatiblePrinters = {std::make_shared()}; return 0; } int PtouchPrint::run() { - auto numFoundPrinters = getCompatiblePrinters(); if (numFoundPrinters == 0) { spdlog::error( @@ -71,9 +80,13 @@ int PtouchPrint::run() { auto status = printer->getPrinterStatus(); spdlog::info("Detected tape width is {}mm", status.tapeWidthMm); auto bm = ptprnt::graphics::Bitmap(512, 128); - printer->printBitmap(bm); + //printer->printBitmap(bm); //printer->printText("wurst", 1); + for (auto& cmd : mCommands) { + std::cout << cmd.second << std::endl; + } + return 0; } @@ -105,5 +118,39 @@ void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) { } void PtouchPrint::setupCliParser() { - mApp.add_option("-v,--verbose", mVerboseFlag, "Enable verbose output"); -} \ No newline at end of file + auto printVersion = [this](std::size_t) { + std::cout << "ptprnt version: " << mVersionString << std::endl; + }; + + mApp.add_flag("-v,--verbose", mVerboseFlag, "Enable verbose output"); + mApp.add_flag("-V,--version", printVersion, "Prints the ptprnt's version"); + mApp.add_option("-t,--text", + "Text to print (can be used multple times, use formatting options before to " + "influence text layout)") + ->group("Printing") + ->multi_option_policy(CLI::MultiOptionPolicy::TakeAll) + ->trigger_on_parse() + ->each([this](std::string text) { mCommands.emplace_back(CliCmdType::Text, text); }); + mApp.add_option("-f,--font", "Font used for the following text occurences") + ->group("Printing") + ->multi_option_policy(CLI::MultiOptionPolicy::TakeAll) + ->trigger_on_parse() + ->each([this](std::string font) { mCommands.emplace_back(CliCmdType::Font, font); }); + mApp.add_option("-s,--fontsize", "Font size of the following text occurences") + ->group("Printing") + ->multi_option_policy(CLI::MultiOptionPolicy::TakeAll) + ->trigger_on_parse() + ->each([this](std::string size) { mCommands.emplace_back(CliCmdType::FontSize, size); }); + mApp.add_option("--valign", "Vertical alignment of the following text occurences") + ->group("Printing") + ->multi_option_policy(CLI::MultiOptionPolicy::TakeAll) + ->trigger_on_parse() + ->each([this](std::string valign) { mCommands.emplace_back(CliCmdType::VAlign, valign); }); + mApp.add_option("--halign", "Vertical alignment of the following text occurences") + ->group("Printing") + ->multi_option_policy(CLI::MultiOptionPolicy::TakeAll) + ->trigger_on_parse() + ->each([this](std::string halign) { mCommands.emplace_back(CliCmdType::HAlign, halign); }); +} + +} // namespace ptprnt \ No newline at end of file diff --git a/src/PtouchPrint.hpp b/src/PtouchPrint.hpp index 9643c84..303d78c 100644 --- a/src/PtouchPrint.hpp +++ b/src/PtouchPrint.hpp @@ -27,11 +27,21 @@ #include "interface/IPrinterDriver.hpp" #include "libusbwrap/UsbDeviceFactory.hpp" +namespace ptprnt { +enum class CliCmdType { None = 0, Text = 1, FontSize = 2, Font = 3, VAlign = 4, HAlign = 5 }; +using CliCmd = std::pair; + class PtouchPrint { public: PtouchPrint(const char* versionString); ~PtouchPrint() = default; + // This is basically a singelton application class, no need to copy or move + PtouchPrint(const PtouchPrint&) = delete; + PtouchPrint& operator=(const PtouchPrint&) = delete; + PtouchPrint(PtouchPrint&&) = delete; + PtouchPrint& operator=(PtouchPrint&&) = delete; + int init(int argc, char** argv); int run(); @@ -43,11 +53,13 @@ class PtouchPrint { // member variables CLI::App mApp{ptprnt::APP_DESC}; - libusbwrap::UsbDeviceFactory mUsbDeviceFactory; - std::vector> mCompatiblePrinters; - std::vector> mDetectedPrinters; + libusbwrap::UsbDeviceFactory mUsbDeviceFactory{}; + std::vector> mCompatiblePrinters{}; + std::vector> mDetectedPrinters{}; + std::vector mCommands{}; std::string mVersionString = ""; // CLI flags bool mVerboseFlag = false; -}; \ No newline at end of file +}; +} // namespace ptprnt \ No newline at end of file diff --git a/src/libusbwrap/UsbDeviceFactory.cpp b/src/libusbwrap/UsbDeviceFactory.cpp index dfc8c8a..7ad09e9 100644 --- a/src/libusbwrap/UsbDeviceFactory.cpp +++ b/src/libusbwrap/UsbDeviceFactory.cpp @@ -29,11 +29,11 @@ #include "libusbwrap/interface/IUsbDevice.hpp" namespace libusbwrap { -UsbDeviceFactory::UsbDeviceFactory() {} UsbDeviceFactory::~UsbDeviceFactory() { - // ToDo: SEGV here - libusb_free_device_list(mLibusbDeviceList, 0); + if (mDeviceListInitialized) { + libusb_free_device_list(mLibusbDeviceList, 1); + } } std::vector> UsbDeviceFactory::findAllDevices() { @@ -53,7 +53,7 @@ int UsbDeviceFactory::refreshDeviceList() { } else if (ret == 0) { spdlog::warn("No USB devices found"); } - + mDeviceListInitialized = true; return ret; } diff --git a/src/libusbwrap/UsbDeviceFactory.hpp b/src/libusbwrap/UsbDeviceFactory.hpp index db4c3fa..e2f4d70 100644 --- a/src/libusbwrap/UsbDeviceFactory.hpp +++ b/src/libusbwrap/UsbDeviceFactory.hpp @@ -24,12 +24,14 @@ namespace libusbwrap { class UsbDeviceFactory : public IUsbDeviceFactory { public: - UsbDeviceFactory(); - ~UsbDeviceFactory(); + UsbDeviceFactory() = default; + virtual ~UsbDeviceFactory(); // delete copy ctor and assignment - UsbDeviceFactory(const UsbDeviceFactory&) = delete; - UsbDeviceFactory& operator=(UsbDeviceFactory&) = delete; + UsbDeviceFactory(const UsbDeviceFactory&) = delete; + UsbDeviceFactory& operator=(UsbDeviceFactory&) = delete; + UsbDeviceFactory(UsbDeviceFactory&&) = delete; + UsbDeviceFactory& operator=(UsbDeviceFactory&&) = delete; bool init(); /** @@ -56,6 +58,7 @@ class UsbDeviceFactory : public IUsbDeviceFactory { uint16_t pid); // members libusb_context* mLibusbCtx{nullptr}; - libusb_device** mLibusbDeviceList; + libusb_device** mLibusbDeviceList{}; + bool mDeviceListInitialized = false; }; } // namespace libusbwrap \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e57eb15..ddc6b74 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,7 @@ #include "PtouchPrint.hpp" int main(int argc, char** argv) { - PtouchPrint ptouchprnt(PROJ_VERSION); + ptprnt::PtouchPrint ptouchprnt(PROJ_VERSION); int ret = ptouchprnt.init(argc, argv); if (ret != 0) { return ret; -- 2.49.1 From 803d518549689ba957060e49b4a3f526a7b41514 Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Sun, 22 Oct 2023 20:51:25 +0200 Subject: [PATCH 8/8] Add image CLI option --- src/PtouchPrint.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp index a9a43bc..f3528b4 100644 --- a/src/PtouchPrint.cpp +++ b/src/PtouchPrint.cpp @@ -122,8 +122,11 @@ void PtouchPrint::setupCliParser() { std::cout << "ptprnt version: " << mVersionString << std::endl; }; + // General options mApp.add_flag("-v,--verbose", mVerboseFlag, "Enable verbose output"); mApp.add_flag("-V,--version", printVersion, "Prints the ptprnt's version"); + + // Text printing options mApp.add_option("-t,--text", "Text to print (can be used multple times, use formatting options before to " "influence text layout)") @@ -132,25 +135,28 @@ void PtouchPrint::setupCliParser() { ->trigger_on_parse() ->each([this](std::string text) { mCommands.emplace_back(CliCmdType::Text, text); }); mApp.add_option("-f,--font", "Font used for the following text occurences") - ->group("Printing") + ->group("Text printing ") ->multi_option_policy(CLI::MultiOptionPolicy::TakeAll) ->trigger_on_parse() ->each([this](std::string font) { mCommands.emplace_back(CliCmdType::Font, font); }); mApp.add_option("-s,--fontsize", "Font size of the following text occurences") - ->group("Printing") + ->group("Text printing ") ->multi_option_policy(CLI::MultiOptionPolicy::TakeAll) ->trigger_on_parse() ->each([this](std::string size) { mCommands.emplace_back(CliCmdType::FontSize, size); }); mApp.add_option("--valign", "Vertical alignment of the following text occurences") - ->group("Printing") + ->group("Text printing ") ->multi_option_policy(CLI::MultiOptionPolicy::TakeAll) ->trigger_on_parse() ->each([this](std::string valign) { mCommands.emplace_back(CliCmdType::VAlign, valign); }); mApp.add_option("--halign", "Vertical alignment of the following text occurences") - ->group("Printing") + ->group("Text printing ") ->multi_option_policy(CLI::MultiOptionPolicy::TakeAll) ->trigger_on_parse() ->each([this](std::string halign) { mCommands.emplace_back(CliCmdType::HAlign, halign); }); -} + // Image options + mApp.add_option("-i,--image", "Image to print. Excludes all text printing ") + ->group("Image printing"); +} } // namespace ptprnt \ No newline at end of file -- 2.49.1