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.

262 lines
9.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "mainwindow.h"
  2. #include <QDateTime>
  3. #include <QtConcurrent>
  4. #include "./ui_mainwindow.h"
  5. using namespace iflytop;
  6. using namespace std;
  7. static MainWindow *m_mainWindow;
  8. #define TAG "MainWindow"
  9. static const char *fmt(const char *fmt, ...) {
  10. va_list args;
  11. va_start(args, fmt);
  12. static char buf[1024] = {0};
  13. vsnprintf(buf, sizeof(buf), fmt, args);
  14. va_end(args);
  15. return buf;
  16. }
  17. static const uint32_t str2int(QString str) {
  18. // 如果0x开头,按16进制转换
  19. // 如果0b开头,按2进制转换
  20. // 否则按10进制转换
  21. // 去除掉str中_
  22. str.remove("_");
  23. if (str.startsWith("0x")) {
  24. return str.toUInt(nullptr, 16);
  25. } else if (str.startsWith("0b")) {
  26. // remove 0b
  27. str.remove(0, 2);
  28. return str.toUInt(nullptr, 2);
  29. } else {
  30. return str.toUInt(nullptr, 10);
  31. }
  32. }
  33. void MainWindow::log_output(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
  34. QString text;
  35. text.append(msg);
  36. m_mainWindow->append_log_signal(text);
  37. }
  38. void MainWindow::append_log_slot(QString text) { ui->logbrowser->append(text); }
  39. void MainWindow::updateUI_timeCodeInfo_slot(QString text) { ui->TimecodeDisplayer->setText(text); }
  40. void MainWindow::updateUI_cameraSyncInfo_slot(QString text) { ui->CameraSyncIndex->setText(text); }
  41. void MainWindow::updateUI_reg_slot(int32_t regadd, uint32_t regval) {
  42. auto it = m_regdisplayer.find(regadd);
  43. if (it != m_regdisplayer.end()) {
  44. ZLOGI(TAG, "updateUI_reg_slot %x %d", regadd, regval);
  45. it->second->regBrowser->setText(QString::number(regval, 16));
  46. }
  47. }
  48. void MainWindow::doinui_slot(QFunction func) {
  49. if (func.get()) func.get()();
  50. }
  51. void MainWindow::push_reg(QWidget *parent, int off, const char *regname, int32_t regadd, int32_t rwflag) {
  52. DispalyRegIterm *regitem = new DispalyRegIterm();
  53. {
  54. auto *label = new QLabel(parent);
  55. label->setObjectName(QString::fromUtf8("label"));
  56. label->setText(QString(fmt("%s(0x%04x)", regname, regadd)));
  57. QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Expanding);
  58. sizePolicy1.setHorizontalStretch(0);
  59. sizePolicy1.setVerticalStretch(0);
  60. sizePolicy1.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
  61. label->setSizePolicy(sizePolicy1);
  62. label->setMinimumSize(QSize(200, 0));
  63. label->setMaximumSize(QSize(16777215, 16777215));
  64. ui->reg_table->addWidget(label, off, 0, 1, 1);
  65. regitem->label = label;
  66. }
  67. {
  68. auto *textbrowser = new QTextBrowser(parent);
  69. textbrowser->setObjectName(QString::fromUtf8("textbrowser"));
  70. textbrowser->setEnabled(true);
  71. textbrowser->setText(QString("demo"));
  72. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  73. sizePolicy.setHorizontalStretch(0);
  74. sizePolicy.setVerticalStretch(0);
  75. sizePolicy.setHeightForWidth(textbrowser->sizePolicy().hasHeightForWidth());
  76. textbrowser->setSizePolicy(sizePolicy);
  77. textbrowser->setMaximumSize(QSize(16777215, 16777215));
  78. ui->reg_table->addWidget(textbrowser, off, 1, 1, 1);
  79. regitem->regBrowser = textbrowser;
  80. }
  81. {
  82. auto *textEdit = new QTextEdit(parent);
  83. textEdit->setObjectName(QString::fromUtf8("textEdit"));
  84. textEdit->setEnabled(true);
  85. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  86. sizePolicy.setHorizontalStretch(0);
  87. sizePolicy.setVerticalStretch(0);
  88. sizePolicy.setHeightForWidth(textEdit->sizePolicy().hasHeightForWidth());
  89. textEdit->setSizePolicy(sizePolicy);
  90. textEdit->setMaximumSize(QSize(16777215, 16777215));
  91. ui->reg_table->addWidget(textEdit, off, 2, 1, 1);
  92. regitem->regEditer = textEdit;
  93. }
  94. {
  95. // new button
  96. auto *button = new QPushButton(parent);
  97. button->setObjectName(QString::fromUtf8("button"));
  98. button->setText(QString("Write"));
  99. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  100. sizePolicy.setHorizontalStretch(0);
  101. sizePolicy.setVerticalStretch(0);
  102. sizePolicy.setHeightForWidth(button->sizePolicy().hasHeightForWidth());
  103. button->setSizePolicy(sizePolicy);
  104. // connect(this,
  105. connect(button, &QPushButton::clicked, [this, regadd](bool) { //
  106. QString regval_str = m_regdisplayer[regadd]->regEditer->toPlainText();
  107. uint32_t regval = str2int(regval_str);
  108. ZLOGI(TAG, "write reg 0x%04x %d", regadd, regval);
  109. uint32_t readbackval = 0;
  110. auto ecode = Xsync::Ins().reg_write(regadd, regval, readbackval);
  111. if (ecode == kxs_ec_success) {
  112. ZLOGI(TAG, "write reg 0x%04x %d success", regadd, regval);
  113. m_regdisplayer[regadd]->regvalcache = regval;
  114. m_regdisplayer[regadd]->regBrowser->setText(QString::number(regval, 16));
  115. } else {
  116. ZLOGE(TAG, "write reg 0x%04x %d fail,ecode:%s", regadd, regval, xs_error_code_2_str(ecode));
  117. }
  118. });
  119. regitem->writerButton = button;
  120. ui->reg_table->addWidget(button, off, 3, 1, 1);
  121. }
  122. m_regdisplayer[regadd] = regitem;
  123. }
  124. void MainWindow::construct_reg_table() { //
  125. int regoff = 1;
  126. push_reg(ui->gridLayoutWidget, regoff++, "software_version", kxsync_reg_software_version, 0);
  127. push_reg(ui->gridLayoutWidget, regoff++, "manufacturer0", kxsync_reg_manufacturer0, 0);
  128. push_reg(ui->gridLayoutWidget, regoff++, "manufacturer1", kxsync_reg_manufacturer1, 0);
  129. push_reg(ui->gridLayoutWidget, regoff++, "product_type_id", kxsync_reg_product_type_id, 0);
  130. push_reg(ui->gridLayoutWidget, regoff++, "sn_id0", kxsync_reg_sn_id0, 0);
  131. push_reg(ui->gridLayoutWidget, regoff++, "sn_id1", kxsync_reg_sn_id1, 0);
  132. push_reg(ui->gridLayoutWidget, regoff++, "sn_id2", kxsync_reg_sn_id2, 0);
  133. push_reg(ui->gridLayoutWidget, regoff++, "mac0", kxsync_reg_mac0, 0);
  134. push_reg(ui->gridLayoutWidget, regoff++, "mac1", kxsync_reg_mac1, 0);
  135. // 设置table的高度
  136. auto qrect = ui->gridLayoutWidget->geometry();
  137. qrect.setHeight(31 * regoff - 1);
  138. ui->gridLayoutWidget->setGeometry(qrect);
  139. }
  140. //
  141. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
  142. ui->setupUi(this);
  143. m_mainWindow = this;
  144. construct_reg_table();
  145. qInstallMessageHandler(log_output);
  146. qRegisterMetaType<int32_t>("int32_t");
  147. qRegisterMetaType<uint32_t>("uint32_t");
  148. qRegisterMetaType<function<void()>>("function<void()>");
  149. qRegisterMetaType<QFunction>("QFunction");
  150. XSyncUdpFactoryImpl::Ins()->initialize();
  151. connect(this, SIGNAL(append_log_signal(QString)), this, SLOT(append_log_slot(QString)));
  152. connect(this, SIGNAL(updateUI_timeCodeInfo_signal(QString)), this, SLOT(updateUI_timeCodeInfo_slot(QString)));
  153. connect(this, SIGNAL(updateUI_cameraSyncInfo_signal(QString)), this, SLOT(updateUI_cameraSyncInfo_slot(QString)));
  154. connect(this, SIGNAL(updateUI_reg_signal(int32_t, uint32_t)), this, SLOT(updateUI_reg_slot(int32_t, uint32_t)));
  155. connect(this, SIGNAL(doinui_signal(QFunction)), this, SLOT(doinui_slot(QFunction)));
  156. m_thread.reset(new ZQThread("test", [this]() { mainWindowsRun(); }));
  157. m_thread->start();
  158. // m_xsync.reset(new Xsync());
  159. Xsync::Ins().initialize(XSyncUdpFactoryImpl::Ins());
  160. Xsync::Ins().regOnTimecodeMsg([this](xysnc_timecode_t *timecode_msg) { //
  161. xysnc_timecode_t timecode = *timecode_msg;
  162. QString text = QString(fmt("%02d:%02d:%02d:%02d", timecode.hour, timecode.minute, timecode.second, timecode.frame));
  163. updateUI_timeCodeInfo_signal(text);
  164. });
  165. Xsync::Ins().regOnCameraSyncMsg([this](xysnc_camera_sync_data_t *camera_sync_msg) { //
  166. xysnc_camera_sync_data_t camera_sync_data = *camera_sync_msg;
  167. updateUI_cameraSyncInfo_signal(QString(fmt("%d", camera_sync_data.frameIndex)));
  168. });
  169. }
  170. MainWindow::~MainWindow() { delete ui; }
  171. #define XS_ASSERT(exptr) \
  172. { \
  173. auto ecode = exptr; \
  174. while (!(ecode == kxs_ec_success)) { \
  175. ZLOGE(TAG, "do: %s fail,ecode:%d", #exptr, ecode); \
  176. ZQThread::sleep(1); \
  177. } \
  178. }
  179. void MainWindow::on_RefreshRegsButton_clicked() { //
  180. ZLOGI(TAG, "on_refreshRegsButton_clicked");
  181. // int32_t _t1, uint32_t _t2
  182. QtConcurrent::run([this]() {
  183. bool suc = true;
  184. for (auto &reg : m_regdisplayer) {
  185. uint32_t regValue = 0;
  186. auto ecode = Xsync::Ins().reg_read(reg.first, regValue);
  187. int regoff = reg.first;
  188. if (ecode == kxs_ec_success) {
  189. ZLOGI(TAG, "reg_read %x success", reg.first);
  190. emit doinui_signal(QFunction([this, regoff, regValue]() {
  191. m_regdisplayer[regoff]->regvalcache = regValue;
  192. m_regdisplayer[regoff]->regBrowser->setText("0x" + QString::number(regValue, 16));
  193. }));
  194. } else {
  195. ZLOGE(TAG, "reg_read %x fail,ecode:%s", reg.first, xs_error_code_2_str(ecode));
  196. suc = false;
  197. break;
  198. }
  199. }
  200. if (suc) {
  201. ZLOGI(TAG, "readreg end...");
  202. } else {
  203. ZLOGI(TAG, "readreg error");
  204. }
  205. });
  206. // xs_error_code_t ecode = Xsync::Ins().reg_write(0xABCDDCBA, 0x12345678);
  207. // ZLOGI(TAG, "reg_write ecode:%s", xs_error_code_2_str(ecode));
  208. }
  209. void MainWindow::on_ClearLogButton_clicked() { //
  210. ui->logbrowser->clear();
  211. }
  212. void MainWindow::on_Connect2XsyncButton_clicked() { //
  213. ZLOGI(TAG, "connect %s", ui->IpInput->text().toStdString().c_str());
  214. xs_error_code_t ecode = Xsync::Ins().connect(ui->IpInput->text().toStdString());
  215. ZLOGI(TAG, "connect %s ecode:%s", ui->IpInput->text().toStdString().c_str(), xs_error_code_2_str(ecode));
  216. }
  217. void MainWindow::mainWindowsRun() { //
  218. // auto xsudp = XSyncUdpFactoryImpl::Ins()->createXSUDP();
  219. // XS_ASSERT(xsudp->initialize("0.0.0.0", 9999));
  220. // xsudp->startReceive([this, xsudp](XsyncNetAdd &from, uint8_t *data, size_t length) {
  221. // // ZLOGI(TAG, "receive from <%s:%d> (%d) :%s", from.ip.c_str(), from.port, data, length);
  222. // xsudp->sendto(from, "hello\n", 5, NULL);
  223. // });
  224. }