#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
int i;
ui->setupUi(this);
//this->setGeometry(0,0,480,272); //Make window show at upper right corner
this->setGeometry(100,100,480,272); //Make window show at upper right corner - For Windows
//this->setWindowFlags(Qt::Dialog|Qt::FramelessWindowHint); //Remove title bar
switchFlag = 0; //Flag for open/close gif button
imgOpen = new QImage(":/qrc/open.png");
*imgOpen = imgOpen->scaled(231, 211); //Qt::KeepAspectRatio(Keep the aspect ratio)
ui->lblGif->setPixmap(QPixmap::fromImage(*imgOpen));
applicationDirPath = QCoreApplication::applicationDirPath(); //Save logs at app path
QDir dir(applicationDirPath+"/CT_log");
logFiles = dir.entryList(QDir::Files|QDir::Readable, QDir::Name);
startTimer(1000); //Return value is 1 = timerId
graphSwitch=0; //Flag for qcustomplot graph to judge if to start or not
for(i=0;i<8;i++) //Clear the root password
rootPassword[i]=0;
//ui->tableHistory->horizontalHeader()->setVisible(false); //Hide row headers
ui->tableHistory->setHorizontalHeaderLabels(QStringList()<< tr("Date")<
btnGif->setEnabled(false);
tcpConnected=0;
ui->butCut->setEnabled(false);
on_butWinSize_clicked();
setCursor(QCursor(Qt::CrossCursor));
for(passwordLengthT=0;passwordLengthT<8;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerEvent(QTimerEvent *t)
{
dateTime = QDateTime::currentDateTime();
strTime = dateTime.toString("yyyy-MM-dd hh:mm:ss");
ui->lblCurDate->setText(strTime);
ui->lblRand->setText(tr("每秒产生一个随机数: %1").arg(qrand() % 10));
//qDebug()<<"CT_"+strTime.mid(2,2)+strTime.mid(5,2)+strTime.mid(8,2)+strTime.mid(11,2)+".log";
/*qDebug()<
lblShow->setText(arg1);
}
void MainWindow::stopGifOpen(int i)
{
//qDebug()<setPaused(true);
ui->btnOpen->setEnabled(true);
ui->btnClose->setEnabled(true);
//ui->btnGif->setEnabled(true);
}
}
void MainWindow::stopGifClose(int i)
{
//qDebug()<setPaused(true);
ui->btnOpen->setEnabled(true);
ui->btnClose->setEnabled(true);
//ui->btnGif->setEnabled(true);
}
}
void MainWindow::on_btnOpen_clicked()
{
movOpen = new QMovie(":/qrc/open.gif");
if(!switchFlag)
{
ui->btnOpen->setEnabled(false);
ui->btnClose->setEnabled(false);
ui->btnGif->setEnabled(false);
movOpen->setScaledSize(ui->lblGif->size());
ui->lblGif->setMovie(movOpen);
movOpen->start();
connect(movOpen, SIGNAL(frameChanged(int)), this, SLOT(stopGifOpen(int)));
ui->btnOpen->setStyleSheet("color: rgb(30, 245, 18);");
ui->btnClose->setStyleSheet("");
switchFlag = 1;
}
}
void MainWindow::on_btnClose_clicked()
{
movClose = new QMovie(":/qrc/close.gif");
if(switchFlag)
{
ui->btnOpen->setEnabled(false);
ui->btnClose->setEnabled(false);
ui->btnGif->setEnabled(false);
movClose->setScaledSize(ui->lblGif->size());
ui->lblGif->setMovie(movClose);
movClose->start();
connect(movClose, SIGNAL(frameChanged(int)), this, SLOT(stopGifClose(int)));
ui->btnOpen->setStyleSheet("");
ui->btnClose->setStyleSheet("color: rgb(30, 245, 18);");
switchFlag = 0;
}
}
void MainWindow::on_btnGif_clicked()
{
movOpen = new QMovie(":/qrc/open.gif");
movClose = new QMovie(":/qrc/close.gif");
if(!switchFlag)
{
ui->btnOpen->setEnabled(false);
ui->btnClose->setEnabled(false);
ui->btnGif->setEnabled(false);
movOpen->setScaledSize(ui->lblGif->size());
ui->lblGif->setMovie(movOpen);
movOpen->start();
connect(movOpen, SIGNAL(frameChanged(int)), this, SLOT(stopGifOpen(int)));
ui->btnOpen->setStyleSheet("color: rgb(30, 245, 18);");
ui->btnClose->setStyleSheet("");
switchFlag = 1;
}
else if(switchFlag)
{
ui->btnOpen->setEnabled(false);
ui->btnClose->setEnabled(false);
ui->btnGif->setEnabled(false);
movClose->setScaledSize(ui->lblGif->size());
ui->lblGif->setMovie(movClose);
movClose->start();
connect(movClose, SIGNAL(frameChanged(int)), this, SLOT(stopGifClose(int)));
ui->btnOpen->setStyleSheet("");
ui->btnClose->setStyleSheet("color: rgb(30, 245, 18);");
switchFlag = 0;
}
}
void MainWindow::graphInit()
{
ui->widgetGraph->addGraph();
ui->widgetGraph->xAxis->setTickLabelType(QCPAxis::ltDateTime);
ui->widgetGraph->xAxis->setDateTimeFormat("hh:mm:ss");
ui->widgetGraph->addGraph();
ui->widgetGraph->graph(1)->setLineStyle(QCPGraph::lsNone);
ui->widgetGraph->graph(1)->setScatterStyle(QCPScatterStyle::ssDisc);
ui->widgetGraph->graph(1)->setName("Point");
ui->widgetGraph->xAxis->setLabel("Time");
ui->widgetGraph->yAxis->setLabel("Value");
ui->widgetGraph->yAxis->setRange(-0.5,9.5);
ui->widgetGraph->xAxis->setAutoTickStep(false);
ui->widgetGraph->yAxis->setAutoTickStep(false);
ui->widgetGraph->xAxis->setTickStep(30);
ui->widgetGraph->yAxis->setTickStep(1);
ui->widgetGraph->legend->setVisible(true);
ui->widgetGraph->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
}
void MainWindow::graphPaint()
{
int lineNum=0,i;
QByteArray logTempByte;
QString logTemp;
static int graphInited=0;
saveLog.open(QIODevice::ReadOnly | QIODevice::Text);
while(!saveLog.atEnd())
{
saveLog.readLine();
lineNum++;
}
saveLog.close();
saveLog.open(QIODevice::ReadOnly | QIODevice::Text);
if((lineNum-100)>=0)
{
for(i=0;iwidgetGraph->setBackground(QColor(255,255,255));
QVector x(101),y(101);
for (int i = 0; i < 101; ++i)
{
// x[i] = i/5 - 10;
// y[i] = qPow(x[i], 3); //The y power of x; just for test
logTempByte = saveLog.readLine();
logTemp.prepend(logTempByte);
x[i]=QDateTime::fromString(logTemp.mid(0,19),"yyyy-MM-dd hh:mm:ss").toTime_t();
y[i]=logTemp.mid(20,1).toDouble();
}
if(graphInited==0)
{
graphInit();
graphInited=1;
}
ui->widgetGraph->graph(0)->setData(x,y);
ui->widgetGraph->graph(0)->setName(saveLog.fileName().right(15)); //Get 15 characters from the end
saveLog.close();
ui->widgetGraph->graph(1)->addData(x,y);
ui->widgetGraph->xAxis->setRange(x[0],x[100]);
ui->widgetGraph->replot();
}
void MainWindow::on_btnLineSeries_clicked()
{
if(graphSwitch)
graphSwitch=0;
else
graphSwitch=1;
}
void MainWindow::getHistoryFiles()
{
int i;
QByteArray logTempByte;
QString logTemp;
QDir dir(applicationDirPath+"/CT_log");
logFiles = dir.entryList(QDir::Files|QDir::Readable, QDir::Name);
qDebug()<comboBoxDate->clear();
for(i=0;icomboBoxDate->addItem(logFiles[i]);
}
}
void MainWindow::on_comboBoxDate_currentIndexChanged(const QString &arg1)
{
qDebug()<tableHistory->clear();
ui->tableHistory->verticalHeader()->show();
ui->tableHistory->horizontalHeader()->show();
saveLog.setFileName(dirLog.filePath(arg1));
saveLog.open(QIODevice::ReadOnly | QIODevice::Text);
while(!saveLog.atEnd())
{
saveLog.readLine();
lineNum++;
}
saveLog.close();
ui->tableHistory->setRowCount(lineNum);
saveLog.open(QIODevice::ReadOnly | QIODevice::Text);
//logTempByte = saveLog.readAll();
for(i=0;itableHistory->setItem(i,0,new QTableWidgetItem(logTemp.mid(0,10))); //Get 10 characters from address 0
ui->tableHistory->setItem(i,1,new QTableWidgetItem(logTemp.mid(11,8)));
ui->tableHistory->setItem(i,2,new QTableWidgetItem(logTemp.mid(19,3)));
ui->tableHistory->item(i,0)->setTextAlignment(0x0004);
ui->tableHistory->item(i,1)->setTextAlignment(0x0004);
ui->tableHistory->item(i,2)->setTextAlignment(0x0004);
}
saveLog.close();
}
void MainWindow::on_btnRefreshH_clicked()
{
getHistoryFiles();
}
int MainWindow::on_btnGPIO_clicked()
{
static int fd_dev, fd_export, fd_dir, ret;
char buf[10], direction[4];
static char value='1';
fd_export = open(EXPORT_PATH, O_WRONLY); //Open GPIO export file
if(fd_export < 0)
{
perror("open export:");
return -1;
}
write(fd_export, GPIO34, strlen(GPIO34));
fd_dev = open(DEV_PATH, O_RDWR); //Open GPIO electric level file
if(fd_dev < 0)
{
perror("open gpio:");
return -1;
}
fd_dir = open(DIRECT_PATH, O_RDWR); //Open GPIOcontrol file
if(fd_dir < 0)
{
perror("open direction:");
return -1;
}
ret = read(fd_dir, direction, sizeof(direction)); //Read the GPIO3_4 in/out direction
if(ret < 0)
{
perror("read direction:");
::close(fd_export);
::close(fd_dir);
::close(fd_dev);
return -1;
}
printf("default direction:%s", direction);
strcpy(buf, OUT0);
ret = write(fd_dir, buf, strlen(OUT0));
if(ret < 0)
{
perror("write direction:");
::close(fd_export);
::close(fd_dir);
::close(fd_dev);
return -1;
}
ret = read(fd_dir, direction, sizeof(direction));
if(ret < 0)
{
perror("read direction:");
::close(fd_export);
::close(fd_dir);
::close(fd_dev);
return -1;
}
if(value=='0')
{
value='1';
ret = write(fd_dev, HIGH_LEVEL, strlen(HIGH_LEVEL));
}
else
{
value='0';
ret = write(fd_dev, LOW_LEVEL, strlen(LOW_LEVEL));
}
if(ret < 0)
{
perror("write gpio:");
::close(fd_export); //Use C's close instead of Qt's
::close(fd_dir);
::close(fd_dev);
return -1;
}
printf("current direction:%soutput level:%c\n", direction, value);
::close(fd_export);
::close(fd_dir);
::close(fd_dev);
return 0;
}
int MainWindow::on_btnUartSend_clicked()
{
int fd;
int len;
char buf[] = "Hello STM32!";
fd = open(DEV_NAME, O_RDWR /*| O_NOCTTY*/);
if(fd < 0)
{
perror(DEV_NAME);
return -1;
}
printf("\nWriting buf...\n");
len = write(fd, buf, sizeof(buf)); //Write string for UART
if (len < 0)
{
printf("write data error \n");
}
printf("\nWrited buf!\n");
printf("\nGetting data...\n");
len = read(fd, buf, sizeof(buf)); //Read string from UART
if (len < 0)
{
printf("read error \n");
return -1;
}
printf("%s", buf); //Print string after read
printf("\nGetted data!\n");
return 0;
}
void MainWindow::rootBut() //Get in root
{
int i;
if(!rootPassword[7])
{
for(i=0;i<7;i++)
{
rootPassword[7]=1; //rootPassword[7] is the root flag, 1 for root, 0 for normal
if(rootPassword[i]==0)
{
rootPassword[7]=0;
break;
}
else if(i==6)
{
ui->btnCloseOwn->setEnabled(true); //Quit app button
ui->btnCloseOwn->setText("Root!");
ui->lblT0->setText("Root!");
}
}
}
else if(rootPassword[0]
&& rootPassword[6]
&& !rootPassword[1]
&& !rootPassword[2]
&& !rootPassword[3]
&& !rootPassword[4]
&& !rootPassword[5]
&& rootPassword[7])
{
for(i=0;i<8;i++)
rootPassword[i]=0;
ui->btnCloseOwn->setEnabled(false);
ui->lblT0->setText("Unroot!");
}
qDebug("%d %d %d %d %d %d %d %d",rootPassword[0],rootPassword[1],rootPassword[2],rootPassword[3],rootPassword[4],rootPassword[5],rootPassword[6],rootPassword[7]);
}
void MainWindow::on_btnCT0_clicked()
{
rootPassword[0]=1;
rootBut();
}
void MainWindow::on_btnCT2_clicked()
{
if(rootPassword[0])
rootPassword[1]=1;
rootBut();
}
void MainWindow::on_btnCT7_clicked()
{
if(rootPassword[1])
rootPassword[2]=1;
rootBut();
}
void MainWindow::on_btnCT9_clicked()
{
if(rootPassword[2])
rootPassword[3]=1;
rootBut();
}
void MainWindow::on_btnCT12_clicked()
{
if(rootPassword[3])
rootPassword[4]=1;
rootBut();
}
void MainWindow::on_btnCT15_clicked()
{
if(rootPassword[4])
rootPassword[5]=1;
rootBut();
}
void MainWindow::on_btnCT20_clicked()
{
if(rootPassword[5])
rootPassword[6]=1;
rootBut();
}
void MainWindow::on_btnCT1_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT3_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT4_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT5_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT6_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT8_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT10_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT11_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT13_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT14_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT16_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT17_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT18_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCT19_clicked()
{
for(passwordLengthT=0;passwordLengthT<7;passwordLengthT++)
rootPassword[passwordLengthT]=0;
}
void MainWindow::on_btnCloseOwn_clicked()
{
if(rootPassword[7])
qApp->quit();
}
void MainWindow::sendByARM(QString str)
{
clientConnection->write(str.toUtf8().data());
connect(clientConnection,SIGNAL(disconnected()),this,SLOT(on_butCut_clicked()));
}
void MainWindow::setTcpFlag()
{
tcpConnected=1;
ui->lblSend->setText("Sending . . .");
clientConnection=tcpServer->nextPendingConnection(); //获取已经建立的连接的子套接字
qDebug()<peerAddress().toString();
qint16 port = clientConnection->peerPort();
ui->lblIpAddr->setText(QString("[%1:%2] connected !").arg(ip).arg(port));
ui->butSend->setEnabled(false);
ui->butCut->setEnabled(true);
}
void MainWindow::on_butSend_clicked()
{
if(tcpConnected==0)
{
tcpServer = new QTcpServer(this);
if(!tcpServer->listen(QHostAddress::Any,6666))
{
qDebug()<errorString();
//close();
}
connect(tcpServer,SIGNAL(newConnection()),this,SLOT(setTcpFlag()));
}
}
void MainWindow::on_butCut_clicked()
{
tcpConnected=2;
clientConnection->disconnectFromHost(); //Active disconnection from client
//clientConnection->close();
//clientConnection = NULL;
ui->lblSend->setText("Pausing . . .");
ui->lblIpAddr->setText("Waiting for connect . . .");
ui->butCut->setEnabled(false);
}
void MainWindow::on_btnMus_clicked()
{
}
void MainWindow::on_butWinSize_clicked()
{
}