Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 60 additions & 18 deletions src/devices/cscbike/cscbike.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,47 @@ void cscbike::characteristicChanged(const QLowEnergyCharacteristic &characterist
return;
}

if (characteristic.uuid() != QBluetoothUuid((quint16)0x2A5B)) {
bool cyclingPowerMeasurement = characteristic.uuid() == QBluetoothUuid::CyclingPowerMeasurement;
if (characteristic.uuid() != QBluetoothUuid((quint16)0x2A5B) && !cyclingPowerMeasurement) {
return;
}

lastPacket = newValue;

bool CrankPresent = (newValue.at(0) & 0x02) == 0x02;
bool WheelPresent = (newValue.at(0) & 0x01) == 0x01;
bool CrankPresent = false;
bool WheelPresent = false;
if (cyclingPowerMeasurement) {
if (newValue.length() < 4) {
return;
}

uint16_t flags = (((uint16_t)((uint8_t)newValue.at(1)) << 8) | (uint16_t)((uint8_t)newValue.at(0)));
CrankPresent = (flags & 0x20) == 0x20;
if (!CrankPresent) {
return;
}

uint8_t index = 4;
if ((flags & 0x01) == 0x01) {
index += 1; // pedal power balance
}
if ((flags & 0x04) == 0x04) {
index += 2; // accumulated torque
}
if ((flags & 0x10) == 0x10) {
index += 6; // wheel revolutions and wheel event time
}
if (newValue.length() < index + 4) {
return;
}

_CrankRevs = (((uint16_t)((uint8_t)newValue.at(index + 1)) << 8) | (uint16_t)((uint8_t)newValue.at(index)));
_LastCrankEventTime =
(((uint16_t)((uint8_t)newValue.at(index + 3)) << 8) | (uint16_t)((uint8_t)newValue.at(index + 2)));
} else {
CrankPresent = (newValue.at(0) & 0x02) == 0x02;
WheelPresent = (newValue.at(0) & 0x01) == 0x01;
}
qDebug() << QStringLiteral("CrankPresent: ") << CrankPresent;
qDebug() << QStringLiteral("WheelPresent: ") << WheelPresent;

Expand All @@ -318,14 +351,18 @@ void cscbike::characteristicChanged(const QLowEnergyCharacteristic &characterist
emit debug(QStringLiteral("Current Wheel Event Time: ") + QString::number(_LastWheelEventTime));
index += 2;
}
if (CrankPresent) {
if (CrankPresent && !cyclingPowerMeasurement) {
_CrankRevs = (((uint16_t)((uint8_t)newValue.at(index + 1)) << 8) | (uint16_t)((uint8_t)newValue.at(index)));
emit debug(QStringLiteral("Current Crank Revs: ") + QString::number(_CrankRevs));
index += 2;
_LastCrankEventTime =
(((uint16_t)((uint8_t)newValue.at(index + 1)) << 8) | (uint16_t)((uint8_t)newValue.at(index)));
emit debug(QStringLiteral("Current Crank Event Time: ") + QString::number(_LastCrankEventTime));
}
if (cyclingPowerMeasurement) {
emit debug(QStringLiteral("Current Crank Revs: ") + QString::number(_CrankRevs));
emit debug(QStringLiteral("Current Crank Event Time: ") + QString::number(_LastCrankEventTime));
}

// CSC Combo Sensor Fallback Logic
//
Expand All @@ -351,9 +388,9 @@ void cscbike::characteristicChanged(const QLowEnergyCharacteristic &characterist
LastCrankEventTime = _LastCrankEventTime;
}

int16_t deltaT = LastCrankEventTime - oldLastCrankEventTime;
int32_t deltaT = LastCrankEventTime - oldLastCrankEventTime;
if (deltaT < 0) {
deltaT = LastCrankEventTime + 65535 - oldLastCrankEventTime;
deltaT = LastCrankEventTime + 65536 - oldLastCrankEventTime;
}

if (CrankRevs != oldCrankRevs && deltaT) {
Expand Down Expand Up @@ -474,13 +511,14 @@ void cscbike::stateChanged(QLowEnergyService::ServiceState state) {
emit debug(QStringLiteral("BTLE stateChanged ") + QString::fromLocal8Bit(metaEnum.valueToKey(state)));

QBluetoothUuid CyclingSpeedAndCadence(QBluetoothUuid::CyclingSpeedAndCadence);
QBluetoothUuid CyclingPower(QBluetoothUuid::CyclingPower);
QBluetoothUuid Battery(QBluetoothUuid::BatteryService);
for (QLowEnergyService *s : qAsConst(gattCommunicationChannelService)) {
qDebug() << QStringLiteral("stateChanged") << s->serviceUuid() << s->state();
#ifdef Q_OS_WINDOWS
qDebug() << "windows workaround, check only CyclingSpeedAndCadence ftms service"
<< (s->serviceUuid() == CyclingSpeedAndCadence);
if (s->serviceUuid() == CyclingSpeedAndCadence)
qDebug() << "windows workaround, check only cycling sensor services"
<< (s->serviceUuid() == CyclingSpeedAndCadence || s->serviceUuid() == CyclingPower);
if (s->serviceUuid() == CyclingSpeedAndCadence || s->serviceUuid() == CyclingPower)
#endif
{
if (s->state() != QLowEnergyService::ServiceDiscovered && s->state() != QLowEnergyService::InvalidService) {
Expand All @@ -495,13 +533,15 @@ void cscbike::stateChanged(QLowEnergyService::ServiceState state) {
for (QLowEnergyService *s : qAsConst(gattCommunicationChannelService)) {
if (s->state() == QLowEnergyService::ServiceDiscovered) {

if(s->serviceUuid() == CyclingSpeedAndCadence) {
qDebug() << "CyclingSpeedAndCadence found";
if (s->serviceUuid() == CyclingSpeedAndCadence || s->serviceUuid() == CyclingPower) {
qDebug() << "Cycling cadence service found" << s->serviceUuid();
cadenceService = s;
}

if(s->serviceUuid() != CyclingSpeedAndCadence && s->serviceUuid() != Battery) {
// No data from sensors and avatar won’t move in Zwift (even when data showed on first try) (Issue #2178)
if (s->serviceUuid() != CyclingSpeedAndCadence && s->serviceUuid() != CyclingPower &&
s->serviceUuid() != Battery) {
// No data from sensors and avatar won’t move in Zwift (even when data showed on first try) (Issue
// #2178)
qDebug() << "avoid unwaned service";
continue;
}
Expand All @@ -520,8 +560,9 @@ void cscbike::stateChanged(QLowEnergyService::ServiceState state) {

auto characteristics_list = s->characteristics();
for (const QLowEnergyCharacteristic &c : qAsConst(characteristics_list)) {
if(c.uuid() == QBluetoothUuid((quint16)0x2A5B)) {
qDebug() << "CyclingSpeedAndCadence char found";
if (c.uuid() == QBluetoothUuid((quint16)0x2A5B) ||
c.uuid() == QBluetoothUuid::CyclingPowerMeasurement) {
qDebug() << "Cycling cadence characteristic found" << c.uuid();
cadenceChar = c;
}
qDebug() << QStringLiteral("char uuid") << c.uuid() << QStringLiteral("handle") << c.handle() << QStringLiteral("properties") << c.properties();
Expand Down Expand Up @@ -647,9 +688,10 @@ void cscbike::serviceScanDone(void) {
for (const QBluetoothUuid &s : qAsConst(services_list)) {
#ifdef Q_OS_WINDOWS
QBluetoothUuid CyclingSpeedAndCadence(QBluetoothUuid::CyclingSpeedAndCadence);
qDebug() << "windows workaround, check only the CyclingSpeedAndCadence service" << s << CyclingSpeedAndCadence
<< (s == CyclingSpeedAndCadence);
if (s == CyclingSpeedAndCadence)
QBluetoothUuid CyclingPower(QBluetoothUuid::CyclingPower);
qDebug() << "windows workaround, check only cycling sensor services" << s << CyclingSpeedAndCadence
<< CyclingPower << (s == CyclingSpeedAndCadence || s == CyclingPower);
if (s == CyclingSpeedAndCadence || s == CyclingPower)
#endif
{
gattCommunicationChannelService.append(m_control->createServiceObject(s));
Expand Down
7 changes: 4 additions & 3 deletions src/devices/stagesbike/stagesbike.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,17 @@ void stagesbike::characteristicChanged(const QLowEnergyCharacteristic &character
index += 2;
}

int16_t deltaT = LastCrankEventTime - oldLastCrankEventTime;
int32_t deltaT = LastCrankEventTime - oldLastCrankEventTime;
if (deltaT < 0) {
deltaT = LastCrankEventTime + time_division - oldLastCrankEventTime;
deltaT = LastCrankEventTime + 65536 - oldLastCrankEventTime;
}

if (settings.value(QZSettings::cadence_sensor_name, QZSettings::default_cadence_sensor_name)
.toString()
.startsWith(QStringLiteral("Disabled"))) {
if (CrankRevs != oldCrankRevs && deltaT) {
double cadence = ((CrankRevs - oldCrankRevs) / deltaT) * time_division * 60;
double cadence = (static_cast<double>(CrankRevs - oldCrankRevs) / static_cast<double>(deltaT)) *
time_division * 60.0;
if (!crank_rev_present)
cadence =
cadence /
Expand Down
Loading