Add write que to microcontroller to avoid overringing the mcu buffer while not stalling the applicaiton
This commit is contained in:
parent
24c168cf64
commit
34769049f9
2 changed files with 34 additions and 19 deletions
|
|
@ -1,8 +1,5 @@
|
||||||
#include "microcontroller.h"
|
#include "microcontroller.h"
|
||||||
|
|
||||||
#include <chrono>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
void Microcontroller::relayToggle(int state, int relay)
|
void Microcontroller::relayToggle(int state, int relay)
|
||||||
{
|
{
|
||||||
char buffer[8];
|
char buffer[8];
|
||||||
|
|
@ -49,22 +46,34 @@ void Microcontroller::setAuxPwm(int duty)
|
||||||
|
|
||||||
void Microcontroller::write(const QByteArray& buffer)
|
void Microcontroller::write(const QByteArray& buffer)
|
||||||
{
|
{
|
||||||
if(_port != nullptr)
|
writeQue.enqueue(buffer);
|
||||||
|
if(!writeTimer.isActive())
|
||||||
{
|
{
|
||||||
_port->write(buffer);
|
writeTimer.setInterval(0);
|
||||||
_port->waitForBytesWritten(1000);
|
writeTimer.start();
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Microcontroller::write(char* buffer, const size_t length)
|
void Microcontroller::write(char* buffer, const size_t length)
|
||||||
{
|
{
|
||||||
if(_port != nullptr)
|
write(QByteArray(buffer, length));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Microcontroller::onWriteTimerTimeout()
|
||||||
|
{
|
||||||
|
writeTimer.setInterval(50);
|
||||||
|
if(connected())
|
||||||
{
|
{
|
||||||
_port->write(buffer, length);
|
if(!writeQue.empty())
|
||||||
_port->waitForBytesWritten(1000);
|
{
|
||||||
|
QByteArray data = writeQue.dequeue();
|
||||||
|
_port->write(data);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
writeTimer.stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(40));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Microcontroller::setPattern(int pattern)
|
void Microcontroller::setPattern(int pattern)
|
||||||
|
|
@ -82,8 +91,10 @@ void Microcontroller::startSunrise()
|
||||||
|
|
||||||
bool Microcontroller::connected()
|
bool Microcontroller::connected()
|
||||||
{
|
{
|
||||||
if(_port != nullptr) return _port->isOpen();
|
if(_port != nullptr)
|
||||||
else return false;
|
return _port->isOpen();
|
||||||
|
else
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Microcontroller::refresh()
|
void Microcontroller::refresh()
|
||||||
|
|
@ -93,13 +104,17 @@ void Microcontroller::refresh()
|
||||||
|
|
||||||
//housekeeping
|
//housekeeping
|
||||||
|
|
||||||
Microcontroller::Microcontroller(QIODevice* port)
|
Microcontroller::Microcontroller(QIODevice* port): Microcontroller()
|
||||||
{
|
{
|
||||||
setIODevice(port);
|
setIODevice(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
Microcontroller::Microcontroller()
|
Microcontroller::Microcontroller()
|
||||||
{
|
{
|
||||||
|
writeTimer.setInterval(50);
|
||||||
|
writeTimer.setSingleShot(false);
|
||||||
|
connect(&writeTimer, &QTimer::timeout, this, &Microcontroller::onWriteTimerTimeout);
|
||||||
|
qDebug()<<__func__<<writeTimer.isActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
Microcontroller::~Microcontroller()
|
Microcontroller::~Microcontroller()
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,9 @@
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QRunnable>
|
#include <QRunnable>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QEventLoop>
|
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
#include <QAbstractButton>
|
#include <QByteArray>
|
||||||
|
#include <QQueue>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "items/item.h"
|
#include "items/item.h"
|
||||||
|
|
@ -32,13 +32,12 @@ private:
|
||||||
|
|
||||||
bool listMode = false;
|
bool listMode = false;
|
||||||
|
|
||||||
//uint8_t _auxState = 0;
|
|
||||||
|
|
||||||
QIODevice* _port = nullptr;
|
QIODevice* _port = nullptr;
|
||||||
|
QQueue<QByteArray> writeQue;
|
||||||
|
QTimer writeTimer;
|
||||||
|
|
||||||
std::vector< std::shared_ptr<Item> > relayList;
|
std::vector< std::shared_ptr<Item> > relayList;
|
||||||
|
|
||||||
QScopedPointer<QEventLoop> loop;
|
|
||||||
QString _buffer;
|
QString _buffer;
|
||||||
|
|
||||||
void processMicroReturn();
|
void processMicroReturn();
|
||||||
|
|
@ -75,6 +74,7 @@ public slots:
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
void isReadyRead();
|
void isReadyRead();
|
||||||
|
void onWriteTimerTimeout();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void textRecived(const QString string);
|
void textRecived(const QString string);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue