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.
 
 
 

124 lines
3.5 KiB

#include "deviceservice.h"
#ifdef Q_OS_LINUX
#include <wiringPi.h>
#else
#include <QtCore>
#endif
#define WIRING_PIN_LED 4 //GPIO.4
#define WIRING_PIN_KEY 3 //GPIO.3
DeviceService::DeviceService(QObject *parent) : QObject(parent)
{
key.flag = None;
key.lastState = None;
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(10);
timer2.start();
connect(&timer2,&QTimer::timeout,[=](){
quint8 keyStatus = keyReadOnce();
if(keyStatus != m_keyStatus){
m_keyStatus = keyStatus;
qDebug() << "m_keyStatus " << m_keyStatus;
}
});
}
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
}
quint8 DeviceService::keyReadOnce()
{
#ifdef Q_OS_LINUX
return digitalRead(WIRING_PIN_KEY);
#else
qDebug() << "keyReadOnce()";
return 0;
#endif
}
//quint8 DeviceService::keyDetection()
//{
// key.flag = None; //清空按键标志
// if ((keyReadOnce() == KEY_Up) && (key.lastState == KEY_Down))
// {
// uint16_t i = Click_Overtime;
// uint16_t j = Press_Overtime;
// key.flag = Click; //先确定按键标志为单击
// key.lastState = KEY_Up;
// //判断是否为长按
// while ((tls_gpio_read(gpio_pin) == KEY_Up)&&(j--))
// {
// //printf("j = %d\r\n", j);
// if (j == 1)
// {
// KEY3.KEY_Flag = Press; //按键标志为长按
// KEY3.KEY_LastState = KEY_Up;
// break;
// }
// delay_ms(50);
// }
// //在确定标志已经为单击的前提下,判断是否为双击
// if (KEY3.KEY_Flag == Click)
// {
// while (i--)
// {
// if ((tls_gpio_read(gpio_pin) == KEY_Up))
// {
// KEY3.KEY_Flag = Dbl_Click;
// KEY3.KEY_LastState = KEY_Up;
// while (tls_gpio_read(gpio_pin) == KEY_Up);
// break;
// }
// delay_ms(50);
// }
// }
// //在确定标志为已经为双击的前提下,判断是否为三击
// if (KEY3.KEY_Flag == Dbl_Click)
// {
// while (i--)
// {
// if (tls_gpio_read(gpio_pin) == KEY_Up)
// {
// KEY3.KEY_Flag = Tri_Click;
// KEY3.KEY_LastState = KEY_Up;
// while (tls_gpio_read(gpio_pin) == KEY_Up);
// break;
// }
// delay_ms(50);
// }
// }
// }
// //将按键置为松开状态,以供下一次检测
// if (tls_gpio_read(gpio_pin) == KEY_Down)
// {
// KEY3.KEY_LastState = KEY_Down;
// }
// return KEY3.KEY_Flag;
//}