Skip to content

Commit

Permalink
Zwift Cog Virtual Gearing in ERG mode (Issue #2685)
Browse files Browse the repository at this point in the history
  • Loading branch information
cagnulein committed Oct 22, 2024
1 parent 9071d8a commit 36dde79
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/ios/virtualbike_zwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ class BLEPeripheralManagerZwift: NSObject, CBPeripheralManagerDelegate {
let expectedHexArray5: [UInt8] = [0x04, 0x22]
let expectedHexArray6: [UInt8] = [0x04, 0x2a, 0x04, 0x10]
let expectedHexArray7: [UInt8] = [0x04, 0x2a, 0x03, 0x10]
let expectedHexArray8: [UInt8] = [0x04, 0x18]

let receivedBytes = [UInt8](receivedData.prefix(expectedHexArray.count))

Expand Down Expand Up @@ -515,6 +516,25 @@ class BLEPeripheralManagerZwift: NSObject, CBPeripheralManagerDelegate {
updateQueue.append((ZwiftPlayIndicateCharacteristic, responseData))

}

let receivedBytes8 = [UInt8](receivedData.prefix(expectedHexArray8.count))

if receivedBytes8 == expectedHexArray8 {
SwiftDebug.qtDebug("Zwift Play Ask 8")
peripheral.respond(to: requests.first!, withResult: .success)

var power: [UInt8] = [ receivedBytes[2], 0x00 ]
var high : UInt16 = (((UInt16)(power[1])) << 8);
self.PowerRequested = (Double)((UInt16)(power[0]) + high);
LastFTMSMessageReceived = Data([0x05, power[0], power[1]])

var response: [UInt8] = [ 0x03, 0x08, 0x82, 0x01, 0x10, 0x22, 0x18, 0x10, 0x20, 0x00, 0x28, 0x98, 0x52, 0x30, 0x86, 0xed, 0x01 ]
response[2] = self.CurrentWatt
var responseData = Data(bytes: &response, count: 17)

updateQueue.append((ZwiftPlayReadUUID, responseData))
}

}
}

Expand Down
32 changes: 31 additions & 1 deletion src/virtualdevices/virtualbike.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ void virtualbike::characteristicChanged(const QLowEnergyCharacteristic &characte
static const QByteArray expectedHexArray5 = QByteArray::fromHex("0422");
static const QByteArray expectedHexArray6 = QByteArray::fromHex("042A0410");
static const QByteArray expectedHexArray7 = QByteArray::fromHex("042A0310");
static const QByteArray expectedHexArray8 = QByteArray::fromHex("0418");

QByteArray receivedData = newValue;

Expand Down Expand Up @@ -910,7 +911,7 @@ void virtualbike::characteristicChanged(const QLowEnergyCharacteristic &characte
for (const QLowEnergyCharacteristic &characteristic : serviceFIT->characteristics()) {
if (characteristic.uuid() == targetUuid) {
targetCharacteristic = characteristic;
break; // Abbiamo trovato la caratteristica, usciamo dal ciclo
break;
}
}

Expand Down Expand Up @@ -948,6 +949,35 @@ void virtualbike::characteristicChanged(const QLowEnergyCharacteristic &characte
handleZwiftGear(receivedData.mid(4));
writeCharacteristic(serviceZwiftPlayBike, zwiftPlayIndicate, response);
}
else if (receivedData.startsWith(expectedHexArray8)) {
qDebug() << "Zwift Play Ask 8";

QByteArray power(2, 0);
power[0] = receivedData[1];
double Power = (qint16(power[0]) + ((qint16(power[1]) << 8) & 0xFF00));
power[0] = quint8(qint16(Power) & 0xFF);
power[1] = quint8((qint16(Power) >> 8) & 0x00FF);

QBluetoothUuid targetUuid = QBluetoothUuid(quint16(0x2ad9));
QLowEnergyCharacteristic targetCharacteristic;

for (const QLowEnergyCharacteristic &characteristic : serviceFIT->characteristics()) {
if (characteristic.uuid() == targetUuid) {
targetCharacteristic = characteristic;
break;
}
}

if (targetCharacteristic.isValid()) {
characteristicChanged(targetCharacteristic, QByteArray::fromHex("05") + power);

QByteArray response = QByteArray::fromHex("030882011022181020002898523086ed01");
response[2] = (uint8_t)Bike->wattsMetric().value();
writeCharacteristic(serviceZwiftPlayBike, zwiftPlayRead, response);
} else {
qDebug() << "ERROR! Zwift Play Ask 8 without answer!";
}
}
}

//******************** ECHELON ***************
Expand Down

0 comments on commit 36dde79

Please sign in to comment.