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.

231 lines
8.8 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
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. * *
  154. *******************************************************************************/
  155. connect(ui->regReadKey, &QPushButton::clicked, this, [=](bool check) { //
  156. uint32_t addr = str2int(ui->RegAdd->text());
  157. uint32_t value = 0;
  158. DO(m_clstc->reg_read(addr, value, 100));
  159. ui->regReadbakVal->setText(fmt("0x%08X", value));
  160. ui->informationBrowser->setText(fmt("读0x%04x成功", addr));
  161. });
  162. connect(ui->regWriteKey, &QPushButton::clicked, this, [=](bool check) { //
  163. uint32_t addr = str2int(ui->RegAdd->text());
  164. uint32_t value = str2int(ui->regWriteVal->text());
  165. uint32_t readkbak = 0;
  166. DO(m_clstc->reg_write(addr, value, readkbak, 100));
  167. ui->regReadbakVal->setText(fmt("0x%04x", readkbak));
  168. ui->informationBrowser->setText(fmt("写0x%04x成功", addr));
  169. });
  170. }
  171. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
  172. G_QTDataChannel.bind(&G_SerialPort);
  173. CLSTControler::ins()->initialize(&G_QTDataChannel);
  174. m_clstc = CLSTControler::ins();
  175. ui->setupUi(this);
  176. m_mainWindow = this;
  177. qRegisterMetaType<int32_t>("int32_t");
  178. qRegisterMetaType<uint32_t>("uint32_t");
  179. qRegisterMetaType<function<void()>>("function<void()>");
  180. qRegisterMetaType<QFunction>("QFunction");
  181. // qInstallMessageHandler(log_output);
  182. connect(this, SIGNAL(doinui_signal(QFunction)), this, SLOT(doinui_slot(QFunction)));
  183. constructUI();
  184. m_clstc->regRawDataListener([this](uart_message_type_t type, uint8_t *data, size_t len) {
  185. QString text;
  186. if (type == kuart_raw_tx) {
  187. text.append("TX: ");
  188. for (size_t i = 0; i < len; i++) {
  189. text.append(fmt("%02X ", data[i]));
  190. }
  191. } else if (type == kuart_raw_rx) {
  192. text.append("RX: ");
  193. for (size_t i = 0; i < len; i++) {
  194. text.append(fmt("%02X ", data[i]));
  195. }
  196. }
  197. emit doinui_signal(QFunction([this, text]() {
  198. if (ui->instructionPreview->document()->lineCount() > 20) {
  199. ui->instructionPreview->clear();
  200. }
  201. ui->instructionPreview->append(text);
  202. }));
  203. });
  204. }
  205. MainWindow::~MainWindow() { delete ui; }