first successful print of gibberish

This commit is contained in:
2023-09-24 11:49:57 +02:00
parent 29a609e855
commit 7905f37150
5 changed files with 71 additions and 36 deletions

View File

@@ -19,21 +19,27 @@
#include "P700Printer.hpp" #include "P700Printer.hpp"
#include <algorithm>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <array>
#include <chrono> #include <chrono>
#include <cstdint> #include <cstdint>
#include <iostream> #include <iostream>
#include <iterator>
#include <ratio> #include <ratio>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include "graphics/Bitmap.hpp"
#include "graphics/Image.hpp" #include "graphics/Image.hpp"
#include "graphics/Monochrome.hpp"
#include "libusb.h" #include "libusb.h"
#include "libusbwrap/LibUsbTypes.hpp" #include "libusbwrap/LibUsbTypes.hpp"
#include "spdlog/fmt/bin_to_hex.h" #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 { namespace ptprnt::printer {
P700Printer::P700Printer() {} P700Printer::P700Printer() {}
@@ -122,26 +128,37 @@ bool P700Printer::detachUsbDevice() {
} }
bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) { bool P700Printer::printBitmap(const graphics::Bitmap<graphics::ALPHA8>& bitmap) {
auto img = graphics::Image(); auto bm = graphics::Bitmap<graphics::ALPHA8>(512, 128);
//uint8_t* imgRef = img.getRaw(); {
std::array<uint32_t, 4> line; auto img = graphics::Image();
bm.setPixels(std::vector<uint8_t>(img.getRaw(), img.getRaw() + 512 * 128));
}
send(commands["rasterstart"]); 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); std::vector<uint8_t> rastercmd(4);
buf[2] = 0; rastercmd[0] = 0x47;
buf[3] = (uint8_t)(8 - 1); rastercmd[1] = 0x00; // size +1
memcpy(buf.data() + 4, line.data(), line.size()); 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(buf);
} }
send(commands["eject"]); send(commands["eject"]);
return true; return true;
} }
@@ -159,10 +176,15 @@ bool P700Printer::send(std::vector<uint8_t>& data) {
spdlog::error("Invalid device handle or invalid data."); spdlog::error("Invalid device handle or invalid data.");
return false; return false;
} }
#ifndef DRYRUN
if (!mUsbHndl->bulkTransfer(0x02, data, nullptr, 0)) { if (!mUsbHndl->bulkTransfer(0x02, data, nullptr, 0)) {
spdlog::error("Error writing command to Printer: {}", mUsbHndl->getLastErrorString()); spdlog::error("Error writing command to Printer: {}", mUsbHndl->getLastErrorString());
return false; return false;
} }
#else
spdlog::debug("USB raw data(len {}): {}", data.size(), spdlog::to_hex(data));
#endif
return true; return true;
} }

View File

@@ -20,6 +20,7 @@
#include "Bitmap.hpp" #include "Bitmap.hpp"
#include <algorithm> #include <algorithm>
#include <spdlog/spdlog.h>
#include <iterator> #include <iterator>
#include <optional> #include <optional>
@@ -40,6 +41,17 @@ uint16_t Bitmap<T>::getHeight() {
return mHeight; 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> template <class T>
std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) { std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
if (line >= mHeight) { if (line >= mHeight) {
@@ -53,22 +65,22 @@ std::optional<std::vector<T>> Bitmap<T>::getLine(uint16_t line) {
} }
template <class T> template <class T>
std::optional<std::vector<T>> Bitmap<T>::getRow(uint16_t row) { std::optional<std::vector<T>> Bitmap<T>::getCol(uint16_t col) {
if (row >= mWidth) { if (col >= mWidth) {
// out of bound // out of bound
return std::nullopt; return std::nullopt;
} }
// first pixel is always beginning of the row // first pixel is always beginning of the col
std::vector<T> rowPixels(mHeight); std::vector<T> colPixels(mHeight);
auto it = mPixels.begin() + row; auto it = mPixels.begin() + col;
for (auto& rowElement : rowPixels) { for (auto& colElement : colPixels) {
rowElement = *it; colElement = *it;
it += mWidth; it += mWidth;
} }
return rowPixels; return colPixels;
} }
} // namespace ptprnt::graphics } // namespace ptprnt::graphics

View File

@@ -40,8 +40,9 @@ class Bitmap {
uint16_t getWidth(); uint16_t getWidth();
uint16_t getHeight(); uint16_t getHeight();
bool setPixels(const std::vector<T>& pixels);
std::optional<std::vector<T>> getLine(uint16_t line); 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: private:
uint16_t mWidth; uint16_t mWidth;

View File

@@ -38,7 +38,7 @@ Image::Image() {
cairo_move_to(cr, 10.0, 94.0); cairo_move_to(cr, 10.0, 94.0);
pango_cairo_show_layout_line(cr, pango_layout_get_line(mLayout, 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() { uint8_t* Image::getRaw() {

View File

@@ -48,19 +48,19 @@ TEST(basic_test, Bitmap_getBitmapLineInsideOfImage_yieldsValidLineSize) {
ASSERT_EQ(16, lineSize); 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); auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
// row 16 is out of bounds, count begins with 0 // col 16 is out of bounds, count begins with 0
auto outOfBoundsRow = bm.getRow(16); auto outOfBoundsCol = bm.getCol(16);
ASSERT_EQ(std::nullopt, outOfBoundsRow); 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 bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(16, 8);
auto row = bm.getRow(15); auto col = bm.getCol(15);
if (!row) { if (!col) {
FAIL() << "Returned Row is invalid"; FAIL() << "Returned Col is invalid";
} }
auto rowSize = row->size(); auto colSize = col->size();
ASSERT_EQ(8, rowSize); ASSERT_EQ(8, colSize);
} }