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 #11 from camrein/progress_display
Browse files Browse the repository at this point in the history
Progress display
  • Loading branch information
camrein authored Sep 10, 2016
2 parents 2489b6c + acf113f commit 62346f3
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 68 deletions.
2 changes: 1 addition & 1 deletion EzGraverCli/EzGraverCli.pro
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include(../common.pri)

QT += core
QT += gui
QT += serialport

TARGET = EzGraverCli
CONFIG += console
Expand Down
2 changes: 1 addition & 1 deletion EzGraverCli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void uploadImage(std::shared_ptr<EzGraver>& engraver, QStringList const& argumen
std::cout << "erasing EEPROM\n";
engraver->erase();
engraver->awaitTransmission();
QThread::sleep(6);
QThread::msleep(EzGraver::EraseTimeMs);

std::cout << "uploading image to EEPROM\n";
engraver->uploadImage(image);
Expand Down
2 changes: 1 addition & 1 deletion EzGraverCore/EzGraverCore.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include(../common.pri)

QT += gui
QT += core
QT += serialport

TARGET = EzGraverCore
Expand Down
29 changes: 21 additions & 8 deletions EzGraverCore/ezgraver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,51 @@ void EzGraver::erase() {
_transmit(QByteArray{8, '\xFE'});
}

#include <QFile>
void EzGraver::uploadImage(QImage const& originalImage) {
int EzGraver::uploadImage(QImage const& originalImage) {
qDebug() << "converting image to bitmap";
QImage image{originalImage
.scaled(512, 512)
.scaled(ImageWidth, ImageHeight)
.mirrored()
.convertToFormat(QImage::Format_Mono)};
image.invertPixels();

QByteArray bytes{};
QBuffer buffer{&bytes};
image.save(&buffer, "BMP");
uploadImage(bytes);
return uploadImage(bytes);
}

void EzGraver::uploadImage(QByteArray const& image) {
int EzGraver::uploadImage(QByteArray const& image) {
qDebug() << "uploading image";
_transmit(image);
// Data is chunked in order to get at least some progress updates
_transmit(image, 8192);
return image.size();
}

void EzGraver::awaitTransmission(int msecs) {
_serial->waitForBytesWritten(msecs);
}

std::shared_ptr<QSerialPort> EzGraver::serialPort() {
return _serial;
}

void EzGraver::_transmit(unsigned char const& data) {
_transmit(QByteArray{1, static_cast<char>(data)});
}

void EzGraver::_transmit(QByteArray const& data) {
QString hex{data.count() < 10 ? data.toHex() : ""};
qDebug() << "transmitting" << data.length() << "bytes:" << hex;
qDebug() << "transmitting" << data.size() << "bytes:" << data.toHex();
_serial->write(data);
_serial->flush();
}

void EzGraver::_transmit(QByteArray const& data, int chunkSize) {
qDebug() << "transmitting" << data.size() << "bytes in chunks of size" << chunkSize;
for(int i{0}; i < data.size(); i += chunkSize) {
_serial->write(data.mid(i, chunkSize));
_serial->flush();
}
}

EzGraver::~EzGraver() {
Expand Down
29 changes: 23 additions & 6 deletions EzGraverCore/ezgraver.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@

#include <QStringList>
#include <QImage>
#include <QSerialPort>
#include <QSize>

#include <memory>

QT_BEGIN_NAMESPACE
class QSerialPort;
QT_END_NAMESPACE

/*!
* Allows accessing a NEJE engraver using the serial port it was instantiated with.
* The connection is closed as soon as the object is destroyed.
*/
struct EZGRAVERCORESHARED_EXPORT EzGraver {
/*! The time required to erase the EEPROM in milliseconds. */
static int const EraseTimeMs{6000};

/*! The image width */
static int const ImageWidth{512};

/*! The image height */
static int const ImageHeight{512};

/*!
* Creates an instance and connects to the given \a portName.
*
Expand Down Expand Up @@ -84,16 +91,18 @@ struct EZGRAVERCORESHARED_EXPORT EzGraver {
* 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.
*/
void uploadImage(QImage const& image);
int uploadImage(QImage const& image);

/*!
* Uploads any given \a image byte array to the EEPROM. It has to be a monochrome
* bitmap of the dimensions 512x512. Every white pixel is being engraved.
*
* \param image The image byte array to upload to the EEPROM.
* \return The number of bytes being sent to the device.
*/
void uploadImage(QByteArray const& image);
int uploadImage(QByteArray const& image);

/*!
* Waits until the current serial port buffer is fully written to the device.
Expand All @@ -102,6 +111,13 @@ struct EZGRAVERCORESHARED_EXPORT EzGraver {
*/
void awaitTransmission(int msecs=-1);

/*!
* Gets the serialport used by the EzGraver instance.
*
* \return The serial port used.
*/
std::shared_ptr<QSerialPort> serialPort();

EzGraver() = delete;
virtual ~EzGraver();

Expand All @@ -111,6 +127,7 @@ struct EZGRAVERCORESHARED_EXPORT EzGraver {

void _transmit(unsigned char const& data);
void _transmit(QByteArray const& data);
void _transmit(QByteArray const& data, int chunkSize);

void _setBurnTime(unsigned char const& burnTime);
};
Expand Down
1 change: 1 addition & 0 deletions EzGraverUi/EzGraverUi.pro
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include(../common.pri)

QT += core
QT += gui
QT += serialport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

Expand Down
10 changes: 9 additions & 1 deletion EzGraverUi/imagelabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <QPainter>

#include "ezgraver.h"

ImageLabel::ImageLabel(QWidget* parent) : ClickLabel{parent}, _image{}, _flags{Qt::DiffuseDither} {}
ImageLabel::~ImageLabel() {}

Expand Down Expand Up @@ -32,7 +34,7 @@ void ImageLabel::updateDisplayedImage() {
}

// Draw white background, otherwise transparency is converted to black.
QImage image{QSize{512, 512}, QImage::Format_ARGB32};
QImage image{QSize{EzGraver::ImageWidth, EzGraver::ImageHeight}, QImage::Format_ARGB32};
image.fill(QColor{Qt::white});
QPainter painter{&image};
painter.drawImage(0, 0, _image.scaled(image.size()));
Expand All @@ -43,3 +45,9 @@ void ImageLabel::updateDisplayedImage() {
bool ImageLabel::imageLoaded() const {
return !_image.isNull();
}

void ImageLabel::setImageDimensions(QSize const& dimensions) {
auto span = this->lineWidth()*2;
setMinimumWidth(dimensions.width() + span);
setMinimumHeight(dimensions.height() + span);
}
8 changes: 8 additions & 0 deletions EzGraverUi/imagelabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ class ImageLabel : public ClickLabel {
* \return Returns \c true if an image is loaded.
*/
bool imageLoaded() const;

/*!
* Sets the image dimensions. This enforces minimum dimensions of the
* component with respect to the border width.
*
* \param dimensions The image dimensions.
*/
void setImageDimensions(QSize const& dimensions);
signals:
/*!
* Fired as soon as the image has been changed.
Expand Down
Loading

0 comments on commit 62346f3

Please sign in to comment.