Skip to content

Instantly share code, notes, and snippets.

@drewB
Forked from anonymous/gist:748f0c95eb5fe4bafbb6
Last active February 22, 2016 16:27
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 drewB/85a0befb2ad9b4fc75ad to your computer and use it in GitHub Desktop.
Save drewB/85a0befb2ad9b4fc75ad to your computer and use it in GitHub Desktop.
Metawear: example of how to persisting event after disconnects
import Foundation
class MetawearConfig:NSObject, MBLRestorable {
var pulseWidthEvent:MBLEvent!
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.pulseWidthEvent, forKey: "pulseWidthEvent")
}
required init(coder: NSCoder) {
super.init()
self.pulseWidthEvent = coder.decodeObjectForKey("pulseWidthEvent") as? MBLEvent
}
override init () {
super.init()
}
func runOnDeviceBoot(device: MBLMetaWear) {
// Log some Accelerometer data
device.accelerometer.fullScaleRange = MBLAccelerometerRange.Range2G
device.accelerometer.filterCutoffFreq = 0
device.accelerometer.highPassFilter = true;
var periodicEvent = device.accelerometer.rmsDataReadyEvent.periodicSampleOfEvent(1000)
self.pulseWidthEvent = periodicEvent.pulseDetectorOfEventWithThreshold(30, width: 3, output: MBLPulseOutput.Width)
self.pulseWidthEvent.startLogging()
println("Logging Started")
}
}
// Config code
self.device.connectWithHandler({ error in
let configuration : MetawearConfig = self.device.configuration as MetawearConfig
if (configuration.pulseWidthEvent == nil) {
self.device.setConfiguration(MetawearConfig()) { error in
if (error != nil) {
println("setConfiguration error: \(error.localizedDescription)")
}
println("set Configuration successful")
}
}
}
@typarker
Copy link

Thanks for this example, I'm also having issues messing with Metawear with Swift. Do you have any idea how to erase all commands for all events? In the Android documentation there's something called removeMacros() but it seems like eraseCommandsToRunOnEventAsync has to be attached to a specific event to remove it.

I'm trying to let the user set a threshold level on the fly and have it update the Metawear to alert them with vibration whenever the threshold gets crossed, even when the device is offline. Right now it seems like it's just stacking multiple commands on top of each other. My email is tysparker@gmail.com if you have any suggestions.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment