Got it almost working...

This commit is contained in:
2023-12-03 22:08:59 +01:00
parent b2971d15dc
commit 4f5af6722e
14 changed files with 143 additions and 78 deletions

View File

@@ -85,5 +85,6 @@
}, },
"clang-tidy.buildPath": "builddir/", "clang-tidy.buildPath": "builddir/",
"clangd.onConfigChanged": "restart", "clangd.onConfigChanged": "restart",
"C_Cpp.default.compileCommands": "./builddir/compile_commands.json" "C_Cpp.default.compileCommands": "/home/moritz/Projekte/ptouch-prnt/builddir/compile_commands.json",
"gcovViewer.buildDirectories": ["/home/moritz/Projekte/ptouch-prnt/builddir"]
} }

View File

@@ -2,6 +2,7 @@
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
HTML_COV_PATH="coverageReport/html" HTML_COV_PATH="coverageReport/html"
XML_COV_PATH="coverageReport/xml"
HTML_START_FILE="index.html" HTML_START_FILE="index.html"
echo "Generating Coverage report for ptouch-prnt" echo "Generating Coverage report for ptouch-prnt"
@@ -12,10 +13,15 @@ ninja -C builddir test
mkdir -p ${HTML_COV_PATH} mkdir -p ${HTML_COV_PATH}
gcovr --html --html-details --html-syntax-highlighting --filter src --output ${HTML_COV_PATH}/${HTML_START_FILE} gcovr --html --html-details --html-syntax-highlighting --filter src --output ${HTML_COV_PATH}/${HTML_START_FILE}
mkdir -p ${XML_COV_PATH}
gcovr --xml-pretty --filter src --output ${XML_COV_PATH}/cov.xml
if [ $? ] if [ $? ]
then then
echo "Coverage report successful generated!" echo "Coverage report successful generated!"
echo "Open: file://${SCRIPT_PATH}/${HTML_COV_PATH}/${HTML_START_FILE}" echo "Open: file://${SCRIPT_PATH}/${HTML_COV_PATH}/${HTML_START_FILE}"
else else
echo "Error generating coverage report!" echo "Error generating coverage report!"
fi fi
rm *.gcov

View File

@@ -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', 'b_sanitize=address,undefined', 'b_lto=true', 'b_lto_mode=thin', 'b_thinlto_cache=true'] default_options : ['c_std=c11', 'cpp_std=c++17', 'b_sanitize=none', 'b_lto=true', 'b_lto_mode=thin', 'b_thinlto_cache=true']
) )
usb_dep = dependency('libusb-1.0') usb_dep = dependency('libusb-1.0')

View File

@@ -122,21 +122,23 @@ 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); #ifdef DRYRUN
{ SPDLOG_DEBUG("DRYRUN enabled");
auto img = graphics::Label(); for (unsigned int lineNo = 0; lineNo < bitmap.getHeight(); lineNo++) {
bm.setPixels(std::vector<uint8_t>(img.getRaw(), img.getRaw() + 512 * 128)); auto line = bitmap.getLine(lineNo);
auto monoLine = graphics::Monochrome(*line);
monoLine.visualize();
} }
#endif
send(commands["rasterstart"]); send(commands["rasterstart"]);
std::vector<uint8_t> rastercmd(4); std::vector<uint8_t> rastercmd(4);
rastercmd[0] = 0x47; rastercmd[0] = 0x47;
rastercmd[1] = 0x00; // size +1 rastercmd[1] = 0x00; // size +1
rastercmd[2] = 0x00; rastercmd[2] = 0x00;
rastercmd[3] = 0x00; // size -1 rastercmd[3] = 0x00; // size -1
for (unsigned int i = 0; i < bm.getWidth(); i++) { for (unsigned int i = 0; i < bitmap.getWidth(); i++) {
auto bmcol = bm.getCol(i); auto bmcol = bitmap.getCol(i);
if (!bmcol) { if (!bmcol) {
spdlog::error("Out of bounds bitmap access"); spdlog::error("Out of bounds bitmap access");
break; break;
@@ -151,6 +153,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
buf[1] = col.size() + 1; buf[1] = col.size() + 1;
buf[3] = col.size() - 1; buf[3] = col.size() - 1;
if (!send(buf)) { if (!send(buf)) {
spdlog::error("Error sending buffer to printer");
break; break;
}; };
} }
@@ -182,11 +185,11 @@ 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::info("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())) {
spdlog::error("Could not transfer all data via USB bulk transfer. Only send {} of {} bytes", spdlog::error("Could not transfer all data via USB bulk transfer. Only sent {} of {} bytes",
tx, data.size()); tx, data.size());
return false; return false;
} }

View File

@@ -17,6 +17,7 @@
*/ */
#include <spdlog/spdlog.h>
#include <sys/types.h> #include <sys/types.h>
#include <map> #include <map>
@@ -67,7 +68,8 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
.name = "Brother P-touch P700", .name = "Brother P-touch P700",
.version = "v1.0", .version = "v1.0",
.vid = 0x04f9, .vid = 0x04f9,
.pid = 0x2061}; .pid = 0x2061,
.pixelLines = 128};
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,

