diff options
Diffstat (limited to 'tracker-hatire/thread.cpp')
| -rw-r--r-- | tracker-hatire/thread.cpp | 72 |
1 files changed, 33 insertions, 39 deletions
diff --git a/tracker-hatire/thread.cpp b/tracker-hatire/thread.cpp index fa09e5da..7d61a90a 100644 --- a/tracker-hatire/thread.cpp +++ b/tracker-hatire/thread.cpp @@ -1,12 +1,15 @@ #include "thread.hpp" +#include "compat/base-path.hpp" #include "compat/sleep.hpp" + #include <utility> +#include <cstring> #include <QTextStream> #include <QTime> -#include <QDebug> +#include <QByteArray> -#include <cstring> +#include <QDebug> void hatire_thread::sendcmd_impl(const QByteArray &cmd) { @@ -24,7 +27,7 @@ void hatire_thread::sendcmd_impl(const QByteArray &cmd) Log(logMess); com_port.write(cmd); if (!com_port.waitForBytesWritten(1000)) { - emit serial_debug_info_str(tr("Timeout during writing command")); + serial_debug_info_str(tr("Timeout during writing command")); } else { Msg.append("\r\n"); @@ -42,7 +45,7 @@ void hatire_thread::sendcmd_impl(const QByteArray &cmd) emit serial_debug_info(Msg); #endif } else { - emit serial_debug_info_str(tr("COM port not open")); + serial_debug_info_str(tr("COM port not open")); } } #endif @@ -52,7 +55,7 @@ struct Diag final : public QFile { Diag() { - setFileName(QCoreApplication::applicationDirPath() + "/HATDiagnostics.txt"); + setFileName(OPENTRACK_BASE_PATH + "/HATDiagnostics.txt"); } }; @@ -68,7 +71,7 @@ void hatire_thread::Log(const QString& message) { QTextStream out(&flDiagnostics); QString milliSeconds; - milliSeconds = QString("%1").arg(QTime::currentTime().msec(), 3, 10, QChar('0')); + milliSeconds = QStringLiteral("%1").arg(QTime::currentTime().msec(), 3, 10, QChar('0')); // We have a file out << QTime::currentTime().toString() << "." << milliSeconds << ": " << message << "\r\n"; flDiagnostics.close(); @@ -88,8 +91,6 @@ hatire_thread::~hatire_thread() hatire_thread::hatire_thread() { - data_read.reserve(65536); - connect(this, &QThread::finished, this, &hatire_thread::teardown_serial, Qt::DirectConnection); connect(this, &hatire_thread::init_serial_port, this, &hatire_thread::init_serial_port_impl, Qt::QueuedConnection); connect(this, &hatire_thread::serial_info, this, &hatire_thread::serial_info_impl, Qt::QueuedConnection); @@ -110,7 +111,7 @@ void hatire_thread::teardown_serial() { QByteArray msg; Log("Tracker shut down"); - com_port.write(to_latin1(s.CmdStop)); + com_port.write(s.CmdStop->toUtf8()); if (!com_port.waitForBytesWritten(1000)) { emit serial_debug_info("TimeOut in writing CMD"); @@ -119,12 +120,13 @@ void hatire_thread::teardown_serial() { msg.append("\r\n"); msg.append("SEND '"); - msg.append(s.CmdStop); + msg.append(s.CmdStop->toUtf8()); msg.append("'\r\n"); } emit serial_debug_info(msg); - disconnect(&com_port, SIGNAL(readyRead()), nullptr, nullptr); + // XXX does this make any sense? -sh 20180703 + //disconnect(&com_port, SIGNAL(readyRead()), nullptr, nullptr); com_port.close(); } } @@ -135,8 +137,8 @@ void hatire_thread::run() com_port.setFileName(HATIRE_DEBUG_LOGFILE); com_port.open(QIODevice::ReadOnly); - read_timer.start(10); connect(&read_timer, &QTimer::timeout, this, &hatire_thread::on_serial_read, Qt::DirectConnection); + read_timer.start(5); #endif (void) exec(); @@ -147,7 +149,7 @@ void hatire_thread::run() void hatire_thread::serial_debug_info_str(const QString& str) { - serial_debug_info(str.toUtf8()); + emit serial_debug_info(str.toLatin1()); } serial_result hatire_thread::init_serial_port_impl() @@ -166,8 +168,8 @@ serial_result hatire_thread::init_serial_port_impl() && com_port.setParity((QSerialPort::Parity)s.pParity) && com_port.setStopBits((QSerialPort::StopBits)s.pStopBits) && com_port.setFlowControl((QSerialPort::FlowControl)s.pFlowControl) + && com_port.setDataTerminalReady(s.pDTR) && com_port.clear(QSerialPort::AllDirections) - && com_port.setDataErrorPolicy(QSerialPort::IgnorePolicy) ) { Log(tr("Port Parameters set")); @@ -199,7 +201,7 @@ serial_result hatire_thread::init_serial_port_impl() } Log(tr("Waiting on init")); qDebug() << QTime::currentTime() << " HAT send INIT "; - sendcmd_str(s.CmdInit); + emit sendcmd_str(s.CmdInit); // Wait init MPU sequence for (int i = 1; i <= s.DelayStart; i+=50) { @@ -207,7 +209,7 @@ serial_result hatire_thread::init_serial_port_impl() } // Send START cmd to IMU qDebug() << QTime::currentTime() << " HAT send START "; - sendcmd_str(s.CmdStart); + emit sendcmd_str(s.CmdStart); // Wait start MPU sequence for (int i = 1; i <=s.DelaySeq; i+=50) @@ -240,13 +242,13 @@ void hatire_thread::serial_info_impl() if (com_port.isOpen()) { msg.append("\r\n"); - msg.append(com_port.portName()); + msg.append(com_port.portName().toUtf8()); msg.append("\r\n"); msg.append("BAUDRATE :"); - msg.append(QString::number(com_port.baudRate())); + msg.append(QString::number(com_port.baudRate()).toLatin1()); msg.append("\r\n"); msg.append("DataBits :"); - msg.append(QString::number(com_port.dataBits())); + msg.append(QString::number(com_port.dataBits()).toLatin1()); msg.append("\r\n"); msg.append("Parity :"); @@ -271,7 +273,6 @@ void hatire_thread::serial_info_impl() switch (com_port.stopBits()) { - msg.append(QString::number(com_port.stopBits())); case 1: msg.append("1 stop bit."); break; case 2: msg.append("2 stop bits."); @@ -303,34 +304,27 @@ void hatire_thread::serial_info_impl() void hatire_thread::on_serial_read() { + const int sz = (int)com_port.read(buf, sizeof(buf)); + + if (sz > 0) { QMutexLocker lck(&data_mtx); -#ifndef HATIRE_DEBUG_LOGFILE - data_read += com_port.readAll(); -#else - QByteArray tmp = com_port.read(30); - data_read += tmp; - if (tmp.length() == 0 && read_timer.isActive()) - { - qDebug() << "eof"; - read_timer.stop(); - } -#endif + data_read.append(buf, sz); } - - stat.input(timer.elapsed_ms()); - timer.start(); - - if (throttle_timer.elapsed_ms() >= 3000) +#if defined HATIRE_DEBUG_LOGFILE + else { - throttle_timer.start(); - qDebug() << "stat:" << "avg" << stat.avg() << "stddev" << stat.stddev(); + qDebug() << "eof"; + read_timer.stop(); } +#endif - if (s.serial_bug_workaround) + if (sz <= 0) { // qt can fire QSerialPort::readyRead() needlessly, causing a busy loop. // see https://github.com/opentrack/opentrack/issues/327#issuecomment-207941003 + + // this probably happens with flaky BT/usb-to-serial converters (?) constexpr int hz = 90; constexpr int ms = 1000/hz; portable::sleep(ms); |
