commands restructured to be const vectors
This commit is contained in:
@@ -41,7 +41,7 @@ BreakBeforeBinaryOperators: None
|
|||||||
BreakBeforeTernaryOperators: true
|
BreakBeforeTernaryOperators: true
|
||||||
BreakConstructorInitializers: BeforeColon
|
BreakConstructorInitializers: BeforeColon
|
||||||
BreakInheritanceList: BeforeColon
|
BreakInheritanceList: BeforeColon
|
||||||
ColumnLimit: 100
|
ColumnLimit: 120
|
||||||
CompactNamespaces: false
|
CompactNamespaces: false
|
||||||
ContinuationIndentWidth: 4
|
ContinuationIndentWidth: 4
|
||||||
Cpp11BracedListStyle: true
|
Cpp11BracedListStyle: true
|
||||||
|
7
.vscode/settings.json
vendored
7
.vscode/settings.json
vendored
@@ -85,6 +85,9 @@
|
|||||||
},
|
},
|
||||||
"clang-tidy.buildPath": "builddir/",
|
"clang-tidy.buildPath": "builddir/",
|
||||||
"clangd.onConfigChanged": "restart",
|
"clangd.onConfigChanged": "restart",
|
||||||
"C_Cpp.default.compileCommands": "/home/moritz/Projekte/ptouch-prnt/builddir/compile_commands.json",
|
"C_Cpp.default.compileCommands": "/home/moritz/src/ptouch-prnt/builddir/compile_commands.json",
|
||||||
"gcovViewer.buildDirectories": ["/home/moritz/Projekte/ptouch-prnt/builddir"]
|
"gcovViewer.buildDirectories": [
|
||||||
|
"/home/moritz/Projekte/ptouch-prnt/builddir"
|
||||||
|
],
|
||||||
|
"C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild"
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
project('ptprnt', 'cpp',
|
project('ptprnt', 'cpp',
|
||||||
version: 'v0.1.0-'+run_command('git', 'rev-parse', '--short', 'HEAD', check: true).stdout().strip(),
|
version: 'v0.1.0-'+run_command('git', 'rev-parse', '--short', 'HEAD', check: true).stdout().strip(),
|
||||||
license: 'GPLv3',
|
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')
|
usb_dep = dependency('libusb-1.0')
|
||||||
|
@@ -37,7 +37,7 @@
|
|||||||
#include "spdlog/fmt/bin_to_hex.h"
|
#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 ;)
|
// 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 {
|
namespace ptprnt::printer {
|
||||||
|
|
||||||
@@ -71,15 +71,14 @@ const PrinterInfo P700Printer::getPrinterInfo() {
|
|||||||
|
|
||||||
const PrinterStatus P700Printer::getPrinterStatus() {
|
const PrinterStatus P700Printer::getPrinterStatus() {
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
std::vector<uint8_t> getStatusCmd({0x1b, 0x69, 0x53}); // status info request
|
send(p700::commands::GET_STATUS);
|
||||||
send(getStatusCmd);
|
|
||||||
|
|
||||||
int tx = 0;
|
int tx = 0;
|
||||||
int tries = 0;
|
int tries = 0;
|
||||||
std::vector<uint8_t> recvBuf(32);
|
std::vector<uint8_t> recvBuf(32);
|
||||||
while (tries++ < MAX_TRIES_GET_STATUS) {
|
while (tries++ < MAX_TRIES_GET_STATUS) {
|
||||||
std::this_thread::sleep_for(100ms);
|
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]};
|
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
|
||||||
@@ -132,7 +131,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
send(commands["rasterstart"]);
|
send(p700::commands::RASTER_START);
|
||||||
std::vector<uint8_t> rastercmd(4);
|
std::vector<uint8_t> rastercmd(4);
|
||||||
rastercmd[0] = 0x47;
|
rastercmd[0] = 0x47;
|
||||||
rastercmd[1] = 0x00; // size +1
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,13 +183,13 @@ bool P700Printer::setVAlign(VAlignPosition vpos) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool P700Printer::print() {
|
bool P700Printer::print() {
|
||||||
send(commands["lf"]);
|
send(p700::commands::LF);
|
||||||
send(commands["ff"]);
|
send(p700::commands::FF);
|
||||||
send(commands["eject"]);
|
send(p700::commands::EJECT);
|
||||||
return true;
|
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) {
|
if (mUsbHndl == nullptr || data.size() > 128) {
|
||||||
spdlog::error("Invalid device handle or invalid data.");
|
spdlog::error("Invalid device handle or invalid data.");
|
||||||
@@ -210,8 +209,7 @@ bool P700Printer::send(std::vector<uint8_t>& data) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (tx != static_cast<int>(data.size())) {
|
if (tx != static_cast<int>(data.size())) {
|
||||||
spdlog::error("Could not transfer all data via USB bulk transfer. Only sent {} of {} bytes",
|
spdlog::error("Could not transfer all data via USB bulk transfer. Only sent {} of {} bytes", tx, data.size());
|
||||||
tx, data.size());
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -32,13 +32,23 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
namespace ptprnt::printer {
|
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;
|
constexpr uint8_t MAX_TRIES_GET_STATUS = 10;
|
||||||
|
|
||||||
class P700Printer : public ::ptprnt::IPrinterDriver {
|
class P700Printer : public ::ptprnt::IPrinterDriver {
|
||||||
public:
|
public:
|
||||||
P700Printer() = default;
|
P700Printer() = default;
|
||||||
~P700Printer();
|
~P700Printer() override;
|
||||||
|
|
||||||
// delete copy ctor and assignment
|
// delete copy ctor and assignment
|
||||||
P700Printer(const P700Printer&) = default;
|
P700Printer(const P700Printer&) = default;
|
||||||
@@ -67,14 +77,20 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
|
|||||||
bool print() override;
|
bool print() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool send(std::vector<uint8_t>& data);
|
bool send(const std::vector<uint8_t>& data);
|
||||||
bool init();
|
bool init();
|
||||||
|
|
||||||
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
|
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{
|
std::map<std::string, std::vector<uint8_t>> commands{
|
||||||
{"rasterstart",
|
{"rasterstart",
|
||||||
{0x1b, 0x69, 0x61,
|
{0x1b, 0x69, 0x61, 0x01}}, // unique for P700, other printers have the 2 byte set to 0x52 instead of 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}},
|
{"info", {0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
|
||||||
{"packbitson", {0x02}},
|
{"packbitson", {0x02}},
|
||||||
{"lf", {0x5a}},
|
{"lf", {0x5a}},
|
||||||
|
@@ -75,8 +75,7 @@ int PtouchPrint::run() {
|
|||||||
mDetectedPrinters = getCompatiblePrinters();
|
mDetectedPrinters = getCompatiblePrinters();
|
||||||
auto numFoundPrinters = mDetectedPrinters.size();
|
auto numFoundPrinters = mDetectedPrinters.size();
|
||||||
if (numFoundPrinters == 0) {
|
if (numFoundPrinters == 0) {
|
||||||
spdlog::error(
|
spdlog::error("No compatible printers found, please make sure that they are turned on and connected");
|
||||||
"No compatible printers found, please make sure that they are turned on and connected");
|
|
||||||
return -1;
|
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.");
|
||||||
@@ -87,8 +86,7 @@ int PtouchPrint::run() {
|
|||||||
const auto printerUsbId = printer->getUsbId();
|
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) {
|
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 -1;
|
return -1;
|
||||||
}
|
}
|
||||||
printer->attachUsbDevice(std::move(devices[0]));
|
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())));
|
printer->setFontSize(static_cast<uint8_t>(std::atoi(cmd.second.c_str())));
|
||||||
break;
|
break;
|
||||||
case CliCmdType::HAlign:;
|
case CliCmdType::HAlign:;
|
||||||
spdlog::debug("[Not implemented] Setting text horizontal alignment to {}",
|
spdlog::debug("[Not implemented] Setting text horizontal alignment to {}", cmd.second);
|
||||||
cmd.second);
|
|
||||||
break;
|
break;
|
||||||
case CliCmdType::VAlign:;
|
case CliCmdType::VAlign:;
|
||||||
spdlog::debug("[Not implemented] Setting text vertical alignment to {}",
|
spdlog::debug("[Not implemented] Setting text vertical alignment to {}", cmd.second);
|
||||||
cmd.second);
|
|
||||||
break;
|
break;
|
||||||
case CliCmdType::None:;
|
case CliCmdType::None:;
|
||||||
[[fallthrough]];
|
[[fallthrough]];
|
||||||
@@ -209,7 +205,6 @@ void PtouchPrint::setupCliParser() {
|
|||||||
->each([this](std::string halign) { mCommands.emplace_back(CliCmdType::HAlign, halign); });
|
->each([this](std::string halign) { mCommands.emplace_back(CliCmdType::HAlign, halign); });
|
||||||
|
|
||||||
// Image options
|
// Image options
|
||||||
mApp.add_option("-i,--image", "Image to print. Excludes all text printing ")
|
mApp.add_option("-i,--image", "Image to print. Excludes all text printing ")->group("Image printing");
|
||||||
->group("Image printing");
|
|
||||||
}
|
}
|
||||||
} // namespace ptprnt
|
} // namespace ptprnt
|
@@ -28,7 +28,6 @@
|
|||||||
#include "libusbwrap/interface/IUsbDevice.hpp"
|
#include "libusbwrap/interface/IUsbDevice.hpp"
|
||||||
|
|
||||||
namespace ptprnt {
|
namespace ptprnt {
|
||||||
|
|
||||||
class IPrinterDriver {
|
class IPrinterDriver {
|
||||||
public:
|
public:
|
||||||
virtual ~IPrinterDriver() = default;
|
virtual ~IPrinterDriver() = default;
|
||||||
|
@@ -22,6 +22,7 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "libusbwrap/LibUsbTypes.hpp"
|
#include "libusbwrap/LibUsbTypes.hpp"
|
||||||
|
|
||||||
@@ -40,6 +41,8 @@ struct PrinterStatus {
|
|||||||
unsigned int tapeWidthMm = 0.0;
|
unsigned int tapeWidthMm = 0.0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using cmd_T = std::vector<uint8_t>;
|
||||||
|
|
||||||
enum class HAlignPosition {
|
enum class HAlignPosition {
|
||||||
UNKNOWN = 0,
|
UNKNOWN = 0,
|
||||||
LEFT = 1,
|
LEFT = 1,
|
||||||
|
Reference in New Issue
Block a user