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.

210 lines
7.9 KiB

2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years 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
2 years ago
1 year ago
2 years 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
2 years ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years 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 <QtSerialPort/QSerialPort>
  6. #include <QtSerialPort/QSerialPortInfo>
  7. #include "./ui_mainwindow.h"
  8. #include "camera_light_src_timing_controller/qt_serial_datachannel.hpp"
  9. #include "logger.hpp"
  10. #include "xsync_regs.hpp"
  11. using namespace iflytop;
  12. using namespace clst;
  13. using namespace std;
  14. static MainWindow *m_mainWindow;
  15. static CLSTControler *m_clstc;
  16. #define TAG "MainWindow"
  17. static const char *fmt(const char *fmt, ...) {
  18. va_list args;
  19. va_start(args, fmt);
  20. static char buf[1024] = {0};
  21. vsnprintf(buf, sizeof(buf), fmt, args);
  22. va_end(args);
  23. return buf;
  24. }
  25. static const uint32_t str2int(QString str) {
  26. // 如果0x开头,按16进制转换
  27. // 如果0b开头,按2进制转换
  28. // 否则按10进制转换
  29. // 去除掉str中_
  30. str.remove("_");
  31. if (str.startsWith("0x")) {
  32. return str.toUInt(nullptr, 16);
  33. } else if (str.startsWith("0b")) {
  34. // remove 0b
  35. str.remove(0, 2);
  36. return str.toUInt(nullptr, 2);
  37. } else {
  38. return str.toUInt(nullptr, 10);
  39. }
  40. }
  41. static QSerialPort G_SerialPort;
  42. static QTDataChannel G_QTDataChannel;
  43. static const QString zaferror_to_str(zaf_error_code_t value) {
  44. if (value == kaf_ec_overtime) {
  45. return "操作超时";
  46. } else if (value == kaf_ec_device_notopen) {
  47. return "设备未打开";
  48. } else {
  49. return "未知错误";
  50. }
  51. }
  52. #define DO(action) \
  53. { \
  54. zaf_error_code_t ecode = action; \
  55. if (ecode != kaf_ec_success) { \
  56. ui->informationBrowser->setText(zaferror_to_str(ecode)); \
  57. return; \
  58. } \
  59. }
  60. void MainWindow::log_output(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
  61. // QString text;
  62. // text.append(msg);
  63. }
  64. // void MainWindow::append_log_slot(QString text) { ui->logbrowser->append(text); }
  65. void MainWindow::doinui_slot(QFunction func) {
  66. if (func.get()) func.get()();
  67. }
  68. void MainWindow::constructUI() {
  69. /*******************************************************************************
  70. * serialPortCB *
  71. *******************************************************************************/
  72. const auto infos = QSerialPortInfo::availablePorts();
  73. for (const QSerialPortInfo &info : infos) {
  74. ui->serialPortCB->addItem(info.portName());
  75. }
  76. /*******************************************************************************
  77. * *
  78. *******************************************************************************/
  79. ui->serialBaudrateCB->addItem("9600");
  80. ui->serialBaudrateCB->addItem("14400");
  81. ui->serialBaudrateCB->addItem("19200");
  82. ui->serialBaudrateCB->addItem("38400");
  83. ui->serialBaudrateCB->addItem("57600");
  84. ui->serialBaudrateCB->addItem("115200");
  85. ui->serialBaudrateCB->setCurrentIndex(5);
  86. /*******************************************************************************
  87. * *
  88. *******************************************************************************/
  89. connect(ui->serialPortRefreshKey, &QPushButton::clicked, this, [this](bool check) {
  90. ui->serialPortCB->clear();
  91. const auto infos = QSerialPortInfo::availablePorts();
  92. for (const QSerialPortInfo &info : infos) {
  93. ui->serialPortCB->addItem(info.portName());
  94. }
  95. });
  96. /*******************************************************************************
  97. * *
  98. *******************************************************************************/
  99. connect(ui->serialOpenKey, &QPushButton::clicked, this, [=](bool check) {
  100. // 打开串口
  101. if (ui->serialOpenKey->text() == "打开") {
  102. G_SerialPort.setPortName(ui->serialPortCB->currentText());
  103. if (G_SerialPort.open(QIODevice::ReadWrite)) {
  104. G_SerialPort.setBaudRate(ui->serialBaudrateCB->currentText().toInt());
  105. G_SerialPort.setDataBits(QSerialPort::Data8);
  106. G_SerialPort.setParity(QSerialPort::NoParity);
  107. G_SerialPort.setFlowControl(QSerialPort::NoFlowControl);
  108. G_SerialPort.setStopBits(QSerialPort::OneStop);
  109. } else {
  110. QMessageBox::about(NULL, "提示", "串口无法打开,串口不存在或已被占用");
  111. return;
  112. }
  113. ui->serialOpenKey->setText("关闭");
  114. // 下拉菜单控件使能
  115. ui->serialBaudrateCB->setEnabled(false);
  116. ui->serialPortCB->setEnabled(false);
  117. ui->serialPortRefreshKey->setEnabled(false);
  118. } else {
  119. G_SerialPort.close();
  120. ui->serialOpenKey->setText("打开");
  121. ui->serialBaudrateCB->setEnabled(true);
  122. ui->serialPortCB->setEnabled(true);
  123. ui->serialPortRefreshKey->setEnabled(true);
  124. }
  125. });
  126. /*******************************************************************************
  127. * *
  128. *******************************************************************************/
  129. connect(ui->refreshPageKey, &QPushButton::clicked, this, [=](bool check) { //
  130. });
  131. /*******************************************************************************
  132. * *
  133. *******************************************************************************/
  134. connect(ui->storageConfigKey, &QPushButton::clicked, this, [=](bool check) { //
  135. DO(m_clstc->storageConfigs());
  136. ui->informationBrowser->setText("保存配置成功");
  137. });
  138. /*******************************************************************************
  139. * *
  140. *******************************************************************************/
  141. connect(ui->rebootDeviceKey, &QPushButton::clicked, this, [=](bool check) { //
  142. DO(m_clstc->reboot());
  143. ui->informationBrowser->setText("重启设备成功");
  144. });
  145. /*******************************************************************************
  146. * *
  147. *******************************************************************************/
  148. connect(ui->factoryResetKey, &QPushButton::clicked, this, [=](bool check) { //
  149. DO(m_clstc->factoryReset());
  150. ui->informationBrowser->setText("恢复出厂设置成功");
  151. });
  152. }
  153. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
  154. G_QTDataChannel.bind(&G_SerialPort);
  155. CLSTControler::ins()->initialize(&G_QTDataChannel);
  156. m_clstc = CLSTControler::ins();
  157. ui->setupUi(this);
  158. m_mainWindow = this;
  159. qRegisterMetaType<int32_t>("int32_t");
  160. qRegisterMetaType<uint32_t>("uint32_t");
  161. qRegisterMetaType<function<void()>>("function<void()>");
  162. qRegisterMetaType<QFunction>("QFunction");
  163. // qInstallMessageHandler(log_output);
  164. connect(this, SIGNAL(doinui_signal(QFunction)), this, SLOT(doinui_slot(QFunction)));
  165. constructUI();
  166. m_clstc->regRawDataListener([this](uart_message_type_t type, uint8_t *data, size_t len) {
  167. QString text;
  168. if (type == kuart_raw_tx) {
  169. text.append("TX: ");
  170. for (size_t i = 0; i < len; i++) {
  171. text.append(fmt("%02X ", data[i]));
  172. }
  173. } else if (type == kuart_raw_rx) {
  174. text.append("RX: ");
  175. for (size_t i = 0; i < len; i++) {
  176. text.append(fmt("%02X ", data[i]));
  177. }
  178. }
  179. emit doinui_signal(QFunction([this, text]() {
  180. if (ui->instructionPreview->document()->lineCount() > 20) {
  181. ui->instructionPreview->clear();
  182. }
  183. ui->instructionPreview->append(text);
  184. }));
  185. });
  186. }
  187. MainWindow::~MainWindow() { delete ui; }