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.

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