Commit 81bee1ec404df2ebd4ced30537aab469c8f07dbd
1 parent
d5dc2c4f41
Exists in
master
and in
2 other branches
원격 패킷 출력 프로그램 추가
Showing
8 changed files
with
299 additions
and
1 deletions
Show diff stats
app/gui/oven_control/udphandler.cpp
| @@ -2,7 +2,7 @@ | @@ -2,7 +2,7 @@ | ||
| 2 | 2 | ||
| 3 | #include <QtDebug> | 3 | #include <QtDebug> |
| 4 | 4 | ||
| 5 | -#define IPC_UDP_SYS_HOST "192.168.2.110" | 5 | +#define IPC_UDP_SYS_HOST "127.0.0.1" |
| 6 | #define IPC_UDP_SYS_PORT 4001 | 6 | #define IPC_UDP_SYS_PORT 4001 |
| 7 | #define IPC_UDP_GUI_PORT 4000 | 7 | #define IPC_UDP_GUI_PORT 4000 |
| 8 | 8 | ||
| @@ -45,6 +45,8 @@ void UdpHandler::readPendingDatagrams() | @@ -45,6 +45,8 @@ void UdpHandler::readPendingDatagrams() | ||
| 45 | 45 | ||
| 46 | void UdpHandler::processDatagram(QByteArray &datagram) | 46 | void UdpHandler::processDatagram(QByteArray &datagram) |
| 47 | { | 47 | { |
| 48 | + qDebug() << "Received"; | ||
| 49 | + sock->writeDatagram(datagram, QHostAddress("192.168.4.191"), 4000); | ||
| 48 | packet_t *packet = (packet_t *) datagram.data(); | 50 | packet_t *packet = (packet_t *) datagram.data(); |
| 49 | switch (packet->header) | 51 | switch (packet->header) |
| 50 | { | 52 | { |
app/gui/packet/main.cpp
app/gui/packet/mainwindow.cpp
| @@ -0,0 +1,130 @@ | @@ -0,0 +1,130 @@ | ||
| 1 | +#include "mainwindow.h" | ||
| 2 | +#include "ui_mainwindow.h" | ||
| 3 | + | ||
| 4 | +#include <QTableWidgetItem> | ||
| 5 | + | ||
| 6 | +#include "tablevalue.h" | ||
| 7 | + | ||
| 8 | +typedef struct { | ||
| 9 | + int header; | ||
| 10 | + char body[]; | ||
| 11 | +} STRUCT_PACK packet_t; | ||
| 12 | + | ||
| 13 | +MainWindow::MainWindow(QWidget *parent) : | ||
| 14 | + QMainWindow(parent), | ||
| 15 | + ui(new Ui::MainWindow) | ||
| 16 | +{ | ||
| 17 | + ui->setupUi(this); | ||
| 18 | + sock = new QUdpSocket(this); | ||
| 19 | + if (!sock->bind(4000)) | ||
| 20 | + exit(EXIT_FAILURE); | ||
| 21 | + | ||
| 22 | + connect(sock, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); | ||
| 23 | + | ||
| 24 | + ui->controlTable->setRowCount(sizeof(oven_control_t) / sizeof(U16)); | ||
| 25 | + ui->controlTable->setColumnCount(3); | ||
| 26 | + ui->controlTable->setHorizontalHeaderLabels(QString("Address,Value,Description").split(",")); | ||
| 27 | + | ||
| 28 | + for (int row = 0; row < ui->controlTable->rowCount(); row++) | ||
| 29 | + { | ||
| 30 | + ui->controlTable->setItem(row, 0, new QTableWidgetItem(QString("").sprintf("0x%04X", row))); | ||
| 31 | + | ||
| 32 | + TableValue *item = new TableValue; | ||
| 33 | + item->setText("0x0000"); | ||
| 34 | + ui->controlTable->setCellWidget(row, 1, item); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + ui->stateTable->setRowCount(sizeof(oven_state_t) / sizeof(U16)); | ||
| 38 | + ui->stateTable->setColumnCount(3); | ||
| 39 | + ui->stateTable->setHorizontalHeaderLabels(QString("Address,Value,Description").split(",")); | ||
| 40 | + | ||
| 41 | + for (int row = 0; row < ui->stateTable->rowCount(); row++) | ||
| 42 | + { | ||
| 43 | + ui->stateTable->setItem(row, 0, new QTableWidgetItem(QString().sprintf("0x%04X", row))); | ||
| 44 | + | ||
| 45 | + TableValue *item = new TableValue; | ||
| 46 | + item->setText("0x0000"); | ||
| 47 | + ui->stateTable->setCellWidget(row, 1, item); | ||
| 48 | + } | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +MainWindow::~MainWindow() | ||
| 52 | +{ | ||
| 53 | + delete ui; | ||
| 54 | +} | ||
| 55 | + | ||
| 56 | +void MainWindow::readPendingDatagrams() | ||
| 57 | +{ | ||
| 58 | + while (sock->hasPendingDatagrams()) { | ||
| 59 | + QByteArray datagram; | ||
| 60 | + datagram.resize(sock->pendingDatagramSize()); | ||
| 61 | + QHostAddress sender; | ||
| 62 | + quint16 senderPort; | ||
| 63 | + | ||
| 64 | + sock->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); | ||
| 65 | + | ||
| 66 | + processDatagram(datagram); | ||
| 67 | + } | ||
| 68 | +} | ||
| 69 | + | ||
| 70 | +void MainWindow::processDatagram(QByteArray &datagram) | ||
| 71 | +{ | ||
| 72 | + packet_t *packet = (packet_t *) datagram.data(); | ||
| 73 | + switch (packet->header) | ||
| 74 | + { | ||
| 75 | + case HDR_OVEN_CONTROL: | ||
| 76 | + processControl((oven_control_t *) packet->body); | ||
| 77 | + break; | ||
| 78 | + case HDR_OVEN_STATE: | ||
| 79 | + processState((oven_state_t *) packet->body); | ||
| 80 | + break; | ||
| 81 | + case HDR_ERROR_CODE: | ||
| 82 | + break; | ||
| 83 | + } | ||
| 84 | +} | ||
| 85 | + | ||
| 86 | +void MainWindow::processControl(oven_control_t *control) | ||
| 87 | +{ | ||
| 88 | + if (memcmp(&this->control, control, sizeof(this->control))) | ||
| 89 | + updateControl(control); | ||
| 90 | +} | ||
| 91 | + | ||
| 92 | +void MainWindow::processState(oven_state_t *state) | ||
| 93 | +{ | ||
| 94 | + if (memcmp(&this->state, state, sizeof(this->state))) | ||
| 95 | + updateState(state); | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +void MainWindow::updateControl(oven_control_t *control) | ||
| 99 | +{ | ||
| 100 | + U16 *base = (U16 *) &this->control; | ||
| 101 | + U16 *operand = (U16 *) control; | ||
| 102 | + for (int row = 0; row < ui->controlTable->rowCount(); row++) | ||
| 103 | + { | ||
| 104 | + U16 b = *(base + row); | ||
| 105 | + U16 o = *(operand + row); | ||
| 106 | + if (b != o) | ||
| 107 | + { | ||
| 108 | + *(base + row) = o; | ||
| 109 | + TableValue *val = (TableValue *) ui->controlTable->cellWidget(row, 1); | ||
| 110 | + val->setText(QString().sprintf("0x%04X", o)); | ||
| 111 | + } | ||
| 112 | + } | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +void MainWindow::updateState(oven_state_t *state) | ||
| 116 | +{ | ||
| 117 | + U16 *base = (U16 *) &this->state; | ||
| 118 | + U16 *operand = (U16 *) state; | ||
| 119 | + for (int row = 0; row < ui->stateTable->rowCount(); row++) | ||
| 120 | + { | ||
| 121 | + U16 b = *(base + row); | ||
| 122 | + U16 o = *(operand + row); | ||
| 123 | + if (b != o) | ||
| 124 | + { | ||
| 125 | + *(base + row) = o; | ||
| 126 | + TableValue *val = (TableValue *) ui->stateTable->cellWidget(row, 1); | ||
| 127 | + val->setText(QString().sprintf("0x%04X", o)); | ||
| 128 | + } | ||
| 129 | + } | ||
| 130 | +} |
app/gui/packet/mainwindow.h
| @@ -0,0 +1,36 @@ | @@ -0,0 +1,36 @@ | ||
| 1 | +#ifndef MAINWINDOW_H | ||
| 2 | +#define MAINWINDOW_H | ||
| 3 | + | ||
| 4 | +#include <QMainWindow> | ||
| 5 | +#include <QUdpSocket> | ||
| 6 | + | ||
| 7 | +#include "../oven_control/all_share.h" | ||
| 8 | + | ||
| 9 | +namespace Ui { | ||
| 10 | +class MainWindow; | ||
| 11 | +} | ||
| 12 | + | ||
| 13 | +class MainWindow : public QMainWindow | ||
| 14 | +{ | ||
| 15 | + Q_OBJECT | ||
| 16 | + | ||
| 17 | +public: | ||
| 18 | + explicit MainWindow(QWidget *parent = 0); | ||
| 19 | + ~MainWindow(); | ||
| 20 | + | ||
| 21 | +private: | ||
| 22 | + Ui::MainWindow *ui; | ||
| 23 | + QUdpSocket *sock; | ||
| 24 | + oven_control_t control; | ||
| 25 | + oven_state_t state; | ||
| 26 | + | ||
| 27 | +private slots: | ||
| 28 | + void readPendingDatagrams(); | ||
| 29 | + void processDatagram(QByteArray &datagram); | ||
| 30 | + void processControl(oven_control_t *control); | ||
| 31 | + void processState(oven_state_t *state); | ||
| 32 | + void updateControl(oven_control_t *control); | ||
| 33 | + void updateState(oven_state_t *state); | ||
| 34 | +}; | ||
| 35 | + | ||
| 36 | +#endif // MAINWINDOW_H |
app/gui/packet/mainwindow.ui
| @@ -0,0 +1,30 @@ | @@ -0,0 +1,30 @@ | ||
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<ui version="4.0"> | ||
| 3 | + <class>MainWindow</class> | ||
| 4 | + <widget class="QMainWindow" name="MainWindow"> | ||
| 5 | + <property name="geometry"> | ||
| 6 | + <rect> | ||
| 7 | + <x>0</x> | ||
| 8 | + <y>0</y> | ||
| 9 | + <width>818</width> | ||
| 10 | + <height>300</height> | ||
| 11 | + </rect> | ||
| 12 | + </property> | ||
| 13 | + <property name="windowTitle"> | ||
| 14 | + <string>Data Viewer</string> | ||
| 15 | + </property> | ||
| 16 | + <widget class="QWidget" name="centralWidget"> | ||
| 17 | + <layout class="QHBoxLayout" name="horizontalLayout"> | ||
| 18 | + <item> | ||
| 19 | + <widget class="QTableWidget" name="controlTable"/> | ||
| 20 | + </item> | ||
| 21 | + <item> | ||
| 22 | + <widget class="QTableWidget" name="stateTable"/> | ||
| 23 | + </item> | ||
| 24 | + </layout> | ||
| 25 | + </widget> | ||
| 26 | + </widget> | ||
| 27 | + <layoutdefault spacing="6" margin="11"/> | ||
| 28 | + <resources/> | ||
| 29 | + <connections/> | ||
| 30 | +</ui> |
app/gui/packet/packet.pro
| @@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
| 1 | +#------------------------------------------------- | ||
| 2 | +# | ||
| 3 | +# Project created by QtCreator 2017-03-03T21:21:48 | ||
| 4 | +# | ||
| 5 | +#------------------------------------------------- | ||
| 6 | + | ||
| 7 | +QT += core gui network | ||
| 8 | + | ||
| 9 | +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
| 10 | + | ||
| 11 | +TARGET = packet | ||
| 12 | +TEMPLATE = app | ||
| 13 | + | ||
| 14 | + | ||
| 15 | +SOURCES += main.cpp\ | ||
| 16 | + mainwindow.cpp \ | ||
| 17 | + tablevalue.cpp | ||
| 18 | + | ||
| 19 | +HEADERS += mainwindow.h \ | ||
| 20 | + tablevalue.h | ||
| 21 | + | ||
| 22 | +FORMS += mainwindow.ui |
app/gui/packet/tablevalue.cpp
| @@ -0,0 +1,34 @@ | @@ -0,0 +1,34 @@ | ||
| 1 | +#include "tablevalue.h" | ||
| 2 | + | ||
| 3 | +#include <QColor> | ||
| 4 | + | ||
| 5 | +#include <QtDebug> | ||
| 6 | + | ||
| 7 | +void TableValue::setText(const QString &str) | ||
| 8 | +{ | ||
| 9 | + QLabel::setText(str); | ||
| 10 | + timer.start(2000); | ||
| 11 | + animationTimer.start(33); | ||
| 12 | + | ||
| 13 | + updateColor(); | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +void TableValue::updateColor() | ||
| 17 | +{ | ||
| 18 | + int remain = timer.remainingTime(); | ||
| 19 | + if (remain < 0) | ||
| 20 | + remain = 0; | ||
| 21 | + | ||
| 22 | + if (remain > 2000) | ||
| 23 | + remain = 2000; | ||
| 24 | + | ||
| 25 | + qreal percentage = ((qreal) remain / 2000) * 0.5; | ||
| 26 | + int b = 255 * percentage; | ||
| 27 | + if (b < 0) | ||
| 28 | + b = 0; | ||
| 29 | + | ||
| 30 | + if (b > 255) | ||
| 31 | + b = 255; | ||
| 32 | + | ||
| 33 | + setStyleSheet(QString().sprintf("background-color: rgba(255, 0, 0, %d); color: rgb(0, 0, 0)", b)); | ||
| 34 | +} |
app/gui/packet/tablevalue.h
| @@ -0,0 +1,33 @@ | @@ -0,0 +1,33 @@ | ||
| 1 | +#ifndef TABLEVALUE_H | ||
| 2 | +#define TABLEVALUE_H | ||
| 3 | + | ||
| 4 | +#include <QObject> | ||
| 5 | +#include <QWidget> | ||
| 6 | +#include <QLabel> | ||
| 7 | +#include <QTableWidgetItem> | ||
| 8 | +#include <QTimer> | ||
| 9 | + | ||
| 10 | +class TableValue : public QLabel | ||
| 11 | +{ | ||
| 12 | + Q_OBJECT | ||
| 13 | + | ||
| 14 | +public: | ||
| 15 | + explicit TableValue() | ||
| 16 | + { | ||
| 17 | + timer.setSingleShot(true); | ||
| 18 | + connect(&animationTimer, SIGNAL(timeout()), this, SLOT(updateColor())); | ||
| 19 | + connect(&timer, SIGNAL(timeout()), &animationTimer, SLOT(stop())); | ||
| 20 | + connect(&timer, SIGNAL(timeout()), this, SLOT(updateColor())); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + void setText(const QString &text); | ||
| 24 | + | ||
| 25 | +private: | ||
| 26 | + QTimer timer; | ||
| 27 | + QTimer animationTimer; | ||
| 28 | + | ||
| 29 | +private slots: | ||
| 30 | + void updateColor(); | ||
| 31 | +}; | ||
| 32 | + | ||
| 33 | +#endif // TABLEVALUE_H |