Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Steering on Zwift (iOS) #2486

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/ios/BLEPeripheralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ let PowerFeatureCharacteristicUUID = CBUUID(string: "0x2A65")
let PowerSensorLocationCharacteristicUUID = CBUUID(string: "0x2A5D")
let PowerMeasurementCharacteristicUUID = CBUUID(string: "0x2A63")

let SterzoServiceUUID = CBUUID(string: "347b0001-7635-408b-8918-8ff3949ce592")
let SterzoWriteUUID = CBUUID(string: "347b0031-7635-408b-8918-8ff3949ce592")
let SterzoNotifyUUID = CBUUID(string: "347b0030-7635-408b-8918-8ff3949ce592")


@objc public class virtualbike_ios_swift: NSObject {
private var peripheralManager: BLEPeripheralManager!

Expand Down
68 changes: 65 additions & 3 deletions src/ios/virtualbike_zwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ class BLEPeripheralManagerZwift: NSObject, CBPeripheralManagerDelegate {
private var PowerFeatureCharacteristic: CBMutableCharacteristic!
private var PowerSensorLocationCharacteristic: CBMutableCharacteristic!
private var PowerMeasurementCharacteristic: CBMutableCharacteristic!

private var SterzoService: CBMutableService!
private var SterzoWriteCharacteristic: CBMutableCharacteristic!
private var SterzoNotifyCharacteristic: CBMutableCharacteristic!


public var LastFTMSMessageReceived: Data?
Expand Down Expand Up @@ -254,6 +258,26 @@ class BLEPeripheralManagerZwift: NSObject, CBPeripheralManagerDelegate {
PowerMeasurementCharacteristic]
self.peripheralManager.add(PowerService)

// Sterzo
self.SterzoService = CBMutableService(type: SterzoServiceUUID, primary: true)

let SterzoWriteProperties: CBCharacteristicProperties = [.write]
let SterzoWritePermissions: CBAttributePermissions = [.writeable]
self.SterzoWriteCharacteristic = CBMutableCharacteristic(type: SterzoWriteUUID,
properties: SterzoWriteProperties, value: nil,
permissions: SterzoWritePermissions)

let SterzoNotifyProperties: CBCharacteristicProperties = [.notify]
let SterzoNotifyPermissions: CBAttributePermissions = []
self.SterzoNotifyCharacteristic = CBMutableCharacteristic(type: SterzoNotifyUUID,
properties: SterzoNotifyProperties,
value: nil,
permissions: SterzoNotifyPermissions)


SterzoService.characteristics = [SterzoWriteCharacteristic,
SterzoNotifyCharacteristic]
self.peripheralManager.add(SterzoService)

default:
print("Peripheral manager is down")
Expand Down Expand Up @@ -323,7 +347,11 @@ class BLEPeripheralManagerZwift: NSObject, CBPeripheralManagerDelegate {
let responseData = Data(bytes: &response, count: 3)

self.peripheralManager.updateValue(responseData, for: self.FitnessMachineControlPointCharacteristic, onSubscribedCentrals: nil)
}
} else if requests.first!.characteristic == self.SterzoWriteCharacteristic {
self.peripheralManager.respond(to: requests.first!, withResult: .success)
print("Responded successfully to a write request")
}

}

func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
Expand Down Expand Up @@ -466,6 +494,12 @@ class BLEPeripheralManagerZwift: NSObject, CBPeripheralManagerDelegate {
let heartRateData = Data(bytes: &heartRateBPM, count: MemoryLayout.size(ofValue: heartRateBPM))
return heartRateData
}

func float32ToUInt8Array(_ value: Float32) -> [UInt8] {
let bytes = withUnsafeBytes(of: value) { Array($0) }
return bytes.reversed()
}


func calculateIndoorBike() -> Data {
let flags0:UInt8 = 0x64
Expand All @@ -477,13 +511,41 @@ class BLEPeripheralManagerZwift: NSObject, CBPeripheralManagerDelegate {
return indoorBikeData
}

static var angle: Float32 = 10
static var angleCount: Int = 0
static var angleToogle: Bool = false

@objc func updateSubscribers() {
if(self.serviceToggle == 3 || garmin_bluetooth_compatibility)
if(self.serviceToggle == 4)
{
BLEPeripheralManagerZwift.angleCount = BLEPeripheralManagerZwift.angleCount + 1
if(BLEPeripheralManagerZwift.angleCount > 10) {
BLEPeripheralManagerZwift.angleCount = 0
if(BLEPeripheralManagerZwift.angleToogle) {
BLEPeripheralManagerZwift.angle = 30
BLEPeripheralManagerZwift.angleToogle = false
} else {
BLEPeripheralManagerZwift.angle = -30;
BLEPeripheralManagerZwift.angleToogle = true
}
} else {
if(!BLEPeripheralManagerZwift.angleToogle) {
BLEPeripheralManagerZwift.angle = BLEPeripheralManagerZwift.angle - 3.3;
} else {
BLEPeripheralManagerZwift.angle = BLEPeripheralManagerZwift.angle + 3.3;
}
}
print("Angle \(BLEPeripheralManagerZwift.angle)")
let sterzoData = Data(bytes: float32ToUInt8Array(BLEPeripheralManagerZwift.angle), count: 4)
let ok = self.peripheralManager.updateValue(sterzoData, for: self.SterzoNotifyCharacteristic, onSubscribedCentrals: nil)
if(ok) {
self.serviceToggle = 0
}
} else if(self.serviceToggle == 3 || garmin_bluetooth_compatibility) {
let powerData = self.calculatePower()
let ok = self.peripheralManager.updateValue(powerData, for: self.PowerMeasurementCharacteristic, onSubscribedCentrals: nil)
if(ok) {
self.serviceToggle = 0
self.serviceToggle = self.serviceToggle + 1
}
} else if(self.serviceToggle == 2) {
let cadenceData = self.calculateCadence()
Expand Down
Loading