diff options
Diffstat (limited to 'tracker-hatire/thread.cpp')
-rw-r--r-- | tracker-hatire/thread.cpp | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/tracker-hatire/thread.cpp b/tracker-hatire/thread.cpp index 6025d74c..7d61a90a 100644 --- a/tracker-hatire/thread.cpp +++ b/tracker-hatire/thread.cpp @@ -1,13 +1,15 @@ #include "thread.hpp" -#include "compat/sleep.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) { @@ -25,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"); @@ -43,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 @@ -69,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(); @@ -109,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"); @@ -118,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(); } } @@ -146,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() @@ -165,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")); @@ -198,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) { @@ -206,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) @@ -239,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 :"); @@ -301,13 +304,10 @@ void hatire_thread::serial_info_impl() void hatire_thread::on_serial_read() { - const int sz = com_port.read(buf, sizeof(buf)); + const int sz = (int)com_port.read(buf, sizeof(buf)); if (sz > 0) { - stat.input(timer.elapsed_ms()); - timer.start(); - QMutexLocker lck(&data_mtx); data_read.append(buf, sz); } @@ -319,20 +319,16 @@ void hatire_thread::on_serial_read() } #endif - if (s.serial_bug_workaround || sz <= 0) + 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); } - - if (throttle_timer.elapsed_ms() >= 3000) - { - throttle_timer.start(); - qDebug() << "stat:" << "avg" << stat.avg() << "stddev" << stat.stddev(); - } } QByteArray& hatire_thread::send_data_read_nolock() |