Add a trace mode for usb tracing

This commit is contained in:
2025-10-12 12:56:09 +02:00
parent bf7ff27b8d
commit 652e687fb0
5 changed files with 51 additions and 26 deletions

View File

@@ -35,6 +35,15 @@ incdir = include_directories('src')
subdir('src') subdir('src')
# Build arguments
cpp_args = ['-DPROJ_VERSION="' + meson.project_version() + '"']
# USB trace mode option (for debugging without sending to hardware)
if get_option('usb_trace_only')
cpp_args += ['-DUSB_TRACE_ONLY']
message('USB_TRACE_ONLY enabled: USB data will be logged but not sent to device')
endif
ptprnt_exe = executable( ptprnt_exe = executable(
'ptprnt', 'ptprnt',
'src/main.cpp', 'src/main.cpp',
@@ -42,7 +51,7 @@ ptprnt_exe = executable(
dependencies: [usb_dep, log_dep, fmt_dep, pangocairo_dep, cli11_dep], dependencies: [usb_dep, log_dep, fmt_dep, pangocairo_dep, cli11_dep],
include_directories: incdir, include_directories: incdir,
sources: [ptprnt_srcs], sources: [ptprnt_srcs],
cpp_args: ['-DPROJ_VERSION="' + meson.project_version() + '"'], cpp_args: cpp_args,
) )

View File

@@ -55,7 +55,10 @@ int PtouchPrint::init(int argc, char** argv) {
return -1; return -1;
} }
if (mVerboseFlag) { // Set log level based on flags
if (mTraceFlag) {
setupLogger(spdlog::level::trace);
} else if (mVerboseFlag) {
setupLogger(spdlog::level::debug); setupLogger(spdlog::level::debug);
} else { } else {
setupLogger(spdlog::level::warn); setupLogger(spdlog::level::warn);
@@ -263,6 +266,7 @@ void PtouchPrint::setupCliParser() {
// General options // General options
mApp.add_flag("-v,--verbose", mVerboseFlag, "Enable verbose output"); mApp.add_flag("-v,--verbose", mVerboseFlag, "Enable verbose output");
mApp.add_flag("--trace", mTraceFlag, "Enable trace output (shows USB communication)");
mApp.add_flag("-V,--version", printVersion, "Prints the ptprnt's version"); mApp.add_flag("-V,--version", printVersion, "Prints the ptprnt's version");
// Printer selection // Printer selection

View File

@@ -60,6 +60,7 @@ class PtouchPrint {
// CLI flags and options // CLI flags and options
bool mVerboseFlag = false; bool mVerboseFlag = false;
bool mTraceFlag = false;
std::string mPrinterSelection = "auto"; std::string mPrinterSelection = "auto";
bool mListDriversFlag = false; bool mListDriversFlag = false;
}; };

View File

@@ -127,42 +127,42 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
} }
bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) { bool P700Printer::printMonochromeData(const graphics::MonochromeData& data) {
// Send initialization sequence
send(p700::commands::INITIALIZE);
// Status is already queried in getPrinterStatus()
send(p700::commands::PRINT_MODE);
send(p700::commands::AUTO_STATUS);
send(p700::commands::MODE_SETTING);
send(p700::commands::RASTER_START); // Send raster data row by row
std::vector<uint8_t> rastercmd(4); for (uint32_t row = 0; row < data.height; row++) {
rastercmd[0] = 0x47; std::vector<uint8_t> rowData;
rastercmd[1] = 0x00; // size +1
rastercmd[2] = 0x00;
rastercmd[3] = 0x00; // size -1
// Process data column by column for the printer // Extract row data byte by byte
for (uint32_t col = 0; col < data.width; col++) { for (uint32_t col = 0; col < data.width; col += 8) {
std::vector<uint8_t> columnData;
// Extract column data bit by bit
for (uint32_t row = 0; row < data.height; row += 8) {
uint8_t byte = 0; uint8_t byte = 0;
for (int bit = 0; bit < 8 && (row + bit) < data.height; bit++) { for (int bit = 0; bit < 8 && (col + bit) < data.width; bit++) {
if (data.getBit(col, row + bit)) { if (data.getBit(col + bit, row)) {
byte |= (1 << (7 - bit)); byte |= (1 << (7 - bit));
} }
} }
columnData.push_back(byte); rowData.push_back(byte);
} }
// Build raster line command: G + length_high + 0x00 + length_low + data
std::vector<uint8_t> buf; std::vector<uint8_t> buf;
buf.insert(buf.begin(), rastercmd.begin(), rastercmd.end()); buf.push_back(0x47); // 'G' command
buf.insert(std::next(buf.begin(), 4), columnData.begin(), columnData.end()); buf.push_back((rowData.size() + 1) & 0xFF); // length + 1 (low byte)
buf[1] = columnData.size() + 1; buf.push_back(0x00); // high byte (always 0 for our data size)
buf[3] = columnData.size() - 1; buf.push_back((rowData.size() - 1) & 0xFF); // length - 1
buf.insert(buf.end(), rowData.begin(), rowData.end());
if (!send(buf)) { if (!send(buf)) {
spdlog::error("Error sending buffer to printer"); spdlog::error("Error sending raster line {} to printer", row);
break; return false;
} }
} }
send(p700::commands::EJECT);
return true; return true;
} }
@@ -178,7 +178,8 @@ bool P700Printer::printLabel(std::unique_ptr<graphics::ILabel> label) {
// Transform to portrait orientation for printing // Transform to portrait orientation for printing
monoData.transformTo(graphics::Orientation::PORTRAIT); monoData.transformTo(graphics::Orientation::PORTRAIT);
spdlog::debug("Label surface is {}x{}, transformed to portrait", label->getWidth(), label->getHeight()); spdlog::debug("Label surface was {}x{}, after transform to portrait: {}x{}", label->getWidth(), label->getHeight(),
monoData.width, monoData.height);
return printMonochromeData(monoData); return printMonochromeData(monoData);
} }
@@ -197,6 +198,11 @@ bool P700Printer::send(const std::vector<uint8_t>& data) {
return false; return false;
} }
spdlog::trace("USB Tx → 0x02 {:03d} bytes: {:Xn}", data.size(), spdlog::to_hex(data));
#ifdef USB_TRACE_ONLY
// Trace mode: Log the data that would be sent without actually sending it
return true;
#else
int tx = 0; int tx = 0;
if (!mUsbHndl->bulkTransfer(0x02, data, &tx, 0)) { if (!mUsbHndl->bulkTransfer(0x02, data, &tx, 0)) {
@@ -211,6 +217,7 @@ bool P700Printer::send(const std::vector<uint8_t>& data) {
} }
return true; return true;
#endif
} }
bool P700Printer::init() { bool P700Printer::init() {

View File

@@ -32,7 +32,11 @@
namespace ptprnt::printer { namespace ptprnt::printer {
namespace p700::commands { namespace p700::commands {
const cmd_T GET_STATUS{0x1b, 0x69, 0x53}; const cmd_T INITIALIZE{0x1b, 0x40}; // ESC @ - Initialize
const cmd_T GET_STATUS{0x1b, 0x69, 0x53}; // ESC i S - Status query
const cmd_T PRINT_MODE{0x4d, 0x02}; // M 0x02 - Print mode
const cmd_T AUTO_STATUS{0x1b, 0x69, 0x61, 0x01}; // ESC i a - Auto status
const cmd_T MODE_SETTING{0x1b, 0x69, 0x4d, 0x40}; // ESC i M @ - Advanced mode
const cmd_T RASTER_START{0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 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 INFO{0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
const cmd_T PACKBITSON{0x02}; const cmd_T PACKBITSON{0x02};