Moved graphics classes into own namespace, added unit testing with gtest

This commit is contained in:
2023-09-23 13:29:54 +02:00
parent 7ab817793d
commit 373debd809
10 changed files with 104 additions and 136 deletions

1
.gitignore vendored
View File

@@ -4,3 +4,4 @@ builddir/
!.vscode/c_cpp_properties.json !.vscode/c_cpp_properties.json
!.vscode/settings.json !.vscode/settings.json
!.vscode/launch.json !.vscode/launch.json
ptouch-print/

2
.vscode/launch.json vendored
View File

@@ -5,7 +5,7 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "(gdb) Starten", "name": "ptprnt_debug",
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/builddir/ptprnt", "program": "${workspaceFolder}/builddir/ptprnt",

View File

@@ -4,23 +4,59 @@ project('ptprnt', 'cpp',
default_options : ['c_std=c11', 'cpp_std=c++17'] default_options : ['c_std=c11', 'cpp_std=c++17']
) )
usbdep = dependency('libusb-1.0') usb_dep = dependency('libusb-1.0')
logdep = dependency('spdlog') log_dep = dependency('spdlog')
pangocairo_dep = dependency('pangocairo')
incdir = include_directories('src') incdir = include_directories('src')
srcs = [ ptprnt_hpps = [
'src/main.cpp', 'src/libusbwrap/interface/IUsbDeviceFactory.hpp',
'src/PtouchPrint.cpp', 'src/libusbwrap/interface/IUsbDevice.hpp',
'src/P700Printer.cpp', 'src/libusbwrap/UsbDeviceFactory.hpp',
'src/libusbwrap/UsbDeviceFactory.cpp', 'src/libusbwrap/LibUsbTypes.hpp',
'src/libusbwrap/UsbDevice.cpp' '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', 'ptprnt',
srcs, 'src/main.cpp',
include_directories : incdir, install: true,
dependencies : [usbdep, logdep], dependencies : [usb_dep, log_dep, ptprnt_dep],
cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"'] cpp_args : ['-DPROJ_VERSION="'+meson.project_version()+'"']
) )
#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')

View File

@@ -1,16 +0,0 @@
#pragma once
#include <stdint.h>
#include <vector>
namespace ptprnt {
struct Bitmap {
uint16_t width;
uint16_t height;
uint16_t bitsPerPixel;
std::vector<uint8_t> data;
};
} // namespace ptprnt

View File

@@ -2,6 +2,7 @@
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <array>
#include <chrono> #include <chrono>
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>
@@ -9,6 +10,7 @@
#include <thread> #include <thread>
#include <vector> #include <vector>
#include "graphics/Image.hpp"
#include "libusb.h" #include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp" #include "libusbwrap/LibUsbTypes.hpp"
#include "spdlog/fmt/bin_to_hex.h" #include "spdlog/fmt/bin_to_hex.h"
@@ -100,11 +102,35 @@ bool P700Printer::detachUsbDevice() {
return true; return true;
} }
bool P700Printer::printBitmap(const Bitmap& bitmap) { bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) {
auto img = graphics::Image();
//uint8_t* imgRef = img.getRaw();
std::array<uint32_t, 4> line;
send(commands["rasterstart"]);
std::vector<uint8_t> 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; return true;
} }
bool P700Printer::printText(const std::string& text, uint16_t fontSize) { bool P700Printer::printText(const std::string& text, uint16_t fontSize) {
send(commands["lf"]);
send(commands["ff"]);
send(commands["eject"]);
return true; return true;
} }
@@ -121,4 +147,11 @@ bool P700Printer::send(std::vector<uint8_t>& data) {
return true; return true;
} }
bool P700Printer::init() {
std::vector<uint8_t> cmd(102);
cmd[100] = 0x1b; /* ESC */
cmd[101] = 0x40; /* @ */
return send(cmd);
}
} // namespace ptprnt::printer } // namespace ptprnt::printer

View File

@@ -1,6 +1,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <map>
#include <memory> #include <memory>
#include <vector>
#include "interface/IPrinterDriver.hpp" #include "interface/IPrinterDriver.hpp"
#include "interface/IPrinterTypes.hpp" #include "interface/IPrinterTypes.hpp"
@@ -29,11 +31,12 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
const PrinterStatus getPrinterStatus() 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 printBitmap(const Bitmap& bitmap) override; bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) override;
bool printText(const std::string& text, uint16_t fontSize) 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();
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr}; std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
@@ -42,6 +45,14 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
.version = "v1.0", .version = "v1.0",
.vid = 0x04f9, .vid = 0x04f9,
.pid = 0x2061}; .pid = 0x2061};
std::map<std::string, std::vector<uint8_t>> 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 } // namespace ptprnt::printer

View File

@@ -3,6 +3,7 @@
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include "P700Printer.hpp" #include "P700Printer.hpp"
#include "graphics/Bitmap.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp" #include "libusbwrap/UsbDeviceFactory.hpp"
PtouchPrint::PtouchPrint() {} PtouchPrint::PtouchPrint() {}
@@ -35,6 +36,9 @@ void PtouchPrint::run() {
printer->attachUsbDevice(devices[0]); printer->attachUsbDevice(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);
printer->printBitmap(bm);
//printer->printText("wurst", 1);
} }
unsigned int PtouchPrint::getCompatiblePrinters() { unsigned int PtouchPrint::getCompatiblePrinters() {

View File

@@ -1,101 +0,0 @@
#include "Usb.hpp"
#include <spdlog/spdlog.h>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <optional>
#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<std::vector<UsbDevice>> 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<UsbDevice> 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

View File

@@ -3,7 +3,7 @@
#include <memory> #include <memory>
#include <string_view> #include <string_view>
#include "Bitmap.hpp" #include "graphics/Bitmap.hpp"
#include "interface/IPrinterTypes.hpp" #include "interface/IPrinterTypes.hpp"
#include "libusbwrap/interface/IUsbDevice.hpp" #include "libusbwrap/interface/IUsbDevice.hpp"
@@ -20,7 +20,7 @@ class IPrinterDriver {
virtual const PrinterStatus getPrinterStatus() = 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 printBitmap(const Bitmap& bitmap) = 0; virtual bool printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) = 0;
virtual bool printText(const std::string& text, uint16_t fontSize) = 0; virtual bool printText(const std::string& text, uint16_t fontSize) = 0;
}; };

View File