diff --git a/src/P700Printer.cpp b/src/P700Printer.cpp index 6d295e5..bb987cd 100644 --- a/src/P700Printer.cpp +++ b/src/P700Printer.cpp @@ -19,21 +19,27 @@ #include "P700Printer.hpp" +#include #include -#include #include #include #include +#include #include #include #include +#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& bitmap) { - auto img = graphics::Image(); - //uint8_t* imgRef = img.getRaw(); - std::array line; + auto bm = graphics::Bitmap(512, 128); + { + auto img = graphics::Image(); + bm.setPixels(std::vector(img.getRaw(), img.getRaw() + 512 * 128)); + } send(commands["rasterstart"]); - std::vector 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 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 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& 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; } diff --git a/src/graphics/Bitmap.cpp b/src/graphics/Bitmap.cpp index ed283bc..72a2d7f 100644 --- a/src/graphics/Bitmap.cpp +++ b/src/graphics/Bitmap.cpp @@ -20,6 +20,7 @@ #include "Bitmap.hpp" #include +#include #include #include @@ -40,6 +41,17 @@ uint16_t Bitmap::getHeight() { return mHeight; } +template +bool Bitmap::setPixels(const std::vector& pixels) { + if (pixels.size() != mPixels.size()) { + spdlog::error("Invalid pixel buffer size."); + return false; + } + + mPixels = pixels; + return true; +} + template std::optional> Bitmap::getLine(uint16_t line) { if (line >= mHeight) { @@ -53,22 +65,22 @@ std::optional> Bitmap::getLine(uint16_t line) { } template -std::optional> Bitmap::getRow(uint16_t row) { - if (row >= mWidth) { +std::optional> Bitmap::getCol(uint16_t col) { + if (col >= mWidth) { // out of bound return std::nullopt; } - // first pixel is always beginning of the row - std::vector rowPixels(mHeight); - auto it = mPixels.begin() + row; + // first pixel is always beginning of the col + std::vector 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 \ No newline at end of file diff --git a/src/graphics/Bitmap.hpp b/src/graphics/Bitmap.hpp index eb30ec6..42c5ec1 100644 --- a/src/graphics/Bitmap.hpp +++ b/src/graphics/Bitmap.hpp @@ -40,8 +40,9 @@ class Bitmap { uint16_t getWidth(); uint16_t getHeight(); + bool setPixels(const std::vector& pixels); std::optional> getLine(uint16_t line); - std::optional> getRow(uint16_t row); + std::optional> getCol(uint16_t col); private: uint16_t mWidth; diff --git a/src/graphics/Image.cpp b/src/graphics/Image.cpp index 677199f..bf57153 100644 --- a/src/graphics/Image.cpp +++ b/src/graphics/Image.cpp @@ -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() { diff --git a/tests/bitmap_test/bitmap_test.cpp b/tests/bitmap_test/bitmap_test.cpp index 7707874..532196a 100644 --- a/tests/bitmap_test/bitmap_test.cpp +++ b/tests/bitmap_test/bitmap_test.cpp @@ -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(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(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); }