代码拉取完成,页面将自动刷新
同步操作将从 nabian/TelinkOTA 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
//write by nabian
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qbluetoothaddress.h>
#include <qbluetoothdevicediscoveryagent.h>
#include <qbluetoothlocaldevice.h>
#include <QMenu>
#include <QLabel>
#include <QDebug>
#include <QSettings>
#include <qapplication.h>
#include <QDateTime>
#include <QFileDialog>
#include <QFile>
#include <QComboBox>
#include <QTextStream>
#include <QtMsgHandler>
#include <QMessageLogContext>
#include <QMutex>
#include <QTimer>
void myMsgOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);
MainWindow *MainWindow::m_mainWin = nullptr;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
QLabel *permanent = new QLabel(this);
//permanent->setFrameStyle(QFrame::Box|QFrame::Sunken);
permanent->setText(tr("<a href=\"http://baidu.com\">write by nabian </a>"));
ui->statusbar->addPermanentWidget(permanent);
m_mainWin = this;
wPackNum = 0;
OtaFlag = 0;
qInstallMessageHandler(myMsgOutput);
mtimer = new QTimer(this);
connect(mtimer, SIGNAL(timeout()), this, SLOT(on_TimerTick()));
QSettings settings("HKEY_CURRENT_USER\\Software\\ble_ota", QSettings::NativeFormat);
FirmPath = settings.value("firm", "").toString(); //读
qDebug() << "FirmPath:" << FirmPath;
QComboBox *cmb_bledev = new QComboBox;
cmb_bledev->setStyleSheet("width:80px;height:24px;text-align:center;");
bledevlist = ui->toolBar->insertWidget(ui->toolBar->actions().at(2), cmb_bledev);
sendtic = new QSpinBox;
sendtic->setValue(15);
sendtic->setStyleSheet("width:30px;height:24px;text-align:center;");
ui->toolBar->insertWidget(ui->actionStart, sendtic);
ui->toolBar->insertSeparator(ui->actionStart);
gpioble = new QGPIOBle();
connect(gpioble, SIGNAL(emit_scanfinish()), this, SLOT(Ble_Scanfinishcb()));
ui->statusbar->setSizeGripEnabled(false);
pProgressBar = new QProgressBar();
pProgressBar->setValue(0);
ui->statusbar->addWidget(pProgressBar);
}
void MainWindow::on_TimerTick()
{
Telink_OTA_wTick();
}
void MainWindow::Ble_Scanfinishcb()
{
QComboBox *cmb_bledev = (QComboBox *)ui->toolBar->widgetForAction(ui->toolBar->actions()[2]);
cmb_bledev->clear();
qDebug() << "scan finish";
for (int i = 0; i < gpioble->Devicelist.length(); i++)
{
cmb_bledev->addItem(gpioble->Devicelist[i]->name());
}
}
/************************************************************************************
* 函数名称: on_OpenFileBtn_clicked()
* 输入参数:
* 返回值 : 无
* 功 能: 打开文件对话框
**************************************************************************************/
void MainWindow::on_actionOpenFile_triggered()
{
QString file_name = QFileDialog::getOpenFileName(NULL, "select firmware", FirmPath, "*.bin");
if (file_name.length() > 0)
{
FirmPath = file_name;
QSettings settings("HKEY_CURRENT_USER\\Software\\ble_ota", QSettings::NativeFormat);
settings.setValue("firm", file_name); //写
qDebug() << "FirmPath" << FirmPath;
}
}
/************************************************************************************
* 函数名称: on_actionStart_triggered()
* 输入参数:
* 返回值 : 无
* 功 能: 开始更新
**************************************************************************************/
void MainWindow::on_actionStart_triggered()
{
qDebug() << OtaFlag;
if (gpioble->connected == true)
{
if (OtaFlag)
{
ui->actionStart->setIcon(QIcon(":/new/prefix1/img/start.png"));
ui->actionStart->setText("Start");
OtaFlag = 0;
return;
}
QFile file(FirmPath);
if(file.open(QIODevice::ReadOnly)==false)
{
qDebug() << "open file failed!";
return;
}
FileData = file.readAll();
file.close();
if( FileData.count()<1024*16)
{
qDebug() << "file size erro!";
return;
}
ui->actionStart->setIcon(QIcon(":/new/prefix1/img/stop.png"));
qDebug() << "Firm size " << FileData.count() << "K Bytes";
FileData.resize(((FileData.count() / 16) + 1) * 16);
ui->actionStart->setText("Stop");
mtimer->stop();
mtimer->start(1000);
run_time = QDateTime::currentMSecsSinceEpoch();
qDebug() << "begin ota" << QDateTime::currentMSecsSinceEpoch();
OtaFlag = 1;
otatimer = 0;
QByteArray sendbuffer;
sendbuffer.resize(2);
sendbuffer[0] = 0x01;
sendbuffer[1] = 0xff;
gpioble->m_service->writeCharacteristic(gpioble->OtaChar, sendbuffer, QLowEnergyService::WriteWithoutResponse);
//gpioble->writeCharacteristics(gpioble->m_service,gpioble->OtaChar,sendbuffer,false);
}
}
/************************************************************************************
* 函数名称: myMsgOutput()
* 输入参数:
* 返回值 : 无
* 功 能: 打印输出函数
**************************************************************************************/
void myMsgOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QMutex mutex;
Q_UNUSED(type);
Q_UNUSED(context);
mutex.lock();
MainWindow::setText(msg);
mutex.unlock();
}
/************************************************************************************
* 函数名称: startScan()
* 输入参数:
* 返回值 : 无
* 功 能: 开始扫描,只扫描低功耗节点
**************************************************************************************/
void MainWindow::on_actionScan_triggered()
{
gpioble->startScan();
}
/************************************************************************************
* 函数名称: setText()
* 输入参数:
* 返回值 : 无
* 功 能: 设置log显示窗口内容
**************************************************************************************/
void MainWindow::setText(QString str)
{
m_mainWin->ui->textEdit->append(str);
m_mainWin->ui->textEdit->moveCursor(QTextCursor::End);
}
/************************************************************************************
* 函数名称: on_ConBtn_clicked()
* 输入参数:
* 返回值 : 无
* 功 能: 链接窗口
**************************************************************************************/
void MainWindow::on_actionConnect_triggered()
{
QComboBox *cmb_bledev = (QComboBox *)ui->toolBar->widgetForAction(ui->toolBar->actions()[2]);
if (ui->actionConnect->text() == "Connect")
{
if(cmb_bledev->currentText().length()>0)
{
gpioble->currentDevice = *gpioble->Devicelist.at(cmb_bledev->currentIndex());
qDebug() << "con start" << gpioble->currentDevice.address();
gpioble->connectDevice();
ui->actionConnect->setText("Disonnect");
ui->actionConnect->setIcon(QIcon(":/new/prefix1/img/link.png"));
}
else
{
qDebug() << "please scan first!" ;
}
}
else
{
ui->actionConnect->setIcon(QIcon(":/new/prefix1/img/dislink.png"));
gpioble->controller->disconnectFromDevice();
ui->actionConnect->setText("Connect");
}
}
/************************************************************************************
* 函数名称: crc16()
* 输入参数:
* 返回值 : 无
* 功 能:
**************************************************************************************/
unsigned short MainWindow::crc16(unsigned char *pD, int len)
{
static unsigned short poly[2] = {0, 0xa001}; //0x8005 <==> 0xa001
unsigned short crc = 0xffff;
int i, j;
for (j = len; j > 0; j--)
{
unsigned char ds = *pD++;
for (i = 0; i < 8; i++)
{
crc = (crc >> 1) ^ poly[(crc ^ ds) & 1];
ds = ds >> 1;
}
}
return crc;
}
/************************************************************************************
* 函数名称: deviceConnected_cb()
* 输入参数:
* 返回值 : 无
* 功 能: 连接设备
**************************************************************************************/
void MainWindow::Telink_OTA_wTick()
{
int i = 0;
if (OtaFlag == 1)
{
mtimer->setInterval(sendtic->value());
wPackNum = 0;
OtaFlag = 2;
pProgressBar->setValue(0);
}
else if (OtaFlag == 2)
{
wPackNum++;
pProgressBar->setValue(wPackNum * 1600 / FileData.count());
if (wPackNum >= (FileData.count() / 16))
{
OtaFlag = 3;
send_buff[0] = 0x02;
send_buff[1] = 0xff;
gpioble->m_service->writeCharacteristic(gpioble->OtaChar, QByteArray::fromRawData((char *)send_buff, 2), QLowEnergyService::WriteWithoutResponse);
}
}
else if (OtaFlag == 3)
{
ui->actionStart->setText("Start");
ui->actionStart->setIcon(QIcon(":/new/prefix1/img/start.png"));
qDebug() << "end ota" << QDateTime::currentMSecsSinceEpoch();
qDebug() << "ota time" << (QDateTime::currentMSecsSinceEpoch() - run_time);
run_time = QDateTime::currentMSecsSinceEpoch();
mtimer->stop();
OtaFlag = 0;
qDebug() << "------------OTA ok------------------";
FileData.clear();
}
if (OtaFlag == 2)
{
send_buff[0] = wPackNum & 0xff;
send_buff[1] = (wPackNum >> 8) & 0xff;
for (i = 0; i < 16; i++)
{
send_buff[i + 2] = FileData[wPackNum * 16 + i];
}
uint16_t crc = crc16(send_buff, 18);
send_buff[18] = crc & 0xff;
send_buff[19] = (crc >> 8) & 0xff;
gpioble->m_service->writeCharacteristic(gpioble->OtaChar, QByteArray::fromRawData((char *)send_buff, 20), QLowEnergyService::WriteWithoutResponse);
qWarning() << "w " << QByteArray::fromRawData((char *)send_buff, 20).toHex();
}
}
MainWindow::~MainWindow()
{
FileData.clear();
mtimer->stop();
delete mtimer;
delete gpioble;
delete ui;
}
void MainWindow::on_actionClear_triggered()
{
ui->textEdit->clear();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。