Some refactorings to incoporate CLI parsing
Some checks failed
Build ptprnt / build (push) Failing after 31s

This commit is contained in:
2023-10-13 00:02:44 +02:00
parent 7d0cb89bda
commit 3431cc2225
10 changed files with 101 additions and 40 deletions

View File

@@ -19,30 +19,45 @@
#include "PtouchPrint.hpp"
#include <CLI/App.hpp>
#include <spdlog/common.h>
#include <spdlog/spdlog.h>
#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<ptprnt::printer::P700Printer>()};
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<ptprnt::graphics::ALPHA8>(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");
}