Skip to content

Commit

Permalink
using variables instead of timer
Browse files Browse the repository at this point in the history
Several Issues Using QZ with Rouvy and Zwift Play Controllers (Issue #2541)
  • Loading branch information
cagnulein committed Dec 21, 2024
1 parent 3dd3c8f commit 445646f
Showing 1 changed file with 76 additions and 67 deletions.
143 changes: 76 additions & 67 deletions src/zwift_play/abstractZapDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <QString>
#include <QDebug>
#include <QSettings>
#include <QTimer>
#include <QDateTime>
//#include "localKeyProvider.h"
//#include "zapCrypto.h"
#include "zapConstants.h"
Expand All @@ -24,18 +24,22 @@ class AbstractZapDevice: public QObject {
RIGHT
};

enum BUTTON_STATE {
OFF,
PLUS_PRESSED,
MINUS_PRESSED
};

QByteArray RIDE_ON;
QByteArray REQUEST_START;
QByteArray RESPONSE_START;

AbstractZapDevice() : autoRepeatTimer(new QTimer(this)) {
AbstractZapDevice() {
RIDE_ON = QByteArray::fromRawData("\x52\x69\x64\x65\x4F\x6E", 6); // "RideOn"
REQUEST_START = QByteArray::fromRawData("\x00\x09", 2); // {0, 9}
RESPONSE_START = QByteArray::fromRawData("\x01\x03", 2); // {1, 3}

// Setup auto-repeat
autoRepeatTimer->setInterval(500);
connect(autoRepeatTimer, &QTimer::timeout, this, &AbstractZapDevice::handleAutoRepeat);
buttonState = OFF;
lastButtonCheck = QDateTime::currentDateTime();
}

int processCharacteristic(const QString& characteristicName, const QByteArray& bytes, ZWIFT_PLAY_TYPE zapType) {
Expand All @@ -47,6 +51,19 @@ class AbstractZapDevice: public QObject {

qDebug() << zapType << characteristicName << bytes.toHex() << zwiftplay_swap << gears_volume_debouncing << risingEdge;

// Check if enough time has passed (500ms) and button is still pressed
if (buttonState != OFF) {
QDateTime currentTime = QDateTime::currentDateTime();
if (lastButtonCheck.msecsTo(currentTime) >= 500) {
lastButtonCheck = currentTime;
if (buttonState == PLUS_PRESSED) {
emit plus();
} else if (buttonState == MINUS_PRESSED) {
emit minus();
}
}
}

#define DEBOUNCE (!gears_volume_debouncing || risingEdge <= 0)

#ifdef Q_OS_ANDROID_ENCRYPTION
Expand Down Expand Up @@ -74,35 +91,35 @@ class AbstractZapDevice: public QObject {
risingEdge = 2;
if(!zwiftplay_swap) {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
} else if(bytes[4] == 0) {
if(DEBOUNCE) {
risingEdge = 2;
if(!zwiftplay_swap) {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
} else {
risingEdge--;
if(risingEdge < 0)
risingEdge = 0;
if(risingEdge == 0)
autoRepeatTimer->stop();
buttonState = OFF;
}
}
break;
Expand All @@ -116,27 +133,27 @@ class AbstractZapDevice: public QObject {
risingEdge = 2;
if(!zwiftplay_swap) {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
} else {
if(DEBOUNCE) {
risingEdge = 2;
if(!zwiftplay_swap) {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
}
Expand All @@ -146,27 +163,27 @@ class AbstractZapDevice: public QObject {
risingEdge = 2;
if(!zwiftplay_swap) {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
} else {
if(DEBOUNCE) {
risingEdge = 2;
if(!zwiftplay_swap) {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
}
Expand All @@ -175,7 +192,7 @@ class AbstractZapDevice: public QObject {
if(risingEdge < 0)
risingEdge = 0;
if(risingEdge == 0)
autoRepeatTimer->stop();
buttonState = OFF;
}
break;
case 0x15: // empty data
Expand All @@ -191,27 +208,27 @@ class AbstractZapDevice: public QObject {
risingEdge = 2;
if(!zwiftplay_swap) {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
} else {
if(DEBOUNCE) {
risingEdge = 2;
if(!zwiftplay_swap) {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
}
Expand All @@ -220,13 +237,13 @@ class AbstractZapDevice: public QObject {
risingEdge = 2;
if(!zwiftplay_swap) {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
} else if(bytes.length() > 3 &&
Expand All @@ -236,13 +253,13 @@ class AbstractZapDevice: public QObject {
risingEdge = 2;
if(!zwiftplay_swap) {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
} else if(bytes.length() > 3 &&
Expand All @@ -252,13 +269,13 @@ class AbstractZapDevice: public QObject {
risingEdge = 2;
if(!zwiftplay_swap) {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
} else if(bytes.length() > 5 &&
Expand All @@ -268,21 +285,21 @@ class AbstractZapDevice: public QObject {
risingEdge = 2;
if(!zwiftplay_swap) {
emit minus();
lastButtonPlus = false;
autoRepeatTimer->start();
buttonState = MINUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
else {
emit plus();
lastButtonPlus = true;
autoRepeatTimer->start();
buttonState = PLUS_PRESSED;
lastButtonCheck = QDateTime::currentDateTime();
}
}
} else {
risingEdge--;
if(risingEdge < 0)
risingEdge = 0;
if(risingEdge == 0)
autoRepeatTimer->stop();
buttonState = OFF;
}
break;
}
Expand Down Expand Up @@ -318,16 +335,8 @@ class AbstractZapDevice: public QObject {
private:
QByteArray devicePublicKeyBytes;
static volatile int8_t risingEdge;
QTimer* autoRepeatTimer; // Timer for auto-repeat
bool lastButtonPlus = false; // Track which button was last pressed

private slots:
void handleAutoRepeat() {
if(lastButtonPlus)
emit plus();
else
emit minus();
}
BUTTON_STATE buttonState;
QDateTime lastButtonCheck;

signals:
void plus();
Expand Down

0 comments on commit 445646f

Please sign in to comment.