commands restructured to be const vectors

This commit is contained in:
2024-04-20 12:03:20 +02:00
parent 5a38600e2a
commit 6857de7b1f
8 changed files with 46 additions and 32 deletions

View File

@@ -41,7 +41,7 @@ BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
ColumnLimit: 100
ColumnLimit: 120
CompactNamespaces: false
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true

View File

@@ -85,6 +85,9 @@
},
"clang-tidy.buildPath": "builddir/",
"clangd.onConfigChanged": "restart",
"C_Cpp.default.compileCommands": "/home/moritz/Projekte/ptouch-prnt/builddir/compile_commands.json",
"gcovViewer.buildDirectories": ["/home/moritz/Projekte/ptouch-prnt/builddir"]
"C_Cpp.default.compileCommands": "/home/moritz/src/ptouch-prnt/builddir/compile_commands.json",
"gcovViewer.buildDirectories": [
"/home/moritz/Projekte/ptouch-prnt/builddir"
],
"C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild"
}

View File

@@ -1,7 +1,7 @@
project('ptprnt', 'cpp',
version: 'v0.1.0-'+run_command('git', 'rev-parse', '--short', 'HEAD', check: true).stdout().strip(),
license: 'GPLv3',
default_options : ['c_std=c11', 'cpp_std=c++17', 'b_sanitize=none', 'b_lto=true', 'b_lto_mode=thin', 'b_thinlto_cache=true']
default_options : ['c_std=c11', 'cpp_std=c++20', 'b_sanitize=none', 'b_lto=true', 'b_lto_mode=thin', 'b_thinlto_cache=true']
)
usb_dep = dependency('libusb-1.0')

View File

