diff --git a/companion/src/simulation/widgets/lcdwidget.cpp b/companion/src/simulation/widgets/lcdwidget.cpp index abb4830ec..5e9d3fa71 100644 --- a/companion/src/simulation/widgets/lcdwidget.cpp +++ b/companion/src/simulation/widgets/lcdwidget.cpp @@ -71,14 +71,26 @@ void LcdWidget::makeScreenshot(const QString &fileName) void LcdWidget::onLcdChanged(uint8_t* lcdBuf, bool light) { + // Ingest only: store the latest frame and let frameTimer drive the repaint. + // Sampling on a fixed cadence caps the repaint rate without dropping the + // final frame of a burst, regardless of how frames are delivered. QMutexLocker locker(&lcdMtx); lightEnable = light; if (lcdBuf) memcpy(localBuf, lcdBuf, lcdSize); - if (!redrawTimer.isValid() || - redrawTimer.hasExpired(LCD_WIDGET_REFRESH_PERIOD)) { - update(); - redrawTimer.start(); + dirty = true; + if (!frameTimer.isActive()) + frameTimer.start(); +} + +void LcdWidget::onFrameTick() +{ + QMutexLocker locker(&lcdMtx); + if (!dirty) { // idle: stop until the next frame arrives + frameTimer.stop(); + return; } + dirty = false; + update(); } void LcdWidget::doPaint(QPainter &p) diff --git a/companion/src/simulation/widgets/lcdwidget.h b/companion/src/simulation/widgets/lcdwidget.h index 261d609ba..b868d888f 100644 --- a/companion/src/simulation/widgets/lcdwidget.h +++ b/companion/src/simulation/widgets/lcdwidget.h @@ -26,15 +26,16 @@ #include #include #include -#include #include #include #include +#include #include #include "appdata.h" -#define LCD_WIDGET_REFRESH_PERIOD 16 // [ms] 16 = 62.5fps +#define LCD_WIDGET_REFRESH_PERIOD 10 // [ms] 10 = 100fps cap (color is + // firmware-limited to ~20fps anyway) class LcdWidget : public QWidget { @@ -50,6 +51,8 @@ class LcdWidget : public QWidget bgDefaultColor(QColor(148, 156, 149)), fgDefaultColor(QColor(0, 0, 0)) { + frameTimer.setInterval(LCD_WIDGET_REFRESH_PERIOD); + connect(&frameTimer, &QTimer::timeout, this, &LcdWidget::onFrameTick); } ~LcdWidget() @@ -84,8 +87,10 @@ class LcdWidget : public QWidget QColor bgDefaultColor; QColor fgDefaultColor; QMutex lcdMtx; - QElapsedTimer redrawTimer; + QTimer frameTimer; + bool dirty = false; + void onFrameTick(); void doPaint(QPainter &p); void paintEvent(QPaintEvent *) override;