From 1c3c64f4475efc44cb758e0964a9eae81cd83f6a Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 13 Aug 2023 22:12:38 +0200 Subject: [PATCH 01/11] Qt Simulation --- simulation/VirtualLedBoard/.gitignore | 73 +++++++++++++++++++ .../VirtualLedBoard/VirtualLedBoard.pro | 27 +++++++ .../VirtualLedBoard/VirtualLedBoard_en_150.ts | 3 + simulation/VirtualLedBoard/main.cpp | 11 +++ simulation/VirtualLedBoard/mainwindow.cpp | 15 ++++ simulation/VirtualLedBoard/mainwindow.h | 21 ++++++ simulation/VirtualLedBoard/mainwindow.ui | 22 ++++++ 7 files changed, 172 insertions(+) create mode 100644 simulation/VirtualLedBoard/.gitignore create mode 100644 simulation/VirtualLedBoard/VirtualLedBoard.pro create mode 100644 simulation/VirtualLedBoard/VirtualLedBoard_en_150.ts create mode 100644 simulation/VirtualLedBoard/main.cpp create mode 100644 simulation/VirtualLedBoard/mainwindow.cpp create mode 100644 simulation/VirtualLedBoard/mainwindow.h create mode 100644 simulation/VirtualLedBoard/mainwindow.ui diff --git a/simulation/VirtualLedBoard/.gitignore b/simulation/VirtualLedBoard/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/simulation/VirtualLedBoard/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/simulation/VirtualLedBoard/VirtualLedBoard.pro b/simulation/VirtualLedBoard/VirtualLedBoard.pro new file mode 100644 index 0000000..e403327 --- /dev/null +++ b/simulation/VirtualLedBoard/VirtualLedBoard.pro @@ -0,0 +1,27 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + mainwindow.cpp + +HEADERS += \ + mainwindow.h + +FORMS += \ + mainwindow.ui + +TRANSLATIONS += \ + VirtualLedBoard_en_150.ts + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/simulation/VirtualLedBoard/VirtualLedBoard_en_150.ts b/simulation/VirtualLedBoard/VirtualLedBoard_en_150.ts new file mode 100644 index 0000000..eb9feca --- /dev/null +++ b/simulation/VirtualLedBoard/VirtualLedBoard_en_150.ts @@ -0,0 +1,3 @@ + + + diff --git a/simulation/VirtualLedBoard/main.cpp b/simulation/VirtualLedBoard/main.cpp new file mode 100644 index 0000000..fd3e533 --- /dev/null +++ b/simulation/VirtualLedBoard/main.cpp @@ -0,0 +1,11 @@ +#include "mainwindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/simulation/VirtualLedBoard/mainwindow.cpp b/simulation/VirtualLedBoard/mainwindow.cpp new file mode 100644 index 0000000..41a26bd --- /dev/null +++ b/simulation/VirtualLedBoard/mainwindow.cpp @@ -0,0 +1,15 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + diff --git a/simulation/VirtualLedBoard/mainwindow.h b/simulation/VirtualLedBoard/mainwindow.h new file mode 100644 index 0000000..4643e32 --- /dev/null +++ b/simulation/VirtualLedBoard/mainwindow.h @@ -0,0 +1,21 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; +#endif // MAINWINDOW_H diff --git a/simulation/VirtualLedBoard/mainwindow.ui b/simulation/VirtualLedBoard/mainwindow.ui new file mode 100644 index 0000000..b232854 --- /dev/null +++ b/simulation/VirtualLedBoard/mainwindow.ui @@ -0,0 +1,22 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + + + From 3aace88c0b92ba2a899f7853b84d00f060071439 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 13 Aug 2023 22:38:07 +0200 Subject: [PATCH 02/11] Example UDP server project --- .../VirtualLedBoard/VirtualLedBoard.pro | 7 ++-- simulation/VirtualLedBoard/udpserver.cpp | 35 +++++++++++++++++++ simulation/VirtualLedBoard/udpserver.h | 22 ++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 simulation/VirtualLedBoard/udpserver.cpp create mode 100644 simulation/VirtualLedBoard/udpserver.h diff --git a/simulation/VirtualLedBoard/VirtualLedBoard.pro b/simulation/VirtualLedBoard/VirtualLedBoard.pro index e403327..4ba635f 100644 --- a/simulation/VirtualLedBoard/VirtualLedBoard.pro +++ b/simulation/VirtualLedBoard/VirtualLedBoard.pro @@ -1,4 +1,5 @@ QT += core gui +QT += network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -10,10 +11,12 @@ CONFIG += c++11 SOURCES += \ main.cpp \ - mainwindow.cpp + mainwindow.cpp \ + udpserver.cpp HEADERS += \ - mainwindow.h + mainwindow.h \ + udpserver.h FORMS += \ mainwindow.ui diff --git a/simulation/VirtualLedBoard/udpserver.cpp b/simulation/VirtualLedBoard/udpserver.cpp new file mode 100644 index 0000000..b884803 --- /dev/null +++ b/simulation/VirtualLedBoard/udpserver.cpp @@ -0,0 +1,35 @@ +#include "udpserver.h" +#include +#include + +#define UDP_IMAGE_PORT 4242 + +UdpServer::UdpServer(QObject *parent) + : QAbstractItemModel(parent) +{ + initSocket(); +} + +void UdpServer::initSocket() +{ + this->mUdpSocket = new QUdpSocket(this); + this->mUdpSocket->bind(QHostAddress::LocalHost, UDP_IMAGE_PORT); + + connect(this->mUdpSocket, &QUdpSocket::readyRead, + this, &UdpServer::readPendingDatagrams); +} + + +void UdpServer::readPendingDatagrams() +{ + while (this->mUdpSocket->hasPendingDatagrams()) { + QNetworkDatagram datagram = this->mUdpSocket->receiveDatagram(); + processTheDatagram(datagram); + } +} + +void UdpServer::processTheDatagram(QNetworkDatagram datagram) { + if (datagram.isValid()) { + qDebug() << "Received datagram:" << datagram.data().size(); + } +} diff --git a/simulation/VirtualLedBoard/udpserver.h b/simulation/VirtualLedBoard/udpserver.h new file mode 100644 index 0000000..8c91894 --- /dev/null +++ b/simulation/VirtualLedBoard/udpserver.h @@ -0,0 +1,22 @@ +#ifndef UDPSERVER_H +#define UDPSERVER_H + +#include +#include + +class UdpServer : public QAbstractItemModel +{ + Q_OBJECT + +public: + explicit UdpServer(QObject *parent = nullptr); + + +private: + void initSocket(); + void readPendingDatagrams(); + QUdpSocket *mUdpSocket; + void processTheDatagram(QNetworkDatagram datagram); +}; + +#endif // UDPSERVER_H From 488a8ed5dd7feaa1502524560e4fa307b28f24b2 Mon Sep 17 00:00:00 2001 From: Ollo Date: Mon, 14 Aug 2023 13:53:34 +0200 Subject: [PATCH 03/11] UDP Server is started --- simulation/VirtualLedBoard/mainwindow.cpp | 1 + simulation/VirtualLedBoard/mainwindow.h | 2 ++ simulation/VirtualLedBoard/udpserver.cpp | 12 ++++++------ simulation/VirtualLedBoard/udpserver.h | 4 ++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/simulation/VirtualLedBoard/mainwindow.cpp b/simulation/VirtualLedBoard/mainwindow.cpp index 41a26bd..7f7f6fa 100644 --- a/simulation/VirtualLedBoard/mainwindow.cpp +++ b/simulation/VirtualLedBoard/mainwindow.cpp @@ -6,6 +6,7 @@ MainWindow::MainWindow(QWidget *parent) , ui(new Ui::MainWindow) { ui->setupUi(this); + this->server = new UdpLedServer (); } MainWindow::~MainWindow() diff --git a/simulation/VirtualLedBoard/mainwindow.h b/simulation/VirtualLedBoard/mainwindow.h index 4643e32..fe5b0ba 100644 --- a/simulation/VirtualLedBoard/mainwindow.h +++ b/simulation/VirtualLedBoard/mainwindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #include +#include "udpserver.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -17,5 +18,6 @@ public: private: Ui::MainWindow *ui; + UdpLedServer *server; }; #endif // MAINWINDOW_H diff --git a/simulation/VirtualLedBoard/udpserver.cpp b/simulation/VirtualLedBoard/udpserver.cpp index b884803..9887cea 100644 --- a/simulation/VirtualLedBoard/udpserver.cpp +++ b/simulation/VirtualLedBoard/udpserver.cpp @@ -4,23 +4,23 @@ #define UDP_IMAGE_PORT 4242 -UdpServer::UdpServer(QObject *parent) - : QAbstractItemModel(parent) +UdpLedServer ::UdpLedServer (QObject *parent) + : QObject(parent) { initSocket(); } -void UdpServer::initSocket() +void UdpLedServer ::initSocket() { this->mUdpSocket = new QUdpSocket(this); this->mUdpSocket->bind(QHostAddress::LocalHost, UDP_IMAGE_PORT); connect(this->mUdpSocket, &QUdpSocket::readyRead, - this, &UdpServer::readPendingDatagrams); + this, &UdpLedServer ::readPendingDatagrams); } -void UdpServer::readPendingDatagrams() +void UdpLedServer ::readPendingDatagrams() { while (this->mUdpSocket->hasPendingDatagrams()) { QNetworkDatagram datagram = this->mUdpSocket->receiveDatagram(); @@ -28,7 +28,7 @@ void UdpServer::readPendingDatagrams() } } -void UdpServer::processTheDatagram(QNetworkDatagram datagram) { +void UdpLedServer::processTheDatagram(QNetworkDatagram datagram) { if (datagram.isValid()) { qDebug() << "Received datagram:" << datagram.data().size(); } diff --git a/simulation/VirtualLedBoard/udpserver.h b/simulation/VirtualLedBoard/udpserver.h index 8c91894..8acbe3f 100644 --- a/simulation/VirtualLedBoard/udpserver.h +++ b/simulation/VirtualLedBoard/udpserver.h @@ -4,12 +4,12 @@ #include #include -class UdpServer : public QAbstractItemModel +class UdpLedServer : public QObject { Q_OBJECT public: - explicit UdpServer(QObject *parent = nullptr); + explicit UdpLedServer (QObject *parent = nullptr); private: From 980ef0812f5992527638d7d3fb92fa1df413b5c0 Mon Sep 17 00:00:00 2001 From: Ollo Date: Tue, 15 Aug 2023 00:09:29 +0200 Subject: [PATCH 04/11] LED framebuffer connected to GUI --- simulation/VirtualLedBoard/mainwindow.cpp | 20 +++++++++++++++ simulation/VirtualLedBoard/mainwindow.h | 18 ++++++++++++++ simulation/VirtualLedBoard/mainwindow.ui | 30 +++++++++++++++++++++-- 3 files changed, 66 insertions(+), 2 deletions(-) diff --git a/simulation/VirtualLedBoard/mainwindow.cpp b/simulation/VirtualLedBoard/mainwindow.cpp index 7f7f6fa..e31bdca 100644 --- a/simulation/VirtualLedBoard/mainwindow.cpp +++ b/simulation/VirtualLedBoard/mainwindow.cpp @@ -1,12 +1,21 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include +#include +#include +#include + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); this->server = new UdpLedServer (); + + this->mOffscreenDiagram = new QImage(DEFAULT_WIDTH, DEFAULT_HEIGHT, QImage::Format_RGB32); + this->mOffscreenDiagram->fill(COLOR_BACKGROUND); + } MainWindow::~MainWindow() @@ -14,3 +23,14 @@ MainWindow::~MainWindow() delete ui; } +void MainWindow::drawImage(QImage *target) { + QGraphicsView *graphicsView = new QGraphicsView(); + graphicsView->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); + QGraphicsScene* scene=new QGraphicsScene() ; + graphicsView->setScene(scene); + QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(*(target))); + scene->addItem(item); + graphicsView->show(); + this->ui->ledPanel->addWidget(graphicsView); +} + diff --git a/simulation/VirtualLedBoard/mainwindow.h b/simulation/VirtualLedBoard/mainwindow.h index fe5b0ba..b0ad118 100644 --- a/simulation/VirtualLedBoard/mainwindow.h +++ b/simulation/VirtualLedBoard/mainwindow.h @@ -2,8 +2,24 @@ #define MAINWINDOW_H #include +#include #include "udpserver.h" + +#define MAXIMUM_PANELSIZE 5 +#define PANEL_WIDTH 32 +#define PANEL_HEIGHT 40 + +#define LED_DIAMETER 5 +#define LED_DISTANCE (LED_DIAMETER + 2) + +#define DEFAULT_WIDTH (LED_DISTANCE * MAXIMUM_PANELSIZE * PANEL_WIDTH) +#define DEFAULT_HEIGHT (LED_DISTANCE * PANEL_HEIGHT) + + +#define COLOR_BACKGROUND Qt::black +#define COLOR_FOREGROUND Qt::orange + QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE @@ -19,5 +35,7 @@ public: private: Ui::MainWindow *ui; UdpLedServer *server; + QImage *mOffscreenDiagram; + void drawImage(QImage *image); }; #endif // MAINWINDOW_H diff --git a/simulation/VirtualLedBoard/mainwindow.ui b/simulation/VirtualLedBoard/mainwindow.ui index b232854..a181b2b 100644 --- a/simulation/VirtualLedBoard/mainwindow.ui +++ b/simulation/VirtualLedBoard/mainwindow.ui @@ -13,8 +13,34 @@ MainWindow - - + + + + 0 + 0 + + + + + + 10 + 0 + 781 + 441 + + + + + + + + 0 + 0 + 800 + 22 + + + From c15368b35e2d6b3a2779d5ed2db6da8cef28b6bc Mon Sep 17 00:00:00 2001 From: Ollo Date: Tue, 15 Aug 2023 12:51:35 +0200 Subject: [PATCH 05/11] LEDs are drawn --- simulation/VirtualLedBoard/mainwindow.cpp | 16 ++++++++++++++-- simulation/VirtualLedBoard/mainwindow.h | 8 ++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/simulation/VirtualLedBoard/mainwindow.cpp b/simulation/VirtualLedBoard/mainwindow.cpp index e31bdca..184d8f0 100644 --- a/simulation/VirtualLedBoard/mainwindow.cpp +++ b/simulation/VirtualLedBoard/mainwindow.cpp @@ -13,9 +13,21 @@ MainWindow::MainWindow(QWidget *parent) ui->setupUi(this); this->server = new UdpLedServer (); - this->mOffscreenDiagram = new QImage(DEFAULT_WIDTH, DEFAULT_HEIGHT, QImage::Format_RGB32); - this->mOffscreenDiagram->fill(COLOR_BACKGROUND); + this->mOffscreenPanel = new QImage(DEFAULT_WIDTH, DEFAULT_HEIGHT, QImage::Format_RGB32); + this->mOffscreenPanel->fill(COLOR_BACKGROUND); + /* draw inital screen */ + QPainter painter(this->mOffscreenPanel); + painter.setRenderHint(QPainter::Antialiasing, true); + painter.setPen(QPen(COLOR_FOREGROUND, 1)); + painter.setBrush(COLOR_FOREGROUND); + for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { + for(int y=0; y < PANEL_HEIGHT; y++) { + painter.drawEllipse(LED_DIAMETER/2 + (x* LED_DISTANCE), LED_DIAMETER/2 + (y * LED_DISTANCE), LED_DIAMETER, LED_DIAMETER); + } + } + + this->drawImage(this->mOffscreenPanel); } MainWindow::~MainWindow() diff --git a/simulation/VirtualLedBoard/mainwindow.h b/simulation/VirtualLedBoard/mainwindow.h index b0ad118..eec930b 100644 --- a/simulation/VirtualLedBoard/mainwindow.h +++ b/simulation/VirtualLedBoard/mainwindow.h @@ -10,15 +10,15 @@ #define PANEL_WIDTH 32 #define PANEL_HEIGHT 40 -#define LED_DIAMETER 5 -#define LED_DISTANCE (LED_DIAMETER + 2) +#define LED_DIAMETER 8 +#define LED_DISTANCE (LED_DIAMETER + 3) #define DEFAULT_WIDTH (LED_DISTANCE * MAXIMUM_PANELSIZE * PANEL_WIDTH) #define DEFAULT_HEIGHT (LED_DISTANCE * PANEL_HEIGHT) #define COLOR_BACKGROUND Qt::black -#define COLOR_FOREGROUND Qt::orange +#define COLOR_FOREGROUND QColor(255, 127, 0, 255) QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -35,7 +35,7 @@ public: private: Ui::MainWindow *ui; UdpLedServer *server; - QImage *mOffscreenDiagram; + QImage *mOffscreenPanel; void drawImage(QImage *image); }; #endif // MAINWINDOW_H From 823b3be52c8e6e909843a43fe818ecfc71a20ab9 Mon Sep 17 00:00:00 2001 From: Ollo Date: Tue, 15 Aug 2023 12:57:46 +0200 Subject: [PATCH 06/11] Refactor: renderPanel --- simulation/VirtualLedBoard/mainwindow.cpp | 30 ++++++++++++----------- simulation/VirtualLedBoard/mainwindow.h | 1 + 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/simulation/VirtualLedBoard/mainwindow.cpp b/simulation/VirtualLedBoard/mainwindow.cpp index 184d8f0..bc9a76f 100644 --- a/simulation/VirtualLedBoard/mainwindow.cpp +++ b/simulation/VirtualLedBoard/mainwindow.cpp @@ -13,20 +13,8 @@ MainWindow::MainWindow(QWidget *parent) ui->setupUi(this); this->server = new UdpLedServer (); - this->mOffscreenPanel = new QImage(DEFAULT_WIDTH, DEFAULT_HEIGHT, QImage::Format_RGB32); - this->mOffscreenPanel->fill(COLOR_BACKGROUND); - - /* draw inital screen */ - QPainter painter(this->mOffscreenPanel); - painter.setRenderHint(QPainter::Antialiasing, true); - painter.setPen(QPen(COLOR_FOREGROUND, 1)); - painter.setBrush(COLOR_FOREGROUND); - for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { - for(int y=0; y < PANEL_HEIGHT; y++) { - painter.drawEllipse(LED_DIAMETER/2 + (x* LED_DISTANCE), LED_DIAMETER/2 + (y * LED_DISTANCE), LED_DIAMETER, LED_DIAMETER); - } - } - + this->mOffscreenPanel = new QImage(DEFAULT_WIDTH + LED_DISTANCE, DEFAULT_HEIGHT + LED_DISTANCE, QImage::Format_RGB32); + this->renderPanel(); this->drawImage(this->mOffscreenPanel); } @@ -46,3 +34,17 @@ void MainWindow::drawImage(QImage *target) { this->ui->ledPanel->addWidget(graphicsView); } +void MainWindow::renderPanel(void) { + this->mOffscreenPanel->fill(COLOR_BACKGROUND); + + /* draw inital screen */ + QPainter painter(this->mOffscreenPanel); + painter.setRenderHint(QPainter::Antialiasing, true); + painter.setPen(QPen(COLOR_FOREGROUND, 1)); + painter.setBrush(COLOR_FOREGROUND); + for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { + for(int y=0; y < PANEL_HEIGHT; y++) { + painter.drawEllipse(LED_DIAMETER/2 + (x* LED_DISTANCE), LED_DIAMETER/2 + (y * LED_DISTANCE), LED_DIAMETER, LED_DIAMETER); + } + } +} diff --git a/simulation/VirtualLedBoard/mainwindow.h b/simulation/VirtualLedBoard/mainwindow.h index eec930b..d96514a 100644 --- a/simulation/VirtualLedBoard/mainwindow.h +++ b/simulation/VirtualLedBoard/mainwindow.h @@ -37,5 +37,6 @@ private: UdpLedServer *server; QImage *mOffscreenPanel; void drawImage(QImage *image); + void renderPanel(void); }; #endif // MAINWINDOW_H From a5e7848e11ca175494c646b0142828820bc5952d Mon Sep 17 00:00:00 2001 From: Ollo Date: Tue, 15 Aug 2023 13:25:07 +0200 Subject: [PATCH 07/11] Change client network port --- client/bin/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/bin/src/main.rs b/client/bin/src/main.rs index 40c1efb..a2027ab 100644 --- a/client/bin/src/main.rs +++ b/client/bin/src/main.rs @@ -238,7 +238,7 @@ fn send_package(ipaddress: String, data: &Option>) { package[1..PACKAGE_LENGTH].copy_from_slice(&display.image); - let socket = UdpSocket::bind("0.0.0.0:4242").expect("couldn't bind to address"); + let socket = UdpSocket::bind("0.0.0.0:14242").expect("couldn't bind to address"); socket .send_to(&package, ipaddress + ":4242") .expect("couldn't send data"); From 3a71ee786ec21b5151d5151eb926d1ca1369b3cd Mon Sep 17 00:00:00 2001 From: Ollo Date: Tue, 15 Aug 2023 13:38:07 +0200 Subject: [PATCH 08/11] Panel definitiones moved into settings --- simulation/VirtualLedBoard/VirtualLedBoard.pro | 1 + simulation/VirtualLedBoard/mainwindow.h | 12 +----------- simulation/VirtualLedBoard/settings.h | 14 ++++++++++++++ simulation/VirtualLedBoard/udpserver.cpp | 3 ++- 4 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 simulation/VirtualLedBoard/settings.h diff --git a/simulation/VirtualLedBoard/VirtualLedBoard.pro b/simulation/VirtualLedBoard/VirtualLedBoard.pro index 4ba635f..208ce2e 100644 --- a/simulation/VirtualLedBoard/VirtualLedBoard.pro +++ b/simulation/VirtualLedBoard/VirtualLedBoard.pro @@ -16,6 +16,7 @@ SOURCES += \ HEADERS += \ mainwindow.h \ + settings.h \ udpserver.h FORMS += \ diff --git a/simulation/VirtualLedBoard/mainwindow.h b/simulation/VirtualLedBoard/mainwindow.h index d96514a..d17aeb9 100644 --- a/simulation/VirtualLedBoard/mainwindow.h +++ b/simulation/VirtualLedBoard/mainwindow.h @@ -5,17 +5,7 @@ #include #include "udpserver.h" - -#define MAXIMUM_PANELSIZE 5 -#define PANEL_WIDTH 32 -#define PANEL_HEIGHT 40 - -#define LED_DIAMETER 8 -#define LED_DISTANCE (LED_DIAMETER + 3) - -#define DEFAULT_WIDTH (LED_DISTANCE * MAXIMUM_PANELSIZE * PANEL_WIDTH) -#define DEFAULT_HEIGHT (LED_DISTANCE * PANEL_HEIGHT) - +#include "settings.h" #define COLOR_BACKGROUND Qt::black #define COLOR_FOREGROUND QColor(255, 127, 0, 255) diff --git a/simulation/VirtualLedBoard/settings.h b/simulation/VirtualLedBoard/settings.h new file mode 100644 index 0000000..c1b23ef --- /dev/null +++ b/simulation/VirtualLedBoard/settings.h @@ -0,0 +1,14 @@ + +#define PACKET_LENGTH 801 +#define PACKET_INDEX_BRIGHTNESS 0 +#define PACKET_INDEX_PANEL0 1 + +#define MAXIMUM_PANELSIZE 5 +#define PANEL_WIDTH 32 +#define PANEL_HEIGHT 40 + +#define LED_DIAMETER 8 +#define LED_DISTANCE (LED_DIAMETER + 3) + +#define DEFAULT_WIDTH (LED_DISTANCE * MAXIMUM_PANELSIZE * PANEL_WIDTH) +#define DEFAULT_HEIGHT (LED_DISTANCE * PANEL_HEIGHT) diff --git a/simulation/VirtualLedBoard/udpserver.cpp b/simulation/VirtualLedBoard/udpserver.cpp index 9887cea..a814395 100644 --- a/simulation/VirtualLedBoard/udpserver.cpp +++ b/simulation/VirtualLedBoard/udpserver.cpp @@ -1,4 +1,5 @@ #include "udpserver.h" +#include "settings.h" #include #include @@ -29,7 +30,7 @@ void UdpLedServer ::readPendingDatagrams() } void UdpLedServer::processTheDatagram(QNetworkDatagram datagram) { - if (datagram.isValid()) { + if (datagram.isValid() && datagram.data().length() == PACKET_LENGTH) { qDebug() << "Received datagram:" << datagram.data().size(); } } From 536779910ee385e91743f3681d02267ee9909cb4 Mon Sep 17 00:00:00 2001 From: Ollo Date: Tue, 15 Aug 2023 14:38:39 +0200 Subject: [PATCH 09/11] Signals and slots prepared to update content, received from UDP packet --- simulation/VirtualLedBoard/mainwindow.cpp | 26 ++++++++++++------- simulation/VirtualLedBoard/mainwindow.h | 4 +++ simulation/VirtualLedBoard/udpserver.cpp | 31 ++++++++++++++++++++++- simulation/VirtualLedBoard/udpserver.h | 5 +++- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/simulation/VirtualLedBoard/mainwindow.cpp b/simulation/VirtualLedBoard/mainwindow.cpp index bc9a76f..6998bf1 100644 --- a/simulation/VirtualLedBoard/mainwindow.cpp +++ b/simulation/VirtualLedBoard/mainwindow.cpp @@ -14,8 +14,12 @@ MainWindow::MainWindow(QWidget *parent) this->server = new UdpLedServer (); this->mOffscreenPanel = new QImage(DEFAULT_WIDTH + LED_DISTANCE, DEFAULT_HEIGHT + LED_DISTANCE, QImage::Format_RGB32); - this->renderPanel(); - this->drawImage(this->mOffscreenPanel); + for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { + for(int y=0; y < PANEL_HEIGHT; y++) { + setLED(x, y); + } + } + updatePanel(); } MainWindow::~MainWindow() @@ -34,17 +38,21 @@ void MainWindow::drawImage(QImage *target) { this->ui->ledPanel->addWidget(graphicsView); } -void MainWindow::renderPanel(void) { - this->mOffscreenPanel->fill(COLOR_BACKGROUND); +void MainWindow::setLED(uint8_t x, uint8_t y) { /* draw inital screen */ QPainter painter(this->mOffscreenPanel); painter.setRenderHint(QPainter::Antialiasing, true); painter.setPen(QPen(COLOR_FOREGROUND, 1)); painter.setBrush(COLOR_FOREGROUND); - for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { - for(int y=0; y < PANEL_HEIGHT; y++) { - painter.drawEllipse(LED_DIAMETER/2 + (x* LED_DISTANCE), LED_DIAMETER/2 + (y * LED_DISTANCE), LED_DIAMETER, LED_DIAMETER); - } - } + painter.drawEllipse(LED_DIAMETER/2 + (x* LED_DISTANCE), LED_DIAMETER/2 + (y * LED_DISTANCE), LED_DIAMETER, LED_DIAMETER); +} + +void MainWindow::updatePanel(void) { + this->drawImage(this->mOffscreenPanel); + this->renderPanel(); +} + +void MainWindow::renderPanel(void) { + this->mOffscreenPanel->fill(COLOR_BACKGROUND); } diff --git a/simulation/VirtualLedBoard/mainwindow.h b/simulation/VirtualLedBoard/mainwindow.h index d17aeb9..6eb3046 100644 --- a/simulation/VirtualLedBoard/mainwindow.h +++ b/simulation/VirtualLedBoard/mainwindow.h @@ -22,6 +22,10 @@ public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); +public slots: + void setLED(uint8_t x, uint8_t y); + void updatePanel(void); + private: Ui::MainWindow *ui; UdpLedServer *server; diff --git a/simulation/VirtualLedBoard/udpserver.cpp b/simulation/VirtualLedBoard/udpserver.cpp index a814395..2b754e2 100644 --- a/simulation/VirtualLedBoard/udpserver.cpp +++ b/simulation/VirtualLedBoard/udpserver.cpp @@ -2,6 +2,7 @@ #include "settings.h" #include #include +#include "mainwindow.h" #define UDP_IMAGE_PORT 4242 @@ -9,6 +10,15 @@ UdpLedServer ::UdpLedServer (QObject *parent) : QObject(parent) { initSocket(); + connect(this, + &UdpLedServer::changeLEDstate, + (MainWindow*) parent, + &MainWindow::setLED); + connect(this, + &UdpLedServer::updatePanelContent, + (MainWindow*) parent, + &MainWindow::updatePanel); + } void UdpLedServer ::initSocket() @@ -31,6 +41,25 @@ void UdpLedServer ::readPendingDatagrams() void UdpLedServer::processTheDatagram(QNetworkDatagram datagram) { if (datagram.isValid() && datagram.data().length() == PACKET_LENGTH) { - qDebug() << "Received datagram:" << datagram.data().size(); + uint8_t brightness = datagram.data().at(PACKET_INDEX_BRIGHTNESS); + int currentIndex = PACKET_INDEX_PANEL0; + + uint16_t mask = 1; + for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { + for(int y=0; y < PANEL_HEIGHT; y++) { + if (datagram.data().at(currentIndex) & mask) { + this->changeLEDstate(x, y); + } + mask = (mask << 1); + if (mask >= 256) { + mask = 1; + currentIndex++; + } + } + } + this->updatePanelContent(); + + qDebug() << "Received datagram:" << brightness; + } } diff --git a/simulation/VirtualLedBoard/udpserver.h b/simulation/VirtualLedBoard/udpserver.h index 8acbe3f..d36c40f 100644 --- a/simulation/VirtualLedBoard/udpserver.h +++ b/simulation/VirtualLedBoard/udpserver.h @@ -11,12 +11,15 @@ class UdpLedServer : public QObject public: explicit UdpLedServer (QObject *parent = nullptr); - private: void initSocket(); void readPendingDatagrams(); QUdpSocket *mUdpSocket; void processTheDatagram(QNetworkDatagram datagram); + +signals: + void changeLEDstate(uint8_t x, uint8_t y); + void updatePanelContent(void); }; #endif // UDPSERVER_H From c5bfaa2a4a4f87342bfa1340a7246de1ba79124c Mon Sep 17 00:00:00 2001 From: Ollo Date: Thu, 17 Aug 2023 21:51:03 +0200 Subject: [PATCH 10/11] Packets are shown on screen --- simulation/VirtualLedBoard/mainwindow.cpp | 9 +-------- simulation/VirtualLedBoard/mainwindow.h | 3 +++ simulation/VirtualLedBoard/udpserver.cpp | 15 ++++++++++++--- simulation/VirtualLedBoard/udpserver.h | 6 +++++- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/simulation/VirtualLedBoard/mainwindow.cpp b/simulation/VirtualLedBoard/mainwindow.cpp index 6998bf1..4593d7c 100644 --- a/simulation/VirtualLedBoard/mainwindow.cpp +++ b/simulation/VirtualLedBoard/mainwindow.cpp @@ -11,15 +11,8 @@ MainWindow::MainWindow(QWidget *parent) , ui(new Ui::MainWindow) { ui->setupUi(this); - this->server = new UdpLedServer (); - this->mOffscreenPanel = new QImage(DEFAULT_WIDTH + LED_DISTANCE, DEFAULT_HEIGHT + LED_DISTANCE, QImage::Format_RGB32); - for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { - for(int y=0; y < PANEL_HEIGHT; y++) { - setLED(x, y); - } - } - updatePanel(); + this->server = new UdpLedServer (NULL, this); } MainWindow::~MainWindow() diff --git a/simulation/VirtualLedBoard/mainwindow.h b/simulation/VirtualLedBoard/mainwindow.h index 6eb3046..0b0617a 100644 --- a/simulation/VirtualLedBoard/mainwindow.h +++ b/simulation/VirtualLedBoard/mainwindow.h @@ -14,6 +14,9 @@ QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE +/* dummy */ +class UdpLedServer; + class MainWindow : public QMainWindow { Q_OBJECT diff --git a/simulation/VirtualLedBoard/udpserver.cpp b/simulation/VirtualLedBoard/udpserver.cpp index 2b754e2..8404ffc 100644 --- a/simulation/VirtualLedBoard/udpserver.cpp +++ b/simulation/VirtualLedBoard/udpserver.cpp @@ -6,19 +6,27 @@ #define UDP_IMAGE_PORT 4242 -UdpLedServer ::UdpLedServer (QObject *parent) +UdpLedServer::UdpLedServer (QObject *parent, MainWindow *window) : QObject(parent) { initSocket(); connect(this, &UdpLedServer::changeLEDstate, - (MainWindow*) parent, + window, &MainWindow::setLED); connect(this, &UdpLedServer::updatePanelContent, - (MainWindow*) parent, + window, &MainWindow::updatePanel); + + for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { + for(int y=0; y < PANEL_HEIGHT; y++) { + changeLEDstate(x, y); + } + } + updatePanelContent(); + } void UdpLedServer ::initSocket() @@ -49,6 +57,7 @@ void UdpLedServer::processTheDatagram(QNetworkDatagram datagram) { for(int y=0; y < PANEL_HEIGHT; y++) { if (datagram.data().at(currentIndex) & mask) { this->changeLEDstate(x, y); + qDebug() << x << "x" << y << " set"; } mask = (mask << 1); if (mask >= 256) { diff --git a/simulation/VirtualLedBoard/udpserver.h b/simulation/VirtualLedBoard/udpserver.h index d36c40f..023370b 100644 --- a/simulation/VirtualLedBoard/udpserver.h +++ b/simulation/VirtualLedBoard/udpserver.h @@ -3,13 +3,17 @@ #include #include +#include "mainwindow.h" + +class MainWindow; class UdpLedServer : public QObject { Q_OBJECT public: - explicit UdpLedServer (QObject *parent = nullptr); + /*UdpLedServer (QObject *parent = nullptr); */ + UdpLedServer (QObject *parent = nullptr, MainWindow *window = nullptr); private: void initSocket(); From aac762e8f15ba1f997bb5453c6106fbcd8ab3769 Mon Sep 17 00:00:00 2001 From: Ollo Date: Thu, 17 Aug 2023 22:19:12 +0200 Subject: [PATCH 11/11] Simulation is working --- simulation/VirtualLedBoard/mainwindow.cpp | 12 +++++++----- simulation/VirtualLedBoard/mainwindow.h | 6 ++++-- simulation/VirtualLedBoard/udpserver.cpp | 4 ++-- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/simulation/VirtualLedBoard/mainwindow.cpp b/simulation/VirtualLedBoard/mainwindow.cpp index 4593d7c..b99fbab 100644 --- a/simulation/VirtualLedBoard/mainwindow.cpp +++ b/simulation/VirtualLedBoard/mainwindow.cpp @@ -12,6 +12,7 @@ MainWindow::MainWindow(QWidget *parent) { ui->setupUi(this); this->mOffscreenPanel = new QImage(DEFAULT_WIDTH + LED_DISTANCE, DEFAULT_HEIGHT + LED_DISTANCE, QImage::Format_RGB32); + this->drawImage(this->mOffscreenPanel); this->server = new UdpLedServer (NULL, this); } @@ -21,12 +22,10 @@ MainWindow::~MainWindow() } void MainWindow::drawImage(QImage *target) { + this->mScene=new QGraphicsScene() ; QGraphicsView *graphicsView = new QGraphicsView(); graphicsView->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform); - QGraphicsScene* scene=new QGraphicsScene() ; - graphicsView->setScene(scene); - QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(*(target))); - scene->addItem(item); + graphicsView->setScene(this->mScene); graphicsView->show(); this->ui->ledPanel->addWidget(graphicsView); } @@ -42,7 +41,10 @@ void MainWindow::setLED(uint8_t x, uint8_t y) { } void MainWindow::updatePanel(void) { - this->drawImage(this->mOffscreenPanel); + + QGraphicsPixmapItem* item = new QGraphicsPixmapItem(QPixmap::fromImage(*(this->mOffscreenPanel))); + this->mScene->clear(); + mScene->addItem(item); this->renderPanel(); } diff --git a/simulation/VirtualLedBoard/mainwindow.h b/simulation/VirtualLedBoard/mainwindow.h index 0b0617a..d32b5f4 100644 --- a/simulation/VirtualLedBoard/mainwindow.h +++ b/simulation/VirtualLedBoard/mainwindow.h @@ -3,6 +3,7 @@ #include #include +#include #include "udpserver.h" #include "settings.h" @@ -31,8 +32,9 @@ public slots: private: Ui::MainWindow *ui; - UdpLedServer *server; - QImage *mOffscreenPanel; + UdpLedServer *server = nullptr; + QImage *mOffscreenPanel = nullptr; + QGraphicsScene* mScene = nullptr; void drawImage(QImage *image); void renderPanel(void); }; diff --git a/simulation/VirtualLedBoard/udpserver.cpp b/simulation/VirtualLedBoard/udpserver.cpp index 8404ffc..0acdf06 100644 --- a/simulation/VirtualLedBoard/udpserver.cpp +++ b/simulation/VirtualLedBoard/udpserver.cpp @@ -53,8 +53,8 @@ void UdpLedServer::processTheDatagram(QNetworkDatagram datagram) { int currentIndex = PACKET_INDEX_PANEL0; uint16_t mask = 1; - for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { - for(int y=0; y < PANEL_HEIGHT; y++) { + for(int y=0; y < PANEL_HEIGHT; y++) { + for(int x=0; x < (PANEL_WIDTH * MAXIMUM_PANELSIZE); x++) { if (datagram.data().at(currentIndex) & mask) { this->changeLEDstate(x, y); qDebug() << x << "x" << y << " set";