first successful print of gibberish
This commit is contained in:
@@ -19,21 +19,27 @@
|
||||
|
||||
#include "P700Printer.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <ratio>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "graphics/Bitmap.hpp"
|
||||
#include "graphics/Image.hpp"
|
||||
#include "graphics/Monochrome.hpp"
|
||||
#include "libusb.h"
|
||||
#include "libusbwrap/LibUsbTypes.hpp"
|
||||
#include "spdlog/fmt/bin_to_hex.h"
|
||||
|
||||
// as long as DRYRUN is defined, no data is actually send to the printer, we need to safe some tape ;)
|
||||
//#define DRYRUN
|
||||
|
||||
namespace ptprnt::printer {
|
||||
|
||||
P700Printer::P700Printer() {}
|
||||
@@ -122,26 +128,37 @@ bool P700Printer::detachUsbDevice() {
|
||||
}
|
||||
|
||||
bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) {
|
||||
auto bm = graphics::Bitmap<graphics::ALPHA8>(512, 128);
|
||||
{
|
||||
auto img = graphics::Image();
|
||||
//uint8_t* imgRef = img.getRaw();
|
||||
std::array<uint32_t, 4> line;
|
||||
bm.setPixels(std::vector<uint8_t>(img.getRaw(), img.getRaw() + 512 * 128));
|
||||
}
|
||||
|
||||
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());
|
||||
std::vector<uint8_t> rastercmd(4);
|
||||
rastercmd[0] = 0x47;
|
||||
rastercmd[1] = 0x00; // size +1
|
||||
rastercmd[2] = 0x00;
|
||||
rastercmd[3] = 0x00; // size -1
|
||||
for (unsigned int i = 0; i < 8; i++) {
|
||||
auto bmcol = bm.getCol(i);
|
||||
if (!bmcol) {
|
||||
spdlog::error("Out of bounds bitmap access");
|
||||
break;
|
||||
}
|
||||
auto monocol = graphics::Monochrome(*bmcol);
|
||||
auto col = monocol.get();
|
||||
|
||||
std::vector<uint8_t> buf(0);
|
||||
buf.insert(buf.begin(), rastercmd.begin(), rastercmd.end());
|
||||
buf.insert(std::next(buf.begin(), 4), col.begin(), col.end());
|
||||
|
||||
buf[1] = col.size() + 1;
|
||||
buf[3] = col.size() - 1;
|
||||
send(buf);
|
||||
}
|
||||
|
||||
send(commands["eject"]);
|
||||
return true;
|
||||
}
|
||||
@@ -159,10 +176,15 @@ bool P700Printer::send(std::vector<uint8_t>& data) {
|
||||
spdlog::error("Invalid device handle or invalid data.");
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifndef DRYRUN
|
||||
if (!mUsbHndl->bulkTransfer(0x02, data, nullptr, 0)) {
|
||||
spdlog::error("Error writing command to Printer: {}", mUsbHndl->getLastErrorString());
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
spdlog::debug("USB raw data(len {}): {}", data.size(), spdlog::to_hex(data));
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@@ -20,6 +20,7 @@
|
||||
#include "Bitmap.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <iterator>
|
||||
#include <optional>
|
||||
@@ -40,6 +41,17 @@ uint16_t Bitmap<T>::getHeight() {
|
||||
return mHeight;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool Bitmap<T>::setPixels(const std::vector<T>& pixels) {
|
||||
if (pixels.size() != mPixels.size()) {
|
||||
spdlog::error("Invalid pixel buffer size.");
|
||||
return false;
|
||||
}
|
||||
|
||||
mPixels = pixels;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
|
||||
if (line >= mHeight) {
|
||||
@@ -53,22 +65,22 @@ std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
|
||||
}
|
||||
|
||||
template <class T>
|
||||
std::optional<std::vector<T>> Bitmap<T>::getRow(uint16_t row) {
|
||||
if (row >= mWidth) {
|
||||
std::optional<std::vector<T>> Bitmap<T>::getCol(uint16_t col) {
|
||||
if (col >= 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;
|
||||
// first pixel is always beginning of the col
|
||||
std::vector<T> colPixels(mHeight);
|
||||
auto it = mPixels.begin() + col;
|
||||
|
||||
for (auto& rowElement : rowPixels) {
|
||||
rowElement = *it;
|
||||
for (auto& colElement : colPixels) {
|
||||
colElement = *it;
|
||||
it += mWidth;
|
||||
}
|
||||
|
||||
return rowPixels;
|
||||
return colPixels;
|
||||
}
|
||||
|
||||
} // namespace ptprnt::graphics
|
@@ -40,8 +40,9 @@ class Bitmap {
|
||||
|
||||
uint16_t getWidth();
|
||||
uint16_t getHeight();
|
||||
bool setPixels(const std::vector<T>& pixels);
|
||||
std::optional<std::vector<T>> getLine(uint16_t line);
|
||||
std::optional<std::vector<T>> getRow(uint16_t row);
|
||||
std::optional<std::vector<T>> getCol(uint16_t col);
|
||||
|
||||
private:
|
||||
uint16_t mWidth;
|
||||
|
@@ -38,7 +38,7 @@ Image::Image() {
|
||||
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");
|
||||
//cairo_surface_write_to_png(mSurface, "hello.png");
|
||||
}
|
||||
|
||||
uint8_t* Image::getRaw() {
|
||||
|
@@ -48,19 +48,19 @@ TEST(basic_test, Bitmap_getBitmapLineInsideOfImage_yieldsValidLineSize) {
|
||||
ASSERT_EQ(16, lineSize);
|
||||
}
|
||||
|
||||
TEST(basic_test, Bitmap_getBitmapRowOutsideOfImage_yieldsNullopt) {
|
||||
TEST(basic_test, Bitmap_getBitmapColOutsideOfImage_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);
|
||||
// col 16 is out of bounds, count begins with 0
|
||||
auto outOfBoundsCol = bm.getCol(16);
|
||||
ASSERT_EQ(std::nullopt, outOfBoundsCol);
|
||||
}
|
||||
|
||||
TEST(basic_test, Bitmap_getBitmapRowInsideOfImage_yieldsValidRowSize) {
|
||||
TEST(basic_test, Bitmap_getBitmapColInsideOfImage_yieldsValidColSize) {
|
||||
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
|
||||
auto row = bm.getRow(15);
|
||||
if (!row) {
|
||||
FAIL() << "Returned Row is invalid";
|
||||
auto col = bm.getCol(15);
|
||||
if (!col) {
|
||||
FAIL() << "Returned Col is invalid";
|
||||
}
|
||||
auto rowSize = row->size();
|
||||
ASSERT_EQ(8, rowSize);
|
||||
auto colSize = col->size();
|
||||
ASSERT_EQ(8, colSize);
|
||||
}
|
||||
|
Reference in New Issue
Block a user