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

Commit

Permalink
Merge pull request #36 from camrein/protocol_v3
Browse files Browse the repository at this point in the history
Protocol v3
  • Loading branch information
camrein authored Oct 20, 2017
2 parents acb6d0c + d62386e commit 34383b1
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 25 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
6 changes: 4 additions & 2 deletions EzGraverCore/EzGraverCore.pro
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ DEFINES += EZGRAVERCORE_LIBRARY
SOURCES += ezgraver.cpp \
ezgraver_v1.cpp \
ezgraver_v2.cpp \
factory.cpp
factory.cpp \
ezgraver_v3.cpp

HEADERS += ezgraver.h\
ezgravercore_global.h \
ezgraver_v1.h \
ezgraver_v2.h \
specifications.h \
factory.h
factory.h \
ezgraver_v3.h

unix {
target.path = /usr/lib
Expand Down
5 changes: 3 additions & 2 deletions EzGraverCore/ezgraver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void EzGraver::_setBurnTime(unsigned char const& burnTime) {
if(burnTime < 0x01 || burnTime > 0xF0) {
throw new std::out_of_range("burntime out of range");
}
qDebug() << "setting burn time to:" << qPrintable(burnTime);
qDebug() << "setting burn time to:" << static_cast<int>(burnTime);
_transmit(burnTime);
}

Expand Down 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
18 changes: 10 additions & 8 deletions EzGraverCore/ezgraver.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ struct EZGRAVERCORESHARED_EXPORT EzGraver {
*
* \param burnTime The burn time to use in milliseconds.
*/
void start(unsigned char const& burnTime);
virtual void start(unsigned char const& burnTime);

/*!
* Pauses the engraving process at the given location. The process
* can be continued by invoking start.
*/
void pause();
virtual void pause();

/*! Resets the engraver. */
void reset();
virtual void reset();

/*! Moves the engraver to the home position. */
void home();
virtual void home();

/*! Moves the engraver to the center. */
void center();
virtual void center();

/*! Draws a preview of the currently loaded image. */
void preview();
virtual void preview();

/*! Moves the engraver up. */
virtual void up() = 0;
Expand All @@ -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
93 changes: 93 additions & 0 deletions EzGraverCore/ezgraver_v3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "ezgraver_v3.h"

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

#include "specifications.h"

namespace Ez {

void EzGraverV3::start(unsigned char const& burnTime) {
_setBurnTime(burnTime);
qDebug() << "starting engrave process";
_transmit(QByteArray::fromRawData("\xFF\x01\x01\x00", 4));
}

void EzGraverV3::_setBurnTime(unsigned char const& burnTime) {
if(burnTime < 0x01 || burnTime > 0xF0) {
throw new std::out_of_range("burntime out of range");
}
qDebug() << "setting burn time to:" << static_cast<int>(burnTime);

QByteArray payload{"\xFF\x05?\x00", 4};
payload[2] = burnTime;
_transmit(payload);
}

void EzGraverV3::pause() {
qDebug() << "pausing engrave process";
_transmit(QByteArray::fromRawData("\xFF\x01\x02\x00", 4));
}

void EzGraverV3::reset() {
qDebug() << "resetting";
_transmit(QByteArray::fromRawData("\xFF\x04\x01\x00", 4));
}

void EzGraverV3::home() {
qDebug() << "moving to home";
_transmit(0xF3);
}

void EzGraverV3::center() {
qDebug() << "moving to center";
_transmit(QByteArray::fromRawData("\xFF\x02\x01\x00", 4));
}

void EzGraverV3::preview() {
qDebug() << "drawing image preview";
_transmit(QByteArray::fromRawData("\xFF\x02\x02\x00", 4));
}

void EzGraverV3::up() {
qDebug() << "moving up";
_transmit(QByteArray::fromRawData("\xFF\x03\x01\x00", 4));
}

void EzGraverV3::down() {
qDebug() << "moving down";
_transmit(QByteArray::fromRawData("\xFF\x03\x02\x00", 4));
}

void EzGraverV3::left() {
qDebug() << "moving left";
_transmit(QByteArray::fromRawData("\xFF\x03\x03\x00", 4));
}

void EzGraverV3::right() {
qDebug() << "moving 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));
}

}
79 changes: 79 additions & 0 deletions EzGraverCore/ezgraver_v3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef EZGRAVERV3_H
#define EZGRAVERV3_H

#include <QSerialPort>

#include <memory>

#include "ezgraver.h"

namespace Ez {

struct EzGraverV3 : EzGraver {
using EzGraver::EzGraver;

/*!
* Starts the engraving process with the given \a burnTime.
*
* \param burnTime The burn time to use in milliseconds.
*/
void start(unsigned char const& burnTime) override;

/*!
* Pauses the engraving process at the given location. The process
* can be continued by invoking start.
*/
void pause() override;

/*! Resets the engraver. */
void reset() override;

/*! Moves the engraver to the home position. */
void home() override;

/*! Moves the engraver to the center. */
void center() override;

/*! Draws a preview of the currently loaded image. */
void preview() override;

/*! Moves the engraver up. */
void up() override;

/*! Moves the engraver down. */
void down() override;

/*! Moves the engraver left. */
void left() override;

/*! 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);
};

}

#endif // EZGRAVERV3_H
5 changes: 4 additions & 1 deletion EzGraverCore/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ezgraver.h"
#include "ezgraver_v1.h"
#include "ezgraver_v2.h"
#include "ezgraver_v3.h"

namespace Ez {

Expand All @@ -33,13 +34,15 @@ std::shared_ptr<EzGraver> create(QString const& portName, int protocol) {
return std::make_shared<EzGraverV1>(serial);
case 2:
return std::make_shared<EzGraverV2>(serial);
case 3:
return std::make_shared<EzGraverV3>(serial);
default:
throw std::invalid_argument{QString{"unsupported protocol '%1' selected"}.arg(protocol).toStdString()};
}
}

QList<int> protocols() {
return QList<int>{1, 2};
return QList<int>{1, 2, 3};
}

QStringList availablePorts() {
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 34383b1

Please sign in to comment.