Custom button press when connected vs disconnected
Hi Mbient Community,
How can you program a Metawear device to simply flash a color (eg green) when the button is pressed WHILE CONNECTED and then switch this button press behavior so that the button press triggers a different color flash (eg red) WHILE in disconnected state? I've tried several approaches that I will include in replies to this post (my current post is over the char limit)
Thanks,
Russell
This discussion has been closed.
Comments
For 3), use setLEDColorAsnyc with NO
Regarding hypothesis 1, you are correct. Here is a expert from the documentation of programCommandsToRunOnEventAsync:
@warning THE BLOCK IS ONLY EXECUTED ONCE DURNING THIS CALL AND
NEVER AGAIN, DON'T ATTEMPT TO USE CALLBACKS INSIDE THIS BLOCK
Regarding hypothesis 2, there is an explanation in the documentation for disconnectionEvent:
Event representing a BLE disconnection event. Note this doesn't make sense to
stream, but it's likely that programCommandsToRunOnEventAsync will have utility.
Basically what is saying is that once you are disconnected you can no longer communicate with the MetaWear (thus no streaming). If you would observe the BFTasks returned by startNotificationsWithHandler: you would see an error.
I like to think about the problem backwards starting with the end:
For more specific details see the code snippets below:
#import <MetaWear/MetaWear.h>
@interface ButtonConfig : NSObject <MBLRestorable>
@property (nonatomic) MBLDataSwitch *disconnectedSwitchEvent;
@property (nonatomic) MBLDataSwitch *connectedSwitchEvent;
- (void)didConnect;
@end
@implementation ButtonConfig
- (void)runOnDeviceBoot:(MBLMetaWear *)device
{
// Pass the switch event through a gate that only opens when we are disconnected
self.disconnectedSwitchEvent = [device.mechanicalSwitch.switchUpdateEvent conditionalDataSwitch:NO];
// When we get a disconnected switch event then flash red
[[[[[self.disconnectedSwitchEvent programCommandsToRunOnEventAsync:^{
[device.led flashLEDColorAsync:[UIColor redColor] withIntensity:1.0 numberOfFlashes:2];
}] continueOnDispatchWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
// Pass the switch event through a gate that only opens when we are connected
self.connectedSwitchEvent = [device.mechanicalSwitch.switchUpdateEvent conditionalDataSwitch:YES];
// When we get a connected switch event then flash green
return [self.connectedSwitchEvent programCommandsToRunOnEventAsync:^{
[device.led flashLEDColorAsync:[UIColor greenColor] withIntensity:1.0 numberOfFlashes:2];
}];
}] continueOnDispatchWithSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
// When a disconnect event occurs, we open program the gate change
return [device.settings.disconnectEvent programCommandsToRunOnEventAsync:^{
[self.disconnectedSwitchEvent resetConditionalAsync:YES];
[self.connectedSwitchEvent resetConditionalAsync:NO];
}];
}] success:^(id _Nonnull result) {
NSLog(@Success);
}] failure:^(NSError * _Nonnull error) {
NSLog(@Fail: %@", error);
}];
}
// Manually reset the gates when we re-connect
- (void)didConnect
{
[self.disconnectedSwitchEvent resetConditionalAsync:NO];
[self.connectedSwitchEvent resetConditionalAsync:YES];
}
@end
// Here is an example unit test
- (void)testButton
{
XCTestExpectation *waitingExpectation = [self expectationWithDescription:@pause for manual verification];
[self.device setConfiguration:[[ButtonConfig alloc] init] handler:^(NSError * _Nullable error) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.device disconnectWithHandler:^(NSError * _Nullable error) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.device connectWithHandler:^(NSError * _Nullable error) {
ButtonConfig *config = self.device.configuration;
[config didConnect];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.device setConfiguration:nil handler:^(NSError * _Nullable error) {
[waitingExpectation fulfill];
}];
});
}];
});
}];
});
}];
[self waitForExpectationsWithTimeout:180 handler:nil];
}