SP713_Upper/mainwindow.cpp
2025-08-02 15:28:41 +08:00

329 lines
8.8 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
drvSPI = new DRV_Spi(this);
drvUart = new DRV_Uart(this);
fHandler = new FrameHandler(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::onLogPrint(QString msg){
auto now = QDateTime::currentDateTime();
QString tmp = now.toString("yyyy-MM-dd HH:mm:ss")+" "+msg;
ui->textBrowser->append(tmp);
}
void MainWindow::on_pushButton_14_clicked()
{
dc= new DialogCalibrate(this);
dc->show();
}
void MainWindow::on_comboBox_activated(int index)
{
ui->stackedWidget->setCurrentIndex(ui->comboBox->currentIndex());
}
void MainWindow::on_pushButton_6_clicked()
{
ui->comboBox_7->clear();
// 刷新UART设备
if(drvUart->Uart_enumDevice()){
// 将串口设备的名称添加至列表中
for(int i = 0; i < drvUart->m_DRV_Uart_Infors.ulDevCnt; i++)
ui->comboBox_7->addItem(drvUart->m_DRV_Uart_Infors.bDeviceName[i]);
}else{
onLogPrint("No devices");
}
}
void MainWindow::on_pushButton_7_clicked()
{
ULONG index = ui->comboBox_7->currentIndex();
// 调用串口打开函数
BOOL status = drvUart->Uart_openDevice(index);
// 判断打开串口设备是否成功
if(status == TRUE){
ui->comboBox_8->setEnabled(true);
ui->comboBox_9->setEnabled(true);
ui->comboBox_10->setEnabled(true);
ui->comboBox_11->setEnabled(true);
ui->pushButton_8->setEnabled(true);
ui->lineEdit->setEnabled(true);
ui->pushButton_5->setEnabled(true);
ui->pushButton_7->setEnabled(false);
onLogPrint("Open device success");
}else{
onLogPrint("Open device failed");
}
}
void MainWindow::on_pushButton_8_clicked()
{
// 读取组件中配置参数的值
ULONG Baudrate = this->ui->comboBox_8->currentText().toLong();
UCHAR StopBits = this->ui->comboBox_10->currentIndex();
UCHAR Parity = this->ui->comboBox_9->currentIndex();
UCHAR DataBits = this->ui->comboBox_11->currentText().toUInt();
UCHAR Timeout = this->ui->lineEdit->text().toUInt();
// 调用配置参数函数进行配置
if(drvUart->Uart_setPara(Baudrate, StopBits, Parity, DataBits, Timeout)){
onLogPrint("Set parameters success");
}else{
onLogPrint("Fail to set parameters");
}
}
void MainWindow::on_pushButton_5_clicked()
{
BOOL status = drvUart->Uart_closeDevice();
if(status == TRUE){
ui->comboBox_8->setEnabled(false);
ui->comboBox_9->setEnabled(false);
ui->comboBox_10->setEnabled(false);
ui->comboBox_11->setEnabled(false);
ui->pushButton_8->setEnabled(false);
ui->lineEdit->setEnabled(false);
ui->pushButton_5->setEnabled(false);
ui->pushButton_7->setEnabled(true);
ui->btnSpiSaveSetting->setEnabled(false);
onLogPrint("Close device success");
}else{
onLogPrint("Close device failed");
}
}
void MainWindow::on_pushButton_clicked()
{
ui->comboBox_2->clear();
// 刷新UART设备
if(drvSPI->spi_enumDevice()){
// 将串口设备的名称添加至列表中
ui->comboBox_2->addItem(drvSPI->m_DRV_Spi_Infors.bDeviceName);
}else{
onLogPrint("No devices");
}
}
void MainWindow::on_pushButton_2_clicked()
{
ULONG index = ui->comboBox_2->currentIndex();
// 调用串口打开函数
BOOL status = drvSPI->spi_openDevice(index);
// 判断打开串口设备是否成功
if(status == TRUE){
ui->comboSpiMode->setEnabled(true);
ui->comboSpiClock->setEnabled(true);
ui->comboSpiByteOrder->setEnabled(true);
ui->comboSpiDataBits->setEnabled(true);
ui->comboSpiCS1Pol->setEnabled(true);
ui->comboSpiCS2Pol->setEnabled(true);
ui->pushButton_3->setEnabled(true);
ui->pushButton_2->setEnabled(false);
ui->btnSpiSaveSetting->setEnabled(true);
onLogPrint("Open device success");
on_btnSpiSaveSetting_clicked(); // 初始化SPI参数
}else{
onLogPrint("Open device failed");
}
}
void MainWindow::on_pushButton_3_clicked()
{
BOOL status = drvSPI->spi_closeDevice();
// 判断打开串口设备是否成功
if(status == TRUE){
ui->comboSpiMode->setEnabled(false);
ui->comboSpiClock->setEnabled(false);
ui->comboSpiByteOrder->setEnabled(false);
ui->comboSpiDataBits->setEnabled(false);
ui->comboSpiCS1Pol->setEnabled(false);
ui->comboSpiCS2Pol->setEnabled(false);
ui->pushButton_3->setEnabled(false);
ui->pushButton_2->setEnabled(true);
onLogPrint("Close device success");
}else{
onLogPrint("Close device failed");
}
}
void MainWindow::on_pushButton_9_clicked() {
unsigned char REG[4] = { 0 };
unsigned char ucDAC = ui->comboBox_15->currentText().toUInt(NULL, 10);
unsigned char ucChannel = 0;
QString tmp = ui->lineEdit_2->text();
if (tmp.toInt() < 0 || tmp.toInt() > 256) {
onLogPrint("Wrong channel #");
return;
}
switch (tmp.toUInt()) {
case 256:
ucChannel = 0;
ucDAC = ucDAC | 0x80;
REG[1] = REG[1] | 0x02;
break;
default:
ucChannel = tmp.toUInt();
REG[3] = ucChannel;
break;
}
unsigned char ucFun1 = 0;
//EN_TADC
if (ui->checkBox->isChecked()) {
ucFun1 = ucFun1 | 0x01;
REG[1] = REG[1] | 0x80;
}
//FBK_EN
if (ui->checkBox_2->isChecked()) {
ucFun1 = ucFun1 | 0x02;
REG[1] = REG[1] | 0x01;
}
//TEMPTEST_EN
if (ui->checkBox_4->isChecked()) {
ucFun1 = ucFun1 | 0x04;
REG[2] = REG[2] | 0x80;
qDebug() << REG[2];
}
//TEST_EN
if (ui->checkBox_3->isChecked()) {
ucFun1 = ucFun1 | 0x08;
REG[2] = REG[2] | 0x20;
}
//TRIG_TADC
if (ui->checkBox_5->isChecked()) {
ucFun1 = ucFun1 | 0x10;
REG[2] = REG[2] | 0x40;
}
unsigned char ucFun2 = 0;
ucFun2 = ucFun2 | ui->comboBox_12->currentText().toUInt(NULL, 10); //N_CLKDIV18
ucFun2 = ucFun2 | (ui->comboBox_14->currentText().toUInt(NULL, 2) << 4); //ICON8
REG[1] = REG[1] | (ui->comboBox_12->currentText().toUInt(NULL, 10) << 5);
REG[2] = REG[2] | (ui->comboBox_14->currentText().toUInt(NULL, 2));
QString sendRegData;
for (auto i : REG) {
sendRegData += "0x" + QString::number(i, 16) + " ";
}
onLogPrint("Ready to write REG[1] - REG[4] : " + sendRegData);
QByteArray tmp1 = fHandler->genREGWrite(ucDAC, ucChannel, ucFun1, ucFun2);
switch (ui->comboBox->currentIndex()) {
case 0:
if (drvUart->Uart_Write(tmp1)) {
onLogPrint("Send data success");
} else {
onLogPrint("Send data fail");
}
break;
case 1:
onLogPrint("write data is " + tmp1.toHex());
if (drvSPI->spi_Write(tmp1)) {
onLogPrint("Send data success");
} else {
onLogPrint("Send data failed");
}
break;
}
}
void MainWindow::on_pushButton_10_clicked() {
unsigned char ucDAC = ui->comboBox_15->currentText().toUInt(NULL, 10);
QByteArray tmp = fHandler->genREGRead(ucDAC);
switch (ui->comboBox->currentIndex()) {
case 0:
if (drvUart->Uart_Write(tmp)) {
onLogPrint("Send data success");
} else {
onLogPrint("Send data fail");
}
break;
case 1:
onLogPrint("write data is " + tmp.toHex());
if (drvSPI->spi_Write(tmp)) {
onLogPrint("Send data success");
QTimer::singleShot(1000, [this]() {
QByteArray ba;
drvSPI->spi_Read(ba);
ui->textBrowser->append("SPI read: " + ba.toHex());
});
} else {
onLogPrint("Send data fail");
}
break;
}
}
void MainWindow::on_btnSpiSaveSetting_clicked() {
drvSPI->spi_Init(
ui->comboSpiMode->currentIndex(),
ui->comboSpiClock->currentIndex(),
ui->comboSpiByteOrder->currentIndex(),
ui->comboSpiDataBits->currentIndex(),
ui->comboSpiCS1Pol->currentIndex(),
ui->comboSpiCS2Pol->currentIndex()
);
}
void MainWindow::onWriteRegButtonClicked() {
QByteArray tmp = fHandler->genREGWrite(0x01, 0x01, 0x09, 0x81);
ui->textBrowser->append("SPI write: " + tmp.toHex());
drvSPI->spi_Write(tmp);
}
void MainWindow::onReadRegButtonClicked() {
QByteArray tmp = fHandler->genREGRead(0x01);
ui->textBrowser->append("SPI write: " + tmp.toHex());
QTimer::singleShot(2000, [this]() {
QByteArray ba;
drvSPI->spi_Read(ba);
ui->textBrowser->append("SPI read: " + ba.toHex());
});
}
void MainWindow::on_comboBox_currentTextChanged(const QString& text) {
// if (text.toLower() == "spi") {
// connect(ui->pushButton_4, &QPushButton::clicked, this, &MainWindow::onWriteRegButtonClicked);
// connect(ui->pushButton_10, &QPushButton::clicked, this, &MainWindow::onReadRegButtonClicked);
// } else {
// disconnect(ui->pushButton_4, &QPushButton::clicked, this, &MainWindow::onWriteRegButtonClicked);
// disconnect(ui->pushButton_10, &QPushButton::clicked, this, &MainWindow::onReadRegButtonClicked);
// }
}