From 536779910ee385e91743f3681d02267ee9909cb4 Mon Sep 17 00:00:00 2001 From: Ollo Date: Tue, 15 Aug 2023 14:38:39 +0200 Subject: [PATCH] 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