diff --git a/deviceservice.cpp b/deviceservice.cpp index 3c7626b..1e106de 100644 --- a/deviceservice.cpp +++ b/deviceservice.cpp @@ -7,11 +7,12 @@ #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 = None; + key.lastState = KEY_Up; m_ledStatus = 0; m_keyStatus = 0; @@ -33,13 +34,19 @@ DeviceService::DeviceService(QObject *parent) : QObject(parent) ledSet(m_ledStatus); }); - timer2.setInterval(10); + timer2.setInterval(50); timer2.start(); connect(&timer2,&QTimer::timeout,[=](){ - quint8 keyStatus = keyReadOnce(); - if(keyStatus != m_keyStatus){ - m_keyStatus = keyStatus; - qDebug() << "m_keyStatus " << m_keyStatus; +// 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(); + } } }); } @@ -53,6 +60,11 @@ void DeviceService::ledSet(int status) #endif } +/** + * @brief DeviceService::keyReadOnce + * 按键按下返回0,按键弹起返回1 + * @return + */ quint8 DeviceService::keyReadOnce() { #ifdef Q_OS_LINUX @@ -63,62 +75,19 @@ quint8 DeviceService::keyReadOnce() #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; -//} +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; +} diff --git a/deviceservice.h b/deviceservice.h index 1bb5e44..f22a343 100644 --- a/deviceservice.h +++ b/deviceservice.h @@ -17,8 +17,8 @@ typedef enum{ Press = (uint8_t)4, }KeyFlag; -#define KEY_Up GPIO_PIN_SET -#define KEY_Down GPIO_PIN_RESET +#define KEY_Up 1 +#define KEY_Down 0 typedef struct{ KeyFlag flag; @@ -35,11 +35,12 @@ private: int m_ledStatus,m_keyStatus; QTimer timer1,timer2; signals: + void keyClicked(); public slots: void ledSet(int status); quint8 keyReadOnce(); - //quint8 keyDetection(); + quint8 keyDetection(); }; #endif // DEVICESERVICE_H diff --git a/main.cpp b/main.cpp index 7b9b052..12a257f 100644 --- a/main.cpp +++ b/main.cpp @@ -34,9 +34,12 @@ int main(int argc, char *argv[]) &w,&MainWidget::onNewPatientMergencyInfo); QObject::connect(&websocketService,&WebsocketService::patientMergencyStatusChanged, &w,&MainWidget::onPatientMergencyStatusChanged); + QObject::connect(&deviceService,&DeviceService::keyClicked, + &w,&MainWidget::onKeyClicked); QObject::connect(&w,&MainWidget::statusChanged,&httpService,&HttpService::updateStatus); + w.show(); // w.showFullScreen(); diff --git a/mainwidget.cpp b/mainwidget.cpp index 08a9305..49067f5 100644 --- a/mainwidget.cpp +++ b/mainwidget.cpp @@ -181,6 +181,12 @@ void MainWidget::onPatientMergencyStatusChanged(QString firstAidId,QString time, } } +void MainWidget::onKeyClicked() +{ +// emit onKeyStart(); + qDebug() << "Key Clicked"; +} + void MainWidget::addPatientToList(PatientEmergencyInfo *info) { //1.如果有就删除,以保证同一个病人按两次应该重新开始计时 diff --git a/mainwidget.h b/mainwidget.h index 4c7e2a2..b9e472b 100644 --- a/mainwidget.h +++ b/mainwidget.h @@ -41,6 +41,8 @@ public slots: void deletePatientFromList(QString firstAidId); void lableScrollDisplay(); + void onKeyClicked(); + signals: void statusChanged(QString firstAidId,QString status); };