Proxmark3GUI/common/util.cpp

53 lines
1.2 KiB
C++
Raw Permalink Normal View History

2020-04-22 16:42:58 +08:00
#include "util.h"
Util::Util(QObject *parent) : QObject(parent)
{
2020-04-22 23:13:00 +08:00
isRequiringOutput = false;
requiredOutput = new QString();
timeStamp = QTime::currentTime();
2020-04-22 21:14:33 +08:00
}
2020-04-22 16:42:58 +08:00
2020-04-22 21:14:33 +08:00
void Util::processOutput(QString output)
{
2020-04-23 17:50:20 +08:00
// qDebug() << "Util::processOutput:" << output;
2020-04-22 21:14:33 +08:00
if(isRequiringOutput)
{
requiredOutput->append(output);
2020-04-22 23:13:00 +08:00
timeStamp = QTime::currentTime();
2020-04-22 21:14:33 +08:00
}
emit refreshOutput(output);
2020-04-22 16:42:58 +08:00
}
2020-04-22 21:14:33 +08:00
void Util::execCMD(QString cmd)
2020-04-22 16:42:58 +08:00
{
2020-04-22 21:14:33 +08:00
qDebug() << cmd;
emit write(cmd + "\r\n");
}
2020-04-22 16:42:58 +08:00
2020-04-22 21:14:33 +08:00
QString Util::execCMDWithOutput(QString cmd, unsigned long timeout)
{
2020-04-22 23:13:00 +08:00
QTime currTime = QTime::currentTime();
2020-04-22 21:14:33 +08:00
QTime targetTime = QTime::currentTime().addMSecs(timeout);
2020-04-22 23:13:00 +08:00
isRequiringOutput = true;
2020-04-22 21:14:33 +08:00
requiredOutput->clear();
execCMD(cmd);
while( QTime::currentTime() < targetTime)
{
QApplication::processEvents();
2020-04-22 23:13:00 +08:00
if(timeStamp > currTime)
2020-04-22 21:14:33 +08:00
{
2020-04-22 23:13:00 +08:00
currTime = timeStamp;
targetTime = timeStamp.addMSecs(timeout);
2020-04-22 21:14:33 +08:00
}
}
2020-04-22 23:13:00 +08:00
isRequiringOutput = false;
2020-04-22 21:14:33 +08:00
return *requiredOutput;
}
void Util::delay(unsigned int msec)
{
QTime timer = QTime::currentTime().addMSecs(msec);
while( QTime::currentTime() < timer )
QApplication::processEvents(QEventLoop::AllEvents, 100);
2020-04-22 16:42:58 +08:00
}