-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBluetooth.swift
More file actions
339 lines (292 loc) · 14.5 KB
/
Bluetooth.swift
File metadata and controls
339 lines (292 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import Foundation
import CoreBluetooth
import SwiftUI
class Bluetooth : NSObject, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate{
var cbCentralManager: CBCentralManager!
var masks = Masks.init()
@Published var currentTemperature = -1
@Published var targetTemperature = -1.0
@Published var isFanOn: Bool?
@Published var isHeatOn: Bool?
@Published var localIsFanOn: Bool?
@Published var localIsHeatOn: Bool?
@Published var serialNumber: String = "Loading..."
@Published var bleFirmwareVersion: String = "Loading..."
@Published var hoursOfOperation: String = "Loading..."
@Published var volcanoFirmwareVersion: String = "Loading..."
@Published var isDisplayOnCooling: Bool = false
@Published var isPulseFanWhenVolcanoReachesTemperature: Bool = false
@Published var ledBrightness = 0.0
@Published var isF = false
lazy var isDisplayOnCoolingBinding: Binding<Bool> = {
Binding{
self.isDisplayOnCooling
} set: { newValue in
self.isDisplayOnCooling = newValue
self.ToggleIsDisplayOnCooling()
}
}()
lazy var isPulseFanWhenVolcanoReachesTemperatureBinding: Binding<Bool> = {
Binding{
self.isPulseFanWhenVolcanoReachesTemperature
} set: { newValue in
self.isPulseFanWhenVolcanoReachesTemperature = newValue
self.ToggleIsPulseFanWhenVolcanoReachesTemperature()
}
}()
func isLoading() -> Bool {
if isFanOn != nil{
return currentTemperature > 40 && targetTemperature > 40
} else{
return false
}
}
var writeTemperatureCharacteristic: CBCharacteristic?
var fanOnCharacterisitc: CBCharacteristic?
var fanOffCharacterisitc: CBCharacteristic?
var heatOnCharacterisitc: CBCharacteristic?
var heatOffCharacterisitc: CBCharacteristic?
var register2Characterisitc: CBCharacteristic?
var register3Characterisitc: CBCharacteristic?
var ledBrightnessCharacteristic: CBCharacteristic?
var currentTemperatureCharacteristic: CBCharacteristic?
override init() {
super.init()
self.cbCentralManager = CBCentralManager.init(delegate: self, queue: nil)
}
func convertToggleCharacteristicToBool(value :Int, mask: Int) -> Bool {
if value & mask == 0 {
return false;
}
return true;
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if central.state == CBManagerState.poweredOn{
central.scanForPeripherals(withServices: nil, options: nil)
print("Scanning")
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
if let name = peripheral.name{
if name.uppercased().contains("S&B VOLCANO"){
central.stopScan()
central.connect(peripheral, options: nil)
volcano = peripheral
}
}
}
var volcano: CBPeripheral?
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("connected \(peripheral.name!)")
peripheral.discoverServices(nil)
peripheral.delegate = self
}
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
if error != nil {
central.scanForPeripherals(withServices: nil, options: nil)
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if let services = peripheral.services{
for service in services{
switch(service.uuid.uuidString){
case CBUuidService.GetCharacteristicCBUuid(uuid: .PrimaryServiceUuidVolcano3).uuidString:
peripheral.discoverCharacteristics(nil, for: service)
case CBUuidService.GetCharacteristicCBUuid(uuid: .PrimaryServiceUuidVolcano4).uuidString:
peripheral.discoverCharacteristics(nil, for: service)
default:
continue
}
}
}
}
func dataToUnsignedBytes16(value : Data) -> Int {
let count = value.count
var array = [UInt8](repeating: 0, count: count)
(value as NSData).getBytes(&array, length:count * MemoryLayout<UInt8>.size)
let firstByte = Int(array[0])
let secondByte = Int(array[1])
return firstByte + secondByte * 256
}
func dataToUnsignedBytes8(value : Data) -> Int {
let count = value.count
var array = [UInt8](repeating: 0, count: count)
(value as NSData).getBytes(&array, length:count * MemoryLayout<UInt8>.size)
let firstByte = Int(array[0])
return firstByte
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if let characteristics = service.characteristics{
for characteristic in characteristics {
switch(characteristic.uuid){
case CBUuidService.GetCharacteristicCBUuid(uuid: .HeatOnUuid):
heatOnCharacterisitc = characteristic
case CBUuidService.GetCharacteristicCBUuid(uuid: .HeatOffUuid):
heatOffCharacterisitc = characteristic
case CBUuidService.GetCharacteristicCBUuid(uuid: .LEDbrightnessUuid):
peripheral.readValue(for: characteristic)
ledBrightnessCharacteristic = characteristic
var x = Data.init()
let defaultBrightness = [UInt8(70)]
x.append(contentsOf: defaultBrightness)
peripheral.writeValue(x, for: characteristic, type: .withResponse)
case CBUuidService.GetCharacteristicCBUuid(uuid: .CurrentTemperatureUuid):
peripheral.setNotifyValue(true, for: characteristic)
peripheral.readValue(for: characteristic)
currentTemperatureCharacteristic = characteristic
case CBUuidService.GetCharacteristicCBUuid(uuid: .WriteTemperatureUuid):
peripheral.setNotifyValue(true, for: characteristic)
peripheral.readValue(for: characteristic)
writeTemperatureCharacteristic = characteristic
WriteTargetTemperature(tempInCelcius: 230)
case CBUuidService.GetCharacteristicCBUuid(uuid: .FanOnUuid):
fanOnCharacterisitc = characteristic
case CBUuidService.GetCharacteristicCBUuid(uuid: .FanOffUuid):
fanOffCharacterisitc = characteristic
case CBUuidService.GetCharacteristicCBUuid(uuid: .Register1Uuid):
peripheral.setNotifyValue(true, for: characteristic)
peripheral.readValue(for: characteristic)
case CBUuidService.GetCharacteristicCBUuid(uuid: .Register2Uuid):
peripheral.setNotifyValue(true, for: characteristic)
peripheral.readValue(for: characteristic)
register2Characterisitc = characteristic
case CBUuidService.GetCharacteristicCBUuid(uuid: .Register3Uuid):
peripheral.setNotifyValue(true, for: characteristic)
peripheral.readValue(for: characteristic)
register3Characterisitc = characteristic
case CBUuidService.GetCharacteristicCBUuid(uuid: .SerialNumberUuid),
CBUuidService.GetCharacteristicCBUuid(uuid: .HoursOfOperationUuid),
CBUuidService.GetCharacteristicCBUuid(uuid: .VolcanoFirmwareVersionUuid),
CBUuidService.GetCharacteristicCBUuid(uuid: .BleFirmwareVersionUuid):
peripheral.readValue(for: characteristic)
default:
continue
}
}
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
if let val = characteristic.value{
if (characteristic.uuid == CBUuidService.GetCharacteristicCBUuid(uuid: .LEDbrightnessUuid)){
ledBrightness = Double(dataToUnsignedBytes8(value: val))
return
}
let normalizedValue = dataToUnsignedBytes16(value: val)
switch(characteristic.uuid){
case CBUuidService.GetCharacteristicCBUuid(uuid: .CurrentTemperatureUuid):
currentTemperature = normalizedValue
case CBUuidService.GetCharacteristicCBUuid(uuid: .WriteTemperatureUuid):
targetTemperature = Double(normalizedValue)
case CBUuidService.GetCharacteristicCBUuid(uuid: .Register1Uuid):
isFanOn = convertToggleCharacteristicToBool(value: normalizedValue, mask: masks.FanMask)
localIsFanOn = isFanOn
isHeatOn = convertToggleCharacteristicToBool(value: normalizedValue, mask: masks.HeatingMask)
localIsHeatOn = isHeatOn
case CBUuidService.GetCharacteristicCBUuid(uuid: .Register2Uuid):
isDisplayOnCooling = !convertToggleCharacteristicToBool(value: normalizedValue, mask: masks.DisplayOnCoolingMask)
isF = convertToggleCharacteristicToBool(value: normalizedValue, mask: masks.FahrenheitMask)
case CBUuidService.GetCharacteristicCBUuid(uuid: .Register3Uuid):
isPulseFanWhenVolcanoReachesTemperature = (normalizedValue & masks.VibrationOnMask) == 0;
case CBUuidService.GetCharacteristicCBUuid(uuid: .SerialNumberUuid),
CBUuidService.GetCharacteristicCBUuid(uuid: .VolcanoFirmwareVersionUuid):
let rawValue = String(decoding: val, as: UTF8.self)
let index = rawValue.index(rawValue.startIndex, offsetBy: 8)
let value = String(rawValue.prefix(upTo: index))
switch(characteristic.uuid){
case CBUuidService.GetCharacteristicCBUuid(uuid: .VolcanoFirmwareVersionUuid):
volcanoFirmwareVersion = value
case CBUuidService.GetCharacteristicCBUuid(uuid: .SerialNumberUuid):
serialNumber = value
default:
print("This should be impossible I can't believe you see this")
}
case CBUuidService.GetCharacteristicCBUuid(uuid: .BleFirmwareVersionUuid):
bleFirmwareVersion = String(decoding: val, as: UTF8.self)
case CBUuidService.GetCharacteristicCBUuid(uuid: .HoursOfOperationUuid):
hoursOfOperation = String(Double(normalizedValue) / 10)
case CBUuidService.GetCharacteristicCBUuid(uuid: .LEDbrightnessUuid):
ledBrightness = Double(normalizedValue)
default:
print ("Could not assign updated value of \(normalizedValue) and/or \(val) to internal state. \(characteristic.uuid) not found in switch")
}
}
}
func convertToUInt32BLE(val: Int) -> Data {
var rawData = Data.init()
rawData.append(contentsOf: [UInt8(val & 255)])
var tempValue = val >> 8
rawData.append(contentsOf: [UInt8(tempValue & 255)])
tempValue = tempValue >> 8
rawData.append(contentsOf: [UInt8(tempValue & 255)])
tempValue = tempValue >> 8
rawData.append(contentsOf: [UInt8(tempValue & 255)])
return rawData
}
func WriteTargetTemperature(tempInCelcius: Int){
if let characteristic = writeTemperatureCharacteristic {
let nextTemperature = Int(tempInCelcius / 10) * 10 //This is done to make the last digit a 0. The volcano uses the last digit for precision but that is causing some undesireable behavior in the early UI.
targetTemperature = Double(nextTemperature)
let payload = convertToUInt32BLE(val: nextTemperature)
volcano?.writeValue(payload, for: characteristic, type: .withResponse)
}
}
func TurnFanOff(){
volcano!.writeValue(Data([00]), for: fanOffCharacterisitc!, type: .withResponse)
localIsFanOn = false
}
func TurnFanOn(){
volcano!.writeValue(Data([00]), for: fanOnCharacterisitc!, type: .withResponse)
localIsFanOn = true
}
func ToggleFan(){
if let fanOn = localIsFanOn {
if fanOn{
TurnFanOff()
} else{
TurnFanOn()
}
}
}
func ToggleHeat(){
if let heatOn = localIsHeatOn {
if heatOn{
volcano!.writeValue(Data([00]), for: heatOffCharacterisitc!, type: .withResponse)
localIsHeatOn = false
} else{
volcano!.writeValue(Data([00]), for: heatOnCharacterisitc!, type: .withResponse)
localIsHeatOn = true
}
}
}
func TurnHeatOn(){
volcano!.writeValue(Data([00]), for: heatOnCharacterisitc!, type: .withResponse)
localIsHeatOn = true
}
func ToggleIsDisplayOnCooling(){
let nextMask = isDisplayOnCooling ? masks.DisplayOnCoolingMask : masks.DisplayOffCoolingMask
let buffer = convertToUInt32BLE(val: nextMask);
volcano!.writeValue(buffer, for: register2Characterisitc!, type: .withResponse)
}
func ToggleIsPulseFanWhenVolcanoReachesTemperature(){
let nextMask = isPulseFanWhenVolcanoReachesTemperature ? masks.VibrationOnMask : masks.VibrationOffMask
let buffer = convertToUInt32BLE(val: nextMask)
volcano!.writeValue(buffer, for: register3Characterisitc!, type: .withResponse)
}
func SetIsF(useF: Bool){
let mask = useF ? masks.FahrenheitMask : masks.CelciusMask
let buffer = convertToUInt32BLE(val: mask)
volcano!.writeValue(buffer, for: register2Characterisitc!, type: .withResponse)
}
func DisconnectVolcano(){
self.cbCentralManager.cancelPeripheralConnection(volcano!)
exit(0)
}
func cToF(temperatureInCelcius: Int) -> Int {
return Int((Double(temperatureInCelcius) * 1.8 + 32).rounded())
}
func setLedBrightness(){
var data = Data.init()
data.append(contentsOf: [UInt8(Int(ledBrightness))])
volcano?.writeValue(data, for: ledBrightnessCharacteristic!, type: .withResponse)
}
}