广播命令已接收的回复不再触发回调
检测命令0x0c只能使用单播方式,广播方式因为不能即时回复停止信号,回复会出错 添加电阻测试命令,添加自检命令
This commit is contained in:
131
base/beep.cpp
Normal file
131
base/beep.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
#include "beep.h"
|
||||
#include "QTimer"
|
||||
#include "QThread"
|
||||
beep::beep(QObject *parent) : QObject(parent)
|
||||
{
|
||||
/* 开发板的 LED 控制接口 */
|
||||
file.setFileName("/sys/devices/platform/leds/leds/beep/brightness");
|
||||
if (!file.exists())
|
||||
{
|
||||
qWarning()<<"beep init Fail!!"<<endl;
|
||||
return ;
|
||||
}
|
||||
|
||||
set_state(false);
|
||||
state = get_state();
|
||||
|
||||
// 在启动此对象时自动鸣叫两次
|
||||
delay_on_ms = 100;
|
||||
delay_off_ms =100;
|
||||
ticks = 2;
|
||||
is_run = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool beep::get_state()
|
||||
{
|
||||
if(!file.exists())
|
||||
return false;
|
||||
if(!file.open(QIODevice::ReadWrite))
|
||||
qWarning()<<file.errorString();
|
||||
QTextStream in(&file);
|
||||
|
||||
QString buf = in.readLine();
|
||||
|
||||
file.close();
|
||||
if(buf == "1")
|
||||
{
|
||||
state = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
state = false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// 控制蜂鸣器,用户程序一般不直接调用
|
||||
bool beep::set_state(bool state)
|
||||
{
|
||||
if (!file.exists())
|
||||
return false;
|
||||
if(!file.open(QIODevice::ReadWrite))
|
||||
qWarning()<<file.errorString();
|
||||
if(state)
|
||||
{
|
||||
file.write("1");
|
||||
this->state = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
file.write("0");
|
||||
this->state = false;
|
||||
}
|
||||
file.close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void beep::start()
|
||||
{
|
||||
QTimer::singleShot(0,this, &beep::run_cb);
|
||||
}
|
||||
|
||||
|
||||
bool beep::stop()
|
||||
{
|
||||
QMutexLocker locker(&lock);
|
||||
is_run = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// 用户程序调用这个函数开启蜂鸣器
|
||||
void beep::set_beep(int delay_on_ms,int delay_off_ms,int ticks)
|
||||
{
|
||||
|
||||
this->delay_on_ms = delay_on_ms;
|
||||
this->delay_off_ms =delay_off_ms;
|
||||
this->ticks = ticks;
|
||||
}
|
||||
|
||||
|
||||
// 执行线程
|
||||
void beep::run_cb()
|
||||
{
|
||||
while(is_run)
|
||||
{
|
||||
QMutexLocker locker(&lock);
|
||||
while(ticks>0)
|
||||
{
|
||||
ticks--;
|
||||
set_state(true);
|
||||
QThread::msleep(delay_on_ms);
|
||||
set_state(false);
|
||||
QThread::msleep(delay_off_ms);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
beep *Beep(){
|
||||
static beep *b=nullptr;
|
||||
static QThread thread;
|
||||
if(b==nullptr){
|
||||
b=new beep();
|
||||
b->moveToThread(&thread);
|
||||
thread.start();
|
||||
b->start();
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
45
base/beep.h
Normal file
45
base/beep.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef BEEP_H
|
||||
#define BEEP_H
|
||||
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
#include <QObject>
|
||||
#include <QThread>
|
||||
#include <QMutexLocker>
|
||||
#include <QMutex>
|
||||
class beep : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit beep(QObject *parent = nullptr);
|
||||
|
||||
bool stop();
|
||||
void start();
|
||||
// 用户程序调用这个函数开启蜂鸣器
|
||||
void set_beep(int delay_on_ms,int delay_off_ms,int ticks);
|
||||
private:
|
||||
bool get_state();
|
||||
bool set_state(bool state);
|
||||
private:
|
||||
bool is_run;
|
||||
bool state;
|
||||
|
||||
int delay_on_ms;
|
||||
int delay_off_ms;
|
||||
int ticks;
|
||||
|
||||
QMutex lock;
|
||||
QFile file;
|
||||
|
||||
private slots:
|
||||
void run_cb();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
beep *Beep();
|
||||
|
||||
|
||||
|
||||
#endif // BEEP_H
|
Reference in New Issue
Block a user