From 772b5141da1b535b67343ebdbb64e876393e8559 Mon Sep 17 00:00:00 2001 From: Christoph Amrein Date: Tue, 17 Oct 2017 19:42:36 +0200 Subject: [PATCH] Added support to upload images in protocol v3. --- EzGraverCli/main.cpp | 4 ++-- EzGraverCore/ezgraver.cpp | 3 ++- EzGraverCore/ezgraver.h | 6 ++++-- EzGraverCore/ezgraver_v3.cpp | 23 +++++++++++++++++++++++ EzGraverCore/ezgraver_v3.h | 21 +++++++++++++++++++++ EzGraverCore/specifications.h | 3 --- EzGraverUi/mainwindow.cpp | 12 ++++++------ EzGraverUi/mainwindow.h | 2 +- 8 files changed, 59 insertions(+), 15 deletions(-) diff --git a/EzGraverCli/main.cpp b/EzGraverCli/main.cpp index c2e8a30..98b7fec 100644 --- a/EzGraverCli/main.cpp +++ b/EzGraverCli/main.cpp @@ -49,9 +49,9 @@ void uploadImage(std::shared_ptr& engraver, QList const& } std::cout << "erasing EEPROM\n"; - engraver->erase(); + auto waitTimeMs = engraver->erase(); engraver->awaitTransmission(); - QThread::msleep(Ez::Specifications::EraseTimeMs); + QThread::msleep(waitTimeMs); std::cout << "uploading image to EEPROM\n"; engraver->uploadImage(image); diff --git a/EzGraverCore/ezgraver.cpp b/EzGraverCore/ezgraver.cpp index 47ee4c8..02cedb7 100644 --- a/EzGraverCore/ezgraver.cpp +++ b/EzGraverCore/ezgraver.cpp @@ -55,9 +55,10 @@ void EzGraver::preview() { _transmit(0xF4); } -void EzGraver::erase() { +int EzGraver::erase() { qDebug() << "erasing EEPROM"; _transmit(QByteArray{8, '\xFE'}); + return 6000; } int EzGraver::uploadImage(QImage const& originalImage) { diff --git a/EzGraverCore/ezgraver.h b/EzGraverCore/ezgraver.h index 3a7f724..aca4380 100644 --- a/EzGraverCore/ezgraver.h +++ b/EzGraverCore/ezgraver.h @@ -65,8 +65,10 @@ struct EZGRAVERCORESHARED_EXPORT EzGraver { * Erasing the EEPROM takes a while. Sending image data to early causes * that some of the leading pixels are lost. Waiting for about 5 seconds * seems to be sufficient. + * + * \return The recommended time in ms to wait until uploading the image. */ - void erase(); + virtual int erase(); /*! * Uploads the given \a image to the EEPROM. It is mandatory to use \a erase() @@ -76,7 +78,7 @@ struct EZGRAVERCORESHARED_EXPORT EzGraver { * \param image The image to upload to the EEPROM for engraving. * \return The number of bytes being sent to the device. */ - int uploadImage(QImage const& image); + virtual int uploadImage(QImage const& image); /*! * Uploads any given \a image byte array to the EEPROM. It has to be a monochrome diff --git a/EzGraverCore/ezgraver_v3.cpp b/EzGraverCore/ezgraver_v3.cpp index 7a8a07c..6df1737 100644 --- a/EzGraverCore/ezgraver_v3.cpp +++ b/EzGraverCore/ezgraver_v3.cpp @@ -2,6 +2,9 @@ #include #include +#include + +#include "specifications.h" namespace Ez { @@ -67,4 +70,24 @@ void EzGraverV3::right() { _transmit(QByteArray::fromRawData("\xFF\x03\x04\x00", 4)); } +int EzGraverV3::erase() { + qDebug() << "erasing EEPROM"; + _transmit(QByteArray::fromRawData("\xFF\x06\x01\x00", 4)); + return 50; +} + +int EzGraverV3::uploadImage(QImage const& originalImage) { + qDebug() << "converting image to bitmap"; + QImage image{originalImage + .scaled(Ez::Specifications::ImageWidth, Ez::Specifications::ImageHeight) + .mirrored() + .convertToFormat(QImage::Format_Mono)}; + QByteArray bytes{}; + QBuffer buffer{&bytes}; + image.save(&buffer, "BMP"); + + // protocol v3 neither needs the BMP header nor the invertion of the pixels. + return EzGraver::uploadImage(bytes.mid(62)); +} + } diff --git a/EzGraverCore/ezgraver_v3.h b/EzGraverCore/ezgraver_v3.h index c983262..506fb1b 100644 --- a/EzGraverCore/ezgraver_v3.h +++ b/EzGraverCore/ezgraver_v3.h @@ -49,6 +49,27 @@ struct EzGraverV3 : EzGraver { /*! Moves the engraver right. */ void right() override; + /*! + * Erases the EEPROM of the engraver. This is necessary before uploading + * any new image to it. + * Erasing the EEPROM takes a while. Sending image data to early causes + * that some of the leading pixels are lost. Waiting for about 5 seconds + * seems to be sufficient. + * + * \return The recommended time in ms to wait until uploading the image. + */ + int erase() override; + + /*! + * Uploads the given \a image to the EEPROM. It is mandatory to use \a erase() + * it prior uploading an image. The image will automatically be scaled, inverted, + * mirrored and converted to a monochrome bitmap. + * + * \param image The image to upload to the EEPROM for engraving. + * \return The number of bytes being sent to the device. + */ + int uploadImage(QImage const& image) override; + private: void _setBurnTime(unsigned char const& burnTime); }; diff --git a/EzGraverCore/specifications.h b/EzGraverCore/specifications.h index 19edad3..2beaf4d 100644 --- a/EzGraverCore/specifications.h +++ b/EzGraverCore/specifications.h @@ -4,9 +4,6 @@ namespace Ez { namespace Specifications { -/*! The time required to erase the EEPROM in milliseconds. */ -int const EraseTimeMs{6000}; - /*! The image width */ int const ImageWidth{512}; diff --git a/EzGraverUi/mainwindow.cpp b/EzGraverUi/mainwindow.cpp index 9b85bba..c01e007 100644 --- a/EzGraverUi/mainwindow.cpp +++ b/EzGraverUi/mainwindow.cpp @@ -246,24 +246,24 @@ void MainWindow::on_down_clicked() { void MainWindow::on_upload_clicked() { _printVerbose("erasing EEPROM"); - _ezGraver->erase(); + auto waitTimeMs = _ezGraver->erase(); QImage image{_ui->image->engraveImage()}; QTimer* eraseProgressTimer{new QTimer{this}}; _ui->progress->setValue(0); - _ui->progress->setMaximum(Ez::Specifications::EraseTimeMs); + _ui->progress->setMaximum(waitTimeMs); - auto eraseProgress = std::bind(&MainWindow::_eraseProgressed, this, eraseProgressTimer, image); + auto eraseProgress = std::bind(&MainWindow::_eraseProgressed, this, eraseProgressTimer, image, waitTimeMs); connect(eraseProgressTimer, &QTimer::timeout, eraseProgress); - eraseProgressTimer->start(EraseProgressDelay); + eraseProgressTimer->start(EraseProgressDelay < waitTimeMs ? EraseProgressDelay : waitTimeMs); _ui->image->resetProgressImage(); } -void MainWindow::_eraseProgressed(QTimer* eraseProgressTimer, QImage const& image) { +void MainWindow::_eraseProgressed(QTimer* eraseProgressTimer, QImage const& image, int const& waitTimeMs) { auto value = _ui->progress->value() + EraseProgressDelay; _ui->progress->setValue(value); - if(value < Ez::Specifications::EraseTimeMs) { + if(value < waitTimeMs) { return; } eraseProgressTimer->stop(); diff --git a/EzGraverUi/mainwindow.h b/EzGraverUi/mainwindow.h index 39fad2c..689d097 100644 --- a/EzGraverUi/mainwindow.h +++ b/EzGraverUi/mainwindow.h @@ -80,7 +80,7 @@ private slots: void _setConnected(bool connected); void _printVerbose(QString const& verbose); void _loadImage(QString const& fileName); - void _eraseProgressed(QTimer* eraseProgressTimer, QImage const& image); + void _eraseProgressed(QTimer* eraseProgressTimer, QImage const& image, int const& waitTimeMs); void _uploadImage(QImage const& image); };