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.

217 lines
7.4 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
  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::push_reg(QWidget *parent, int off, const char *regname, int32_t regadd, int32_t rwflag) {
  40. DispalyRegIterm *regitem = new DispalyRegIterm();
  41. {
  42. auto *label = new QLabel(parent);
  43. label->setObjectName(QString::fromUtf8("label"));
  44. label->setText(QString(fmt("%s(0x%04x)", regname, regadd)));
  45. QSizePolicy sizePolicy1(QSizePolicy::Minimum, QSizePolicy::Expanding);
  46. sizePolicy1.setHorizontalStretch(0);
  47. sizePolicy1.setVerticalStretch(0);
  48. sizePolicy1.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
  49. label->setSizePolicy(sizePolicy1);
  50. label->setMinimumSize(QSize(100, 0));
  51. label->setMaximumSize(QSize(16777215, 16777215));
  52. ui->reg_table->addWidget(label, off, 0, 1, 1);
  53. regitem->label = label;
  54. }
  55. {
  56. auto *textbrowser = new QTextBrowser(parent);
  57. textbrowser->setObjectName(QString::fromUtf8("textbrowser"));
  58. textbrowser->setEnabled(true);
  59. textbrowser->setText(QString("demo"));
  60. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  61. sizePolicy.setHorizontalStretch(0);
  62. sizePolicy.setVerticalStretch(0);
  63. sizePolicy.setHeightForWidth(textbrowser->sizePolicy().hasHeightForWidth());
  64. textbrowser->setSizePolicy(sizePolicy);
  65. textbrowser->setMaximumSize(QSize(16777215, 16777215));
  66. ui->reg_table->addWidget(textbrowser, off, 1, 1, 1);
  67. regitem->regBrowser = textbrowser;
  68. }
  69. {
  70. auto *textEdit = new QTextEdit(parent);
  71. textEdit->setObjectName(QString::fromUtf8("textEdit"));
  72. textEdit->setEnabled(true);
  73. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  74. sizePolicy.setHorizontalStretch(0);
  75. sizePolicy.setVerticalStretch(0);
  76. sizePolicy.setHeightForWidth(textEdit->sizePolicy().hasHeightForWidth());
  77. textEdit->setSizePolicy(sizePolicy);
  78. textEdit->setMaximumSize(QSize(16777215, 16777215));
  79. ui->reg_table->addWidget(textEdit, off, 2, 1, 1);
  80. regitem->regEditer = textEdit;
  81. }
  82. {
  83. // new button
  84. auto *button = new QPushButton(parent);
  85. button->setObjectName(QString::fromUtf8("button"));
  86. button->setText(QString("Write"));
  87. QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  88. sizePolicy.setHorizontalStretch(0);
  89. sizePolicy.setVerticalStretch(0);
  90. sizePolicy.setHeightForWidth(button->sizePolicy().hasHeightForWidth());
  91. button->setSizePolicy(sizePolicy);
  92. // connect(this,
  93. connect(button, &QPushButton::clicked, [this, regadd](bool) { //
  94. QString regval_str = m_regdisplayer[regadd]->regEditer->toPlainText();
  95. uint32_t regval = str2int(regval_str);
  96. ZLOGI(TAG, "write reg 0x%04x %d", regadd, regval);
  97. uint32_t readbackval = 0;
  98. auto ecode = Xsync::Ins().reg_write(regadd, regval, readbackval);
  99. if (ecode == kxs_ec_success) {
  100. ZLOGI(TAG, "write reg 0x%04x %d success", regadd, regval);
  101. m_regdisplayer[regadd]->regvalcache = regval;
  102. m_regdisplayer[regadd]->regBrowser->setText(QString::number(regval, 16));
  103. } else {
  104. ZLOGE(TAG, "write reg 0x%04x %d fail,ecode:%s", regadd, regval, xs_error_code_2_str(ecode));
  105. }
  106. });
  107. regitem->writerButton = button;
  108. ui->reg_table->addWidget(button, off, 3, 1, 1);
  109. }
  110. m_regdisplayer[regadd] = regitem;
  111. }
  112. void MainWindow::construct_reg_table() { //
  113. int regoff = 1;
  114. push_reg(ui->gridLayoutWidget, regoff++, "reg1", 0x1000, 0);
  115. push_reg(ui->gridLayoutWidget, regoff++, "reg1", 0x1001, 0);
  116. push_reg(ui->gridLayoutWidget, regoff++, "reg1", 0x1002, 0);
  117. push_reg(ui->gridLayoutWidget, regoff++, "reg1", 0x1003, 0);
  118. push_reg(ui->gridLayoutWidget, regoff++, "reg1", 0x1004, 0);
  119. // 设置table的高度
  120. auto qrect = ui->gridLayoutWidget->geometry();
  121. qrect.setHeight(31 * regoff - 1);
  122. ui->gridLayoutWidget->setGeometry(qrect);
  123. }
  124. //
  125. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
  126. ui->setupUi(this);
  127. m_mainWindow = this;
  128. construct_reg_table();
  129. connect(this, SIGNAL(append_log_signal(QString)), this, SLOT(append_log_slot(QString)));
  130. qInstallMessageHandler(log_output);
  131. m_thread.reset(new ZQThread("test", [this]() { mainWindowsRun(); }));
  132. m_thread->start();
  133. // m_xsync.reset(new Xsync());
  134. Xsync::Ins().initialize(XSyncUdpFactoryImpl::Ins());
  135. }
  136. MainWindow::~MainWindow() { delete ui; }
  137. #define XS_ASSERT(exptr) \
  138. { \
  139. auto ecode = exptr; \
  140. while (!(ecode == kxs_ec_success)) { \
  141. ZLOGE(TAG, "do: %s fail,ecode:%d", #exptr, ecode); \
  142. ZQThread::sleep(1); \
  143. } \
  144. }
  145. void MainWindow::on_RefreshRegsButton_clicked() { //
  146. ZLOGI(TAG, "on_refreshRegsButton_clicked");
  147. QtConcurrent::run([this]() {
  148. bool suc = true;
  149. for (auto &reg : m_regdisplayer) {
  150. uint32_t regValue = 0;
  151. auto ecode = Xsync::Ins().reg_read(reg.first, regValue);
  152. if (ecode == kxs_ec_success) {
  153. ZLOGI(TAG, "reg_read %x success", reg.first);
  154. reg.second->regvalcache = regValue;
  155. reg.second->regBrowser->setText(QString::number(regValue, 16));
  156. } else {
  157. ZLOGE(TAG, "reg_read %x fail,ecode:%s", reg.first, xs_error_code_2_str(ecode));
  158. suc = false;
  159. break;
  160. }
  161. }
  162. if (suc) {
  163. ZLOGI(TAG, "readreg end...");
  164. } else {
  165. ZLOGI(TAG, "readreg error");
  166. }
  167. });
  168. // xs_error_code_t ecode = Xsync::Ins().reg_write(0xABCDDCBA, 0x12345678);
  169. // ZLOGI(TAG, "reg_write ecode:%s", xs_error_code_2_str(ecode));
  170. }
  171. void MainWindow::on_ClearLogButton_clicked() { //
  172. ui->logbrowser->clear();
  173. }
  174. void MainWindow::on_Connect2XsyncButton_clicked() { //
  175. ZLOGI(TAG, "connect %s", ui->IpInput->text().toStdString().c_str());
  176. xs_error_code_t ecode = Xsync::Ins().connect(ui->IpInput->text().toStdString());
  177. ZLOGI(TAG, "connect %s ecode:%s", ui->IpInput->text().toStdString().c_str(), xs_error_code_2_str(ecode));
  178. }
  179. void MainWindow::mainWindowsRun() { //
  180. XSyncUdpFactoryImpl::Ins()->initialize();
  181. // auto xsudp = XSyncUdpFactoryImpl::Ins()->createXSUDP();
  182. // XS_ASSERT(xsudp->initialize("0.0.0.0", 9999));
  183. // xsudp->startReceive([this, xsudp](XsyncNetAdd &from, uint8_t *data, size_t length) {
  184. // // ZLOGI(TAG, "receive from <%s:%d> (%d) :%s", from.ip.c_str(), from.port, data, length);
  185. // xsudp->sendto(from, "hello\n", 5, NULL);
  186. // });
  187. }