Implemented printer status retrieval

This commit is contained in:
2023-08-27 09:29:37 +02:00
parent 5644c84c83
commit f467a39919
9 changed files with 202 additions and 9 deletions

View File

@@ -1,8 +1,29 @@
#include "P700Printer.hpp"
#include <spdlog/spdlog.h>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <ratio>
#include <thread>
#include <vector>
#include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp"
#include "spdlog/fmt/bin_to_hex.h"
namespace ptprnt::printer {
P700Printer::P700Printer() {}
P700Printer::~P700Printer() {
detachUsbDevice();
if (mUsbHndl) {
mUsbHndl->close();
}
}
const std::string_view P700Printer::getDriverName() {
return mInfo.driverName;
}
@@ -19,6 +40,25 @@ const PrinterInfo P700Printer::getPrinterInfo() {
return mInfo;
}
const PrinterStatus P700Printer::getPrinterStatus() {
using namespace std::chrono_literals;
std::vector<uint8_t> getStatusCmd({0x1b, 0x69, 0x53}); // status info request
send(getStatusCmd);
int tx = 0;
int tries = 0;
std::vector<uint8_t> recvBuf(32);
do {
std::this_thread::sleep_for(100ms);
mUsbHndl->bulkTransfer(0x81, recvBuf, &tx, 0);
if (tries++ > 10) {
break;
}
} while (tx == 0);
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
}
const uint16_t P700Printer::getVid() {
return mInfo.vid;
}
@@ -28,10 +68,35 @@ const uint16_t P700Printer::getPid() {
}
bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) {
if (!usbHndl->open()) {
spdlog::error("Unable to open USB device: {}", usbHndl->getLastErrorString());
return false;
}
if (!usbHndl->detachKernelDriver(0)) {
spdlog::error("Device is already in use or couldn't be detached from kernel: {}",
usbHndl->getLastErrorString());
return false;
}
if (!usbHndl->claimInterface(0)) {
spdlog::error("Could not claim interface 0: {}", usbHndl->getLastErrorString());
return false;
}
mUsbHndl = std::move(usbHndl);
return true;
}
bool P700Printer::detachUsbDevice() {
if (!mUsbHndl) {
spdlog::warn("No device to detach...");
return true;
}
if (!mUsbHndl->releaseInterface(0)) {
spdlog::error("Could not release interface 0: {}", mUsbHndl->getLastErrorString());
return false;
}
return true;
}
@@ -42,4 +107,18 @@ bool P700Printer::printBitmap(const Bitmap& bitmap) {
bool P700Printer::printText(const std::string& text, uint16_t fontSize) {
return true;
}
bool P700Printer::send(std::vector<uint8_t>& data) {
if (mUsbHndl == nullptr || data.size() > 128) {
spdlog::error("Invalid device handle or invalid data.");
return false;
}
if (!mUsbHndl->bulkTransfer(0x02, data, nullptr, 0)) {
spdlog::error("Error writing command to Printer: {}", mUsbHndl->getLastErrorString());
return false;
}
return true;
}
} // namespace ptprnt::printer