View File

@@ -16,12 +16,13 @@
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>
#include "graphics/Monochrome.hpp"
#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>
@@ -38,6 +39,7 @@
#include "CLI/Option.hpp" #include "CLI/Option.hpp"
#include "P700Printer.hpp" #include "P700Printer.hpp"
#include "PtouchPrint.hpp"
#include "graphics/Bitmap.hpp" #include "graphics/Bitmap.hpp"
#include "graphics/Label.hpp" #include "graphics/Label.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp" #include "libusbwrap/UsbDeviceFactory.hpp"
@@ -93,9 +95,6 @@ int PtouchPrint::run() {
printer->attachUsbDevice(std::move(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);
//printer->printBitmap(bm);
//printer->printText("wurst", 1);
if (0 == mCommands.size()) { if (0 == mCommands.size()) {
spdlog::warn("No command specified, nothing to do..."); spdlog::warn("No command specified, nothing to do...");
@@ -106,7 +105,15 @@ int PtouchPrint::run() {
SPDLOG_DEBUG("Command: {}", cmd.second); SPDLOG_DEBUG("Command: {}", cmd.second);
if (cmd.first == CliCmdType::Text) { if (cmd.first == CliCmdType::Text) {
auto label{graphics::Label()}; auto label{graphics::Label()};
label.createLabel(cmd.second); label.create(cmd.second, printer->getPrinterInfo().pixelLines);
auto bm = graphics::Bitmap<graphics::ALPHA8>(label.getLayoutWidth(),
printer->getPrinterInfo().pixelLines);
if (!bm.setPixels(label.getRaw())) {
spdlog::error("Non-matching bitmap size");
return -1;
}
label.writeToPng("salat.png");
printer->printBitmap(bm);
} }
} }

View File

@@ -22,6 +22,7 @@
#include <algorithm> #include <algorithm>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <iostream>
#include <iterator> #include <iterator>
#include <optional> #include <optional>
#include <vector> #include <vector>
@@ -32,19 +33,20 @@ Bitmap<T>::Bitmap(uint16_t width, uint16_t height)
: mWidth{width}, mHeight{height}, mPixels(width * height) {} : mWidth{width}, mHeight{height}, mPixels(width * height) {}
template <class T> template <class T>
uint16_t Bitmap<T>::getWidth() { [[nodiscard]] uint16_t Bitmap<T>::getWidth() const {
return mWidth; return mWidth;
} }
template <class T> template <class T>
uint16_t Bitmap<T>::getHeight() { [[nodiscard]] uint16_t Bitmap<T>::getHeight() const {
return mHeight; return mHeight;
} }
template <class T> template <class T>
bool Bitmap<T>::setPixels(const std::vector<T>& pixels) { bool Bitmap<T>::setPixels(const std::vector<T>& pixels) {
if (pixels.size() != mPixels.size()) { if (pixels.size() != mPixels.size()) {
spdlog::error("Invalid pixel buffer size."); spdlog::error("Invalid pixel buffer size (got {} vs. {} bitmap size).", pixels.size(),
mPixels.size());
return false; return false;
} }
@@ -53,12 +55,12 @@ bool Bitmap<T>::setPixels(const std::vector<T>& pixels) {
} }
template <class T> template <class T>
std::vector<T> Bitmap<T>::getPixelsCpy() { [[nodiscard]] std::vector<T> Bitmap<T>::getPixelsCpy() const {
return mPixels; return mPixels;
} }
template <class T> template <class T>
std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) { [[nodiscard]] std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) const {
if (line >= mHeight) { if (line >= mHeight) {
// out of bound // out of bound
return std::nullopt; return std::nullopt;
@@ -70,7 +72,7 @@ std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
} }
template <class T> template <class T>
std::optional<std::vector<T>> Bitmap<T>::getCol(uint16_t col) { [[nodiscard]] std::optional<std::vector<T>> Bitmap<T>::getCol(uint16_t col) const {
if (col >= mWidth) { if (col >= mWidth) {
// out of bound // out of bound
return std::nullopt; return std::nullopt;

View File

@@ -39,12 +39,18 @@ class Bitmap {
Bitmap(uint16_t width, uint16_t height); Bitmap(uint16_t width, uint16_t height);
~Bitmap() = default; ~Bitmap() = default;
uint16_t getWidth(); Bitmap(const Bitmap&) = default;
uint16_t getHeight(); Bitmap& operator=(const Bitmap&) = default;
Bitmap(Bitmap&&) = default;
Bitmap& operator=(Bitmap&&) = default;
[[nodiscard]] uint16_t getWidth() const;
[[nodiscard]] uint16_t getHeight() const;
bool setPixels(const std::vector<T>& pixels); bool setPixels(const std::vector<T>& pixels);
std::vector<T> getPixelsCpy(); [[nodiscard]] std::vector<T> getPixelsCpy() const;
std::optional<std::vector<T>> getLine(uint16_t line); [[nodiscard]] std::optional<std::vector<T>> getLine(uint16_t line) const;
std::optional<std::vector<T>> getCol(uint16_t col); [[nodiscard]] std::optional<std::vector<T>> getCol(uint16_t col) const;
void visualize() const;
private: private:
uint16_t mWidth; uint16_t mWidth;

View File

@@ -22,9 +22,11 @@
#include <algorithm> #include <algorithm>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <cassert>
#include <cstddef> #include <cstddef>
#include <iostream> // remove me #include <iostream> // remove me
#include <string> #include <string>
#include <vector>
#include "cairo.h" #include "cairo.h"
#include "pango/pango-context.h" #include "pango/pango-context.h"
@@ -35,67 +37,69 @@
#include "pango/pangocairo.h" #include "pango/pangocairo.h"
namespace ptprnt::graphics { namespace ptprnt::graphics {
Label::Label() { Label::Label()
/*mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, 512, 128); : mPangoCtx(pango_font_map_create_context(pango_cairo_font_map_get_default())),
cairo_t* cr = cairo_create(mSurface); mPangoLyt(pango_layout_new(mPangoCtx)),
mFontDescription = pango_font_description_new(); mPangoFontDesc(pango_font_description_from_string("Noto sans 32")) {}
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 💩"); std::vector<uint8_t> Label::getRaw() {
assert(mSurface != nullptr);
size_t len = mPrinterHeight * mLayoutWidth;
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); cairo_surface_flush(mSurface);
return cairo_image_surface_get_data(mSurface); auto data = cairo_image_surface_get_data(mSurface);
return {data, data + len};
} }
uint8_t Label::getNumLines(std::string_view strv) { uint8_t Label::getNumLines(std::string_view strv) {
return std::count(strv.begin(), strv.end(), '\n'); return std::count(strv.begin(), strv.end(), '\n');
} }
void Label::createLabel(const std::string& str) { int Label::getLayoutHeight() {
return mLayoutHeight;
}
// create Pango layout first, to get the render dimensions int Label::getLayoutWidth() {
auto pangoCtx{pango_font_map_create_context(pango_cairo_font_map_get_default())}; return mLayoutWidth;
auto pangoLyt{pango_layout_new(pangoCtx)}; }
auto pangoFontDesc{pango_font_description_from_string("Noto sans 32")};
pango_layout_set_single_paragraph_mode(pangoLyt, true); void Label::create(const std::string& labelText, const uint16_t heightPixel) {
pango_layout_set_height(pangoLyt, getNumLines(str) * -1); mPrinterHeight = heightPixel;
pango_layout_set_alignment(pangoLyt, PANGO_ALIGN_CENTER); pango_layout_set_single_paragraph_mode(mPangoLyt, true);
pango_layout_set_font_description(pangoLyt, pangoFontDesc); pango_layout_set_height(mPangoLyt, getNumLines(labelText) * -1);
pango_context_load_font(pangoCtx, pangoFontDesc); pango_layout_set_alignment(mPangoLyt, PANGO_ALIGN_CENTER);
pango_layout_set_text(pangoLyt, str.c_str(), static_cast<int>(str.length())); pango_layout_set_font_description(mPangoLyt, mPangoFontDesc);
pango_context_load_font(mPangoCtx, mPangoFontDesc);
pango_layout_set_text(mPangoLyt, labelText.c_str(), static_cast<int>(labelText.length()));
int width = 0, height = 0; pango_layout_get_size(mPangoLyt, &mLayoutWidth, &mLayoutHeight);
pango_layout_get_size(pangoLyt, &width, &height); mLayoutWidth /= PANGO_SCALE;
mLayoutHeight /= PANGO_SCALE;
SPDLOG_DEBUG("Layout width: {}, height: {}", width / PANGO_SCALE, height / PANGO_SCALE); SPDLOG_DEBUG("Layout width: {}, height: {}", mLayoutWidth, mLayoutHeight);
auto surf = cairo_image_surface_create(CAIRO_FORMAT_A8, width / PANGO_SCALE, 128); mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, mLayoutWidth, mPrinterHeight);
cairo_t* cr = cairo_create(surf); cairo_t* cr = cairo_create(mSurface);
pango_cairo_show_layout(cr, pangoLyt); pango_cairo_show_layout(cr, mPangoLyt);
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_surface_flush(mSurface);
cairo_destroy(cr);
}
// Debug only void Label::writeToPng(const std::string& file) {
cairo_surface_write_to_png(surf, "hellow.png"); if (mSurface) {
cairo_surface_flush(mSurface);
cairo_surface_write_to_png(mSurface, file.c_str());
}
} }
Label::~Label() { Label::~Label() {
SPDLOG_DEBUG("Image dtor..."); SPDLOG_DEBUG("Image dtor...");
pango_font_description_free(mPangoFontDesc);
g_object_unref(mPangoCtx);
g_object_unref(mPangoLyt);
cairo_surface_destroy(mSurface);
} }
} // namespace ptprnt::graphics } // namespace ptprnt::graphics

View File

@@ -23,6 +23,11 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <vector>
#include "cairo.h"
#include "pango/pango-font.h"
#include "pango/pango-types.h"
namespace ptprnt::graphics { namespace ptprnt::graphics {
@@ -39,15 +44,21 @@ class Label {
Label(Label&&) = delete; Label(Label&&) = delete;
Label& operator=(Label&&) = delete; Label& operator=(Label&&) = delete;
uint8_t* getRaw(); std::vector<uint8_t> getRaw();
void createLabel(const std::string& labelText); void create(const std::string& labelText, const uint16_t heightPixel);
void writeToPng(const std::string& file);
int getLayoutWidth();
int getLayoutHeight();
private: private:
// methods // methods
uint8_t getNumLines(std::string_view str); uint8_t getNumLines(std::string_view str);
// members // members
PangoLayout* mLayout; PangoContext* mPangoCtx{nullptr};
PangoFontDescription* mFontDescription; PangoLayout* mPangoLyt{nullptr};
cairo_surface_t* mSurface; PangoFontDescription* mPangoFontDesc{nullptr};
cairo_surface_t* mSurface{nullptr};
int mLayoutWidth = 0, mLayoutHeight = 0;
int mPrinterHeight = 0;
}; };
} // namespace ptprnt::graphics } // namespace ptprnt::graphics

View File

@@ -21,7 +21,8 @@
#include <cmath> #include <cmath>
#include <cstdint> #include <cstdio>
#include <cstdlib>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
@@ -57,4 +58,20 @@ std::vector<uint8_t> Monochrome::get() {
} }
return outPixels; return outPixels;
} }
void Monochrome::visualize() {
auto mono = get();
for (unsigned char pix : mono) {
std::cout << ((pix & (1 << 7)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 6)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 5)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 4)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 3)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 2)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 1)) == 0 ? "." : "x");
std::cout << ((pix & (1 << 0)) == 0 ? "." : "x");
}
std::cout << std::endl;
}
} // namespace ptprnt::graphics } // namespace ptprnt::graphics

View File

@@ -31,11 +31,12 @@ class Monochrome {
void setThreshold(uint8_t); void setThreshold(uint8_t);
void invert(bool shouldInvert); void invert(bool shouldInvert);
void visualize();
std::vector<uint8_t> get(); std::vector<uint8_t> get();
private: private:
const std::vector<uint8_t>& mPixels; const std::vector<uint8_t>& mPixels;
uint8_t mThreshhold = 127; uint8_t mThreshhold = UINT8_MAX / 2;
bool mShouldInvert = false; bool mShouldInvert = false;
}; };
} // namespace ptprnt::graphics } // namespace ptprnt::graphics

View File

@@ -30,6 +30,7 @@ struct PrinterInfo {
std::string_view version = ""; std::string_view version = "";
uint16_t vid = 0x00; uint16_t vid = 0x00;
uint16_t pid = 0x00; uint16_t pid = 0x00;
uint16_t pixelLines = 0;
}; };
struct PrinterStatus { struct PrinterStatus {

View File

@@ -32,7 +32,11 @@
namespace libusbwrap { namespace libusbwrap {
UsbDeviceFactory::~UsbDeviceFactory() = default; UsbDeviceFactory::~UsbDeviceFactory() {
if (mLibusbCtx) {
libusb_exit(mLibusbCtx);
}
};
std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() { std::vector<std::unique_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
refreshDeviceList(); refreshDeviceList();