Last active
May 11, 2017 02:28
-
-
Save JoshuaSullivan/3b5ee005775842eb49ef3197b5673a58 to your computer and use it in GitHub Desktop.
Files related to my blog post about communicating over BLE using protocol buffers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
message Packet { | |
float time = 1; | |
sint32 rx = 2; | |
sint32 ry = 3; | |
sint32 rz = 4; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do { | |
let packet = try Packet(serializedData: data) | |
let date = Date(timeIntervalSinceReferenceDate: Double(packet.time)) | |
NSLog("[\(formatter.string(from: date))] x: \(packet.rx), y: \(packet.ry), z: \(packet.rz)") | |
} catch { | |
NSLog("Could not parse protobuf data: \(error.localizedDescription)") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var packet = Packet() | |
if let motionData = self.motionData { | |
packet.rx = Int32(motionData.attitude.pitch * rad2Deg) | |
packet.ry = Int32(motionData.attitude.yaw * rad2Deg) | |
packet.rz = Int32(motionData.attitude.roll * rad2Deg) | |
} | |
let now = Date() | |
packet.time = Float(now.timeIntervalSinceReferenceDate) | |
guard let data = try? packet.serializedData() else { | |
NSLog("Unable to create data from protocol buffer.") | |
return | |
} | |
// Write the data to the appropriate characteristic. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment