#include "mainwindow.h" #include "ui_mainwindow.h" #include #include "tablevalue.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); sock = new QUdpSocket(this); if (!sock->bind(40001)) exit(EXIT_FAILURE); bzero(&control, sizeof(control)); bzero(&state, sizeof(state)); connect(sock, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); ui->controlTable->setRowCount(sizeof(oven_control_t) / sizeof(U16)); ui->controlTable->setColumnCount(5); 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(5); 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); ui->controlTable->setItem(0x05, 3, new QTableWidgetItem("Oven Mode")); ui->controlTable->setItem(0x06, 3, new QTableWidgetItem("Cook On/Off")); ui->controlTable->setItem(0x07, 3, new QTableWidgetItem("Wash On/Off")); ui->controlTable->setItem(0x08, 3, new QTableWidgetItem("System On/Off")); ui->controlTable->setItem(0x09, 3, new QTableWidgetItem("Temp")); ui->controlTable->setItem(0x0A, 3, new QTableWidgetItem("Humidity")); ui->controlTable->setItem(0x0B, 3, new QTableWidgetItem("Time")); ui->controlTable->setItem(0x0C, 3, new QTableWidgetItem("Preheat On/Off")); // ui->controlTable->setItem(0x0D, 3, new QTableWidgetItem("")); // ui->controlTable->setItem(0x0E, 3, new QTableWidgetItem("")); ui->controlTable->setItem(0x0F, 3, new QTableWidgetItem("Convection RPM")); ui->controlTable->setItem(0x10, 3, new QTableWidgetItem("Cool Down On/Off")); ui->controlTable->setItem(0x11, 3, new QTableWidgetItem("InterTemp")); // ui->controlTable->setItem(0x12, 3, new QTableWidgetItem("InterTemp")); // ui->controlTable->setItem(0x13, 3, new QTableWidgetItem("InterTemp")); ui->controlTable->setItem(0x14, 3, new QTableWidgetItem("Wash Type")); ui->controlTable->setItem(0x15, 3, new QTableWidgetItem("Wash Total")); ui->controlTable->setItem(0x16, 3, new QTableWidgetItem("Wash Steps")); ui->controlTable->setItem(0x17, 3, new QTableWidgetItem("Wash Step Type")); ui->stateTable->setItem(0x00, 3, new QTableWidgetItem("Firmware V")); ui->stateTable->setItem(0x01, 3, new QTableWidgetItem("Firmware Y")); ui->stateTable->setItem(0x02, 3, new QTableWidgetItem("Firmware M")); ui->stateTable->setItem(0x03, 3, new QTableWidgetItem("Firmware D")); ui->stateTable->setItem(0x04, 3, new QTableWidgetItem("Cook State")); ui->stateTable->setItem(0x05, 3, new QTableWidgetItem("Setting Temp")); ui->stateTable->setItem(0x06, 3, new QTableWidgetItem("Setting Humidity")); ui->stateTable->setItem(0x07, 3, new QTableWidgetItem("Setting Time")); ui->stateTable->setItem(0x08, 3, new QTableWidgetItem("Current Temp")); ui->stateTable->setItem(0x09, 3, new QTableWidgetItem("Current Humidity")); ui->stateTable->setItem(0x0A, 3, new QTableWidgetItem("Current Time")); ui->stateTable->setItem(0x0B, 3, new QTableWidgetItem("Wash State")); ui->stateTable->setItem(0x0C, 3, new QTableWidgetItem("Wash Type")); ui->stateTable->setItem(0x0D, 3, new QTableWidgetItem("Wash Steps")); // ui->stateTable->setItem(0x0E, 3, new QTableWidgetItem("Oven Mode")); // ui->stateTable->setItem(0x0F, 3, new QTableWidgetItem("Oven Mode")); ui->stateTable->setItem(0x10, 3, new QTableWidgetItem("Door Open/Close")); // ui->stateTable->setItem(0x11, 3, new QTableWidgetItem("Oven Mode")); ui->stateTable->setItem(0x12, 3, new QTableWidgetItem("Burner 1")); ui->stateTable->setItem(0x13, 3, new QTableWidgetItem("Burner 2")); ui->stateTable->setItem(0x14, 3, new QTableWidgetItem("Burner 3")); // ui->stateTable->setItem(0x15, 3, new QTableWidgetItem("Oven Mode")); // ui->stateTable->setItem(0x16, 3, new QTableWidgetItem("Oven Mode")); // ui->stateTable->setItem(0x17, 3, new QTableWidgetItem("Oven Mode")); // ui->stateTable->setItem(0x18, 3, new QTableWidgetItem("Oven Mode")); // ui->stateTable->setItem(0x19, 3, new QTableWidgetItem("Oven Mode")); // ui->stateTable->setItem(0x1A, 3, new QTableWidgetItem("Burner 3")); ui->stateTable->setItem(0x1B, 3, new QTableWidgetItem("Sensor 1 Cab")); ui->stateTable->setItem(0x1C, 3, new QTableWidgetItem("Sensor 2")); ui->stateTable->setItem(0x1D, 3, new QTableWidgetItem("Sensor 3")); ui->stateTable->setItem(0x1E, 3, new QTableWidgetItem("Sensor 4")); ui->stateTable->setItem(0x1F, 3, new QTableWidgetItem("Sensor 5")); ui->stateTable->setItem(0x20, 3, new QTableWidgetItem("Sensor 6 Food 1")); ui->stateTable->setItem(0x21, 3, new QTableWidgetItem("Sensor 7 Food 2")); ui->stateTable->setItem(0x22, 3, new QTableWidgetItem("Sensor 8 Food 3")); ui->stateTable->setItem(0x23, 3, new QTableWidgetItem("Sensor 9 Food 4")); ui->stateTable->setItem(0x24, 3, new QTableWidgetItem("Sensor 10")); ui->stateTable->setItem(0x25, 3, new QTableWidgetItem("Sensor 11 mbar")); ui->stateTable->setItem(0x26, 3, new QTableWidgetItem("Sensor 12 mbar")); ui->stateTable->setItem(0x27, 3, new QTableWidgetItem("Sensor 13 PCB")); ui->stateTable->setItem(0x28, 3, new QTableWidgetItem("Sensor 14 ADC")); ui->stateTable->setItem(0x29, 3, new QTableWidgetItem("Sensor 15 Conv 1 RPM")); ui->stateTable->setItem(0x2A, 3, new QTableWidgetItem("Sensor 16 Burner 1 RPM")); ui->controlTable->resizeColumnToContents(3); ui->stateTable->resizeColumnToContents(3); } 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: qDebug() << "Received Control"; processControl((oven_control_t *) packet->body); break; case HDR_OVEN_STATE: qDebug() << "Received State"; processState((oven_state_t *) packet->body); break; case HDR_ERROR_CODE: qDebug() << "Received Error"; 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++) { qDebug() << "Diff"; 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", o)); val = (TableValue *) ui->controlTable->cellWidget(row, 2); val->setText(QString::number(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", o)); val = (TableValue *) ui->stateTable->cellWidget(row, 2); val->setText(QString::number(o)); } } } void MainWindow::on_pushControlButton_clicked() { oven_control_t control = this->control; U16 *base = (U16 *) &control; for (int row = 0; row < ui->controlTable->rowCount(); row++) { QTableWidgetItem *val = (QTableWidgetItem *) ui->controlTable->item(row, 4); if (val) *(base + row) = val->text().toUShort(nullptr, 10); } gui_oven_control_t c; c.header = HDR_OVEN_CONTROL; memcpy(&c.control, &control, sizeof(this->control)); QByteArray datagram = QByteArray::fromRawData((char *) &c, sizeof(c)); sock->writeDatagram(datagram, QHostAddress(ui->lineEdit->text()), 4000); } void MainWindow::on_pushStateButton_clicked() { oven_state_t state = this->state; U16 *base = (U16 *) &state; for (int row = 0; row < ui->stateTable->rowCount(); row++) { QTableWidgetItem *val = (QTableWidgetItem *) ui->stateTable->item(row, 4); if (val) *(base + row) = val->text().toUShort(nullptr, 10); } gui_oven_state_t c; c.header = HDR_OVEN_STATE; memcpy(&c.state, &state, sizeof(state)); QByteArray datagram = QByteArray::fromRawData((char *) &c, sizeof(c)); sock->writeDatagram(datagram, QHostAddress(ui->lineEdit->text()), 4000); }