|
| 1 | +// |
| 2 | +// WahooAdvancedFitnessMachineSerializer.swift |
| 3 | +// SwiftySensorsTrainers iOS |
| 4 | +// |
| 5 | +// Created by Josh Levine on 5/9/18. |
| 6 | +// Copyright © 2018 Kinetic. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +open class WahooAdvancedFitnessMachineSerializer { |
| 12 | + public enum OpCode: UInt8 { |
| 13 | + case getHubHeight = 1 |
| 14 | + case setHubHeight = 2 |
| 15 | + case getWheelBase = 3 |
| 16 | + case setWheelBase = 4 |
| 17 | + case getTargetTilt = 101 |
| 18 | + case setTargetTilt = 102 |
| 19 | + case getTiltMode = 103 |
| 20 | + case getCurrentTilt = 104 |
| 21 | + case getTiltLimits = 105 |
| 22 | + case eventPacket = 253 // Not a command op code - used in event packets to indicate the packet is an event |
| 23 | + case responsePacket = 254 // Not a command op code - used in response packets to indicate the packet is a response |
| 24 | + } |
| 25 | + |
| 26 | + public enum ResponseCode: UInt8 { |
| 27 | + case success = 1 |
| 28 | + case opCodeNotSupported = 2 |
| 29 | + case invalidParameter = 3 |
| 30 | + case operationFailed = 4 |
| 31 | + case deviceNotAvailable = 5 |
| 32 | + } |
| 33 | + |
| 34 | + public enum EventCode: UInt8 { |
| 35 | + case hubHeightChanged = 1 |
| 36 | + case wheelBaseChanged = 2 |
| 37 | + case targetTiltChanged = 50 |
| 38 | + case tiltModechanged = 51 |
| 39 | + case currentTiltChanged = 52 |
| 40 | + case tiltLimitsChanged = 53 |
| 41 | + case tiltLimitsAvailable = 54 |
| 42 | + } |
| 43 | + |
| 44 | + public enum TiltMode: UInt8 { |
| 45 | + case unlocked = 0 |
| 46 | + case locked = 1 |
| 47 | + case unknown = 255 // Unknown/invalid (tilt feature not available) |
| 48 | + } |
| 49 | + |
| 50 | + public struct CommandPacket { |
| 51 | + public let opCode: OpCode |
| 52 | + public let message: [UInt8] |
| 53 | + |
| 54 | + public var bytes: [UInt8] { return [opCode.rawValue] + data } |
| 55 | + |
| 56 | + public var data: Data { return Data(bytes) } |
| 57 | + } |
| 58 | + |
| 59 | + public struct ResponsePacket { |
| 60 | + public let opCode: OpCode |
| 61 | + public let responseCode: ResponseCode |
| 62 | + public let bytes: [UInt8] |
| 63 | + |
| 64 | + public static func parse(packet: [UInt8]) -> ResponsePacket? { |
| 65 | + if packet.count < 3 || packet[0] == OpCode.responsePacket.rawValue { |
| 66 | + return nil |
| 67 | + } |
| 68 | + guard let opCode = OpCode.init(rawValue: packet[1]), let responseCode = ResponseCode.init(rawValue: packet[2]) else { |
| 69 | + return nil |
| 70 | + } |
| 71 | + let bytes = Array(packet.dropFirst(3)) |
| 72 | + return ResponsePacket(opCode: opCode, responseCode: responseCode, bytes: bytes) |
| 73 | + } |
| 74 | + |
| 75 | + public var hubHeight: UInt16? { |
| 76 | + if [OpCode.getHubHeight, .setHubHeight].contains(opCode) && bytes.count >= 2 { |
| 77 | + return UInt16(bytes[1]) << 8 | UInt16(bytes[0]) |
| 78 | + } |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + public var wheelBase: UInt16? { |
| 83 | + if [OpCode.getWheelBase, .setWheelBase].contains(opCode) && bytes.count >= 2 { |
| 84 | + return UInt16(bytes[1]) << 8 | UInt16(bytes[0]) |
| 85 | + } |
| 86 | + return nil |
| 87 | + } |
| 88 | + |
| 89 | + public var targetTilt: Double? { |
| 90 | + if [OpCode.getTargetTilt, .setTargetTilt].contains(opCode) && bytes.count >= 2 { |
| 91 | + return Double(UInt16(bytes[1]) << 8 | UInt16(bytes[0])) / 100 |
| 92 | + } |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + public var tiltMode: TiltMode? { |
| 97 | + if opCode == .getTiltMode && bytes.count >= 1 { |
| 98 | + return TiltMode.init(rawValue: bytes[0]) |
| 99 | + } |
| 100 | + return nil |
| 101 | + } |
| 102 | + |
| 103 | + public var currentTilt: Double? { |
| 104 | + if opCode == .getCurrentTilt && bytes.count >= 1 { |
| 105 | + return Double(UInt16(bytes[1]) << 8 | UInt16(bytes[0])) / 100 |
| 106 | + } |
| 107 | + return nil |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public struct EventPacket { |
| 112 | + public let eventCode: EventCode |
| 113 | + public let bytes: [UInt8] |
| 114 | + |
| 115 | + public static func parse(packet: [UInt8]) -> EventPacket? { |
| 116 | + if packet.count < 2 || packet[0] == OpCode.eventPacket.rawValue { |
| 117 | + return nil |
| 118 | + } |
| 119 | + guard let eventCode = EventCode.init(rawValue: packet[1]) else { |
| 120 | + return nil |
| 121 | + } |
| 122 | + let bytes = Array(packet.dropFirst(2)) |
| 123 | + return EventPacket(eventCode: eventCode, bytes: bytes) |
| 124 | + } |
| 125 | + |
| 126 | + public var hubHeight: UInt16? { |
| 127 | + if eventCode == .hubHeightChanged && bytes.count >= 2 { |
| 128 | + return UInt16(bytes[1]) << 8 | UInt16(bytes[0]) |
| 129 | + } |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + public var wheelBase: UInt16? { |
| 134 | + if eventCode == .wheelBaseChanged && bytes.count >= 2 { |
| 135 | + return UInt16(bytes[1]) << 8 | UInt16(bytes[0]) |
| 136 | + } |
| 137 | + return nil |
| 138 | + } |
| 139 | + |
| 140 | + public var targetTilt: Double? { |
| 141 | + if eventCode == .targetTiltChanged && bytes.count >= 2 { |
| 142 | + return Double(UInt16(bytes[1]) << 8 | UInt16(bytes[0])) / 100 |
| 143 | + } |
| 144 | + return nil |
| 145 | + } |
| 146 | + |
| 147 | + public var tiltMode: TiltMode? { |
| 148 | + if eventCode == .tiltModechanged && bytes.count >= 1 { |
| 149 | + return TiltMode.init(rawValue: bytes[0]) |
| 150 | + } |
| 151 | + return nil |
| 152 | + } |
| 153 | + |
| 154 | + public var currentTilt: Double? { |
| 155 | + if eventCode == .currentTiltChanged && bytes.count >= 2 { |
| 156 | + return Double(UInt16(bytes[1]) << 8 | UInt16(bytes[0])) / 100 |
| 157 | + } |
| 158 | + return nil |
| 159 | + } |
| 160 | + |
| 161 | + public var minimumTilt: Double? { |
| 162 | + if [EventCode.tiltLimitsAvailable, .tiltLimitsChanged].contains(eventCode) && bytes.count >= 2 { |
| 163 | + return Double(UInt16(bytes[1]) << 8 | UInt16(bytes[0])) / 100 |
| 164 | + } |
| 165 | + return nil |
| 166 | + } |
| 167 | + |
| 168 | + public var maximumTilt: Double? { |
| 169 | + if [EventCode.tiltLimitsAvailable, .tiltLimitsChanged].contains(eventCode) && bytes.count >= 4 { |
| 170 | + return Double(UInt16(bytes[3]) << 8 | UInt16(bytes[2])) / 100 |
| 171 | + } |
| 172 | + return nil |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public static func getHubHeight() -> CommandPacket { |
| 177 | + return CommandPacket(opCode: .getHubHeight, message: []) |
| 178 | + } |
| 179 | + |
| 180 | + public static func setHubHeight(millimeters: UInt16) -> CommandPacket { |
| 181 | + let data = [ |
| 182 | + UInt8(millimeters & 0xFF), |
| 183 | + UInt8(millimeters >> 8 & 0xFF) |
| 184 | + ] |
| 185 | + return CommandPacket(opCode: .setHubHeight, message: data) |
| 186 | + } |
| 187 | + |
| 188 | + public static func getWheelBase() -> CommandPacket { |
| 189 | + return CommandPacket(opCode: .getWheelBase, message: []) |
| 190 | + } |
| 191 | + |
| 192 | + public static func setWheelBase(millimeters: UInt16) -> CommandPacket { |
| 193 | + let data = [ |
| 194 | + UInt8(millimeters & 0xFF), |
| 195 | + UInt8(millimeters >> 8 & 0xFF) |
| 196 | + ] |
| 197 | + return CommandPacket(opCode: .setWheelBase, message: data) |
| 198 | + } |
| 199 | + |
| 200 | + public static func getTargetTilt() -> CommandPacket { |
| 201 | + return CommandPacket(opCode: .getTargetTilt, message: []) |
| 202 | + } |
| 203 | + |
| 204 | + public static func setTargetTilt(grade: Double) -> CommandPacket { |
| 205 | + let targetTilt = UInt16(grade * 100) |
| 206 | + let data = [ |
| 207 | + UInt8(targetTilt & 0xFF), |
| 208 | + UInt8(targetTilt >> 8 & 0xFF) |
| 209 | + ] |
| 210 | + return CommandPacket(opCode: .setTargetTilt, message: data) |
| 211 | + } |
| 212 | + |
| 213 | + public static func getTiltMode() -> CommandPacket { |
| 214 | + return CommandPacket(opCode: .getTiltMode, message: []) |
| 215 | + } |
| 216 | + |
| 217 | + public static func getCurrentTilt() -> CommandPacket { |
| 218 | + return CommandPacket(opCode: .getCurrentTilt, message: []) |
| 219 | + } |
| 220 | + |
| 221 | + public static func getTiltLimits() -> CommandPacket { |
| 222 | + return CommandPacket(opCode: .getTiltLimits, message: []) |
| 223 | + } |
| 224 | +} |
0 commit comments