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 c0811df4bb
18 changed files with 334 additions and 136 deletions

3
.gitignore vendored
View File

@@ -1,6 +1,9 @@
builddir/
subprojects/*
!subprojects/*.wrap
.cache/
.vscode/*
!.vscode/c_cpp_properties.json
!.vscode/settings.json
!.vscode/launch.json
ptouch-print/

2
.vscode/launch.json vendored
View File

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

View File

@@ -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,
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,
dependencies : [usbdep, logdep],
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',
'src/main.cpp',
install: true,
dependencies : [usb_dep, log_dep, ptprnt_dep],
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 <array>
#include <chrono>
#include <cstdint>
#include <iostream>
@@ -9,6 +10,7 @@
#include <thread>
#include <vector>
#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<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;
}
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<uint8_t>& data) {
return true;
}
bool P700Printer::init() {
std::vector<uint8_t> cmd(102);
cmd[100] = 0x1b; /* ESC */
cmd[101] = 0x40; /* @ */
return send(cmd);
}
} // namespace ptprnt::printer

View File

@@ -1,6 +1,8 @@
#include <sys/types.h>
#include <map>
#include <memory>
#include <vector>
#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<libusbwrap::IUsbDevice> usbHndl) 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;
private:
bool send(std::vector<uint8_t>& data);
bool init();
std::shared_ptr<libusbwrap::IUsbDevice> mUsbHndl{nullptr};
@@ -42,6 +45,14 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
.version = "v1.0",
.vid = 0x04f9,
.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

View File

@@ -3,6 +3,7 @@
#include <spdlog/spdlog.h>
#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<ptprnt::graphics::ALPHA8>(512, 128);
printer->printBitmap(bm);
//printer->printText("wurst", 1);
}
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

55
src/graphics/Bitmap.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include "Bitmap.hpp"
#include <algorithm>
#include <iterator>
#include <optional>
#include <vector>
namespace ptprnt::graphics {
template <class T>
Bitmap<T>::Bitmap(uint16_t width, uint16_t height)
: mWidth{width}, mHeight{height}, mPixels(width * height) {}
template <class T>
uint16_t Bitmap<T>::getWidth() {
return mWidth;
}
template <class T>
uint16_t Bitmap<T>::getHeight() {
return mHeight;
}
template <class T>
std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
if (line >= mHeight) {
// out of bound
return std::nullopt;
}
auto lineStart = mPixels.begin() + (line * mWidth);
auto lineEnd = mPixels.begin() + ((line + 1) * mWidth);
return std::vector<T>(lineStart, lineEnd);
}
template <class T>
std::optional<std::vector<T>> Bitmap<T>::getRow(uint16_t row) {
if (row >= mWidth) {
// out of bound
return std::nullopt;
}
// first pixel is always beginning of the row
std::vector<T> rowPixels(mHeight);
auto it = mPixels.begin() + row;
for (auto& rowElement : rowPixels) {
rowElement = *it;
it += mWidth;
}
return rowPixels;
}
} // namespace ptprnt::graphics

37
src/graphics/Bitmap.hpp Normal file
View File

@@ -0,0 +1,37 @@
#pragma once
#include <stdint.h>
#include <cstdint>
#include <optional>
#include <vector>
namespace ptprnt::graphics {
typedef uint8_t ALPHA8; // Alpha only, 8 bit per pixel
typedef uint32_t RGBX8; // RGB, least significant byte unused, 8 bit per channel
typedef uint32_t RGBA8; // RGB, least significant byte alpha, 8 bit per channel
typedef uint32_t ARGB8; // RGB, most significant byte alpha, 8 bit per channel
template <class T>
class Bitmap {
public:
Bitmap(uint16_t width, uint16_t height);
~Bitmap() = default;
uint16_t getWidth();
uint16_t getHeight();
std::optional<std::vector<T>> getLine(uint16_t line);
std::optional<std::vector<T>> getRow(uint16_t row);
private:
uint16_t mWidth;
uint16_t mHeight;
std::vector<T> mPixels;
};
// force compiler to generate class for type ALPHA8 and RGBX8
template class Bitmap<ALPHA8>;
template class Bitmap<RGBX8>;
} // namespace ptprnt::graphics

