Skip to content

Instantly share code, notes, and snippets.

@AminPlusPlus
Created July 9, 2020 02:28
Show Gist options
  • Save AminPlusPlus/e23def8f8aa80f668b2239fe87ce96d7 to your computer and use it in GitHub Desktop.
Save AminPlusPlus/e23def8f8aa80f668b2239fe87ce96d7 to your computer and use it in GitHub Desktop.
WriteValue
fileprivate var ledMask: UInt8 = 0
fileprivate let digitalBits = 2
func setDigitalOutput(_ index: Int, on: Bool, characteristic :CBCharacteristic) {
let shift = UInt(index) * UInt(digitalBits)
var mask = ledMask
if on {
mask = mask | UInt8(1 << shift)
}
else {
mask = mask & ~UInt8(1 << shift)
}
let data = Data(bytes: [mask])
self.peripheral?.writeValue(data, for: characteristic, type: .withResponse)
ledMask = mask
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment