summaryrefslogtreecommitdiffhomepage
path: root/tracker-hatire/thread.hpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-04-29 11:30:37 +0200
committerStanislaw Halik <sthalik@misaki.pl>2016-04-29 11:30:37 +0200
commit38dd6e55d20adfd830d834c394fc6ce7373a4805 (patch)
tree0a94bf051ff0bc153abebaa74e474748c2d4d8a6 /tracker-hatire/thread.hpp
parent4db6f6334d13ed5e8696dfa0208b42b3e9a2352a (diff)
tracker/hatire: move io to a separate thread
We can't have async io on the main thread because QSerialPort's readyRead() signal can fire constantly, thus consuming all CPU time. We can't sleep in the main thread either as that blocks too many things. We can't ignore readyRead() invocations over a threshold as that'll make us lose some of data notifications. Refactor hatire to put IO on a thread. Since this is a separate Qt event loop, we may sleep in there. Further, add a debug mode reading data from a file, as if it came from a serial-attached device. Issue: #327
Diffstat (limited to 'tracker-hatire/thread.hpp')
-rw-r--r--tracker-hatire/thread.hpp167
1 files changed, 167 insertions, 0 deletions
diff --git a/tracker-hatire/thread.hpp b/tracker-hatire/thread.hpp
new file mode 100644
index 00000000..dd0d8c14
--- /dev/null
+++ b/tracker-hatire/thread.hpp
@@ -0,0 +1,167 @@
+#pragma once
+
+#include <QSerialPort>
+#include <QByteArray>
+#include <QThread>
+#include <QMutex>
+
+#include <QFile>
+#include <QCoreApplication>
+
+enum results
+{
+ result_ok,
+ result_open_error,
+ result_error,
+};
+
+//#define HATIRE_DEBUG_LOGFILE "d:/putty-hatire.log"
+
+#ifdef HATIRE_DEBUG_LOGFILE
+# include <QFile>
+# include <QTimer>
+#endif
+
+struct thread_settings
+{
+ QByteArray sCmdStart;
+ QByteArray sCmdStop;
+ QByteArray sCmdInit;
+ QByteArray sCmdReset;
+ QByteArray sCmdCenter;
+ QByteArray sCmdZero;
+
+ QString sSerialPortName;
+ QSerialPort::BaudRate iBaudRate;
+ QSerialPort::DataBits iDataBits;
+ QSerialPort::Parity iParity;
+ QSerialPort::StopBits iStopBits;
+ QSerialPort::FlowControl iFlowControl;
+
+ int iDelayInit;
+ int iDelayStart;
+ int iDelaySeq;
+ bool bBigEndian;
+ volatile bool bEnableLogging;
+
+ thread_settings() :
+ iBaudRate(QSerialPort::UnknownBaud),
+ iDataBits(QSerialPort::UnknownDataBits),
+ iParity(QSerialPort::UnknownParity),
+ iStopBits(QSerialPort::UnknownStopBits),
+ iFlowControl(QSerialPort::UnknownFlowControl),
+ iDelayInit(0),
+ iDelayStart(0),
+ iDelaySeq(0),
+ bBigEndian(false),
+ bEnableLogging(false)
+ {
+ }
+};
+
+#include <QMetaType>
+
+Q_DECLARE_METATYPE(thread_settings)
+
+struct serial_result
+{
+ serial_result() : code(result_ok) {}
+ serial_result(results code, const QString& error) : error(error), code(code) {}
+
+ QString error;
+ results code;
+};
+
+struct Diag : public QFile
+{
+ Diag()
+ {
+ setFileName(QCoreApplication::applicationDirPath() + "/HATDiagnostics.txt");
+ }
+};
+
+class hatire_thread : public QThread
+{
+ Q_OBJECT
+
+#ifdef HATIRE_DEBUG_LOGFILE
+ using serial_t = QFile;
+ QTimer read_timer;
+#else
+ using serial_t = QSerialPort;
+#endif
+
+ QByteArray data_read;
+ serial_t com_port;
+ thread_settings s;
+ QMutex data_mtx;
+
+ void run() override;
+
+private slots:
+ void on_serial_read();
+ void teardown_serial();
+
+ void sendcmd_impl(const QByteArray& cmd)
+ {
+#ifndef HATIRE_DEBUG_LOGFILE
+ QByteArray Msg;
+
+ if (cmd.length() > 0)
+ {
+ if (com_port.isOpen())
+ {
+ QString logMess;
+ logMess.append("SEND '");
+ logMess.append(cmd);
+ logMess.append("'");
+ Log(logMess);
+ com_port.write(cmd);
+ if (!com_port.waitForBytesWritten(1000)) {
+ emit serial_debug_info("TimeOut in writing CMD");
+ } else
+ {
+ Msg.append("\r\n");
+ Msg.append("SEND '");
+ Msg.append(cmd);
+ Msg.append("'\r\n");
+ }
+#if 0 // WaitForReadyRead isn't working well and there are some reports of it being a win32 issue. We can live without it anyway
+ if ( !com_port.waitForReadyRead(1000)) {
+ emit serial_debug_info("TimeOut in response to CMD") ;
+ } else {
+ emit serial_debug_info(Msg);
+ }
+#else
+ emit serial_debug_info(Msg);
+#endif
+ } else {
+ emit serial_debug_info("ComPort not open") ;
+ }
+ }
+#endif
+ }
+ void serial_info_impl();
+ serial_result init_serial_port_impl();
+ void update_serial_settings_impl(const thread_settings& s);
+ thread_settings serial_settings_impl();
+
+signals:
+ void serial_debug_info(const QByteArray &MsgInfo);
+
+ void sendcmd(const QByteArray& cmd);
+ void serial_info();
+ serial_result init_serial_port();
+ void update_serial_settings(const thread_settings& s);
+ thread_settings serial_settings();
+
+public:
+ void start(const thread_settings &s_);
+ ~hatire_thread() override;
+ hatire_thread();
+
+ void prepend_unread_data(const QByteArray& data);
+
+ QByteArray flush_data_read();
+ void Log(const QString& message);
+};