Successful label printing

This commit is contained in:
2023-09-24 18:08:18 +02:00
parent b940890268
commit 4b3247ef6d
4 changed files with 30 additions and 11 deletions

View File

@@ -22,7 +22,6 @@
#include <algorithm>
#include <spdlog/spdlog.h>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <iterator>
@@ -141,7 +140,7 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
rastercmd[1] = 0x00; // size +1
rastercmd[2] = 0x00;
rastercmd[3] = 0x00; // size -1
for (unsigned int i = 0; i < 8; i++) {
for (unsigned int i = 0; i < bm.getWidth(); i++) {
auto bmcol = bm.getCol(i);
if (!bmcol) {
spdlog::error("Out of bounds bitmap access");
@@ -156,7 +155,9 @@ bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap)
buf[1] = col.size() + 1;
buf[3] = col.size() - 1;
send(buf);
if (!send(buf)) {
break;
};
}
send(commands["eject"]);
@@ -177,15 +178,24 @@ bool P700Printer::send(std::vector<uint8_t>& data) {
return false;
}
int tx = 0;
#ifndef DRYRUN
if (!mUsbHndl->bulkTransfer(0x02, data, nullptr, 0)) {
if (!mUsbHndl->bulkTransfer(0x02, data, &tx, 0)) {
spdlog::error("Error writing command to Printer: {}", mUsbHndl->getLastErrorString());
return false;
}
#else
tx = data.size();
spdlog::debug("USB raw data(len {}): {}", data.size(), spdlog::to_hex(data));
#endif
if (tx != static_cast<int>(data.size())) {
spdlog::error("Could not transfer all data via USB bulk transfer. Only send {} of {} bytes",
tx, data.size());
return false;
}
return true;
}

View File

@@ -65,7 +65,9 @@ class P700Printer : public ::ptprnt::IPrinterDriver {
.vid = 0x04f9,
.pid = 0x2061};
std::map<std::string, std::vector<uint8_t>> commands{
{"rasterstart", {0x1b, 0x69, 0x52, 0x01}},
{"rasterstart",
{0x1b, 0x69, 0x61,
0x01}}, // unique for P700, other printers have the 2 byte set to 0x52 instead of 0x61
{"info", {0x1b, 0x69, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
{"packbitson", {0x02}},
{"lf", {0x5a}},

View File

@@ -73,11 +73,13 @@ std::optional<std::vector<T>> Bitmap<T>::getCol(uint16_t col) {
// first pixel is always beginning of the col
std::vector<T> colPixels(mHeight);
auto it = mPixels.begin() + col;
auto it = std::next(mPixels.begin(), col);
for (auto& colElement : colPixels) {
int offset = std::distance(mPixels.begin(), it);
//spdlog::debug("Distance {}: {}", offset, *it);
colElement = *it;
it += mWidth;
std::advance(it, mWidth);
}
return colPixels;

View File

@@ -19,29 +19,34 @@
#include "Image.hpp"
#include <string>
#include "pango/pango-font.h"
namespace ptprnt::graphics {
Image::Image() {
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A1, 512, 128);
mSurface = cairo_image_surface_create(CAIRO_FORMAT_A8, 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);
std::string printThis("Mist 💩");
mLayout = pango_cairo_create_layout(cr);
pango_layout_set_font_description(mLayout, mFontDescription);
pango_layout_set_text(mLayout, "Hello, world", -1);
pango_layout_set_text(mLayout, printThis.c_str(), -1);
cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
cairo_move_to(cr, 10.0, 94.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");
cairo_surface_write_to_png(mSurface, "hello.png");
}
uint8_t* Image::getRaw() {
cairo_surface_flush(mSurface);
return cairo_image_surface_get_data(mSurface);
}