Skip to content

Commit

Permalink
update readme, update mdifyregister
Browse files Browse the repository at this point in the history
  • Loading branch information
BriFuture committed Oct 16, 2019
1 parent 99d7029 commit 48b91d9
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 23 deletions.
49 changes: 47 additions & 2 deletions 18-06to09/modifyregister/main.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
#include <QCoreApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include "registermodifier.h"
#include <iostream>

#include <QDebug>

#ifdef _MSC_VER
#pragma execution_character_set("utf-8")
#endif


int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QCoreApplication::setApplicationName("Com Property Modifier");
QCoreApplication::setApplicationVersion("1.0.0");

QCommandLineParser parser;
// parser.setOptionsAfterPositionalArgumentsMode(QCommandLineParser::ParseAsOptions);
// parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
parser.setApplicationDescription("Used for modify com delay.");
parser.addHelpOption();
parser.addVersionOption();

QCommandLineOption comOption({"c", "com"}, "specify com name (COM4 for example)", "portname");
parser.addOption(comOption);

QCommandLineOption delayOption({"d", "delay"}, "the delay value needs to be set.", "delay");
parser.addOption(delayOption);

QCommandLineOption deviceOption({{"D", "devices"}, "Read port name and latency of All Devices."});
parser.addOption(deviceOption);

parser.process(a);


RegisterModifier rm;
rm.modify();
// return a.exec();
if(parser.isSet(deviceOption)) {
int delay = rm.getLatency(parser.value(comOption));
std::cout << delay << std::endl;
return 0;
}

if(parser.isSet(delayOption)) {
rm.setLatency(parser.value(delayOption).toInt());
}

if(parser.isSet(comOption)) {
rm.setCom(parser.value(comOption));
rm.modify();
}

return 0;
}
6 changes: 3 additions & 3 deletions 18-06to09/modifyregister/modifyregister.pro
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ win32 {
QMAKE_LFLAGS += /MANIFESTUAC:\"level=\'requireAdministrator\' uiAccess=\'false\'\"
}

DISTFILES += \
manifest.xml

DISTFILES += manifest.xml
VERSION = 1.0.0.0
DESTDIR = $$OUT_PWD/app
84 changes: 66 additions & 18 deletions 18-06to09/modifyregister/registermodifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@


#include "registermodifier.h"
#include <QSettings>
#include <QDebug>
#include <QString>
#include <QFile>
#include <QTextStream>


const int maxLatency = 3;
const QString registerPath = QString("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\FTDIBUS");
const QString concretePath = QString("\\0000\\Device Parameters\\LatencyTimer");
const int RegisterModifier::defaultMaxLatency = 3;
const QString RegisterModifier::registerPath = QString("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Enum\\FTDIBUS");
const QString RegisterModifier::concretePath = QString("0000/Device Parameters");
const QString latencyKey = QString("LatencyTimer");
const QString nameKey = QString("PortName");

RegisterModifier::RegisterModifier()
{

latency = defaultMaxLatency;
set = new QSettings(registerPath, QSettings::NativeFormat);
}

void RegisterModifier::setCom(const QString &com)
Expand All @@ -35,26 +35,74 @@ void RegisterModifier::modify()
file.open(QFile::WriteOnly);
QTextStream ts(&file);

QSettings set( registerPath, QSettings::NativeFormat );
int latency = 0;
int currLatency = 0;
bool needModify = false;
foreach (QString device, set.allKeys()) {
if( device.endsWith( latencyKey ) ) {
latency = set.value( device ).toInt();
if( latency > maxLatency ) {
set.setValue( device, maxLatency );
set.sync();
latency = set.value( device ).toInt();
ts << device << " ==> " << latency;
qDebug() << device << " ==> " << latency;
needModify = true;
}
readDevices();
QMapIterator<QString, RegisterDevice> it(m_devices);
while(it.hasNext()) {
it.next();
const RegisterDevice &rd = it.value();
if(!com.isEmpty() && it.key() != com) {
continue;
}
QString key = getKey(rd.id, latencyKey);
currLatency = set->value(key).toInt();
if(currLatency > latency) {
QString info = QString("Set %1 latency, from %2 to %3").arg(it.key()).arg(currLatency).arg(latency);
qInfo() << info;
ts << info;
set->setValue(key, latency);
needModify = true;
}
}
set->sync();

if( !needModify ) {
ts << "No need to modify.";
qDebug() << "No need to modify.";
}
file.close();
}

void RegisterModifier::setLatency(int l) {
if(l < 1) {
l = 1;
}
latency = l;
}

int RegisterModifier::getLatency(const QString &com)
{
readDevices();
if(m_devices.contains(com)) {
return m_devices[com].latency;
}
return 16;
}

QString RegisterModifier::getKey(const QString &device, const QString &item)
{
return QString("%2/%3/%4").arg(device).arg(concretePath).arg(item);
}

void RegisterModifier::readDevices()
{
int slash;
RegisterDevice rd;
foreach (QString device, set->allKeys()) {
slash = device.indexOf("/");
if(slash > 0) {
QString name = device.left(slash);
QString portname = set->value(getKey(name, nameKey)).toString();
int latency = set->value(getKey(name, latencyKey)).toInt();
if(m_devices.contains(portname)) {
continue;
}
rd.id = name;
rd.latency = latency;
// qDebug() << name << portname << getKey(name, nameKey);
m_devices.insert(portname, rd);
}
}
}

23 changes: 23 additions & 0 deletions 18-06to09/modifyregister/registermodifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,39 @@
#define REGISTERMODIFIER_H

#include <QString>
#include <QStringList>
#include <QSettings>
#include <QMap>

class RegisterModifier
{
public:
static const int defaultMaxLatency;
static const QString registerPath;
static const QString concretePath;
static QString getKey(const QString &device, const QString &item);
struct RegisterDevice {
QString id;
int latency;
};

RegisterModifier();
void setCom(const QString &com);
void modify();

// QString

void setLatency(int l);

int getLatency(const QString &com);

protected:
void readDevices();
QString com;
int latency = 0;
QMap<QString, RegisterDevice> m_devices;

QSettings *set;
};

#endif // REGISTERMODIFIER_H
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [椭圆示例][oval-circle-example]
* [Qt Style 样式示例](./19-01to03/qt_style/readme.md)
* [Compassvue WebGL 3D 程序][compassvue-demo]
* [华为鲲鹏开发者比赛代码及模拟服务器](./19-q3q4/huawei kunpen race/README.md)

## About The code

Expand Down

0 comments on commit 48b91d9

Please sign in to comment.