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.
93 lines
2.3 KiB
93 lines
2.3 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
|
|
|
|
DeviceService::DeviceService(QObject *parent) : QObject(parent)
|
|
{
|
|
key.flag = None;
|
|
key.lastState = KEY_Up;
|
|
m_ledStatus = 0;
|
|
m_keyStatus = 0;
|
|
|
|
#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();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
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()
|
|
{
|
|
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;
|
|
}
|
|
|