diff --git a/kx_utils/CMakeLists.txt b/kx_utils/CMakeLists.txt index f3a98f8..f2e144f 100644 --- a/kx_utils/CMakeLists.txt +++ b/kx_utils/CMakeLists.txt @@ -16,7 +16,7 @@ if (DEBUG) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3") endif () set(CPPS_UTILS "kx_coeffs.cpp") -set(HDRS_UTILS "kx_coeffs.h" "kx_protocol_x.h") +set(HDRS_UTILS "kx_coeffs.h" "kx_protocol_x.h" "kx_protocol_c.h") if (DEFINED ENV{QNX_HOST}) add_library(${PROJECT_NAME} STATIC ${CPPS_UTILS}) else () diff --git a/kx_utils/kx_coeffs.cpp b/kx_utils/kx_coeffs.cpp index 040e4b4..940ff36 100644 --- a/kx_utils/kx_coeffs.cpp +++ b/kx_utils/kx_coeffs.cpp @@ -90,6 +90,7 @@ void KX_Coefficients::sendCoeffs() { memcpy(k_protocol->to_k.coeffs, k_content.data(k_protocol->to_k.first_index), curcnt); //for (int j = 0; j < curcnt; j++) k_protocol->to_k.coeffs[j] = K.at(k_protocol->to_k.first_index + j); k_protocol->send(); + piMSleep(5); } //cout << "waiting for commit ...\n"; waitingCommit = true; diff --git a/kx_utils/kx_protocol_c.h b/kx_utils/kx_protocol_c.h new file mode 100644 index 0000000..66a5441 --- /dev/null +++ b/kx_utils/kx_protocol_c.h @@ -0,0 +1,126 @@ +#ifndef KX_PROTOCOL_C_H +#define KX_PROTOCOL_C_H + +#include "piprotocol.h" + + +#pragma pack (push, 1) + + +struct KX_C_Header { + uchar type; + uchar addr_to; +}; + + + +struct KX_C_Command: KX_C_Header { + KX_C_Command() {command = -1;} + int command; + uint checksum; +}; + + +struct KX_C_Event: KX_C_Header { + KX_C_Event() {event = -1;} + PISystemTime time; + int event; + uint checksum; +}; + + +#pragma pack (pop) + + +// Client side + +class KX_Protocol_C: public PIProtocol +{ + PIOBJECT_SUBCLASS(KX_Protocol_C, PIProtocol) +public: + KX_Protocol_C(const PIString & config, const PIString & name = "c"): PIProtocol(config, name, &from_buff, 2, &(from_buff.command), sizeof(from_buff) - 2, &to_pult, sizeof(to_pult)) { + PIConfig conf(config, PIIODevice::ReadOnly); + PIConfig::Entry ce = conf.getValue(name); + to_pult.type = from_buff.type = ce.getValue("type", 0xC); + to_pult.addr_to = ce.getValue("addr_pult", 0x15); + from_buff.addr_to = ce.getValue("addr_c", 0x75); + from_pult = from_buff; + packetExtractor()->setSplitMode(PIPacketExtractor::Header); + packetExtractor()->setHeader(PIByteArray(&from_buff, 2)); + startReceive(); + } + + void sendEvent(int ev) { + to_pult.time = PISystemTime::current(); + to_pult.event = ev; + send(); + } + EVENT1(commandReceived, int, command) + + KX_C_Command from_pult; + KX_C_Event to_pult; + +private: + bool validate() { + if (checksum_i(&from_buff, sizeof(from_buff) - 4) != from_buff.checksum) return false; + from_pult = from_buff; + commandReceived(from_pult.command); + return true; + } + bool aboutSend() { + to_pult.checksum = checksum_i(&to_pult, sizeof(to_pult) - 4); + return true; + } + + KX_C_Command from_buff; + +}; + + + +// Pult side + +class __KX_Protocol_C: public PIProtocol +{ + PIOBJECT_SUBCLASS(__KX_Protocol_C, PIProtocol) +public: + __KX_Protocol_C(const PIString & config, const PIString & name): PIProtocol(config, name, &from_buff, 2, &(from_buff.time), sizeof(from_buff) - 2, &to_c, sizeof(to_c)) { + PIConfig conf(config, PIIODevice::ReadOnly); + PIConfig::Entry ce = conf.getValue(name); + to_c.type = from_buff.type = ce.getValue("type", 0xC); + to_c.addr_to = ce.getValue("addr_c", 0x75); + from_buff.addr_to = ce.getValue("addr_pult", 0x15); + from_c = from_buff; + packetExtractor()->setSplitMode(PIPacketExtractor::Header); + packetExtractor()->setHeader(PIByteArray(&from_buff, 2)); + startReceive(); + } + + void sendCommand(int cmd) { + to_c.command = cmd; + send(); + } + EVENT2(eventReceived, int, event, PISystemTime, time) + + KX_C_Event from_c; + KX_C_Command to_c; + +private: + bool validate() { + if (checksum_i(&from_buff, sizeof(from_buff) - 4) != from_buff.checksum) return false; + from_c = from_buff; + eventReceived(from_c.event, from_c.time); + return true; + } + bool aboutSend() { + //piCout << "send command" << to_c.command; + to_c.checksum = checksum_i(&to_c, sizeof(to_c) - 4); + return true; + } + + KX_C_Event from_buff; + +}; + + +#endif // KX_PROTOCOL_C_H diff --git a/kx_utils/kx_pult.cpp b/kx_utils/kx_pult.cpp index 7dc0ee8..9e77a09 100644 --- a/kx_utils/kx_pult.cpp +++ b/kx_utils/kx_pult.cpp @@ -45,7 +45,8 @@ bool XCheck::eventFilter(QObject * o, QEvent * e) { -KX_Pult::KX_Pult(): QMainWindow(), config_("kx_pult.conf"), name_("x"), config(piqt(config_), QIODevice::ReadWrite), coeffs(config_, "k", true) { +KX_Pult::KX_Pult(): QMainWindow(), config_("kx_pult.conf"), name_x("x"), name_c("c"), +config(piqt(config_), QIODevice::ReadWrite), coeffs(config_, "k", true) { //cout << sizeof(coeffsK.k_protocol->to_k) << endl; ui = new Ui::KX_Pult(); ui->setupUi(this); @@ -53,8 +54,12 @@ KX_Pult::KX_Pult(): QMainWindow(), config_("kx_pult.conf"), name_("x"), config(p ui->configWidget->expandAll(); ui->list->viewport()->installEventFilter(this); ui->treeK->viewport()->installEventFilter(this); + ui->scrollArea->setAutoFillBackground(false); + ui->scrollAreaWidgetContents->setAutoFillBackground(false); + ui->widget->setAutoFillBackground(false); log_menu.addAction(ui->actionClear); prot_x = 0; + prot_c = 0; show_x = config.getValue("show_x", true); if (!show_x) ui->tabWidget->removeTab(1); @@ -121,6 +126,7 @@ KX_Pult::KX_Pult(): QMainWindow(), config_("kx_pult.conf"), name_("x"), config(p connect(ui->lineKSearch, SIGNAL(textChanged(QString)), this, SLOT(filterTree())); session.load(); updateKDesc(); + updateCDesc(); timer_diag.start(40); timer_update = startTimer(25); } @@ -133,11 +139,13 @@ KX_Pult::~KX_Pult() { void KX_Pult::loading(QPIConfig & conf) { kdesc_file = conf.getValue("kdesc_file").stringValue(); + cdesc_file = conf.getValue("cdesc_file").stringValue(); } void KX_Pult::saving(QPIConfig & conf) { conf.setValue("kdesc_file", kdesc_file); + conf.setValue("cdesc_file", cdesc_file); } @@ -403,7 +411,7 @@ void KX_Pult::on_buttonResize_clicked() { } -void KX_Pult::on_buttonSetDesc_clicked() { +void KX_Pult::on_buttonSetKDesc_clicked() { QString ret = QFileDialog::getOpenFileName(this, trUtf8("Select *.h file with K description"), kdesc_file, "C/C++ header files(*.h *.hpp);;All files(*)"); if (ret.isEmpty()) return; kdesc_file = QDir::current().relativeFilePath(ret); @@ -411,6 +419,14 @@ void KX_Pult::on_buttonSetDesc_clicked() { } +void KX_Pult::on_buttonSetCDesc_clicked() { + QString ret = QFileDialog::getOpenFileName(this, trUtf8("Select *.h file with C description"), cdesc_file, "C/C++ header files(*.h *.hpp);;All files(*)"); + if (ret.isEmpty()) return; + cdesc_file = QDir::current().relativeFilePath(ret); + updateCDesc(); +} + + void KX_Pult::on_spinSize_valueChanged(int) { ui->spinSize->setStyleSheet(""); } @@ -462,7 +478,6 @@ void KX_Pult::updateGraph() { void KX_Pult::updateDiag() { - ui->labelKReceiver->setText(piqt(coeffs.k_protocol->receiverDeviceName() + " - " + coeffs.k_protocol->receiverDeviceState())); ui->labelKSender->setText(piqt(coeffs.k_protocol->senderDeviceName())); ui->spinKSended->setValue(coeffs.k_protocol->sendCount()); @@ -482,23 +497,32 @@ void KX_Pult::updateDiag() { ui->labelXType->setText("0x" + QString::number(prot_x->from_x.type, 16).toUpper().rightJustified(2, '0')); ui->labelXAddrPult->setText("0x" + QString::number(prot_x->from_x.addr_to, 16).toUpper().rightJustified(2, '0')); ui->labelXAddr->setText("0x" + QString::number(prot_x->to_x.addr_to, 16).toUpper().rightJustified(2, '0')); - + + ui->labelCReceiver->setText(piqt(prot_c->receiverDeviceName() + " - " + prot_c->receiverDeviceState())); + ui->labelCSender->setText(piqt(prot_c->senderDeviceName())); + ui->spinCSended->setValue(prot_c->sendCount()); + ui->spinCReceived->setValue(prot_c->receiveCount()); + ui->spinCWrong->setValue(prot_c->wrongCount()); + ui->spinCMissed->setValue(prot_c->missedCount()); + ui->labelCType->setText("0x" + QString::number(prot_c->from_c.type, 16).toUpper().rightJustified(2, '0')); + ui->labelCAddrPult->setText("0x" + QString::number(prot_c->from_c.addr_to, 16).toUpper().rightJustified(2, '0')); + ui->labelCAddr->setText("0x" + QString::number(prot_c->to_c.addr_to, 16).toUpper().rightJustified(2, '0')); } -void KX_Pult::updateKDesc(bool ask_move) { - kdesc.clear(); - QFile f(kdesc_file); +int KX_Pult::parseHeader(const QString & file, QMap & map) { + map.clear(); + QFile f(file); if (!f.open(QIODevice::ReadOnly)) { updateTree(); - addToList(trUtf8("Update descriptions from \"%1\": error").arg(kdesc_file), Qt::darkRed); - return; + addToList(trUtf8("Update descriptions from \"%1\": error").arg(file), Qt::darkRed); + return 0; } - addToList(trUtf8("Update descriptions from \"%1\"").arg(kdesc_file), Qt::darkMagenta); + addToList(trUtf8("Update descriptions from \"%1\"").arg(file), Qt::darkMagenta); QTextStream s(&f); int cind = -1; bool found = false; - //qDebug() << "\nparse" << kdesc_file; + //qDebug() << "\nparse" << file; while (!s.atEnd()) { QString line = s.readLine().trimmed(), num, name, type, comment; int i = line.indexOf("//"); @@ -532,10 +556,16 @@ void KX_Pult::updateKDesc(bool ask_move) { kd.name = name; kd.type = type; kd.comment = comment; - kdesc[kd.index] = kd; + map[kd.index] = kd; //qDebug() << name << cind << type << comment; } cind++; + return cind; +} + + +void KX_Pult::updateKDesc(bool ask_move) { + int cind = parseHeader(kdesc_file, kdesc); if (K.size_s() < cind) { ui->spinSize->setValue(cind); ui->spinSize->setStyleSheet("background-color: rgb(220, 220, 255);"); @@ -547,6 +577,12 @@ void KX_Pult::updateKDesc(bool ask_move) { } +void KX_Pult::updateCDesc() { + parseHeader(cdesc_file, cdesc); + updateCommands(); +} + + bool stringComp(const QString & s1, const QString & s2) { if (s1.size() != s2.size()) return s1.size() > s2.size(); @@ -608,6 +644,25 @@ void KX_Pult::updateTree(bool move) { } +void KX_Pult::updateCommands() { + while (ui->layoutCommands->count() > 0) + delete ui->layoutCommands->itemAt(0)->widget(); + QMapIterator it(cdesc); + while (it.hasNext()) { + it.next(); + KDesc kd = it.value(); + QPushButton * b = new QPushButton(); + QString text = kd.name; + if (!kd.comment.isEmpty()) + text += QString("\n(%1)").arg(kd.comment); + b->setText(text); + b->setProperty("_command", kd.index); + connect(b, SIGNAL(clicked()), this, SLOT(commandClicked())); + ui->layoutCommands->addWidget(b); + } +} + + void KX_Pult::filterTree() { bool he = ui->checkKHideEmpty->isChecked(); bool hn = ui->checkKHideNormal->isChecked(); @@ -763,7 +818,12 @@ void KX_Pult::renew(bool write) { prot_x->stop(); delete prot_x; } - prot_x = new __KX_Protocol_X(config_, name_); + if (prot_c != 0) { + prot_c->stop(); + delete prot_c; + } + prot_x = new __KX_Protocol_X(config_, name_x); + prot_c = new __KX_Protocol_C(config_, name_c); ui->graphic->setAutoXIncrement(prot_x->expectedFrequency() > 0. ? 1. / prot_x->expectedFrequency() : 1.); coeffs.renew(); CONNECT1(void, bool, prot_x, received, this, received); @@ -778,3 +838,10 @@ void KX_Pult::toggledX(int index, bool on) { void KX_Pult::changedX(int index, int num) { prot_x->to_x.x_num[index] = num; } + + +void KX_Pult::commandClicked() { + QPushButton * b = qobject_cast(sender()); + if (!b) return; + prot_c->sendCommand(b->property("_command").toInt()); +} diff --git a/kx_utils/kx_pult.h b/kx_utils/kx_pult.h index 7851e0f..2e2d21b 100644 --- a/kx_utils/kx_pult.h +++ b/kx_utils/kx_pult.h @@ -21,6 +21,7 @@ #include #include "kx_coeffs.h" #include "kx_protocol_x.h" +#include "kx_protocol_c.h" #include "piqt.h" #include "session_manager.h" #include "qpievaluator.h" @@ -80,6 +81,7 @@ private: void progress(int val, int max); void clearSelected(); QString typeName(const QString & n) const; + int parseHeader(const QString & file, QMap & map); EVENT_HANDLER1(void, received, bool, ok); EVENT_HANDLER(void, pip_sendFailed) {emit q_k_sendFailed();} @@ -91,15 +93,15 @@ private: QVector values; Ui::KX_Pult * ui; - PIString config_, name_; + PIString config_, name_x, name_c; QDir dir; - QString outdir, kdesc_file; + QString outdir, kdesc_file, cdesc_file; QFile file; QTime tm, ctm; QIcon icon_record, icon_stop; QTextStream stream; QTimer timer_diag; - QMap kdesc; + QMap kdesc, cdesc; QMap knames; QSet calculated; QStringList knames_sort; @@ -110,6 +112,7 @@ private: //QVector k, x; KX_Coefficients coeffs; __KX_Protocol_X * prot_x; + __KX_Protocol_C * prot_c; int csize, wcnt, timer, timer_update, clear_target; bool needWrite, isPause, need_update, show_x; @@ -119,12 +122,15 @@ private slots: void updateGraph(); void updateDiag(); void updateKDesc(bool ask_move = false); + void updateCDesc(); void updateTree(bool move = false); + void updateCommands(); void filterTree(); void calculate(); void renew(bool write = true); void toggledX(int index, bool on); void changedX(int index, int num); + void commandClicked(); void k_sendFailed(); void k_sendSucceed(); void k_receiveFailed(); @@ -137,8 +143,10 @@ private slots: void on_buttonRead_clicked(); void on_buttonWrite_clicked(); void on_buttonResize_clicked(); - void on_buttonSetDesc_clicked(); - void on_buttonReparseDesc_clicked() {updateKDesc(true);} + void on_buttonSetKDesc_clicked(); + void on_buttonReparseKDesc_clicked() {updateKDesc(true);} + void on_buttonSetCDesc_clicked(); + void on_buttonReparseCDesc_clicked() {updateCDesc();} void on_buttonCalculate_clicked() {calculate();} void on_buttonApply_clicked() {renew();} void on_spinSize_valueChanged(int); diff --git a/kx_utils/kx_pult.ui b/kx_utils/kx_pult.ui index bf4dca6..b69c0da 100644 --- a/kx_utils/kx_pult.ui +++ b/kx_utils/kx_pult.ui @@ -6,8 +6,8 @@ 0 0 - 1142 - 598 + 1034 + 559 @@ -28,7 +28,7 @@ - + Reparse K desc @@ -119,7 +119,7 @@ - + Set K desc file ... @@ -392,6 +392,114 @@ + + + Commands (C) + + + + + + + + Set C desc file ... + + + + :/icons/document-open.png:/icons/document-open.png + + + + + + + Reparse C desc + + + + :/icons/view-refresh.png:/icons/view-refresh.png + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + + + Commands + + + + + + QFrame::NoFrame + + + true + + + + + 0 + 0 + 851 + 462 + + + + + 0 + + + 0 + + + + + + + + + + Qt::Vertical + + + QSizePolicy::Expanding + + + + 66 + 441 + + + + + + + + + + + + + + + Graphics (X) @@ -941,6 +1049,174 @@ + + + + C + + + + QFormLayout::AllNonFixedFieldsGrow + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + receiver: + + + + + + + + + + + + + + sender: + + + + + + + + + + + + + + type: + + + + + + + + + + + + + + address C: + + + + + + + address pult: + + + + + + + + + + + + + + sended count: + + + + + + + true + + + 0 + + + 1999999999 + + + + + + + received count: + + + + + + + true + + + 0 + + + 1999999999 + + + + + + + wrong received count: + + + + + + + true + + + 0 + + + 1999999999 + + + + + + + + + + + + + + missed received count: + + + + + + + true + + + 0 + + + 1999999999 + + + + + + diff --git a/qad_widgets/qrectedit.h b/qad_widgets/qrectedit.h index 49f973b..ed79136 100644 --- a/qad_widgets/qrectedit.h +++ b/qad_widgets/qrectedit.h @@ -13,18 +13,27 @@ class QRectEdit: public QWidget Q_OBJECT Q_PROPERTY(QRectF value READ value WRITE setValue) Q_PROPERTY(int decimals READ decimals WRITE setDecimals) - + Q_PROPERTY(double maximum READ maximum WRITE setMaximum) + Q_PROPERTY(double minimum READ minimum WRITE setMinimum) + Q_PROPERTY(double singleStep READ singleStep WRITE setSingleStep) + public: explicit QRectEdit(QWidget * parent = 0); ~QRectEdit() {delete s_x; delete s_y; delete s_w; delete s_h; delete lbl_0; delete lbl_1; delete lbl_2;} QRectF value() const {return QRectF(s_x->value(), s_y->value(), s_w->value(), s_h->value());} int decimals() const {return s_x->decimals();} - + double maximum() const {return s_x->maximum();} + double minimum() const {return s_x->minimum();} + double singleStep() const {return s_x->singleStep();} + public slots: void setValue(QRectF v) {s_x->setValue(v.x()); s_y->setValue(v.y()); s_w->setValue(v.width()); s_h->setValue(v.height());} void setDecimals(int d) {s_x->setDecimals(d); s_y->setDecimals(d); s_w->setDecimals(d); s_h->setDecimals(d);} - + void setMaximum(double m) {s_x->setMaximum(m); s_y->setMaximum(m); s_w->setMaximum(m); s_h->setMaximum(m);} + void setMinimum(double m) {s_x->setMinimum(m); s_y->setMinimum(m); s_w->setMinimum(m); s_h->setMinimum(m);} + void setSingleStep(double m) {s_x->setSingleStep(m); s_y->setSingleStep(m); s_w->setSingleStep(m); s_h->setSingleStep(m);} + private: virtual void changeEvent(QEvent * e);