Compare commits
6 Commits
add-printe
...
46cc16e431
Author | SHA1 | Date | |
---|---|---|---|
46cc16e431
|
|||
0704081326
|
|||
f0812d3166
|
|||
c3110ff32a
|
|||
f4036e6a83
|
|||
778a01429c |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,7 +6,6 @@ subprojects/*
|
|||||||
coverageReport/
|
coverageReport/
|
||||||
|
|
||||||
# Files
|
# Files
|
||||||
ptprnt.log
|
|
||||||
!subprojects/*.wrap
|
!subprojects/*.wrap
|
||||||
.vscode/*
|
.vscode/*
|
||||||
!.vscode/c_cpp_properties.json
|
!.vscode/c_cpp_properties.json
|
||||||
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -85,6 +85,5 @@
|
|||||||
},
|
},
|
||||||
"clang-tidy.buildPath": "builddir/",
|
"clang-tidy.buildPath": "builddir/",
|
||||||
"clangd.onConfigChanged": "restart",
|
"clangd.onConfigChanged": "restart",
|
||||||
"C_Cpp.default.compileCommands": "/home/moritz/src/ptouch-prnt/builddir/compile_commands.json",
|
"C_Cpp.default.compileCommands": "/home/moritz/Projekte/ptouch-prnt/builddir/compile_commands.json"
|
||||||
"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']
|
default_options : ['c_std=c11', 'cpp_std=c++17', 'b_sanitize=address,undefined', 'b_lto=true', 'b_lto_mode=thin', 'b_thinlto_cache=true']
|
||||||
)
|
)
|
||||||
|
|
||||||
usb_dep = dependency('libusb-1.0')
|
usb_dep = dependency('libusb-1.0')
|
||||||
|
@@ -30,7 +30,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "graphics/Bitmap.hpp"
|
#include "graphics/Bitmap.hpp"
|
||||||
#include "graphics/Image.hpp"
|
#include "graphics/Label.hpp"
|
||||||
#include "graphics/Monochrome.hpp"
|
#include "graphics/Monochrome.hpp"
|
||||||
#include "libusb.h"
|
#include "libusb.h"
|
||||||
#include "libusbwrap/LibUsbTypes.hpp"
|
#include "libusbwrap/LibUsbTypes.hpp"
|
||||||
@@ -41,11 +41,6 @@
|
|||||||
|
|
||||||
namespace ptprnt::printer {
|
namespace ptprnt::printer {
|
||||||
|
|
||||||
const PrinterInfo P700Printer::mInfo = {.driverName = "P700",
|
|
||||||
.name = "Brother P-touch P700",
|
|
||||||
.version = "v1.0",
|
|
||||||
.usbId{0x04f9, 0x2061}};
|
|
||||||
|
|
||||||
P700Printer::~P700Printer() {
|
P700Printer::~P700Printer() {
|
||||||
detachUsbDevice();
|
detachUsbDevice();
|
||||||
if (mUsbHndl) {
|
if (mUsbHndl) {
|
||||||
@@ -85,8 +80,12 @@ const PrinterStatus P700Printer::getPrinterStatus() {
|
|||||||
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
|
return PrinterStatus{.tapeWidthMm = recvBuf[10]};
|
||||||
}
|
}
|
||||||
|
|
||||||
const libusbwrap::usbId P700Printer::getUsbId() {
|
const uint16_t P700Printer::getVid() {
|
||||||
return mInfo.usbId;
|
return mInfo.vid;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint16_t P700Printer::getPid() {
|
||||||
|
return mInfo.pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) {
|
bool P700Printer::attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) {
|
||||||
@@ -125,7 +124,7 @@ bool P700Printer::detachUsbDevice() {
|
|||||||
bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) {
|
bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) {
|
||||||
auto bm = graphics::Bitmap<graphics::ALPHA8>(512, 128);
|
auto bm = graphics::Bitmap<graphics::ALPHA8>(512, 128);
|
||||||
{
|
{
|
||||||
auto img = graphics::Image();
|
auto img = graphics::Label();
|
||||||
bm.setPixels(std::vector<uint8_t>(img.getRaw(), img.getRaw() + 512 * 128));
|
bm.setPixels(std::vector<uint8_t>(img.getRaw(), img.getRaw() + 512 * 128));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,27 +159,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool P700Printer::setText(const std::string& text) {
|
bool P700Printer::printText(const std::string& text, uint16_t fontSize) {
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool P700Printer::setFont(const std::string& text) {
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool P700Printer::setFontSize(uint8_t fontSize) {
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool P700Printer::setHAlign(HAlignPosition hpos) {
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool P700Printer::setVAlign(VAlignPosition vpos) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool P700Printer::print() {
|
|
||||||
send(commands["lf"]);
|
send(commands["lf"]);
|
||||||
send(commands["ff"]);
|
send(commands["ff"]);
|
||||||
send(commands["eject"]);
|
send(commands["eject"]);
|
||||||
@@ -203,7 +182,7 @@ bool P700Printer::send(std::vector<uint8_t>& data) {
|
|||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
tx = data.size();
|
tx = data.size();
|
||||||
spdlog::debug("USB raw data(len {}): {}", data.size(), spdlog::to_hex(data));
|
SPDLOG_DEBUG("USB raw data(len {}): {}", data.size(), spdlog::to_hex(data));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (tx != static_cast<int>(data.size())) {
|
if (tx != static_cast<int>(data.size())) {
|
||||||
|
@@ -25,7 +25,6 @@
|
|||||||
|
|
||||||
#include "interface/IPrinterDriver.hpp"
|
#include "interface/IPrinterDriver.hpp"
|
||||||
#include "interface/IPrinterTypes.hpp"
|
#include "interface/IPrinterTypes.hpp"
|
||||||
#include "libusbwrap/LibUsbTypes.hpp"
|
|
||||||
#include "libusbwrap/interface/IUsbDevice.hpp"
|
#include "libusbwrap/interface/IUsbDevice.hpp"
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
@@ -45,31 +44,30 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
|
|||||||
P700Printer(P700Printer&&) = default;
|
P700Printer(P700Printer&&) = default;
|
||||||
P700Printer& operator=(P700Printer&&) = default;
|
P700Printer& operator=(P700Printer&&) = default;
|
||||||
|
|
||||||
// Printer info has to be static to be accessed without instantiation
|
|
||||||
static const PrinterInfo mInfo;
|
|
||||||
|
|
||||||
// IPrinterDriver
|
// IPrinterDriver
|
||||||
[[nodiscard]] const std::string_view getDriverName() override;
|
const std::string_view getDriverName() override;
|
||||||
[[nodiscard]] const std::string_view getName() override;
|
const std::string_view getName() override;
|
||||||
[[nodiscard]] const libusbwrap::usbId getUsbId() override;
|
const uint16_t getPid() override;
|
||||||
[[nodiscard]] const std::string_view getVersion() override;
|
const uint16_t getVid() override;
|
||||||
[[nodiscard]] const PrinterInfo getPrinterInfo() override;
|
const std::string_view getVersion() override;
|
||||||
[[nodiscard]] const PrinterStatus getPrinterStatus() override;
|
const PrinterInfo getPrinterInfo() override;
|
||||||
|
const PrinterStatus getPrinterStatus() override;
|
||||||
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
|
bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) override;
|
||||||
bool detachUsbDevice() override;
|
bool detachUsbDevice() override;
|
||||||
bool setText(const std::string& text) override;
|
|
||||||
bool setFont(const std::string& text) override;
|
|
||||||
bool setFontSize(uint8_t fontSize) override;
|
|
||||||
bool setHAlign(HAlignPosition hpos) override;
|
|
||||||
bool setVAlign(VAlignPosition vpos) override;
|
|
||||||
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
|
bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
|
||||||
bool print() override;
|
bool printText(const std::string& text, uint16_t fontSize) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool send(std::vector<uint8_t>& data);
|
bool send(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};
|
||||||
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,
|
||||||
|
@@ -1,23 +0,0 @@
|
|||||||
#include "PrinterDriverFactory.hpp"
|
|
||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
#include "P700Printer.hpp"
|
|
||||||
#include "libusbwrap/LibUsbTypes.hpp"
|
|
||||||
|
|
||||||
namespace ptprnt {
|
|
||||||
|
|
||||||
std::shared_ptr<IPrinterDriver> PrinterDriverFactory::create(libusbwrap::usbId id) {
|
|
||||||
|
|
||||||
if (printer::P700Printer::mInfo.usbId == id) {
|
|
||||||
spdlog::info("Found printer P700!");
|
|
||||||
return std::make_shared<printer::P700Printer>();
|
|
||||||
} else {
|
|
||||||
spdlog::trace("{:04x}:{:04x} is not a compatible printer", id.first, id.second);
|
|
||||||
}
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ptprnt
|
|
@@ -1,22 +0,0 @@
|
|||||||
#include <memory>
|
|
||||||
#include "interface/IPrinterDriver.hpp"
|
|
||||||
#include "libusbwrap/LibUsbTypes.hpp"
|
|
||||||
|
|
||||||
namespace ptprnt {
|
|
||||||
|
|
||||||
class PrinterDriverFactory {
|
|
||||||
public:
|
|
||||||
PrinterDriverFactory() = default;
|
|
||||||
~PrinterDriverFactory() = default;
|
|
||||||
|
|
||||||
PrinterDriverFactory(const PrinterDriverFactory&) = delete;
|
|
||||||
PrinterDriverFactory& operator=(const PrinterDriverFactory&) = delete;
|
|
||||||
PrinterDriverFactory(PrinterDriverFactory&&) = delete;
|
|
||||||
PrinterDriverFactory& operator=(PrinterDriverFactory&&) = delete;
|
|
||||||
|
|
||||||
std::shared_ptr<IPrinterDriver> create(libusbwrap::usbId id);
|
|
||||||
|
|
||||||
private:
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
@@ -16,11 +16,12 @@
|
|||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
*/
|
*/
|
||||||
#include <cstdint>
|
|
||||||
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
|
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
|
||||||
#define SPDLOG_DEBUG_ON
|
#define SPDLOG_DEBUG_ON
|
||||||
#define SPDLOG_TRACE_ON
|
#define SPDLOG_TRACE_ON
|
||||||
|
|
||||||
|
#include "PtouchPrint.hpp"
|
||||||
|
|
||||||
#include <CLI/App.hpp>
|
#include <CLI/App.hpp>
|
||||||
#include <fmt/core.h>
|
#include <fmt/core.h>
|
||||||
#include <spdlog/common.h>
|
#include <spdlog/common.h>
|
||||||
@@ -36,9 +37,9 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "CLI/Option.hpp"
|
#include "CLI/Option.hpp"
|
||||||
#include "PrinterDriverFactory.hpp"
|
#include "P700Printer.hpp"
|
||||||
#include "PtouchPrint.hpp"
|
|
||||||
#include "graphics/Bitmap.hpp"
|
#include "graphics/Bitmap.hpp"
|
||||||
|
#include "graphics/Label.hpp"
|
||||||
#include "libusbwrap/UsbDeviceFactory.hpp"
|
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||||
|
|
||||||
namespace ptprnt {
|
namespace ptprnt {
|
||||||
@@ -58,21 +59,21 @@ int PtouchPrint::init(int argc, char** argv) {
|
|||||||
if (mVerboseFlag) {
|
if (mVerboseFlag) {
|
||||||
setupLogger(spdlog::level::debug);
|
setupLogger(spdlog::level::debug);
|
||||||
} else {
|
} else {
|
||||||
setupLogger(spdlog::level::err);
|
setupLogger(spdlog::level::warn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mUsbDeviceFactory.init()) {
|
if (!mUsbDeviceFactory.init()) {
|
||||||
spdlog::error("Could not initialize libusb");
|
spdlog::error("Could not initialize libusb");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
mCompatiblePrinters = {std::make_shared<ptprnt::printer::P700Printer>()};
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int PtouchPrint::run() {
|
int PtouchPrint::run() {
|
||||||
spdlog::info("ptprnt version {}", mVersionString);
|
SPDLOG_DEBUG("ptprnt version {}", mVersionString);
|
||||||
SPDLOG_TRACE("testing trace");
|
SPDLOG_TRACE("testing trace");
|
||||||
mDetectedPrinters = getCompatiblePrinters();
|
auto numFoundPrinters = getCompatiblePrinters();
|
||||||
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");
|
||||||
@@ -82,76 +83,67 @@ int PtouchPrint::run() {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto printer = mDetectedPrinters[0];
|
auto printer = mCompatiblePrinters[0];
|
||||||
const auto printerUsbId = printer->getUsbId();
|
auto devices = mUsbDeviceFactory.findDevices(printer->getVid(), printer->getPid());
|
||||||
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(devices[0]);
|
printer->attachUsbDevice(std::move(devices[0]));
|
||||||
auto status = printer->getPrinterStatus();
|
auto status = printer->getPrinterStatus();
|
||||||
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
|
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
|
||||||
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(512, 128);
|
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(512, 128);
|
||||||
//printer->printBitmap(bm);
|
//printer->printBitmap(bm);
|
||||||
//printer->printText("wurst", 1);
|
//printer->printText("wurst", 1);
|
||||||
|
|
||||||
|
if (0 == mCommands.size()) {
|
||||||
|
spdlog::warn("No command specified, nothing to do...");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
for (auto& cmd : mCommands) {
|
for (auto& cmd : mCommands) {
|
||||||
switch (cmd.first) {
|
SPDLOG_DEBUG("Command: {}", cmd.second);
|
||||||
case CliCmdType::Text:
|
if (cmd.first == CliCmdType::Text) {
|
||||||
spdlog::debug("Setting text to {}", cmd.second);
|
auto label{graphics::Label()};
|
||||||
printer->setText(cmd.second);
|
label.createLabel(cmd.second);
|
||||||
break;
|
|
||||||
case CliCmdType::Font:;
|
|
||||||
spdlog::debug("Setting font to {}", cmd.second);
|
|
||||||
printer->setFont(cmd.second);
|
|
||||||
break;
|
|
||||||
case CliCmdType::FontSize:;
|
|
||||||
spdlog::debug("Setting font size to {}", cmd.second);
|
|
||||||
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);
|
|
||||||
break;
|
|
||||||
case CliCmdType::VAlign:;
|
|
||||||
spdlog::debug("[Not implemented] Setting text vertical alignment to {}",
|
|
||||||
cmd.second);
|
|
||||||
break;
|
|
||||||
case CliCmdType::None:;
|
|
||||||
[[fallthrough]];
|
|
||||||
default:
|
|
||||||
spdlog::warn("This command is currently not supported.");
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!printer->print()) {
|
|
||||||
spdlog::error("An error occured while printing");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<IPrinterDriver>> PtouchPrint::getCompatiblePrinters() {
|
unsigned int PtouchPrint::getCompatiblePrinters() {
|
||||||
|
|
||||||
auto usbDevs = mUsbDeviceFactory.findAllDevices();
|
auto usbDevs = mUsbDeviceFactory.findAllDevices();
|
||||||
auto driverFactory = std::make_unique<PrinterDriverFactory>();
|
|
||||||
std::vector<std::shared_ptr<IPrinterDriver>> foundPrinterDrivers{};
|
|
||||||
|
|
||||||
for (auto usbDev : usbDevs) {
|
for (auto& usbDev : usbDevs) {
|
||||||
auto driver = driverFactory->create(usbDev->getUsbId());
|
auto foundPrinterIt =
|
||||||
if (driver != nullptr) {
|
std::find_if(mCompatiblePrinters.begin(), mCompatiblePrinters.end(),
|
||||||
foundPrinterDrivers.push_back(driver);
|
[&usbDev](const std::shared_ptr<ptprnt::IPrinterDriver>& printer) {
|
||||||
|
return printer->getPid() == usbDev->getPid() &&
|
||||||
|
printer->getVid() == usbDev->getVid();
|
||||||
|
});
|
||||||
|
if (foundPrinterIt == mCompatiblePrinters.end()) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
auto foundPrinter = *foundPrinterIt;
|
||||||
|
spdlog::info("Found Printer {}", foundPrinter->getName());
|
||||||
|
mDetectedPrinters.push_back(foundPrinter);
|
||||||
}
|
}
|
||||||
return foundPrinterDrivers;
|
// we can delete all instantiated printers that are not compatible
|
||||||
|
mCompatiblePrinters.clear();
|
||||||
|
return mDetectedPrinters.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
|
void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
|
||||||
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
auto consoleSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
|
||||||
consoleSink->set_level(lvl);
|
consoleSink->set_level(lvl);
|
||||||
|
if (spdlog::level::level_enum::debug == lvl || spdlog::level::level_enum::trace == lvl) {
|
||||||
|
// This will enable file and line number for debug and trace macros
|
||||||
|
consoleSink->set_pattern("%^%L:%$ %v (%s:%#)");
|
||||||
|
} else {
|
||||||
consoleSink->set_pattern("%^%L:%$ %v");
|
consoleSink->set_pattern("%^%L:%$ %v");
|
||||||
|
}
|
||||||
|
|
||||||
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("ptprnt.log", true);
|
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("ptprnt.log", true);
|
||||||
fileSink->set_level(spdlog::level::trace);
|
fileSink->set_level(spdlog::level::trace);
|
||||||
|
@@ -49,12 +49,13 @@ class PtouchPrint {
|
|||||||
// methods
|
// methods
|
||||||
void setupLogger(spdlog::level::level_enum lvl);
|
void setupLogger(spdlog::level::level_enum lvl);
|
||||||
void setupCliParser();
|
void setupCliParser();
|
||||||
std::vector<std::shared_ptr<IPrinterDriver>> getCompatiblePrinters();
|
unsigned int getCompatiblePrinters();
|
||||||
|
|
||||||
// member variables
|
// member variables
|
||||||
CLI::App mApp{ptprnt::APP_DESC};
|
CLI::App mApp{ptprnt::APP_DESC};
|
||||||
libusbwrap::UsbDeviceFactory mUsbDeviceFactory{};
|
libusbwrap::UsbDeviceFactory mUsbDeviceFactory{};
|
||||||
std::vector<std::shared_ptr<IPrinterDriver>> mDetectedPrinters{};
|
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mCompatiblePrinters{};
|
||||||
|
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mDetectedPrinters{};
|
||||||
std::vector<CliCmd> mCommands{};
|
std::vector<CliCmd> mCommands{};
|
||||||
std::string mVersionString = "";
|
std::string mVersionString = "";
|
||||||
|
|
||||||
|
@@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
ptrnt - print labels on linux
|
|
||||||
Copyright (C) 2023 Moritz Martinius
|
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
|
||||||
it under the terms of the GNU General Public License as published by
|
|
||||||
the Free Software Foundation, either version 3 of the License, or
|
|
||||||
(at your option) any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
|
||||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
GNU General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Image.hpp"
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "pango/pango-font.h"
|
|
||||||
|
|
||||||
namespace ptprnt::graphics {
|
|
||||||
Image::Image() {
|
|
||||||
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, 512, 128);
|
|
||||||
cairo_t* cr = cairo_create(mSurface);
|
|
||||||
mFontDescription = pango_font_description_new();
|
|
||||||
pango_font_description_set_family(mFontDescription, "sans");
|
|
||||||
pango_font_description_set_weight(mFontDescription, PANGO_WEIGHT_SEMIBOLD);
|
|
||||||
pango_font_description_set_size(mFontDescription, 60 * PANGO_SCALE);
|
|
||||||
|
|
||||||
std::string printThis("Mist 💩");
|
|
||||||
|
|
||||||
mLayout = pango_cairo_create_layout(cr);
|
|
||||||
pango_layout_set_font_description(mLayout, mFontDescription);
|
|
||||||
pango_layout_set_text(mLayout, printThis.c_str(), -1);
|
|
||||||
|
|
||||||
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
|
|
||||||
cairo_move_to(cr, 0.0, 94.0);
|
|
||||||
pango_cairo_show_layout_line(cr, pango_layout_get_line(mLayout, 0));
|
|
||||||
|
|
||||||
cairo_surface_write_to_png(mSurface, "hello.png");
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t* Image::getRaw() {
|
|
||||||
cairo_surface_flush(mSurface);
|
|
||||||
return cairo_image_surface_get_data(mSurface);
|
|
||||||
}
|
|
||||||
|
|
||||||
Image::~Image() {
|
|
||||||
g_object_unref(mLayout);
|
|
||||||
pango_font_description_free(mFontDescription);
|
|
||||||
}
|
|
||||||
} // namespace ptprnt::graphics
|
|
101
src/graphics/Label.cpp
Normal file
101
src/graphics/Label.cpp
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
ptrnt - print labels on linux
|
||||||
|
Copyright (C) 2023 Moritz Martinius
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "graphics/Label.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <spdlog/spdlog.h>
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <iostream> // remove me
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "cairo.h"
|
||||||
|
#include "pango/pango-context.h"
|
||||||
|
#include "pango/pango-font.h"
|
||||||
|
#include "pango/pango-fontmap.h"
|
||||||
|
#include "pango/pango-layout.h"
|
||||||
|
#include "pango/pango-types.h"
|
||||||
|
#include "pango/pangocairo.h"
|
||||||
|
|
||||||
|
namespace ptprnt::graphics {
|
||||||
|
Label::Label() {
|
||||||
|
/*mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, 512, 128);
|
||||||
|
cairo_t* cr = cairo_create(mSurface);
|
||||||
|
mFontDescription = pango_font_description_new();
|
||||||
|
pango_font_description_set_family(mFontDescription, "sans");
|
||||||
|
pango_font_description_set_weight(mFontDescription, PANGO_WEIGHT_SEMIBOLD);
|
||||||
|
pango_font_description_set_size(mFontDescription, 60 * PANGO_SCALE);
|
||||||
|
|
||||||
|
std::string printThis("Mist 💩");
|
||||||
|
|
||||||
|
mLayout = pango_cairo_create_layout(cr);
|
||||||
|
pango_layout_set_font_description(mLayout, mFontDescription);
|
||||||
|
pango_layout_set_text(mLayout, printThis.c_str(), -1);
|
||||||
|
|
||||||
|
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
|
||||||
|
cairo_move_to(cr, 0.0, 94.0);
|
||||||
|
pango_cairo_show_layout_line(cr, pango_layout_get_line(mLayout, 0));
|
||||||
|
|
||||||
|
cairo_surface_write_to_png(mSurface, "hello.png");*/
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* Label::getRaw() {
|
||||||
|
cairo_surface_flush(mSurface);
|
||||||
|
return cairo_image_surface_get_data(mSurface);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Label::getNumLines(std::string_view strv) {
|
||||||
|
return std::count(strv.begin(), strv.end(), '\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
void Label::createLabel(const std::string& str) {
|
||||||
|
|
||||||
|
// create Pango layout first, to get the render dimensions
|
||||||
|
auto pangoCtx{pango_font_map_create_context(pango_cairo_font_map_get_default())};
|
||||||
|
auto pangoLyt{pango_layout_new(pangoCtx)};
|
||||||
|
auto pangoFontDesc{pango_font_description_from_string("Noto sans 32")};
|
||||||
|
|
||||||
|
pango_layout_set_single_paragraph_mode(pangoLyt, true);
|
||||||
|
pango_layout_set_height(pangoLyt, getNumLines(str) * -1);
|
||||||
|
pango_layout_set_alignment(pangoLyt, PANGO_ALIGN_CENTER);
|
||||||
|
pango_layout_set_font_description(pangoLyt, pangoFontDesc);
|
||||||
|
pango_context_load_font(pangoCtx, pangoFontDesc);
|
||||||
|
pango_layout_set_text(pangoLyt, str.c_str(), static_cast<int>(str.length()));
|
||||||
|
|
||||||
|
int width = 0, height = 0;
|
||||||
|
|
||||||
|
pango_layout_get_size(pangoLyt, &width, &height);
|
||||||
|
|
||||||
|
SPDLOG_DEBUG("Layout width: {}, height: {}", width / PANGO_SCALE, height / PANGO_SCALE);
|
||||||
|
|
||||||
|
auto surf = cairo_image_surface_create(CAIRO_FORMAT_A8, width / PANGO_SCALE, 128);
|
||||||
|
cairo_t* cr = cairo_create(surf);
|
||||||
|
pango_cairo_show_layout(cr, pangoLyt);
|
||||||
|
|
||||||
|
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
|
||||||
|
|
||||||
|
// Debug only
|
||||||
|
cairo_surface_write_to_png(surf, "hellow.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
Label::~Label() {
|
||||||
|
SPDLOG_DEBUG("Image dtor...");
|
||||||
|
}
|
||||||
|
} // namespace ptprnt::graphics
|
@@ -22,15 +22,30 @@
|
|||||||
#include <pango/pangocairo.h>
|
#include <pango/pangocairo.h>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace ptprnt::graphics {
|
namespace ptprnt::graphics {
|
||||||
class Image {
|
|
||||||
|
constexpr const char* DEFAULT_FONT_FAMILY = "sans";
|
||||||
|
constexpr const uint8_t DEFAULT_FONT_SIZE = 32;
|
||||||
|
|
||||||
|
class Label {
|
||||||
public:
|
public:
|
||||||
Image();
|
Label();
|
||||||
~Image();
|
~Label();
|
||||||
|
|
||||||
|
Label(const Label&) = delete;
|
||||||
|
Label& operator=(const Label&) = delete;
|
||||||
|
Label(Label&&) = delete;
|
||||||
|
Label& operator=(Label&&) = delete;
|
||||||
|
|
||||||
uint8_t* getRaw();
|
uint8_t* getRaw();
|
||||||
|
void createLabel(const std::string& labelText);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// methods
|
||||||
|
uint8_t getNumLines(std::string_view str);
|
||||||
|
// members
|
||||||
PangoLayout* mLayout;
|
PangoLayout* mLayout;
|
||||||
PangoFontDescription* mFontDescription;
|
PangoFontDescription* mFontDescription;
|
||||||
cairo_surface_t* mSurface;
|
cairo_surface_t* mSurface;
|
@@ -22,6 +22,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace ptprnt::graphics {
|
namespace ptprnt::graphics {
|
||||||
@@ -41,9 +42,9 @@ std::vector<uint8_t> Monochrome::get() {
|
|||||||
|
|
||||||
unsigned int outIndex = 0;
|
unsigned int outIndex = 0;
|
||||||
|
|
||||||
for (unsigned int byteNum = 0; byteNum < mPixels.size(); byteNum += 8) {
|
for (unsigned int byteNo = 0; byteNo < mPixels.size(); byteNo += 8) {
|
||||||
for (unsigned int bitNo = 0; bitNo < 8; bitNo++) {
|
for (unsigned int bitNo = 0; bitNo <= 7 && (byteNo + bitNo < mPixels.size()); bitNo++) {
|
||||||
if (mPixels[byteNum + bitNo] > mThreshhold) {
|
if (mPixels[byteNo + bitNo] > mThreshhold) {
|
||||||
outPixels[outIndex] |= (1 << (7 - bitNo));
|
outPixels[outIndex] |= (1 << (7 - bitNo));
|
||||||
} else {
|
} else {
|
||||||
outPixels[outIndex] &= ~(1 << (7 - bitNo));
|
outPixels[outIndex] &= ~(1 << (7 - bitNo));
|
||||||
|
6
src/graphics/hello.png
Normal file
6
src/graphics/hello.png
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"text" : "Hello, world :)!",
|
||||||
|
"font" : "Liberation Sans",
|
||||||
|
"single-paragraph" : true,
|
||||||
|
"height" : 0
|
||||||
|
}
|
7
src/graphics/hello.txt
Normal file
7
src/graphics/hello.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"text" : " Hello",
|
||||||
|
"font" : "FreeSans 32",
|
||||||
|
"single-paragraph" : true,
|
||||||
|
"alignment" : "center",
|
||||||
|
"height" : 0
|
||||||
|
}
|
0
src/graphics/ptprnt.log
Normal file
0
src/graphics/ptprnt.log
Normal file
7
src/hello.txt
Normal file
7
src/hello.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"text" : " Hello",
|
||||||
|
"font" : "FreeSans 32",
|
||||||
|
"single-paragraph" : true,
|
||||||
|
"alignment" : "center",
|
||||||
|
"height" : 0
|
||||||
|
}
|
@@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
@@ -32,21 +31,17 @@ namespace ptprnt {
|
|||||||
class IPrinterDriver {
|
class IPrinterDriver {
|
||||||
public:
|
public:
|
||||||
virtual ~IPrinterDriver() = default;
|
virtual ~IPrinterDriver() = default;
|
||||||
[[nodiscard]] virtual const std::string_view getDriverName() = 0;
|
virtual const std::string_view getDriverName() = 0;
|
||||||
[[nodiscard]] virtual const std::string_view getName() = 0;
|
virtual const std::string_view getName() = 0;
|
||||||
[[nodiscard]] virtual const std::string_view getVersion() = 0;
|
virtual const std::string_view getVersion() = 0;
|
||||||
[[nodiscard]] virtual const libusbwrap::usbId getUsbId() = 0;
|
virtual const uint16_t getVid() = 0;
|
||||||
[[nodiscard]] virtual const PrinterInfo getPrinterInfo() = 0;
|
virtual const uint16_t getPid() = 0;
|
||||||
[[nodiscard]] virtual const PrinterStatus getPrinterStatus() = 0;
|
virtual const PrinterInfo getPrinterInfo() = 0;
|
||||||
|
virtual const PrinterStatus getPrinterStatus() = 0;
|
||||||
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
|
virtual bool attachUsbDevice(std::shared_ptr<libusbwrap::IUsbDevice> usbHndl) = 0;
|
||||||
virtual bool detachUsbDevice() = 0;
|
virtual bool detachUsbDevice() = 0;
|
||||||
virtual bool setText(const std::string& text) = 0;
|
|
||||||
virtual bool setFont(const std::string& text) = 0;
|
|
||||||
virtual bool setFontSize(uint8_t fontSize) = 0;
|
|
||||||
virtual bool setHAlign(HAlignPosition hpos) = 0;
|
|
||||||
virtual bool setVAlign(VAlignPosition vpos) = 0;
|
|
||||||
virtual bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) = 0;
|
virtual bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) = 0;
|
||||||
virtual bool print() = 0;
|
virtual bool printText(const std::string& text, uint16_t fontSize) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace ptprnt
|
} // namespace ptprnt
|
@@ -20,18 +20,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
#include "libusbwrap/LibUsbTypes.hpp"
|
|
||||||
|
|
||||||
namespace ptprnt {
|
namespace ptprnt {
|
||||||
|
|
||||||
struct PrinterInfo {
|
struct PrinterInfo {
|
||||||
std::string_view driverName = "";
|
std::string_view driverName = "";
|
||||||
std::string_view name = "";
|
std::string_view name = "";
|
||||||
std::string_view version = "";
|
std::string_view version = "";
|
||||||
libusbwrap::usbId usbId{0x00, 0x00};
|
uint16_t vid = 0x00;
|
||||||
|
uint16_t pid = 0x00;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct PrinterStatus {
|
struct PrinterStatus {
|
||||||
@@ -39,27 +37,4 @@ struct PrinterStatus {
|
|||||||
unsigned int tapeWidthMm = 0.0;
|
unsigned int tapeWidthMm = 0.0;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class HAlignPosition {
|
|
||||||
UNKNOWN = 0,
|
|
||||||
LEFT = 1,
|
|
||||||
CENTER = 2,
|
|
||||||
RIGHT = 3,
|
|
||||||
JUSTIFY = 4,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class VAlignPosition {
|
|
||||||
UNKNOWN = 0,
|
|
||||||
TOP = 1,
|
|
||||||
MIDDLE = 2,
|
|
||||||
BOTTOM = 3,
|
|
||||||
};
|
|
||||||
|
|
||||||
struct PrintableText {
|
|
||||||
std::string text{""};
|
|
||||||
std::string font{"Noto"};
|
|
||||||
uint8_t fontSize{0};
|
|
||||||
HAlignPosition hAlign{HAlignPosition::LEFT};
|
|
||||||
VAlignPosition vAlign{VAlignPosition::MIDDLE};
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace ptprnt
|
} // namespace ptprnt
|
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <utility>
|
#include <memory>
|
||||||
|
|
||||||
#include "libusb.h"
|
#include "libusb.h"
|
||||||
|
|
||||||
@@ -40,7 +40,4 @@ enum class Error {
|
|||||||
NOT_SUPPORTED = LIBUSB_ERROR_NOT_SUPPORTED,
|
NOT_SUPPORTED = LIBUSB_ERROR_NOT_SUPPORTED,
|
||||||
OTHER = LIBUSB_ERROR_OTHER
|
OTHER = LIBUSB_ERROR_OTHER
|
||||||
};
|
};
|
||||||
|
}
|
||||||
using usbId = std::pair<uint16_t, uint16_t>;
|
|
||||||
|
|
||||||
} // namespace libusbwrap
|
|
@@ -27,12 +27,13 @@
|
|||||||
#include "libusbwrap/interface/IUsbDevice.hpp"
|
#include "libusbwrap/interface/IUsbDevice.hpp"
|
||||||
|
|
||||||
namespace libusbwrap {
|
namespace libusbwrap {
|
||||||
UsbDevice::UsbDevice(libusb_context* ctx, libusb_device* dev) : mLibusbCtx(ctx), mLibusbDev(dev) {
|
UsbDevice::UsbDevice(libusb_context* ctx, usbDevice_ptr dev)
|
||||||
|
: mLibusbCtx(ctx), mLibusbDev(std::move(dev)) {
|
||||||
if (mLibusbCtx == nullptr || mLibusbDev == nullptr) {
|
if (mLibusbCtx == nullptr || mLibusbDev == nullptr) {
|
||||||
throw std::invalid_argument("ctx or device are nullptr");
|
throw std::invalid_argument("ctx or device are nullptr");
|
||||||
}
|
}
|
||||||
|
|
||||||
libusb_get_device_descriptor(dev, &mLibusbDevDesc);
|
libusb_get_device_descriptor(mLibusbDev.get(), &mLibusbDevDesc);
|
||||||
}
|
}
|
||||||
|
|
||||||
UsbDevice::~UsbDevice() {
|
UsbDevice::~UsbDevice() {
|
||||||
@@ -44,7 +45,7 @@ UsbDevice::~UsbDevice() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool UsbDevice::open() {
|
bool UsbDevice::open() {
|
||||||
int openStatus = libusb_open(mLibusbDev, &mLibusbDevHandle);
|
int openStatus = libusb_open(mLibusbDev.get(), &mLibusbDevHandle);
|
||||||
if (openStatus != 0) {
|
if (openStatus != 0) {
|
||||||
mLastError = static_cast<Error>(openStatus);
|
mLastError = static_cast<Error>(openStatus);
|
||||||
return false;
|
return false;
|
||||||
@@ -104,20 +105,24 @@ bool UsbDevice::bulkTransfer(uint8_t endpoint, std::vector<uint8_t>& data, int*
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
const usbId UsbDevice::getUsbId() {
|
const uint16_t UsbDevice::getVid() {
|
||||||
return {mLibusbDevDesc.idVendor, mLibusbDevDesc.idProduct};
|
return mLibusbDevDesc.idVendor;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint16_t UsbDevice::getPid() {
|
||||||
|
return mLibusbDevDesc.idProduct;
|
||||||
}
|
}
|
||||||
|
|
||||||
const device::Speed UsbDevice::getSpeed() {
|
const device::Speed UsbDevice::getSpeed() {
|
||||||
return static_cast<device::Speed>(libusb_get_device_speed(mLibusbDev));
|
return static_cast<device::Speed>(libusb_get_device_speed(mLibusbDev.get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t UsbDevice::getBusNumber() {
|
const uint8_t UsbDevice::getBusNumber() {
|
||||||
return libusb_get_bus_number(mLibusbDev);
|
return libusb_get_bus_number(mLibusbDev.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t UsbDevice::getPortNumber() {
|
const uint8_t UsbDevice::getPortNumber() {
|
||||||
return libusb_get_port_number(mLibusbDev);
|
return libusb_get_port_number(mLibusbDev.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
const Error UsbDevice::getLastError() {
|
const Error UsbDevice::getLastError() {
|
||||||
|
@@ -27,14 +27,27 @@
|
|||||||
#include "libusbwrap/interface/IUsbDevice.hpp"
|
#include "libusbwrap/interface/IUsbDevice.hpp"
|
||||||
|
|
||||||
namespace libusbwrap {
|
namespace libusbwrap {
|
||||||
|
|
||||||
|
struct usbDevice_deleter {
|
||||||
|
void operator()(libusb_device* dev_ptr) const {
|
||||||
|
if (nullptr != dev_ptr) {
|
||||||
|
libusb_unref_device(dev_ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using usbDevice_ptr = std::unique_ptr<libusb_device, usbDevice_deleter>;
|
||||||
|
|
||||||
class UsbDevice : public IUsbDevice {
|
class UsbDevice : public IUsbDevice {
|
||||||
public:
|
public:
|
||||||
explicit UsbDevice(libusb_context* ctx, libusb_device* dev);
|
explicit UsbDevice(libusb_context* ctx, usbDevice_ptr dev);
|
||||||
~UsbDevice() override;
|
~UsbDevice() override;
|
||||||
|
|
||||||
// delete copy ctor and assignment
|
// delete copy ctor and assignment
|
||||||
UsbDevice(const UsbDevice&) = delete;
|
UsbDevice(const UsbDevice&) = delete;
|
||||||
UsbDevice& operator=(UsbDevice&) = delete;
|
UsbDevice& operator=(const UsbDevice&) = delete;
|
||||||
|
UsbDevice(UsbDevice&&) = delete;
|
||||||
|
UsbDevice& operator=(UsbDevice&&) = delete;
|
||||||
|
|
||||||
bool open() override;
|
bool open() override;
|
||||||
void close() override;
|
void close() override;
|
||||||
@@ -47,7 +60,8 @@ class UsbDevice : public IUsbDevice {
|
|||||||
unsigned int timeout) override;
|
unsigned int timeout) override;
|
||||||
|
|
||||||
// getters
|
// getters
|
||||||
const usbId getUsbId() override;
|
const uint16_t getVid() override;
|
||||||
|
const uint16_t getPid() override;
|
||||||
const device::Speed getSpeed() override;
|
const device::Speed getSpeed() override;
|
||||||
const uint8_t getBusNumber() override;
|
const uint8_t getBusNumber() override;
|
||||||
const uint8_t getPortNumber() override;
|
const uint8_t getPortNumber() override;
|
||||||
@@ -58,9 +72,9 @@ class UsbDevice : public IUsbDevice {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
libusb_context* mLibusbCtx{nullptr};
|
libusb_context* mLibusbCtx{nullptr};
|
||||||
libusb_device* mLibusbDev{nullptr};
|
usbDevice_ptr mLibusbDev{nullptr};
|
||||||
libusb_device_handle* mLibusbDevHandle{nullptr};
|
libusb_device_handle* mLibusbDevHandle{nullptr};
|
||||||
libusb_device_descriptor mLibusbDevDesc;
|
libusb_device_descriptor mLibusbDevDesc{0};
|
||||||
std::atomic<bool> mIsOpen = false;
|
std::atomic<bool> mIsOpen = false;
|
||||||
Error mLastError = Error::SUCCESS;
|
Error mLastError = Error::SUCCESS;
|
||||||
};
|
};
|
||||||
|
@@ -20,9 +20,11 @@
|
|||||||
#include "libusbwrap/UsbDeviceFactory.hpp"
|
#include "libusbwrap/UsbDeviceFactory.hpp"
|
||||||
|
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "libusb.h"
|
#include "libusb.h"
|
||||||
#include "libusbwrap/UsbDevice.hpp"
|
#include "libusbwrap/UsbDevice.hpp"
|
||||||
@@ -30,50 +32,53 @@
|
|||||||
|
|
||||||
namespace libusbwrap {
|
namespace libusbwrap {
|
||||||
|
|
||||||
UsbDeviceFactory::~UsbDeviceFactory() {
|
UsbDeviceFactory::~UsbDeviceFactory() = default;
|
||||||
if (mDeviceListInitialized) {
|
|
||||||
libusb_free_device_list(mLibusbDeviceList, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
|
||||||
refreshDeviceList();
|
refreshDeviceList();
|
||||||
return buildMaskedDeviceVector(0x0, 0x0, 0x0, 0x0);
|
return buildMaskedDeviceVector(0x0, 0x0, 0x0, 0x0);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
|
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findDevices(uint16_t vid, uint16_t pid) {
|
||||||
refreshDeviceList();
|
refreshDeviceList();
|
||||||
return buildMaskedDeviceVector(0xffff, 0xffff, vid, pid);
|
return buildMaskedDeviceVector(LIBUSB_BITMASK_ALL, LIBUSB_BITMASK_ALL, vid, pid);
|
||||||
}
|
}
|
||||||
|
|
||||||
int UsbDeviceFactory::refreshDeviceList() {
|
ssize_t UsbDeviceFactory::refreshDeviceList() {
|
||||||
int ret = libusb_get_device_list(mLibusbCtx, &mLibusbDeviceList);
|
libusb_device** list{nullptr};
|
||||||
|
|
||||||
|
ssize_t ret = libusb_get_device_list(mLibusbCtx, &list);
|
||||||
|
mLibusbDeviceList.clear();
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
spdlog::error("Error enumarating USB devices");
|
spdlog::error("Error enumarating USB devices");
|
||||||
} else if (ret == 0) {
|
} else if (ret == 0) {
|
||||||
spdlog::warn("No USB devices found");
|
spdlog::warn("No USB devices found");
|
||||||
}
|
}
|
||||||
mDeviceListInitialized = true;
|
|
||||||
|
for (ssize_t i = 0; i < ret; i++) {
|
||||||
|
mLibusbDeviceList.emplace_back(list[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
libusb_free_device_list(list, false);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask,
|
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::buildMaskedDeviceVector(uint16_t vidMask,
|
||||||
uint16_t pidMask,
|
uint16_t pidMask,
|
||||||
uint16_t vid,
|
uint16_t vid,
|
||||||
uint16_t pid) {
|
uint16_t pid) {
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> matchedDevices;
|
std::vector<std::unique_ptr<IUsbDevice>> matchedDevices;
|
||||||
// see libusb/examples/listdevs.c
|
// see libusb/examples/listdevs.c
|
||||||
int i = 0;
|
for (auto& currDev : mLibusbDeviceList) {
|
||||||
libusb_device* currDev = nullptr;
|
struct libusb_device_descriptor currDevDesc {};
|
||||||
while ((currDev = mLibusbDeviceList[i++]) != nullptr) {
|
|
||||||
struct libusb_device_descriptor currDevDesc;
|
int ret = libusb_get_device_descriptor(currDev.get(), &currDevDesc);
|
||||||
int ret = libusb_get_device_descriptor(currDev, &currDevDesc);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (((currDevDesc.idVendor & vidMask) == vid) &&
|
if (((currDevDesc.idVendor & vidMask) == vid) &&
|
||||||
((currDevDesc.idProduct & pidMask) == pid)) {
|
((currDevDesc.idProduct & pidMask) == pid)) {
|
||||||
matchedDevices.push_back(std::make_shared<UsbDevice>(mLibusbCtx, currDev));
|
matchedDevices.push_back(std::make_unique<UsbDevice>(mLibusbCtx, std::move(currDev)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matchedDevices;
|
return matchedDevices;
|
||||||
|
@@ -19,9 +19,16 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "libusb.h"
|
||||||
|
#include "libusbwrap/UsbDevice.hpp"
|
||||||
#include "libusbwrap/interface/IUsbDeviceFactory.hpp"
|
#include "libusbwrap/interface/IUsbDeviceFactory.hpp"
|
||||||
|
|
||||||
namespace libusbwrap {
|
namespace libusbwrap {
|
||||||
|
|
||||||
|
constexpr const uint16_t LIBUSB_BITMASK_ALL = 0xffff;
|
||||||
|
|
||||||
class UsbDeviceFactory : public IUsbDeviceFactory {
|
class UsbDeviceFactory : public IUsbDeviceFactory {
|
||||||
public:
|
public:
|
||||||
UsbDeviceFactory() = default;
|
UsbDeviceFactory() = default;
|
||||||
@@ -39,7 +46,7 @@ class UsbDeviceFactory : public IUsbDeviceFactory {
|
|||||||
*
|
*
|
||||||
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of all detected USB devices
|
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of all detected USB devices
|
||||||
*/
|
*/
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() override;
|
std::vector<std::unique_ptr<IUsbDevice>> findAllDevices() override;
|
||||||
/**
|
/**
|
||||||
* @brief Gets all devices with certain vid/pid combination.
|
* @brief Gets all devices with certain vid/pid combination.
|
||||||
* If only one device of certain type is connected, vector is usually only one element
|
* If only one device of certain type is connected, vector is usually only one element
|
||||||
@@ -48,17 +55,17 @@ class UsbDeviceFactory : public IUsbDeviceFactory {
|
|||||||
* @param pid ProductId of the devices to find
|
* @param pid ProductId of the devices to find
|
||||||
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of detected USB devices based on vid/pid
|
* @return std::vector<std::shared_ptr<IUsbDevice>> Vector of detected USB devices based on vid/pid
|
||||||
*/
|
*/
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) override;
|
std::vector<std::unique_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// methods
|
// methods
|
||||||
int refreshDeviceList();
|
ssize_t refreshDeviceList();
|
||||||
std::vector<std::shared_ptr<IUsbDevice>> buildMaskedDeviceVector(uint16_t vidMask,
|
std::vector<std::unique_ptr<IUsbDevice>> buildMaskedDeviceVector(uint16_t vidMask,
|
||||||
uint16_t pidMask, uint16_t vid,
|
uint16_t pidMask, uint16_t vid,
|
||||||
uint16_t pid);
|
uint16_t pid);
|
||||||
// members
|
// members
|
||||||
libusb_context* mLibusbCtx{nullptr};
|
libusb_context* mLibusbCtx{nullptr};
|
||||||
libusb_device** mLibusbDeviceList{};
|
std::vector<usbDevice_ptr> mLibusbDeviceList{};
|
||||||
bool mDeviceListInitialized = false;
|
bool mDeviceListInitialized = false;
|
||||||
};
|
};
|
||||||
} // namespace libusbwrap
|
} // namespace libusbwrap
|
@@ -36,7 +36,8 @@ class IUsbDevice {
|
|||||||
unsigned int timeout) = 0;
|
unsigned int timeout) = 0;
|
||||||
|
|
||||||
// getters
|
// getters
|
||||||
virtual const usbId getUsbId() = 0;
|
virtual const uint16_t getVid() = 0;
|
||||||
|
virtual const uint16_t getPid() = 0;
|
||||||
virtual const device::Speed getSpeed() = 0;
|
virtual const device::Speed getSpeed() = 0;
|
||||||
virtual const uint8_t getBusNumber() = 0;
|
virtual const uint8_t getBusNumber() = 0;
|
||||||
virtual const uint8_t getPortNumber() = 0;
|
virtual const uint8_t getPortNumber() = 0;
|
||||||
|
@@ -10,7 +10,7 @@ namespace libusbwrap {
|
|||||||
class IUsbDeviceFactory {
|
class IUsbDeviceFactory {
|
||||||
public:
|
public:
|
||||||
virtual ~IUsbDeviceFactory() = default;
|
virtual ~IUsbDeviceFactory() = default;
|
||||||
virtual std::vector<std::shared_ptr<IUsbDevice>> findAllDevices() = 0;
|
virtual std::vector<std::unique_ptr<IUsbDevice>> findAllDevices() = 0;
|
||||||
virtual std::vector<std::shared_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
|
virtual std::vector<std::unique_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
|
||||||
};
|
};
|
||||||
} // namespace libusbwrap
|
} // namespace libusbwrap
|
@@ -8,17 +8,15 @@ ptprnt_hpps = files (
|
|||||||
'interface/IPrinterTypes.hpp',
|
'interface/IPrinterTypes.hpp',
|
||||||
'P700Printer.hpp',
|
'P700Printer.hpp',
|
||||||
'PtouchPrint.hpp',
|
'PtouchPrint.hpp',
|
||||||
'PrinterDriverFactory.hpp',
|
|
||||||
'graphics/Bitmap.hpp',
|
'graphics/Bitmap.hpp',
|
||||||
'graphics/Image.hpp',
|
'graphics/Label.hpp',
|
||||||
'graphics/Monochrome.hpp'
|
'graphics/Monochrome.hpp'
|
||||||
)
|
)
|
||||||
|
|
||||||
ptprnt_srcs = files (
|
ptprnt_srcs = files (
|
||||||
'PtouchPrint.cpp',
|
'PtouchPrint.cpp',
|
||||||
'PrinterDriverFactory.cpp',
|
|
||||||
'P700Printer.cpp',
|
'P700Printer.cpp',
|
||||||
'graphics/Image.cpp',
|
'graphics/Label.cpp',
|
||||||
'graphics/Bitmap.cpp',
|
'graphics/Bitmap.cpp',
|
||||||
'graphics/Monochrome.cpp',
|
'graphics/Monochrome.cpp',
|
||||||
'libusbwrap/UsbDeviceFactory.cpp',
|
'libusbwrap/UsbDeviceFactory.cpp',
|
||||||
|
0
src/ptprnt.log
Normal file
0
src/ptprnt.log
Normal file
@@ -17,10 +17,12 @@
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "graphics/Image.hpp"
|
#include "graphics/Label.hpp"
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
TEST(basic_test, Image_smokeTest_succeeds) {
|
#include <optional>
|
||||||
auto im = ptprnt::graphics::Image();
|
|
||||||
|
TEST(basic_test, Label_smokeTest_succeeds) {
|
||||||
|
auto im = ptprnt::graphics::Label();
|
||||||
}
|
}
|
@@ -1,38 +1,14 @@
|
|||||||
tests = [
|
tests = [['bitmap_test', 'bitmap_test_exe', ptprnt_srcs + ['bitmap_test/bitmap_test.cpp']],
|
||||||
[
|
['label_test', 'label_test_exe', ptprnt_srcs +['label_test/label_test.cpp']],
|
||||||
'bitmap_test',
|
['monochrome_test', 'monochrome_test_exe', ptprnt_srcs +['monochrome_test/monochrome_test.cpp']]
|
||||||
'bitmap_test_exe',
|
]
|
||||||
['../src/graphics/Bitmap.cpp', 'bitmap_test/bitmap_test.cpp'],
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'image_test',
|
|
||||||
'image_test_exe',
|
|
||||||
['../src/graphics/Image.cpp', 'image_test/image_test.cpp'],
|
|
||||||
],
|
|
||||||
[
|
|
||||||
'monochrome_test',
|
|
||||||
'monochrome_test_exe',
|
|
||||||
[
|
|
||||||
'../src/graphics/Monochrome.cpp',
|
|
||||||
'monochrome_test/monochrome_test.cpp',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
]
|
|
||||||
|
|
||||||
foreach test : tests
|
foreach test : tests
|
||||||
test(
|
test(test.get(0),
|
||||||
test.get(0),
|
executable(test.get(1),
|
||||||
executable(
|
|
||||||
test.get(1),
|
|
||||||
sources: test.get(2),
|
sources: test.get(2),
|
||||||
include_directories: incdir,
|
include_directories: incdir,
|
||||||
dependencies: [
|
dependencies: [gtest_dep, usb_dep, log_dep, pangocairo_dep, cli11_dep]
|
||||||
gtest_dep,
|
)
|
||||||
usb_dep,
|
|
||||||
log_dep,
|
|
||||||
pangocairo_dep,
|
|
||||||
cli11_dep,
|
|
||||||
],
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
endforeach
|
endforeach
|
@@ -57,9 +57,6 @@ TEST(basic_test, Monochrome_convertWithCustomThreshhold_yieldsMonochromeRespecti
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(basic_test, Monochrome_convertNonAlignedPixels_spillsOverIntoNewByte) {
|
TEST(basic_test, Monochrome_convertNonAlignedPixels_spillsOverIntoNewByte) {
|
||||||
// TODO: We need to find to access the vector without the possiblity of out-of-bounds access
|
|
||||||
// Ideas: constexpr? compile time check?
|
|
||||||
GTEST_SKIP() << "Skipping this test, as ASAN will halt as this is an out-of-bounds access";
|
|
||||||
const std::vector<uint8_t> pixels({0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
|
const std::vector<uint8_t> pixels({0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00,
|
||||||
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF});
|
0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user