Skip to content

Instantly share code, notes, and snippets.

@JoshuaSullivan
Last active May 11, 2017 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshuaSullivan/3b5ee005775842eb49ef3197b5673a58 to your computer and use it in GitHub Desktop.
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.
syntax = "proto3";
message Packet {
float time = 1;
sint32 rx = 2;
sint32 ry = 3;
sint32 rz = 4;
}
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)")
}
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