From fd01376f1e3505a0e196314709af04faeaf60559 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Tue, 27 Dec 2016 22:00:23 +0100 Subject: [PATCH] Reduced Companion simulator CPU usage by a factor of 5. (#4167) --- companion/src/simulation/simulatordialog.cpp | 24 ++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/companion/src/simulation/simulatordialog.cpp b/companion/src/simulation/simulatordialog.cpp index e0d5281fd..c38f82908 100644 --- a/companion/src/simulation/simulatordialog.cpp +++ b/companion/src/simulation/simulatordialog.cpp @@ -478,6 +478,7 @@ void SimulatorDialog::setupGVarsDisplay() if ((i % 2) ==0 ) { value->setStyleSheet("QLabel { background-color: rgb(220, 220, 220) }"); } + value->setText("0"); gvarValues << value; gvarsLayout->addWidget(value, i+1, fm+1); } @@ -620,12 +621,17 @@ inline int chVal(int val) void SimulatorDialog::setValues() { + static const int numOutputs = GetCurrentFirmware()->getCapability(Outputs); + static const int numLogicalSwitches = GetCurrentFirmware()->getCapability(LogicalSwitches); + static TxOutputs prevOutputs; + static unsigned int prevPhase = -1; + TxOutputs outputs; simulator->getValues(outputs); Trims trims; simulator->getTrims(trims); - for (int i=0; igetCapability(Outputs); i++) { + for (int i=0; isetValue(chVal(outputs.chans[i])); channelValues[i]->setText(QString("%1").arg((qreal)outputs.chans[i]*100/1024, 0, 'f', 1)); @@ -635,19 +641,29 @@ void SimulatorDialog::setValues() QString CSWITCH_ON = "QLabel { background-color: #4CC417 }"; QString CSWITCH_OFF = "QLabel { }"; - for (int i=0; igetCapability(LogicalSwitches); i++) { - logicalSwitchLabels[i]->setStyleSheet(outputs.vsw[i] ? CSWITCH_ON : CSWITCH_OFF); + for (int i=0; isetStyleSheet(outputs.vsw[i] ? CSWITCH_ON : CSWITCH_OFF); + } } for (unsigned int gv=0; gvsetText(QString((fm==lastPhase)?"%1":"%1").arg(outputs.gvars[fm][gv])); + if (prevPhase != lastPhase || prevOutputs.gvars[fm][gv] != outputs.gvars[fm][gv]) { + // same trick for GVARS, but this has far less effect on CPU usage as setStyleSheet() + gvarValues[gv*numFlightModes+fm]->setText(QString((fm==lastPhase)?"%1":"%1").arg(outputs.gvars[fm][gv])); + } } } if (outputs.beep) { beepVal = outputs.beep; } + + prevOutputs = outputs; + prevPhase = lastPhase; } #ifdef JOYSTICKS