From 373debd809b0f632eaa55ea4586218badf84547c Mon Sep 17 00:00:00 2001 From: Moritz Martinius Date: Sat, 23 Sep 2023 13:29:54 +0200 Subject: [PATCH] Moved graphics classes into own namespace, added unit testing with gtest --- .gitignore | 3 +- .vscode/launch.json | 2 +- meson.build | 62 +++++++++++++++---- src/Bitmap.hpp | 16 ----- src/P700Printer.cpp | 35 ++++++++++- src/P700Printer.hpp | 13 +++- src/PtouchPrint.cpp | 4 ++ src/Usb.cpp | 101 ------------------------------- src/interface/IPrinterDriver.hpp | 4 +- tests/.gitkeep | 0 10 files changed, 104 insertions(+), 136 deletions(-) delete mode 100644 src/Bitmap.hpp delete mode 100644 src/Usb.cpp delete mode 100644 tests/.gitkeep diff --git a/.gitignore b/.gitignore index 7b016fe..4639553 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ builddir/ .vscode/* !.vscode/c_cpp_properties.json !.vscode/settings.json -!.vscode/launch.json \ No newline at end of file +!.vscode/launch.json +ptouch-print/ \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 53ce6ff..33a2921 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "version": "0.2.0", "configurations": [ { - "name": "(gdb) Starten", + "name": "ptprnt_debug", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/builddir/ptprnt", diff --git a/meson.build b/meson.build index 1298279..98d36ca 100644 --- a/meson.build +++ b/meson.build @@ -4,23 +4,59 @@ project('ptprnt', 'cpp', default_options : ['c_std=c11', 'cpp_std=c++17'] ) -usbdep = dependency('libusb-1.0') -logdep = dependency('spdlog') +usb_dep = dependency('libusb-1.0') +log_dep = dependency('spdlog') +pangocairo_dep = dependency('pangocairo') incdir = include_directories('src') -srcs = [ - 'src/main.cpp', - 'src/PtouchPrint.cpp', - 'src/P700Printer.cpp', - 'src/libusbwrap/UsbDeviceFactory.cpp', - 'src/libusbwrap/UsbDevice.cpp' +ptprnt_hpps = [ + 'src/libusbwrap/interface/IUsbDeviceFactory.hpp', + 'src/libusbwrap/interface/IUsbDevice.hpp', + 'src/libusbwrap/UsbDeviceFactory.hpp', + 'src/libusbwrap/LibUsbTypes.hpp', + 'src/libusbwrap/UsbDevice.hpp', + 'src/interface/IPrinterDriver.hpp', + 'src/interface/IPrinterTypes.hpp', + 'src/P700Printer.hpp', + 'src/PtouchPrint.hpp', + 'src/graphics/Bitmap.hpp', + 'src/graphics/Image.hpp', ] -executable( +ptprnt_srcs = [ + 'src/PtouchPrint.cpp', + 'src/P700Printer.cpp', + 'src/graphics/Image.cpp', + 'src/graphics/Bitmap.cpp', + 'src/libusbwrap/UsbDeviceFactory.cpp', + 'src/libusbwrap/UsbDevice.cpp', +] + +ptprnt_lib = library('ptprnt', + include_directories: incdir, + install: true, + dependencies: [usb_dep, log_dep, pangocairo_dep], + sources: [ptprnt_hpps, ptprnt_srcs]) + +ptprnt_dep = declare_dependency(include_directories: incdir, + link_with: ptprnt_lib) + +ptprnt_debug_exe = executable( 'ptprnt', - srcs, - include_directories : incdir, - dependencies : [usbdep, logdep], + 'src/main.cpp', + install: true, + dependencies : [usb_dep, log_dep, ptprnt_dep], cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'] -) \ No newline at end of file +) + + + +#cmake = import('cmake') +gtest_proj = subproject('gtest') +gtest_dep = gtest_proj.get_variable('gtest_main_dep') +if not gtest_dep.found() + error('MESON_SKIP_TEST: gtest not installed.') +endif + +subdir('tests') \ No newline at end of file diff --git a/src/Bitmap.hpp b/src/Bitmap.hpp deleted file mode 100644 index 9a960ad..0000000 --- a/src/Bitmap.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include - -#include - -namespace ptprnt { - -struct Bitmap { - uint16_t width; - uint16_t height; - uint16_t bitsPerPixel; - std::vector data; -}; - -} // namespace ptprnt \ No newline at end of file diff --git a/src/P700Printer.cpp b/src/P700Printer.cpp index 4e5cba0..13941fa 100644 --- a/src/P700Printer.cpp +++ b/src/P700Printer.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -9,6 +10,7 @@ #include #include +#include "graphics/Image.hpp" #include "libusb.h" #include "libusbwrap/LibUsbTypes.hpp" #include "spdlog/fmt/bin_to_hex.h" @@ -100,11 +102,35 @@ bool P700Printer::detachUsbDevice() { return true; } -bool P700Printer::printBitmap(const Bitmap& bitmap) { +bool P700Printer::printBitmap(const graphics::Bitmap& bitmap) { + auto img = graphics::Image(); + //uint8_t* imgRef = img.getRaw(); + std::array line; + + send(commands["rasterstart"]); + std::vector buf(64); + buf[0] = 0x47; + for (int i = 0; i < 8; i++) { + if (i % 2) { + line.fill(0xffffffff); + } else { + line.fill(0x00000000); + } + + buf[1] = (uint8_t)(8 + 1); + buf[2] = 0; + buf[3] = (uint8_t)(8 - 1); + memcpy(buf.data() + 4, line.data(), line.size()); + send(buf); + } + send(commands["eject"]); return true; } bool P700Printer::printText(const std::string& text, uint16_t fontSize) { + send(commands["lf"]); + send(commands["ff"]); + send(commands["eject"]); return true; } @@ -121,4 +147,11 @@ bool P700Printer::send(std::vector& data) { return true; } + +bool P700Printer::init() { + std::vector cmd(102); + cmd[100] = 0x1b; /* ESC */ + cmd[101] = 0x40; /* @ */ + return send(cmd); +} } // namespace ptprnt::printer \ No newline at end of file diff --git a/src/P700Printer.hpp b/src/P700Printer.hpp index 66222dc..34f09f8 100644 --- a/src/P700Printer.hpp +++ b/src/P700Printer.hpp @@ -1,6 +1,8 @@ #include +#include #include +#include #include "interface/IPrinterDriver.hpp" #include "interface/IPrinterTypes.hpp" @@ -29,11 +31,12 @@ class P700Printer : public ::ptprnt::IPrinterDriver { const PrinterStatus getPrinterStatus() override; bool attachUsbDevice(std::shared_ptr usbHndl) override; bool detachUsbDevice() override; - bool printBitmap(const Bitmap& bitmap) override; + bool printBitmap(const graphics::Bitmap& bitmap) override; bool printText(const std::string& text, uint16_t fontSize) override; private: bool send(std::vector& data); + bool init(); std::shared_ptr mUsbHndl{nullptr}; @@ -42,6 +45,14 @@ class P700Printer : public ::ptprnt::IPrinterDriver { .version = "v1.0", .vid = 0x04f9, .pid = 0x2061}; + std::map> commands{ + {"rasterstart", {0x1b, 0x69, 0x52, 0x01}}, + {"info", {0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, + {"packbitson", {0x02}}, + {"lf", {0x5a}}, + {"ff", {0x0c}}, + {"eject", {0x1a}}, + }; }; } // namespace ptprnt::printer \ No newline at end of file diff --git a/src/PtouchPrint.cpp b/src/PtouchPrint.cpp index a69a103..a2c8859 100644 --- a/src/PtouchPrint.cpp +++ b/src/PtouchPrint.cpp @@ -3,6 +3,7 @@ #include #include "P700Printer.hpp" +#include "graphics/Bitmap.hpp" #include "libusbwrap/UsbDeviceFactory.hpp" PtouchPrint::PtouchPrint() {} @@ -35,6 +36,9 @@ void PtouchPrint::run() { printer->attachUsbDevice(devices[0]); auto status = printer->getPrinterStatus(); spdlog::info("Detected tape width is {}mm", status.tapeWidthMm); + auto bm = ptprnt::graphics::Bitmap(512, 128); + printer->printBitmap(bm); + //printer->printText("wurst", 1); } unsigned int PtouchPrint::getCompatiblePrinters() { diff --git a/src/Usb.cpp b/src/Usb.cpp deleted file mode 100644 index af4fff1..0000000 --- a/src/Usb.cpp +++ /dev/null @@ -1,101 +0,0 @@ -#include "Usb.hpp" - -#include - -#include -#include -#include -#include - -#include "UsbTypes.hpp" -#include "libusb.h" - -namespace ptprnt::driver { - -Usb::Usb() { - spdlog::debug("Usb constructing"); - libusb_init(NULL); -} - -Usb::~Usb() { - spdlog::debug("Usb destructing"); -} - -std::optional> Usb::getDevices() { - libusb_device* libUsbDev; - struct libusb_device_descriptor libUsbDesc; - int ret, i = 0; - - mDevices.clear(); - - if (0 == (ret = libusb_get_device_list(NULL, &mLibUsbDevs))) { - spdlog::error("Could not find any USB devices: {}", libusb_error_name(ret)); - return std::nullopt; - } - - while ((libUsbDev = mLibUsbDevs[i++]) != NULL) { - if ((ret = libusb_get_device_descriptor(libUsbDev, &libUsbDesc)) < 0) { - spdlog::error("failed to get device"); - return std::nullopt; - } - - UsbDevice newDev{.vendorId = libUsbDesc.idVendor, - .productId = libUsbDesc.idProduct, - .device = libUsbDev, - .hndl = nullptr}; // handle is only available after we opened the dev - - mDevices.push_back(newDev); - } - - return mDevices; -} - -std::optional Usb::open(UsbDevice dev) { - libusb_device_handle* libUsbHandle = nullptr; - int ret = 0; - - if ((ret = libusb_open(dev.device, &libUsbHandle) != 0)) { - spdlog::error("Could not open device {0:04X}:{1:04X}: {2}", dev.vendorId, dev.productId, - libusb_error_name(ret)); - return std::nullopt; - } - - // detach the device from the kernel if it is active - if ((ret = libusb_kernel_driver_active(libUsbHandle, 0)) == 1) { - if ((ret = libusb_detach_kernel_driver(libUsbHandle, 0)) != 0) { - spdlog::error( - "Could not detach kernel driver for device {0:04X}:{1:04X}: " - "{2}", - dev.vendorId, dev.productId, libusb_error_name(ret)); - return std::nullopt; - } - } - - // claim interface - if ((ret = libusb_claim_interface(libUsbHandle, 0)) != 0) { - spdlog::error( - "Could not claim interface {0:04X}:{1:04X}: " - "{2}", - dev.vendorId, dev.productId, libusb_error_name(ret)); - } - - dev.hndl = libUsbHandle; - - return dev; -} - -bool Usb::close(UsbDevice dev) { - int ret = 0; - if (0 != (ret = libusb_release_interface(dev.hndl, 0))) { - spdlog::error( - "Could not close USB device {0:04X}:{1:04X}: " - "{2}", - dev.vendorId, dev.productId, libusb_error_name(ret)); - } - - spdlog::debug("test"); - libusb_close(dev.hndl); - return true; -} - -} // namespace ptprnt::driver \ No newline at end of file diff --git a/src/interface/IPrinterDriver.hpp b/src/interface/IPrinterDriver.hpp index 33838de..0fcede6 100644 --- a/src/interface/IPrinterDriver.hpp +++ b/src/interface/IPrinterDriver.hpp @@ -3,7 +3,7 @@ #include #include -#include "Bitmap.hpp" +#include "graphics/Bitmap.hpp" #include "interface/IPrinterTypes.hpp" #include "libusbwrap/interface/IUsbDevice.hpp" @@ -20,7 +20,7 @@ class IPrinterDriver { virtual const PrinterStatus getPrinterStatus() = 0; virtual bool attachUsbDevice(std::shared_ptr usbHndl) = 0; virtual bool detachUsbDevice() = 0; - virtual bool printBitmap(const Bitmap& bitmap) = 0; + virtual bool printBitmap(const graphics::Bitmap& bitmap) = 0; virtual bool printText(const std::string& text, uint16_t fontSize) = 0; }; diff --git a/tests/.gitkeep b/tests/.gitkeep deleted file mode 100644 index e69de29..0000000