forked from ccsens_hardware/qt_qcp_show
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.
180 lines
4.5 KiB
180 lines
4.5 KiB
#include "deviceservice.h"
|
|
#include <QtCore>
|
|
#ifdef Q_OS_LINUX
|
|
#include <wiringPi.h>
|
|
#else
|
|
#endif
|
|
|
|
#define WIRING_PIN_LED 4 //GPIO.4
|
|
#define WIRING_PIN_KEY 3 //GPIO.3
|
|
#define Click_Overtime 50
|
|
#define Press_Overtime 1500
|
|
#define DblClick_Difftime 2000
|
|
|
|
DeviceService::DeviceService(QObject *parent) : QObject(parent)
|
|
{
|
|
key.flag = None;
|
|
key.state = KEY_Up;
|
|
m_ledStatus = 0;
|
|
m_keyStatus = 0;
|
|
lastStateTime = QTime::currentTime();
|
|
|
|
#ifdef Q_OS_LINUX
|
|
wiringPiSetup();
|
|
pinMode(WIRING_PIN_LED,OUTPUT);//LED输出模式 可选值:INPUT、OUTPUT、PWM_OUTPUT,GPIO_CLOCK
|
|
pinMode(WIRING_PIN_KEY,INPUT); //KEY输入模式
|
|
pullUpDnControl(WIRING_PIN_KEY,PUD_UP); //KEY设置上拉
|
|
#else
|
|
qDebug() << "wiringPiSetup()";
|
|
qDebug() << "pinMode(WIRING_PIN_LED,OUTPUT)";
|
|
qDebug() << "pinMode(WIRING_PIN_BTN,INPUT)";
|
|
#endif
|
|
|
|
//按键灯闪烁
|
|
timer1.setInterval(1000);
|
|
// timer1.start();
|
|
// connect(&timer1,&QTimer::timeout,[=](){
|
|
// m_ledStatus ^= 1;
|
|
// ledSet(m_ledStatus);
|
|
// });
|
|
|
|
|
|
//扫描按键状态,发送
|
|
timer2.setInterval(50);
|
|
timer2.start();
|
|
connect(&timer2,&QTimer::timeout,[=](){
|
|
// quint8 keyStatus = keyReadOnce();
|
|
// if(keyStatus != m_keyStatus){
|
|
// m_keyStatus = keyStatus;
|
|
// qDebug() << "m_keyStatus " << m_keyStatus;
|
|
// }
|
|
// key.flag = (KeyFlag)keyDetection();
|
|
// if(key.flag != None){
|
|
// if(key.flag == Click){
|
|
// emit keyClicked();
|
|
// }
|
|
// }
|
|
keyDetection();
|
|
});
|
|
}
|
|
|
|
void DeviceService::ledSet(int status)
|
|
{
|
|
#ifdef Q_OS_LINUX
|
|
digitalWrite(WIRING_PIN_LED,status == 1 ? LOW : HIGH);//'HIGH'代表高电平,’LOW‘则为低电平
|
|
#else
|
|
qDebug() << "LED set: " << status;
|
|
#endif
|
|
}
|
|
|
|
/**
|
|
* @brief DeviceService::keyReadOnce
|
|
* 按键按下返回0,按键弹起返回1
|
|
* @return
|
|
*/
|
|
quint8 DeviceService::keyReadOnce()
|
|
{
|
|
#ifdef Q_OS_LINUX
|
|
return digitalRead(WIRING_PIN_KEY);
|
|
#else
|
|
//qDebug() << "keyReadOnce()";
|
|
return KEY_Up;
|
|
#endif
|
|
}
|
|
|
|
quint8 DeviceService::keyDetection()
|
|
{
|
|
#if 0
|
|
key.flag = None; //清空按键标志
|
|
quint8 status = keyReadOnce();
|
|
if ((status == KEY_Down) && (key.lastState == KEY_Up))
|
|
{
|
|
uint16_t i = Click_Overtime;
|
|
while(i--);
|
|
status = keyReadOnce();
|
|
if(status == KEY_Down){
|
|
key.flag = Click; //先确定按键标志为单击
|
|
}
|
|
}
|
|
key.lastState = status;
|
|
return key.flag;
|
|
|
|
#else
|
|
//判定上次有行为,且上次行为产生时间间隔大于组合行为最长时间间隔,说明行为结束(不在判定组合行为),发送行为信号,并清空key.flag
|
|
if(key.flag != None && lastFlagTime.msecsTo(QTime::currentTime()) >= DblClick_Difftime)
|
|
{
|
|
// emit keyWorked(key.flag);
|
|
qDebug() << "keyWorked = " << key.flag;
|
|
key.flag = None;
|
|
}
|
|
|
|
KeyFlag flag = None; //清空按键标志(行为)
|
|
|
|
//获取当前按键状态
|
|
// quint8 status = getbtnstatus();//获取当前按键状态********注意替换为防抖后的按键函数getbtnstatus()
|
|
quint8 status = keyReadOnce();
|
|
|
|
//判定标志行为
|
|
if(status == KEY_Up)
|
|
{
|
|
if(key.state == KEY_Down)
|
|
{
|
|
int difftime = lastStateTime.msecsTo(QTime::currentTime());
|
|
if(difftime >= Press_Overtime)
|
|
{
|
|
flag = Press;
|
|
}else if(difftime >= Click_Overtime)
|
|
{
|
|
flag = Click;
|
|
}
|
|
}
|
|
}
|
|
|
|
//判定组合行为
|
|
if(flag == Click)
|
|
{
|
|
if(key.flag == Click)
|
|
{
|
|
flag = Dbl_Click;
|
|
}else if(key.flag == Dbl_Click)
|
|
{
|
|
flag = Tri_Click;
|
|
}
|
|
}
|
|
|
|
//记录上一行为及时间
|
|
if(flag != key.flag)
|
|
{
|
|
key.flag = flag;
|
|
lastFlagTime = QTime::currentTime();
|
|
}
|
|
|
|
//记录按键按下时间
|
|
if(status != key.state)
|
|
{
|
|
key.state = status;
|
|
lastStateTime = QTime::currentTime();
|
|
if(key.state != KEY_Up)
|
|
{
|
|
// emit keyDown();//发送至mainwidget,用于记录按下时操作的患者是哪个
|
|
qDebug() << "key.state = " << key.state;
|
|
}
|
|
}
|
|
|
|
return key.flag;
|
|
#endif
|
|
}
|
|
|
|
quint8 DeviceService::getbtnstatus()
|
|
{
|
|
quint8 keyRead = keyReadOnce();
|
|
if(keyRead == KEY_Down)
|
|
{
|
|
uint16_t i = Click_Overtime;
|
|
while(i--);
|
|
if(keyRead == keyReadOnce())
|
|
{
|
|
return keyRead;
|
|
}
|
|
}
|
|
}
|
|
|