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.

236 lines
9.0 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
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
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
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 QThread G_SerialPortThread;
  43. static QTDataChannel G_QTDataChannel;
  44. static const QString zaferror_to_str(zaf_error_code_t value) {
  45. if (value == kaf_ec_overtime) {
  46. return "操作超时";
  47. } else if (value == kaf_ec_device_notopen) {
  48. return "设备未打开";
  49. } else {
  50. return "未知错误";
  51. }
  52. }
  53. #define DO(action) \
  54. { \
  55. zaf_error_code_t ecode = action; \
  56. if (ecode != kaf_ec_success) { \
  57. ui->informationBrowser->setText(zaferror_to_str(ecode)); \
  58. return; \
  59. } \
  60. }
  61. void MainWindow::log_output(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
  62. // QString text;
  63. // text.append(msg);
  64. }
  65. // void MainWindow::append_log_slot(QString text) { ui->logbrowser->append(text); }
  66. void MainWindow::doinui_slot(QFunction func) {
  67. if (func.get()) func.get()();
  68. }
  69. void MainWindow::constructUI() {
  70. /*******************************************************************************
  71. * serialPortCB *
  72. *******************************************************************************/
  73. const auto infos = QSerialPortInfo::availablePorts();
  74. for (const QSerialPortInfo &info : infos) {
  75. ui->serialPortCB->addItem(info.portName());
  76. }
  77. /*******************************************************************************
  78. * *
  79. *******************************************************************************/
  80. ui->serialBaudrateCB->addItem("9600");
  81. ui->serialBaudrateCB->addItem("14400");
  82. ui->serialBaudrateCB->addItem("19200");
  83. ui->serialBaudrateCB->addItem("38400");
  84. ui->serialBaudrateCB->addItem("57600");
  85. ui->serialBaudrateCB->addItem("115200");
  86. ui->serialBaudrateCB->setCurrentIndex(5);
  87. /*******************************************************************************
  88. * *
  89. *******************************************************************************/
  90. connect(ui->serialPortRefreshKey, &QPushButton::clicked, this, [this](bool check) {
  91. ui->serialPortCB->clear();
  92. const auto infos = QSerialPortInfo::availablePorts();
  93. for (const QSerialPortInfo &info : infos) {
  94. ui->serialPortCB->addItem(info.portName());
  95. }
  96. });
  97. /*******************************************************************************
  98. * *
  99. *******************************************************************************/
  100. connect(ui->serialOpenKey, &QPushButton::clicked, this, [=](bool check) {
  101. // 打开串口
  102. if (ui->serialOpenKey->text() == "打开") {
  103. G_QTDataChannel.setPortName(ui->serialPortCB->currentText().toStdString());
  104. G_QTDataChannel.setBaudRate(ui->serialBaudrateCB->currentText().toInt());
  105. G_QTDataChannel.setDataBits(QSerialPort::Data8);
  106. G_QTDataChannel.setParity(QSerialPort::NoParity);
  107. G_QTDataChannel.setFlowControl(QSerialPort::NoFlowControl);
  108. G_QTDataChannel.setStopBits(QSerialPort::OneStop);
  109. if (!G_QTDataChannel.open()) {
  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_QTDataChannel.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. * *
  154. *******************************************************************************/
  155. ui->RegAdd->setText("0x00000000");
  156. connect(ui->regReadKey, &QPushButton::clicked, this, [=](bool check) { //
  157. uint32_t addr = str2int(ui->RegAdd->text());
  158. uint32_t value = 0;
  159. DO(m_clstc->reg_read(addr, value, 100));
  160. ui->regReadbakVal->setText(fmt("0x%08X", value));
  161. ui->informationBrowser->setText(fmt("读0x%04x成功", addr));
  162. });
  163. connect(ui->regWriteKey, &QPushButton::clicked, this, [=](bool check) { //
  164. uint32_t addr = str2int(ui->RegAdd->text());
  165. uint32_t value = str2int(ui->regWriteVal->text());
  166. uint32_t readkbak = 0;
  167. DO(m_clstc->reg_write(addr, value, readkbak, 100));
  168. ui->regReadbakVal->setText(fmt("0x%04x", readkbak));
  169. ui->informationBrowser->setText(fmt("写0x%04x成功", addr));
  170. });
  171. }
  172. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
  173. // G_SerialPort.moveToThread();
  174. // QObject::connect(&G_SerialPortThread, &QThread::started, &G_SerialPort, &QSerialPort::open);
  175. G_QTDataChannel.init();
  176. CLSTControler::ins()->initialize(&G_QTDataChannel);
  177. m_clstc = CLSTControler::ins();
  178. ui->setupUi(this);
  179. m_mainWindow = this;
  180. qRegisterMetaType<int32_t>("int32_t");
  181. qRegisterMetaType<uint32_t>("uint32_t");
  182. qRegisterMetaType<function<void()>>("function<void()>");
  183. qRegisterMetaType<QFunction>("QFunction");
  184. // qInstallMessageHandler(log_output);
  185. connect(this, SIGNAL(doinui_signal(QFunction)), this, SLOT(doinui_slot(QFunction)));
  186. constructUI();
  187. m_clstc->regRawDataListener([this](uart_message_type_t type, uint8_t *data, size_t len) {
  188. QString text;
  189. if (type == kuart_raw_tx) {
  190. text.append("TX: ");
  191. for (size_t i = 0; i < len; i++) {
  192. text.append(fmt("%02X ", data[i]));
  193. }
  194. } else if (type == kuart_raw_rx) {
  195. text.append("RX: ");
  196. for (size_t i = 0; i < len; i++) {
  197. text.append(fmt("%02X ", data[i]));
  198. }
  199. }
  200. emit doinui_signal(QFunction([this, text]() {
  201. if (ui->instructionPreview->document()->lineCount() > 20) {
  202. ui->instructionPreview->clear();
  203. }
  204. ui->instructionPreview->append(text);
  205. }));
  206. });
  207. }
  208. MainWindow::~MainWindow() { delete ui; }