Moved graphics classes into own namespace, added unit testing with gtest
This commit is contained in:
@@ -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
|
@@ -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
|
@@ -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
|
@@ -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() {
|
||||
|
101
src/Usb.cpp
101
src/Usb.cpp
@@ -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
55
src/graphics/Bitmap.cpp
Normal 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
37
src/graphics/Bitmap.hpp
Normal 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
33
src/graphics/Image.cpp
Normal 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
17
src/graphics/Image.hpp
Normal 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
|
@@ -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;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user