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.

285 lines
11 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
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
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
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
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. using namespace iflytop;
  11. using namespace clst;
  12. using namespace std;
  13. static MainWindow *m_mainWindow;
  14. static CLSTControler *m_clstc;
  15. #define TAG "MainWindow"
  16. static const char *fmt(const char *fmt, ...) {
  17. va_list args;
  18. va_start(args, fmt);
  19. static char buf[1024] = {0};
  20. vsnprintf(buf, sizeof(buf), fmt, args);
  21. va_end(args);
  22. return buf;
  23. }
  24. static const uint32_t str2int(QString str) {
  25. // 如果0x开头,??16进制转换
  26. // 如果0b开头,??2进制转换
  27. // 否则??10进制转换
  28. // 去除掉str中_
  29. str.remove("_");
  30. if (str.startsWith("0x")) {
  31. return str.toUInt(nullptr, 16);
  32. } else if (str.startsWith("0b")) {
  33. // remove 0b
  34. str.remove(0, 2);
  35. return str.toUInt(nullptr, 2);
  36. } else {
  37. return str.toUInt(nullptr, 10);
  38. }
  39. }
  40. // static QSerialPort G_SerialPort;
  41. // static QThread G_SerialPortThread;
  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::updatePage() {
  69. {
  70. float Freq;
  71. uint32_t PluseCnt;
  72. uint32_t SeqCtrlPluseCntMax;
  73. Freq = ui->InterClk_Freq_Text->text().toFloat();
  74. PluseCnt = ui->InterClk_PluseCnt_Text->text().toUInt();
  75. SeqCtrlPluseCntMax = ui->InterClk_SeqCtrlPluseCntMax_Text->text().toUInt();
  76. DO(m_clstc->InterClk_setFreq(Freq));
  77. DO(m_clstc->InterClk_setPluseCnt(PluseCnt));
  78. DO(m_clstc->InterClk_setSeqCtrlPluseCntMax(SeqCtrlPluseCntMax));
  79. }
  80. }
  81. void MainWindow::refreshPage() {
  82. {
  83. float Freq;
  84. uint32_t PluseCnt;
  85. uint32_t SeqCtrlPluseCntMax;
  86. float TriOutSignalFreq;
  87. DO(m_clstc->InterClk_getFreq(Freq));
  88. DO(m_clstc->InterClk_getPluseCnt(PluseCnt));
  89. DO(m_clstc->InterClk_getSeqCtrlPluseCntMax(SeqCtrlPluseCntMax));
  90. DO(m_clstc->InterClk_readTriOutSignalFreq(TriOutSignalFreq));
  91. ui->InterClk_Freq_Text->setText(QString::number(Freq, 'f', 2));
  92. ui->InterClk_PluseCnt_Text->setText(QString::number(PluseCnt));
  93. ui->InterClk_SeqCtrlPluseCntMax_Text->setText(QString::number(SeqCtrlPluseCntMax));
  94. ui->InterClk_TriOutSignalFreq_Text->setText(QString::number(TriOutSignalFreq, 'f', 2));
  95. }
  96. }
  97. void MainWindow::constructUI() {
  98. /*******************************************************************************
  99. * serialPortCB *
  100. *******************************************************************************/
  101. const auto infos = QSerialPortInfo::availablePorts();
  102. for (const QSerialPortInfo &info : infos) {
  103. ui->serialPortCB->addItem(info.portName());
  104. }
  105. /*******************************************************************************
  106. * ?? *
  107. *******************************************************************************/
  108. ui->serialBaudrateCB->addItem("9600");
  109. ui->serialBaudrateCB->addItem("14400");
  110. ui->serialBaudrateCB->addItem("19200");
  111. ui->serialBaudrateCB->addItem("38400");
  112. ui->serialBaudrateCB->addItem("57600");
  113. ui->serialBaudrateCB->addItem("115200");
  114. ui->serialBaudrateCB->setCurrentIndex(5);
  115. /*******************************************************************************
  116. * *
  117. *******************************************************************************/
  118. connect(ui->serialPortRefreshKey, &QPushButton::clicked, this, [this](bool check) {
  119. ui->serialPortCB->clear();
  120. const auto infos = QSerialPortInfo::availablePorts();
  121. for (const QSerialPortInfo &info : infos) {
  122. ui->serialPortCB->addItem(info.portName());
  123. }
  124. });
  125. /*******************************************************************************
  126. * *
  127. *******************************************************************************/
  128. connect(ui->serialOpenKey, &QPushButton::clicked, this, [=](bool check) {
  129. // 打开串口
  130. if (ui->serialOpenKey->text() == "打开") {
  131. G_QTDataChannel.setPortName(ui->serialPortCB->currentText().toStdString());
  132. G_QTDataChannel.setBaudRate(ui->serialBaudrateCB->currentText().toInt());
  133. G_QTDataChannel.setDataBits(QSerialPort::Data8);
  134. G_QTDataChannel.setParity(QSerialPort::NoParity);
  135. G_QTDataChannel.setFlowControl(QSerialPort::NoFlowControl);
  136. G_QTDataChannel.setStopBits(QSerialPort::OneStop);
  137. if (!G_QTDataChannel.open()) {
  138. QMessageBox::about(NULL, "提示", "串口无法打开,串口不存在或已被占??");
  139. return;
  140. }
  141. ui->serialOpenKey->setText("关闭");
  142. // 下拉菜单控件使能
  143. ui->serialBaudrateCB->setEnabled(false);
  144. ui->serialPortCB->setEnabled(false);
  145. ui->serialPortRefreshKey->setEnabled(false);
  146. } else {
  147. G_QTDataChannel.close();
  148. ui->serialOpenKey->setText("打开");
  149. ui->serialBaudrateCB->setEnabled(true);
  150. ui->serialPortCB->setEnabled(true);
  151. ui->serialPortRefreshKey->setEnabled(true);
  152. }
  153. });
  154. /*******************************************************************************
  155. * *
  156. *******************************************************************************/
  157. connect(ui->refreshPageKey, &QPushButton::clicked, this, [=](bool check) { //
  158. refreshPage();
  159. });
  160. connect(ui->UpdateCfg_Key, &QPushButton::clicked, this, [=](bool check) { //
  161. updatePage();
  162. refreshPage();
  163. });
  164. /*******************************************************************************
  165. * *
  166. *******************************************************************************/
  167. connect(ui->storageConfigKey, &QPushButton::clicked, this, [=](bool check) { //
  168. DO(m_clstc->storageConfigs());
  169. ui->informationBrowser->setText("保存配置成功");
  170. });
  171. /*******************************************************************************
  172. * *
  173. *******************************************************************************/
  174. connect(ui->rebootDeviceKey, &QPushButton::clicked, this, [=](bool check) { //
  175. DO(m_clstc->reboot());
  176. ui->informationBrowser->setText("重启设备成功");
  177. });
  178. /*******************************************************************************
  179. * *
  180. *******************************************************************************/
  181. connect(ui->factoryResetKey, &QPushButton::clicked, this, [=](bool check) { //
  182. DO(m_clstc->factoryReset());
  183. ui->informationBrowser->setText("恢复出厂设置成功");
  184. });
  185. /*******************************************************************************
  186. * ?? *
  187. *******************************************************************************/
  188. ui->RegAdd->setText("0x00000000");
  189. connect(ui->regReadKey, &QPushButton::clicked, this, [=](bool check) { //
  190. uint32_t addr = str2int(ui->RegAdd->text());
  191. uint32_t value = 0;
  192. DO(m_clstc->reg_read(addr, value, 100));
  193. ui->regReadbakVal->setText(fmt("0x%08X", value));
  194. ui->informationBrowser->setText(fmt("读取0x%04x成功", addr));
  195. });
  196. connect(ui->regWriteKey, &QPushButton::clicked, this, [=](bool check) { //
  197. uint32_t addr = str2int(ui->RegAdd->text());
  198. uint32_t value = str2int(ui->regWriteVal->text());
  199. uint32_t readkbak = 0;
  200. DO(m_clstc->reg_write(addr, value, readkbak, 100));
  201. ui->regReadbakVal->setText(fmt("0x%04x", readkbak));
  202. ui->informationBrowser->setText(fmt("写入0x%08x成功", addr));
  203. });
  204. connect(ui->InterClk_trigger_Key, &QPushButton::clicked, this, [=](bool check) { //
  205. DO(m_clstc->InterClk_trigger());
  206. ui->informationBrowser->setText(fmt("触发成功"));
  207. refreshPage();
  208. });
  209. connect(ui->InterClk_stop_Key, &QPushButton::clicked, this, [=](bool check) { //
  210. DO(m_clstc->InterClk_stop());
  211. ui->informationBrowser->setText(fmt("停止触发成功"));
  212. });
  213. }
  214. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
  215. // G_SerialPort.moveToThread();
  216. // QObject::connect(&G_SerialPortThread, &QThread::started, &G_SerialPort, &QSerialPort::open);
  217. G_QTDataChannel.init();
  218. CLSTControler::ins()->initialize(&G_QTDataChannel);
  219. m_clstc = CLSTControler::ins();
  220. ui->setupUi(this);
  221. m_mainWindow = this;
  222. qRegisterMetaType<int32_t>("int32_t");
  223. qRegisterMetaType<uint32_t>("uint32_t");
  224. qRegisterMetaType<function<void()>>("function<void()>");
  225. qRegisterMetaType<QFunction>("QFunction");
  226. // qInstallMessageHandler(log_output);
  227. connect(this, SIGNAL(doinui_signal(QFunction)), this, SLOT(doinui_slot(QFunction)));
  228. constructUI();
  229. m_clstc->regRawDataListener([this](uart_message_type_t type, uint8_t *data, size_t len) {
  230. QString text;
  231. if (type == kuart_raw_tx) {
  232. text.append("TX: ");
  233. for (size_t i = 0; i < len; i++) {
  234. text.append(fmt("%02X ", data[i]));
  235. }
  236. } else if (type == kuart_raw_rx) {
  237. text.append("RX: ");
  238. for (size_t i = 0; i < len; i++) {
  239. text.append(fmt("%02X ", data[i]));
  240. }
  241. }
  242. emit doinui_signal(QFunction([this, text]() {
  243. if (ui->instructionPreview->document()->lineCount() > 20) {
  244. ui->instructionPreview->clear();
  245. }
  246. ui->instructionPreview->append(text);
  247. }));
  248. });
  249. }
  250. MainWindow::~MainWindow() { delete ui; }