@@ -37,7 +37,7 @@
#include "spdlog/fmt/bin_to_hex.h"
// as long as DRYRUN is defined, no data is actually send to the printer, we need to save some tape ;)
//#define DRYRUN
#define DRYRUN
namespace ptprnt::printer {
@@ -71,15 +71,14 @@ const PrinterInfo P700Printer::getPrinterInfo() {
const PrinterStatus P700Printer::getPrinterStatus() {
using namespace std::chrono_literals;
std::vector<uint8_t> getStatusCmd({0x1b, 0x69, 0x53}); // status info request
send(getStatusCmd);
send(p700::commands::GET_STATUS);
int tx = 0;
int tries = 0;
std::vector<uint8_t> recvBuf(32);
while (tries++ < MAX_TRIES_GET_STATUS) {
std::this_thread::sleep_for(100ms);
mUsbHndl->bulkTransfer(commands["printerinfo"][0], recvBuf, &tx, 0);
mUsbHndl->bulkTransfer(p700::commands::INFO[0], recvBuf, &tx, 0);
}
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
@@ -132,7 +131,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
}
#endif
send(commands["rasterstart"]);
send(p700::commands::RASTER_START);
std::vector<uint8_t> rastercmd(4);
rastercmd[0] = 0x47;
rastercmd[1] = 0x00; // size +1
@@ -159,7 +158,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
};
}
send(commands["eject"]);
send(p700::commands::EJECT);
return true;
}
@@ -184,13 +183,13 @@ bool P700Printer::setVAlign(VAlignPosition vpos) {
}
bool P700Printer::print() {
send(commands["lf"]);
send(commands["ff"]);
send(commands["eject"]);
send(p700::commands::LF);
send(p700::commands::FF);
send(p700::commands::EJECT);
return true;
}
bool P700Printer::send(std::vector<uint8_t>& data) {
bool P700Printer::send(const std::vector<uint8_t>& data) {
if (mUsbHndl == nullptr || data.size() > 128) {
spdlog::error("Invalid device handle or invalid data.");
@@ -210,8 +209,7 @@ bool P700Printer::send(std::vector<uint8_t>& data) {
#endif
if (tx != static_cast<int>(data.size())) {
spdlog::error("Could not transfer all data via USB bulk transfer. Only sent {} of {} bytes",
tx, data.size());
spdlog::error("Could not transfer all data via USB bulk transfer. Only sent {} of {} bytes", tx, data.size());
return false;
}

View File

@@ -32,13 +32,23 @@
#pragma once
namespace ptprnt::printer {
namespace p700::commands {
const cmd_T GET_STATUS{0x1b, 0x69, 0x53};
const cmd_T RASTER_START{0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const cmd_T INFO{0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const cmd_T PACKBITSON{0x02};
const cmd_T LF{0x5a};
const cmd_T FF{0x0c};
const cmd_T EJECT{0x1a};
const cmd_T PRINTER_INFO{0x81};
} // namespace p700::commands
constexpr uint8_t MAX_TRIES_GET_STATUS = 10;
class P700Printer : public ::ptprnt::IPrinterDriver {
public:
P700Printer() = default;
~P700Printer();
~P700Printer() override;
// delete copy ctor and assignment
P700Printer(const P700Printer&) = default;
@@ -67,14 +77,20 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
bool print() override;
private:
bool send(std::vector<uint8_t>& data);
bool send(const std::vector<uint8_t>& data);
bool init();
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
static constexpr PrinterInfo mInfo{.driverName = "P700",
.name = "Brother P-touch P700",
.version = "v1.0",
.vid = 0x04f9,
.pid = 0x2061,
.pixelLines = 128};
std::map<std::string, std::vector<uint8_t>> commands{
{"rasterstart",
{0x1b, 0x69, 0x61,
0x01}}, // unique for P700, other printers have the 2 byte set to 0x52 instead of 0x61
{0x1b, 0x69, 0x61, 0x01}}, // unique for P700, other printers have the 2 byte set to 0x52 instead of 0x61
{"info", {0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
{"packbitson", {0x02}},
{"lf", {0x5a}},

View File

@@ -75,8 +75,7 @@ int PtouchPrint::run() {
mDetectedPrinters = getCompatiblePrinters();
auto numFoundPrinters = mDetectedPrinters.size();
if (numFoundPrinters == 0) {
spdlog::error(
"No compatible printers found, please make sure that they are turned on and connected");
spdlog::error("No compatible printers found, please make sure that they are turned on and connected");
return -1;
} else if (numFoundPrinters > 1) {
spdlog::warn("Found more than one compatible printer. Currently not supported.");
@@ -85,10 +84,9 @@ int PtouchPrint::run() {
auto printer = mDetectedPrinters[0];
const auto printerUsbId = printer->getUsbId();
auto devices = mUsbDeviceFactory.findDevices(printerUsbId.first, printerUsbId.second);
auto devices = mUsbDeviceFactory.findDevices(printerUsbId.first, printerUsbId.second);
if (devices.size() != 1) {
spdlog::warn(
"Found more than one device of the same printer on bus. Currently not supported");
spdlog::warn("Found more than one device of the same printer on bus. Currently not supported");
return -1;
}
printer->attachUsbDevice(std::move(devices[0]));
@@ -115,12 +113,10 @@ int PtouchPrint::run() {
printer->setFontSize(static_cast<uint8_t>(std::atoi(cmd.second.c_str())));
break;
case CliCmdType::HAlign:;
spdlog::debug("[Not implemented] Setting text horizontal alignment to {}",
cmd.second);
spdlog::debug("[Not implemented] Setting text horizontal alignment to {}", cmd.second);
break;
case CliCmdType::VAlign:;
spdlog::debug("[Not implemented] Setting text vertical alignment to {}",
cmd.second);
spdlog::debug("[Not implemented] Setting text vertical alignment to {}", cmd.second);
break;
case CliCmdType::None:;
[[fallthrough]];
@@ -209,7 +205,6 @@ void PtouchPrint::setupCliParser() {
->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");
mApp.add_option("-i,--image", "Image to print. Excludes all text printing ")->group("Image printing");
}
} // namespace ptprnt

View File

@@ -28,7 +28,6 @@
#include "libusbwrap/interface/IUsbDevice.hpp"
namespace ptprnt {
class IPrinterDriver {
public:
virtual ~IPrinterDriver() = default;

View File

@@ -22,6 +22,7 @@
#include <cstdint>
#include <string>
#include <string_view>
#include <vector>
#include "libusbwrap/LibUsbTypes.hpp"
@@ -40,6 +41,8 @@ struct PrinterStatus {
unsigned int tapeWidthMm = 0.0;
};
using cmd_T = std::vector<uint8_t>;
enum class HAlignPosition {
UNKNOWN = 0,
LEFT = 1,