33
src/graphics/Image.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include "Image.hpp"
#include "pango/pango-font.h"
namespace ptprnt::graphics {
Image::Image() {
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A1, 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);
mLayout = pango_cairo_create_layout(cr);
pango_layout_set_font_description(mLayout, mFontDescription);
pango_layout_set_text(mLayout, "Hello, world", -1);
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_move_to(cr, 10.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() {
return cairo_image_surface_get_data(mSurface);
}
Image::~Image() {
g_object_unref(mLayout);
pango_font_description_free(mFontDescription);
}
} // namespace ptprnt::graphics

17
src/graphics/Image.hpp Normal file
View File

@@ -0,0 +1,17 @@
#include <pango/pangocairo.h>
#include <cstdint>
namespace ptprnt::graphics {
class Image {
public:
Image();
~Image();
uint8_t* getRaw();
private:
PangoLayout* mLayout;
PangoFontDescription* mFontDescription;
cairo_surface_t* mSurface;
};
} // namespace ptprnt::graphics

View File

@@ -3,7 +3,7 @@
#include <memory>
#include <string_view>
#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<libusbwrap::IUsbDevice> usbHndl) = 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;
};

16
subprojects/gtest.wrap Normal file
View File

@@ -0,0 +1,16 @@
[wrap-file]
directory = googletest-1.14.0
source_url = https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz
source_filename = gtest-1.14.0.tar.gz
source_hash = 8ad598c73ad796e0d8280b082cebd82a630d73e73cd3c70057938a6501bba5d7
patch_filename = gtest_1.14.0-1_patch.zip
patch_url = https://wrapdb.mesonbuild.com/v2/gtest_1.14.0-1/get_patch
patch_hash = 2e693c7d3f9370a7aa6dac802bada0874d3198ad4cfdf75647b818f691182b50
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/gtest_1.14.0-1/gtest-1.14.0.tar.gz
wrapdb_version = 1.14.0-1
[provide]
gtest = gtest_dep
gtest_main = gtest_main_dep
gmock = gmock_dep
gmock_main = gmock_main_dep

View File

View File

@@ -0,0 +1,47 @@
#include "graphics/Bitmap.hpp"
#include <gtest/gtest.h>
#include <optional>
TEST(basic_test, Bitmap_createBitmapWithCertainSize_yieldsSpecifiedSize) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
auto width = bm.getWidth();
auto height = bm.getHeight();
ASSERT_EQ(width, 16);
ASSERT_EQ(height, 8);
}
TEST(basic_test, Bitmap_getBitmapLineOutsideOfImage_yieldsNullopt) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
// line 8 is out of bounds, count begins with 0
auto outOfBoundsLine = bm.getLine(8);
ASSERT_EQ(std::nullopt, outOfBoundsLine);
}
TEST(basic_test, Bitmap_getBitmapLineInsideOfImage_yieldsValidLineSize) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
auto line = bm.getLine(7);
if (!line) {
FAIL() << "Returned line is invalid";
}
auto lineSize = line->size();
ASSERT_EQ(16, lineSize);
}
TEST(basic_test, Bitmap_getBitmapRowOutsideOfImage_yieldsNullopt) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
// row 16 is out of bounds, count begins with 0
auto outOfBoundsRow = bm.getRow(16);
ASSERT_EQ(std::nullopt, outOfBoundsRow);
}
TEST(basic_test, Bitmap_getBitmapRowInsideOfImage_yieldsValidRowSize) {
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
auto row = bm.getRow(15);
if (!row) {
FAIL() << "Returned Row is invalid";
}
auto rowSize = row->size();
ASSERT_EQ(8, rowSize);
}

View File

@@ -0,0 +1,9 @@
#include "graphics/Image.hpp"
#include <gtest/gtest.h>
#include <optional>
TEST(basic_test, Image_smokeTest_succeeds) {
auto im = ptprnt::graphics::Image();
}

14
tests/meson.build Normal file
View File

@@ -0,0 +1,14 @@
tests = [['bitmap_test', 'bitmap_test_exe', ['bitmap_test/bitmap_test.cpp']],
['image_test', 'image_test_exe', ['image_test/image_test.cpp']]
]
foreach test : tests
test(test.get(0),
executable(test.get(1),
sources: test.get(2),
include_directories: incdir,
link_with:[ptprnt_lib],
dependencies: [gtest_dep, pangocairo_dep]
)
)
endforeach