Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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