You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
10 KiB

2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "mainwindow.h"
  2. #include <QDateTime>
  3. #include <QMessageBox>
  4. #include <QtConcurrent>
  5. #include <QtCore/QVariant>
  6. #include <QtSerialPort/QSerialPort>
  7. #include <QtSerialPort/QSerialPortInfo>
  8. #include <QtWidgets/QLineEdit>
  9. #include "./ui_mainwindow.h"
  10. #include "iflytop_canbus/iflytop_canbus_master.hpp"
  11. #include "logger.hpp"
  12. #include "qt_serial_datachannel.hpp"
  13. #include "reglist.hpp"
  14. #include "zexception.hpp"
  15. #include "zui/z_function_list_box.hpp"
  16. #include "zui/zq_vtab_page.hpp"
  17. //
  18. #include <thread>
  19. #include "zui/zui.hpp"
  20. //
  21. #include "tab/board_ext_tab.hpp"
  22. #include "tab/code_scaner_tab.hpp"
  23. #include "tab/fan_ctrl_tab.hpp"
  24. #include "tab/id_card_read_tab.hpp"
  25. #include "tab/mini_servo_tab.hpp"
  26. #include "tab/module_opera_tab.hpp"
  27. #include "tab/pipette_ctrl_tab.hpp"
  28. #include "tab/step_motor_ctrl_tab.hpp"
  29. #include "tab/water_cooling_temperature_tab.hpp"
  30. #include "tab/xyrobot_tab.hpp"
  31. #include "tab/plate_code_scaner_tab.hpp"
  32. #include "version.h"
  33. using namespace std;
  34. using namespace iflytop;
  35. using namespace zcr;
  36. typedef enum {
  37. kone_lead_ecg,
  38. kthree_lead_ecg,
  39. } device_type_t;
  40. static MainWindow *m_mainWindow;
  41. static QTDataChannel G_QTDataChannel;
  42. static QTDataChannel G_WaveDataChannel;
  43. int PublicState_DeviceIDVal = 0;
  44. int getDeviceId() { return PublicState_DeviceIDVal; }
  45. Ui::MainWindow *main_ui;
  46. QT_REQUIRE_CONFIG(groupbox);
  47. #define TAG "MainWindow"
  48. // static inline const char *fmt(const char *fmt, ...) {
  49. // va_list args;
  50. // va_start(args, fmt);
  51. // static char buf[1024] = {0};
  52. // vsnprintf(buf, sizeof(buf), fmt, args);
  53. // va_end(args);
  54. // return buf;
  55. // }
  56. void MainWindow::log_output(QtMsgType type, const QMessageLogContext &context, const QString &msg) {}
  57. void MainWindow::doinui_slot(QFunction func) {
  58. if (func.get()) func.get()();
  59. }
  60. /***********************************************************************************************************************
  61. * PreviewShow *
  62. ***********************************************************************************************************************/
  63. void MainWindow::ishow(QString text) {
  64. QString info;
  65. info.append(QDateTime::currentDateTime().toString("hh:mm:ss"));
  66. info.append("|");
  67. info.append(text);
  68. emit doinui_signal(QFunction([this, info]() { ui->instructionPreview->append(info); }));
  69. }
  70. void MainWindow::rawshow(QString text) {
  71. QString info;
  72. info.append(QDateTime::currentDateTime().toString("hh:mm:ss.zzz"));
  73. info.append(text);
  74. emit doinui_signal(QFunction([this, info]() {
  75. if (ui->rawDataPreview->document()->lineCount() > 1000) {
  76. ui->rawDataPreview->document()->clear();
  77. }
  78. ui->rawDataPreview->append(info);
  79. }));
  80. }
  81. void MainWindow::instructionPreviewClear() {
  82. doinui_signal(QFunction([this]() { ui->instructionPreview->clear(); }));
  83. }
  84. /***********************************************************************************************************************
  85. * UI相关构造 *
  86. ***********************************************************************************************************************/
  87. void MainWindow::constructBaseUI() {
  88. /**
  89. * @brief UI
  90. */
  91. {
  92. const auto infos = QSerialPortInfo::availablePorts();
  93. for (const QSerialPortInfo &info : infos) {
  94. ui->serialPortCB->addItem(info.portName());
  95. }
  96. }
  97. ui->serialBaudrateCB->addItem("2000000");
  98. ui->serialBaudrateCB->setCurrentIndex(0);
  99. connect(ui->serialPortRefreshKey, &QPushButton::clicked, this, [this](bool check) {
  100. ui->serialPortCB->clear();
  101. const auto infos = QSerialPortInfo::availablePorts();
  102. for (const QSerialPortInfo &info : infos) {
  103. ui->serialPortCB->addItem(info.portName());
  104. }
  105. });
  106. connect(ui->serialOpenKey, &QPushButton::clicked, this, [=](bool check) {
  107. // 打开串口
  108. if (ui->serialOpenKey->text() == "打开") {
  109. G_QTDataChannel.setPortName(ui->serialPortCB->currentText().toStdString());
  110. G_QTDataChannel.setBaudRate(ui->serialBaudrateCB->currentText().toInt());
  111. G_QTDataChannel.setDataBits(QSerialPort::Data8);
  112. G_QTDataChannel.setParity(QSerialPort::NoParity);
  113. G_QTDataChannel.setFlowControl(QSerialPort::NoFlowControl);
  114. G_QTDataChannel.setStopBits(QSerialPort::OneStop);
  115. /**
  116. * @brief
  117. */
  118. if (!G_QTDataChannel.open()) {
  119. QMessageBox::about(NULL, "提示", "串口无法打开,串口不存在或已被占??");
  120. return;
  121. }
  122. ui->serialOpenKey->setText("关闭");
  123. // 下拉菜单控件使能
  124. ui->serialBaudrateCB->setEnabled(false);
  125. ui->serialPortCB->setEnabled(false);
  126. ui->serialPortRefreshKey->setEnabled(false);
  127. IflytopCanbusMaster::ins()->updateChannelConfig();
  128. } else {
  129. G_QTDataChannel.close();
  130. ui->serialOpenKey->setText("打开");
  131. ui->serialBaudrateCB->setEnabled(true);
  132. ui->serialPortCB->setEnabled(true);
  133. ui->serialPortRefreshKey->setEnabled(true);
  134. }
  135. });
  136. }
  137. void MainWindow::displayInfo(bool suc, QString info) {}
  138. static const char *fmt(const char *fmt, ...) {
  139. va_list args;
  140. va_start(args, fmt);
  141. static char buf[1024] = {0};
  142. vsnprintf(buf, sizeof(buf), fmt, args);
  143. va_end(args);
  144. return buf;
  145. }
  146. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
  147. /**
  148. * @brief QT初始化
  149. */
  150. // VERSION
  151. main_ui = ui;
  152. ui->setupUi(this);
  153. m_mainWindow = this;
  154. qRegisterMetaType<int32_t>("int32_t");
  155. qRegisterMetaType<uint32_t>("uint32_t");
  156. qRegisterMetaType<float>("float");
  157. qRegisterMetaType<function<void()>>("function<void()>");
  158. qRegisterMetaType<QFunction>("QFunction");
  159. connect(this, SIGNAL(doinui_signal(QFunction)), this, SLOT(doinui_slot(QFunction)));
  160. ZQUI::ins()->initialize();
  161. ZQUI::ins()->setishow([this](QString text) { ishow(text); });
  162. ZQUI::ins()->setrawshow([this](QString text) { rawshow(text); });
  163. ZQUI::ins()->setInstructionPreviewClear([this]() { instructionPreviewClear(); });
  164. ui->menu_version->setTitle(QApplication::translate("MainWindow", VERSION, nullptr));
  165. /**
  166. * @brief
  167. */
  168. constructBaseUI();
  169. constructAppUI();
  170. /**
  171. * @brief
  172. */
  173. G_QTDataChannel.init();
  174. G_WaveDataChannel.init();
  175. IflytopCanbusMaster::ins()->initialize(&G_QTDataChannel);
  176. IflytopCanbusMaster::ins()->regOnRawData([this](raw_data_type_t type, uint8_t *hex, uint32_t hexlen) {
  177. zcr_cmd_header_t *frame = (zcr_cmd_header_t *)hex;
  178. int cmdId = frame->cmdMainId * 256 + frame->cmdSubId;
  179. int32_t *param = (int32_t *)frame->data;
  180. int32_t mid = frame->subModuleid;
  181. int32_t packetType = frame->packetType;
  182. if (type == kcmd_cmd) {
  183. if (cmdId == kmodule_set_reg) {
  184. // ZQUI::ins()->rawshow("[CMD ] %s", zhex2str(hex, hexlen).c_str());
  185. ZQUI::ins()->rawshow(" [-->] module_set_reg %d(mid) %s(%d) %d(val)", mid, regindex2str(param[0]), param[0], param[1]);
  186. } else if (cmdId == kmodule_get_reg) {
  187. // ZQUI::ins()->rawshow("[CMD ] %s", zhex2str(hex, hexlen).c_str());
  188. ZQUI::ins()->rawshow(" [-->] module_get_reg %d(mid) %s(%d)", mid, regindex2str(param[0]), param[0]);
  189. } else {
  190. if (cmdId != kmodule_get_status) {
  191. ZQUI::ins()->rawshow("[CMD ] %d cmdid:(%s)0x%04x mid:%d param:%s", frame->packetindex, cmdid2str(cmdId), cmdId, mid, zhex2str(frame->data, hexlen - sizeof(zcr_cmd_header_t)).c_str());
  192. } else {
  193. // ZQUI::ins()->rawshow("[CMD ] %s", zhex2str(hex, hexlen).c_str());
  194. }
  195. }
  196. } else if (type == kcmd_receipt) {
  197. if (kptv2_error_ack == packetType) {
  198. ZQUI::ins()->rawshow("[E-RECEI] %s(%d)", err::error2str(param[0]), param[0]);
  199. } else {
  200. if (cmdId != kmodule_get_status) {
  201. ZQUI::ins()->rawshow("[ RECEI] %s", zhex2str(hex, hexlen).c_str());
  202. } else {
  203. }
  204. }
  205. } else if (type == kcmd_report) {
  206. ZQUI::ins()->rawshow("[REPORT ] %s", zhex2str(hex, hexlen).c_str());
  207. }
  208. });
  209. }
  210. MainWindow::~MainWindow() { delete ui; }
  211. void MainWindow::processException(const zexception &e) { //
  212. ZQUI::ins()->ishow("%s", e.what());
  213. }
  214. int32_t MainWindow::getDeviceId() { return PublicState_DeviceIDVal; }
  215. void MainWindow::constructAppUI() {
  216. ModuleOperaTab::inst()->construct(ui->moduleOpera);
  217. BoardExtTab::inst()->construct(ui->buttonTabWidget);
  218. StepMotorCtrlTab::inst()->construct(ui->buttonTabWidget);
  219. StepMotorCtrlTab::inst()->constructRegTab(ui->buttonTabWidget);
  220. MiniServoTab::inst()->construct(ui->buttonTabWidget);
  221. CodeScanerTab::inst()->construct(ui->buttonTabWidget);
  222. PipetteCtrlTab::inst()->construct(ui->buttonTabWidget);
  223. WaterCoolingTemperatureControlerTab::inst()->construct(ui->buttonTabWidget);
  224. FanCtrlTab::inst()->construct(ui->buttonTabWidget);
  225. XYRobotTab::inst()->construct(ui->buttonTabWidget);
  226. IDCardReaderTab::inst()->construct(ui->buttonTabWidget);
  227. PlateCodeScanerTab::inst()->construct(ui->buttonTabWidget);
  228. }
  229. void MainWindow::on_PublicState_ConfirmKey_clicked() {}
  230. void MainWindow::on_PublicState_DeviceID_textChanged() { PublicState_DeviceIDVal = ui->PublicState_DeviceID->toPlainText().toInt(); }
  231. void MainWindow::on_clearPreview_clicked() {
  232. doinui_signal(QFunction([this]() { ui->instructionPreview->clear(); }));
  233. doinui_signal(QFunction([this]() { ui->rawDataPreview->clear(); }));
  234. }
  235. void MainWindow::on_dumpFuncListButton_clicked() {
  236. zcr::cmdinfo_t *cmdtable = cmdid_get_table();
  237. ui->instructionPreview->clear();
  238. for (int i = 0; i < cmdid_get_table_size(); i++) {
  239. ZQUI::ins()->ishow("%-50s %8d (0x%04x)", cmdtable[i].fnname, cmdtable[i].index, cmdtable[i].index);
  240. }
  241. }
  242. void MainWindow::on_dumpErrorCodeButton_clicked() {
  243. ui->instructionPreview->clear();
  244. err::ecode_table_item_t *err_table = err::error_get_table();
  245. for (int i = 0; i < err::error_get_table_size(); i++) {
  246. ZQUI::ins()->ishow("%-50s %d", err_table[i].info, err_table[i].index);
  247. }
  248. }
  249. void MainWindow::on_dumpRegListButton_clicked() {
  250. ui->instructionPreview->clear();
  251. reginfo_t *regtable = reg_get_table();
  252. for (int i = 0; i < reg_get_table_size(); i++) {
  253. ZQUI::ins()->ishow("%-50s %d", regtable[i].regname, regtable[i].index);
  254. }
  255. }