From 2d7307a4f8b692af95cbb2b4dc9b59d6fdbdc7d7 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Mon, 13 Sep 2021 08:24:14 +0200 Subject: [PATCH 1/4] Fixed warnings --- companion/src/firmwares/eepromimportexport.h | 2 +- companion/src/firmwares/opentx/opentxeeprom.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/companion/src/firmwares/eepromimportexport.h b/companion/src/firmwares/eepromimportexport.h index 96eb35fbf..965810731 100644 --- a/companion/src/firmwares/eepromimportexport.h +++ b/companion/src/firmwares/eepromimportexport.h @@ -515,7 +515,7 @@ class StructField: public DataField { return result; } - virtual int dump(int level=0, int offset=0) + int dump(int level=0, int offset=0) override { for (int i=0; i > { { } - virtual void beforeExport() + void beforeExport() override { _switch = sw.toValue(); ConversionField< SignedField >::beforeExport(); @@ -477,7 +477,7 @@ class SourceField: public ConversionField< UnsignedField > { { } - virtual void beforeExport() + void beforeExport() override { _source = source.toValue(); ConversionField< UnsignedField >::beforeExport(); @@ -603,7 +603,7 @@ class CurveReferenceField: public TransformedField { internalField.Append(new SignedField<8>(this, _curve_value)); } - virtual void beforeExport() + void beforeExport() override { if (curve.value != 0) { _curve_type = (unsigned int)curve.type; @@ -729,7 +729,7 @@ class FlightModeField: public TransformedField { } } - virtual void beforeExport() + void beforeExport() override { for (int i=0; i= 218) { From 1b57e415be4788ae5aaa6f3a037055fd874997d4 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Mon, 13 Sep 2021 11:10:32 +0200 Subject: [PATCH 2/4] Add touch support to simulation --- .../src/simulation/simulateduiwidget.cpp | 1 + companion/src/simulation/simulatorinterface.h | 1 + companion/src/simulation/widgets/lcdwidget.h | 26 ++++++++++- radio/src/targets/simu/opentxsimulator.cpp | 45 +++++++++++++++++++ radio/src/targets/simu/opentxsimulator.h | 7 +++ 5 files changed, 79 insertions(+), 1 deletion(-) diff --git a/companion/src/simulation/simulateduiwidget.cpp b/companion/src/simulation/simulateduiwidget.cpp index c3db6f4ef..a2c257a19 100644 --- a/companion/src/simulation/simulateduiwidget.cpp +++ b/companion/src/simulation/simulateduiwidget.cpp @@ -197,6 +197,7 @@ void SimulatedUIWidget::setLcd(LcdWidget * lcd) m_backLight = 0; m_lcd->setBackgroundColor(m_backlightColors.at(m_backLight)); + connect(m_lcd, &LcdWidget::touchEvent, m_simulator, &SimulatorInterface::touchEvent); } void SimulatedUIWidget::connectScrollActions() diff --git a/companion/src/simulation/simulatorinterface.h b/companion/src/simulation/simulatorinterface.h index 9c641dccd..25adc9c6b 100644 --- a/companion/src/simulation/simulatorinterface.h +++ b/companion/src/simulation/simulatorinterface.h @@ -144,6 +144,7 @@ class SimulatorInterface : public QObject virtual void setTrainerInput(unsigned int inputNumber, int16_t value) = 0; virtual void setInputValue(int type, uint8_t index, int16_t value) = 0; virtual void rotaryEncoderEvent(int steps) = 0; + virtual void touchEvent(int type, int x, int y) = 0; virtual void setTrainerTimeout(uint16_t ms) = 0; virtual void sendTelemetry(const QByteArray data) = 0; virtual void setLuaStateReloadPermanentScripts() = 0; diff --git a/companion/src/simulation/widgets/lcdwidget.h b/companion/src/simulation/widgets/lcdwidget.h index 879ce3236..35f88cf27 100644 --- a/companion/src/simulation/widgets/lcdwidget.h +++ b/companion/src/simulation/widgets/lcdwidget.h @@ -29,6 +29,7 @@ #include #include #include +#include #include "appdata.h" #include "appdebugmessagehandler.h" @@ -41,6 +42,12 @@ class LcdWidget : public QWidget public: + enum TouchEvent { + TouchUp=0, + TouchDown, + TouchSlide + }; + LcdWidget(QWidget * parent = 0): QWidget(parent), lcdBuf(NULL), @@ -117,6 +124,9 @@ class LcdWidget : public QWidget } } + signals: + void touchEvent(int type, int x, int y); + protected: int lcdWidth; @@ -213,12 +223,26 @@ class LcdWidget : public QWidget } - void paintEvent(QPaintEvent*) + void paintEvent(QPaintEvent*) override { QPainter p(this); doPaint(p); } + void mouseMoveEvent(QMouseEvent *event) override + { + emit touchEvent(TouchSlide, event->pos().x(), event->pos().y()); + } + + void mousePressEvent(QMouseEvent *event) override + { + emit touchEvent(TouchDown, event->pos().x(), event->pos().y()); + } + + void mouseReleaseEvent(QMouseEvent *event) override + { + emit touchEvent(TouchUp, event->pos().x(), event->pos().y()); + } }; #endif // _LCDWIDGET_H_ diff --git a/radio/src/targets/simu/opentxsimulator.cpp b/radio/src/targets/simu/opentxsimulator.cpp index 49f58d380..a2cedccb2 100644 --- a/radio/src/targets/simu/opentxsimulator.cpp +++ b/radio/src/targets/simu/opentxsimulator.cpp @@ -299,6 +299,51 @@ void OpenTxSimulator::rotaryEncoderEvent(int steps) #endif // defined(ROTARY_ENCODER_NAVIGATION) } +void OpenTxSimulator::touchEvent(int type, int x, int y) +{ + switch (type) { + case TouchDown: + TRACE_WINDOWS("[Mouse Press] %d %d", x, y); + +#if defined(HARDWARE_TOUCH) + touchState.event = TE_DOWN; + touchState.startX = touchState.x = x; + touchState.startY = touchState.y = y; +#endif + break; + + case TouchUp: + TRACE_WINDOWS("[Mouse Release] %d %d", x, y); + +#if defined(HARDWARE_TOUCH) + if (touchState.event == TE_DOWN) { + touchState.event = TE_UP; + touchState.x = touchState.startX; + touchState.y = touchState.startY; + } else { + touchState.event = TE_SLIDE_END; + } +#endif + break; + + case TouchSlide: + TRACE_WINDOWS("[Mouse Move] %d %d", x, y); + +#if defined(HARDWARE_TOUCH) + touchState.deltaX += x - touchState.x; + touchState.deltaY += y - touchState.y; + if (touchState.event == TE_SLIDE || + abs(touchState.deltaX) >= SLIDE_RANGE || + abs(touchState.deltaY) >= SLIDE_RANGE) { + touchState.event = TE_SLIDE; + touchState.x = x; + touchState.y = y; + } +#endif + break; + } +} + void OpenTxSimulator::setTrainerTimeout(uint16_t ms) { ppmInputValidityTimer = ms; diff --git a/radio/src/targets/simu/opentxsimulator.h b/radio/src/targets/simu/opentxsimulator.h index 7369a56c3..94c4a97a4 100644 --- a/radio/src/targets/simu/opentxsimulator.h +++ b/radio/src/targets/simu/opentxsimulator.h @@ -35,6 +35,12 @@ class DLLEXPORT OpenTxSimulator : public SimulatorInterface public: + enum TouchEvent { + TouchUp=0, + TouchDown, + TouchSlide + }; + OpenTxSimulator(); virtual ~OpenTxSimulator(); @@ -64,6 +70,7 @@ class DLLEXPORT OpenTxSimulator : public SimulatorInterface virtual void setTrainerInput(unsigned int inputNumber, int16_t value); virtual void setInputValue(int type, uint8_t index, int16_t value); virtual void rotaryEncoderEvent(int steps); + virtual void touchEvent(int type, int x, int y); virtual void setTrainerTimeout(uint16_t ms); virtual void sendTelemetry(const QByteArray data); virtual void setLuaStateReloadPermanentScripts(); From 21b29899fdcb082bf9a4fac48b34dd824dde6346 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Mon, 13 Sep 2021 12:18:46 +0200 Subject: [PATCH 3/4] Moved LcdWidget implementation into own .cpp file --- companion/src/simulation/CMakeLists.txt | 1 + .../src/simulation/widgets/lcdwidget.cpp | 173 ++++++++++++++ companion/src/simulation/widgets/lcdwidget.h | 226 +++--------------- 3 files changed, 210 insertions(+), 190 deletions(-) create mode 100644 companion/src/simulation/widgets/lcdwidget.cpp diff --git a/companion/src/simulation/CMakeLists.txt b/companion/src/simulation/CMakeLists.txt index 209f38517..8963c0ef5 100644 --- a/companion/src/simulation/CMakeLists.txt +++ b/companion/src/simulation/CMakeLists.txt @@ -28,6 +28,7 @@ set(simulation_SRCS trainersimu.cpp widgets/radiowidget.cpp widgets/virtualjoystickwidget.cpp + widgets/lcdwidget.cpp ) set(simulation_UIS diff --git a/companion/src/simulation/widgets/lcdwidget.cpp b/companion/src/simulation/widgets/lcdwidget.cpp new file mode 100644 index 000000000..b01e0e62a --- /dev/null +++ b/companion/src/simulation/widgets/lcdwidget.cpp @@ -0,0 +1,173 @@ +/* + * Copyright (C) EdgeTX + * + * Based on code named + * opentx - https://github.com/opentx/opentx + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "lcdwidget.h" + +void LcdWidget::setData(unsigned char *buf, int width, int height, int depth) +{ + lcdBuf = buf; + lcdWidth = width; + lcdHeight = height; + lcdDepth = depth; + if (depth >= 8) + lcdSize = (width * height) * ((depth + 7) / 8); + else + lcdSize = (width * ((height + 7) / 8)) * depth; + + localBuf = (unsigned char *)malloc(lcdSize); + memset(localBuf, 0, lcdSize); +} + +void LcdWidget::setBgDefaultColor(const QColor &color) +{ + bgDefaultColor = color; +} + +void LcdWidget::setBackgroundColor(const QColor &color) { bgColor = color; } + +void LcdWidget::makeScreenshot(const QString &fileName) +{ + int width, height; + if (lcdDepth < 12) { + width = 2 * lcdWidth; + height = 2 * lcdHeight; + } else { + width = lcdWidth; + height = lcdHeight; + } + QPixmap buffer(width, height); + QPainter p(&buffer); + doPaint(p); + if (fileName.isEmpty()) { + QApplication::clipboard()->setPixmap(buffer); + qInfo() << "Screenshot saved to clipboard"; + } else { + buffer.toImage().save(fileName); + qInfo() << "Screenshot saved to:" << fileName; + } +} + +void LcdWidget::onLcdChanged(bool light) +{ + QMutexLocker locker(&lcdMtx); + lightEnable = light; + memcpy(localBuf, lcdBuf, lcdSize); + if (!redrawTimer.isValid() || + redrawTimer.hasExpired(LCD_WIDGET_REFRESH_PERIOD)) { + update(); + redrawTimer.start(); + } +} + +void LcdWidget::doPaint(QPainter &p) +{ + QRgb rgb; + uint16_t z; + + if (!localBuf) return; + + if (lcdDepth == 16) { + for (int x = 0; x < lcdWidth; x++) { + for (int y = 0; y < lcdHeight; y++) { + z = ((uint16_t *)localBuf)[y * lcdWidth + x]; + rgb = qRgb(255 * ((z & 0xF800) >> 11) / 0x1F, + 255 * ((z & 0x07E0) >> 5) / 0x3F, 255 * (z & 0x001F) / 0x1F); + p.setPen(rgb); + p.drawPoint(x, y); + } + } + return; + } + if (lcdDepth == 12) { + for (int x = 0; x < lcdWidth; x++) { + for (int y = 0; y < lcdHeight; y++) { + z = ((uint16_t *)localBuf)[y * lcdWidth + x]; + rgb = qRgb(255 * ((z & 0xF00) >> 8) / 0x0F, + 255 * ((z & 0x0F0) >> 4) / 0x0F, 255 * (z & 0x00F) / 0x0F); + p.setPen(rgb); + p.drawPoint(x, y); + } + } + return; + } + + QColor bg; + if (lightEnable) + bg = bgColor; + else + bg = bgDefaultColor; + + p.setBackground(QBrush(bg)); + p.eraseRect(0, 0, 2 * lcdWidth, 2 * lcdHeight); + + if (lcdDepth == 1) { + rgb = qRgb(0, 0, 0); + p.setPen(rgb); + p.setBrush(QBrush(rgb)); + } + + uint16_t idx, mask; + uint16_t previousDepth = 0xFF; + + for (int y = 0; y < lcdHeight; y++) { + idx = (y * lcdDepth / 8) * lcdWidth; + mask = (1 << (y % 8)); + for (int x = 0; x < lcdWidth; x++, idx++) { + if (lcdDepth == 1) { + if (localBuf[idx] & mask) p.drawRect(2 * x, 2 * y, 1, 1); + continue; + } + // lcdDepth == 4 + z = (y & 1) ? (localBuf[idx] >> 4) : (localBuf[idx] & 0x0F); + if (!z) continue; + if (z != previousDepth) { + previousDepth = z; + rgb = qRgb(bg.red() - (z * bg.red()) / 15, + bg.green() - (z * bg.green()) / 15, + bg.blue() - (z * bg.blue()) / 15); + p.setPen(rgb); + p.setBrush(QBrush(rgb)); + } + p.drawRect(2 * x, 2 * y, 1, 1); + } + } +} + +void LcdWidget::paintEvent(QPaintEvent *) +{ + QPainter p(this); + doPaint(p); +} + +void LcdWidget::mouseMoveEvent(QMouseEvent *event) +{ + emit touchEvent(TouchSlide, event->pos().x(), event->pos().y()); +} + +void LcdWidget::mousePressEvent(QMouseEvent *event) +{ + emit touchEvent(TouchDown, event->pos().x(), event->pos().y()); +} + +void LcdWidget::mouseReleaseEvent(QMouseEvent *event) +{ + emit touchEvent(TouchUp, event->pos().x(), event->pos().y()); +} diff --git a/companion/src/simulation/widgets/lcdwidget.h b/companion/src/simulation/widgets/lcdwidget.h index 35f88cf27..107b94866 100644 --- a/companion/src/simulation/widgets/lcdwidget.h +++ b/companion/src/simulation/widgets/lcdwidget.h @@ -18,8 +18,7 @@ * GNU General Public License for more details. */ -#ifndef _LCDWIDGET_H_ -#define _LCDWIDGET_H_ +#pragma once #include #include @@ -40,209 +39,56 @@ class LcdWidget : public QWidget { Q_OBJECT - public: + public: + enum TouchEvent { TouchUp = 0, TouchDown, TouchSlide }; - enum TouchEvent { - TouchUp=0, - TouchDown, - TouchSlide - }; - - LcdWidget(QWidget * parent = 0): + LcdWidget(QWidget *parent = 0) : QWidget(parent), lcdBuf(NULL), localBuf(NULL), lightEnable(false), bgDefaultColor(QColor(198, 208, 199)) - { + { + } + + ~LcdWidget() + { + if (localBuf) { + free(localBuf); } + } - ~LcdWidget() - { - if (localBuf) { - free(localBuf); - } - } + void setData(unsigned char *buf, int width, int height, int depth = 1); + void setBgDefaultColor(const QColor &color); + void setBackgroundColor(const QColor &color); - void setData(unsigned char *buf, int width, int height, int depth=1) - { - lcdBuf = buf; - lcdWidth = width; - lcdHeight = height; - lcdDepth = depth; - if (depth >= 8) - lcdSize = (width * height) * ((depth+7) / 8); - else - lcdSize = (width * ((height+7)/8)) * depth; + void makeScreenshot(const QString &fileName); - localBuf = (unsigned char *)malloc(lcdSize); - memset(localBuf, 0, lcdSize); - } + void onLcdChanged(bool light); - void setBgDefaultColor(const QColor & color) - { - bgDefaultColor = color; - } + signals: + void touchEvent(int type, int x, int y); - void setBackgroundColor(const QColor & color) - { - bgColor = color; - } + protected: + int lcdWidth; + int lcdHeight; + int lcdDepth; + int lcdSize; - void makeScreenshot(const QString & fileName) - { - int width, height; - if (lcdDepth < 12) { - width = 2 * lcdWidth; - height = 2 * lcdHeight; - } - else { - width = lcdWidth; - height = lcdHeight; - } - QPixmap buffer(width, height); - QPainter p(&buffer); - doPaint(p); - if (fileName.isEmpty()) { - QApplication::clipboard()->setPixmap( buffer ); - qInfo() << "Screenshot saved to clipboard"; - } - else { - buffer.toImage().save(fileName); - qInfo() << "Screenshot saved to:" << fileName; - } - } + unsigned char *lcdBuf; + unsigned char *localBuf; - void onLcdChanged(bool light) - { - QMutexLocker locker(&lcdMtx); - lightEnable = light; - memcpy(localBuf, lcdBuf, lcdSize); - if (!redrawTimer.isValid() || redrawTimer.hasExpired(LCD_WIDGET_REFRESH_PERIOD)) { - update(); - redrawTimer.start(); - } - } + bool lightEnable; + QColor bgColor; + QColor bgDefaultColor; + QMutex lcdMtx; + QElapsedTimer redrawTimer; - signals: - void touchEvent(int type, int x, int y); - - protected: + void doPaint(QPainter &p); - int lcdWidth; - int lcdHeight; - int lcdDepth; - int lcdSize; + void paintEvent(QPaintEvent *) override; - unsigned char *lcdBuf; - unsigned char *localBuf; - - bool lightEnable; - QColor bgColor; - QColor bgDefaultColor; - QMutex lcdMtx; - QElapsedTimer redrawTimer; - - inline void doPaint(QPainter & p) - { - QRgb rgb; - uint16_t z; - - if (!localBuf) - return; - - if (lcdDepth == 16) { - for (int x = 0; x < lcdWidth; x++) { - for (int y = 0; y < lcdHeight; y++) { - z = ((uint16_t *)localBuf)[y * lcdWidth + x]; - rgb = qRgb(255 * ((z & 0xF800) >> 11) / 0x1F, - 255 * ((z & 0x07E0) >> 5) / 0x3F, - 255 * (z & 0x001F) / 0x1F); - p.setPen(rgb); - p.drawPoint(x, y); - } - } - return; - } - if (lcdDepth == 12) { - for (int x = 0; x < lcdWidth; x++) { - for (int y = 0; y < lcdHeight; y++) { - z = ((uint16_t *)localBuf)[y * lcdWidth + x]; - rgb = qRgb(255 * ((z & 0xF00) >> 8) / 0x0F, - 255 * ((z & 0x0F0) >> 4) / 0x0F, - 255 * (z & 0x00F) / 0x0F); - p.setPen(rgb); - p.drawPoint(x, y); - } - } - return; - } - - QColor bg; - if (lightEnable) - bg = bgColor; - else - bg = bgDefaultColor; - - p.setBackground(QBrush(bg)); - p.eraseRect(0, 0, 2*lcdWidth, 2*lcdHeight); - - if (lcdDepth == 1) { - rgb = qRgb(0, 0, 0); - p.setPen(rgb); - p.setBrush(QBrush(rgb)); - } - - uint16_t idx, mask; - uint16_t previousDepth = 0xFF; - - for (int y = 0; y < lcdHeight; y++) { - idx = (y * lcdDepth / 8) * lcdWidth; - mask = (1 << (y % 8)); - for (int x = 0; x < lcdWidth; x++, idx++) { - if (lcdDepth == 1) { - if (localBuf[idx] & mask) - p.drawRect(2 * x, 2 * y, 1, 1); - continue; - } - // lcdDepth == 4 - z = (y & 1) ? (localBuf[idx] >> 4) : (localBuf[idx] & 0x0F); - if (!z) - continue; - if (z != previousDepth) { - previousDepth = z; - rgb = qRgb(bg.red() - (z * bg.red()) / 15, - bg.green() - (z * bg.green()) / 15, - bg.blue() - (z * bg.blue()) / 15); - p.setPen(rgb); - p.setBrush(QBrush(rgb)); - } - p.drawRect(2*x, 2*y, 1, 1); - } - } - - } - - void paintEvent(QPaintEvent*) override - { - QPainter p(this); - doPaint(p); - } - - void mouseMoveEvent(QMouseEvent *event) override - { - emit touchEvent(TouchSlide, event->pos().x(), event->pos().y()); - } - - void mousePressEvent(QMouseEvent *event) override - { - emit touchEvent(TouchDown, event->pos().x(), event->pos().y()); - } - - void mouseReleaseEvent(QMouseEvent *event) override - { - emit touchEvent(TouchUp, event->pos().x(), event->pos().y()); - } + void mouseMoveEvent(QMouseEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; }; - -#endif // _LCDWIDGET_H_ From 669e33e7f2b5d9bb329f6b6778e383e3b1c5c234 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Mon, 13 Sep 2021 12:56:33 +0200 Subject: [PATCH 4/4] Add NV14 to the simulator targets --- tools/build-companion-nightly.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tools/build-companion-nightly.sh b/tools/build-companion-nightly.sh index 3ffca32de..38c8f532b 100755 --- a/tools/build-companion-nightly.sh +++ b/tools/build-companion-nightly.sh @@ -101,6 +101,10 @@ cmake ${COMMON_OPTIONS} -DPCB=X9E ${SRCDIR} make -j${JOBS} libsimulator rm CMakeCache.txt +cmake ${COMMON_OPTIONS} -DPCB=NV14 ${SRCDIR} +make -j${JOBS} libsimulator +rm CMakeCache.txt + cmake ${COMMON_OPTIONS} -DPCB=X10 ${SRCDIR} make -j${JOBS} libsimulator rm CMakeCache.txt