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 <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 img = graphics::Image();
//uint8_t* imgRef = img.getRaw();
std::array<uint32_t, 4> line;
auto bm = graphics::Bitmap<graphics::ALPHA8>(512, 128);
{
auto img = graphics::Image();
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;
}