mainwindow.cpp
4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTableWidgetItem>
#include "tablevalue.h"
typedef struct {
int header;
char body[];
} STRUCT_PACK packet_t;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sock = new QUdpSocket(this);
if (!sock->bind(4000))
exit(EXIT_FAILURE);
connect(sock, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
ui->controlTable->setRowCount(sizeof(oven_control_t) / sizeof(U16));
ui->controlTable->setColumnCount(4);
ui->controlTable->setHorizontalHeaderLabels(QString("Addr,Value,Value,Description").split(","));
for (int row = 0; row < ui->controlTable->rowCount(); row++)
{
QTableWidgetItem *witem = new QTableWidgetItem(QString("").sprintf("0x%04X", row));
witem->setTextAlignment(Qt::AlignCenter);
ui->controlTable->setItem(row, 0, witem);
TableValue *item = new TableValue;
item->setText("0x0000");
item->setMargin(3);
item->setAlignment(Qt::AlignCenter);
ui->controlTable->setCellWidget(row, 1, item);
item = new TableValue;
item->setText("0");
item->setMargin(3);
item->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
ui->controlTable->setCellWidget(row, 2, item);
}
ui->controlTable->resizeColumnToContents(0);
ui->controlTable->resizeColumnToContents(1);
ui->stateTable->setRowCount(sizeof(oven_state_t) / sizeof(U16));
ui->stateTable->setColumnCount(4);
ui->stateTable->setHorizontalHeaderLabels(QString("Addr,Value,Value,Description").split(","));
for (int row = 0; row < ui->stateTable->rowCount(); row++)
{
QTableWidgetItem *witem = new QTableWidgetItem(QString("").sprintf("0x%04X", row));
witem->setTextAlignment(Qt::AlignCenter);
ui->stateTable->setItem(row, 0, witem);
TableValue *item = new TableValue;
item->setText("0x0000");
item->setMargin(3);
item->setAlignment(Qt::AlignCenter);
ui->stateTable->setCellWidget(row, 1, item);
item = new TableValue;
item->setText("0");
item->setMargin(3);
item->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
ui->stateTable->setCellWidget(row, 2, item);
}
ui->stateTable->resizeColumnToContents(0);
ui->stateTable->resizeColumnToContents(1);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::readPendingDatagrams()
{
while (sock->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(sock->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
sock->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
processDatagram(datagram);
}
}
void MainWindow::processDatagram(QByteArray &datagram)
{
packet_t *packet = (packet_t *) datagram.data();
switch (packet->header)
{
case HDR_OVEN_CONTROL:
processControl((oven_control_t *) packet->body);
break;
case HDR_OVEN_STATE:
processState((oven_state_t *) packet->body);
break;
case HDR_ERROR_CODE:
break;
}
}
void MainWindow::processControl(oven_control_t *control)
{
if (memcmp(&this->control, control, sizeof(this->control)))
updateControl(control);
}
void MainWindow::processState(oven_state_t *state)
{
if (memcmp(&this->state, state, sizeof(this->state)))
updateState(state);
}
void MainWindow::updateControl(oven_control_t *control)
{
U16 *base = (U16 *) &this->control;
U16 *operand = (U16 *) control;
for (int row = 0; row < ui->controlTable->rowCount(); row++)
{
U16 b = *(base + row);
U16 o = *(operand + row);
if (b != o)
{
*(base + row) = o;
TableValue *val = (TableValue *) ui->controlTable->cellWidget(row, 1);
val->setText(QString().sprintf("0x%04X - %d", o, o));
}
}
}
void MainWindow::updateState(oven_state_t *state)
{
U16 *base = (U16 *) &this->state;
U16 *operand = (U16 *) state;
for (int row = 0; row < ui->stateTable->rowCount(); row++)
{
U16 b = *(base + row);
U16 o = *(operand + row);
if (b != o)
{
*(base + row) = o;
TableValue *val = (TableValue *) ui->stateTable->cellWidget(row, 1);
val->setText(QString().sprintf("0x%04X - %d", o, o));
}
}
}