Skip to content
This repository has been archived by the owner on Mar 4, 2021. It is now read-only.

Commit

Permalink
Added support to upload images in protocol v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
camrein committed Oct 17, 2017
1 parent 68b6e4f commit 772b514
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 15 deletions.
4 changes: 2 additions & 2 deletions EzGraverCli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ void uploadImage(std::shared_ptr<Ez::EzGraver>& engraver, QList<QString> 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);
Expand Down
3 changes: 2 additions & 1 deletion EzGraverCore/ezgraver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions EzGraverCore/ezgraver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions EzGraverCore/ezgraver_v3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <QDebug>
#include <QByteArray>
#include <QBuffer>

#include "specifications.h"

namespace Ez {

Expand Down Expand Up @@ -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));
}

}
21 changes: 21 additions & 0 deletions EzGraverCore/ezgraver_v3.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
3 changes: 0 additions & 3 deletions EzGraverCore/specifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
12 changes: 6 additions & 6 deletions EzGraverUi/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion EzGraverUi/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down

0 comments on commit 772b514

Please sign in to comment.