From e89f9947ae51eee20902432c4758b2d8e24185ea Mon Sep 17 00:00:00 2001 From: Tim Redfern Date: Fri, 11 Oct 2013 17:46:32 -0700 Subject: talking to the device --- .DS_Store | Bin 6148 -> 6148 bytes Waydio.h | 16 +- Waydio.m | 228 +++++++++++++++++++-- sampleclient/ConnectionController.h | 2 +- sampleclient/ConnectionController.m | 56 ++++- sampleclient/MainStoryboard.storyboard | 27 ++- .../tim.xcuserdatad/UserInterfaceState.xcuserstate | Bin 41393 -> 53532 bytes .../xcdebugger/Breakpoints.xcbkptlist | 17 +- 8 files changed, 314 insertions(+), 32 deletions(-) diff --git a/.DS_Store b/.DS_Store index cd8be86..085faa4 100755 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Waydio.h b/Waydio.h index 33dbecc..346d687 100644 --- a/Waydio.h +++ b/Waydio.h @@ -17,6 +17,7 @@ #import #import +#import "AppDelegate.h" #import "Brsp.h" @class Waydio; @@ -63,9 +64,16 @@ /** Class used to interact with the Waydio peripheral */ -@interface Waydio : NSObject { +@interface Waydio : NSObject { id delegate; + BrspMode _lastMode; + NSTimer * weighTimer; + NSTimer * batteryTimer; + NSTimer * buttonTimer; + int command; + bool button; + int battery; } /** @@ -124,7 +132,10 @@ /** Opens a Waydio connection. (Prepares peipheral for using the Waydio service, characteristics, and notifications) */ -- (void)open; +//- (void)open; + +- (void)sendCommand:(NSString *)str; + /** Closes a Waydio connection. (Turns off notifications, etc) @@ -132,5 +143,4 @@ - (void)close; - @end diff --git a/Waydio.m b/Waydio.m index 161aaee..a2d43ff 100644 --- a/Waydio.m +++ b/Waydio.m @@ -12,8 +12,177 @@ @synthesize delegate; +@synthesize brspObject; + +#pragma mark BrspDelegate + +- (void)brsp:(Brsp*)brsp OpenStatusChanged:(BOOL)isOpen { + // NSLog(@"OpenStatusChanged == %d", isOpen); + if (isOpen) { + //The BRSP object is ready to be used + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; + //Print the security level of the brsp service to console + NSLog(@"BRSP Security Level is %d", brspObject.securityLevel); + + + /* + weighTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self + selector:@selector(checkWeight) userInfo:nil repeats:YES]; + batteryTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self + selector:@selector(checkBattery) userInfo:nil repeats:YES]; + buttonTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self + selector:@selector(checkButton) userInfo:nil repeats:YES]; + */ + button=false; + command=3; + [self checkButton]; + + } else { + //brsp object has been closed + /* + [weighTimer invalidate]; + [batteryTimer invalidate]; + [buttonTimer invalidate]; + */ + } +} +- (void)brsp:(Brsp*)brsp SendingStatusChanged:(BOOL)isSending { + //This is a good place to change BRSP mode + //If we are on the last command in the queue and we are no longer sending, change the mode back to previous value + if (isSending == NO) + { + if (_lastMode == brspObject.brspMode) + return; //Nothing to do here + //Change mode back to previous setting + NSError *error = [brspObject changeBrspMode:_lastMode]; + if (error) + NSLog(@"%@", error); + } +} +- (void)brspDataReceived:(Brsp*)brsp { + //If there are items in the _commandQueue array, assume this data is part of a command response + + /* + if (_commandQueue.count > 0) + { + //The data incomming is in response to a sent command. + NSString *response = [self parseFullCommandResponse]; + if (!response) + return; //Buffer doesn't contain a full command reponse yet; + + NSString *responseData = [self parseCommandData:response]; + [self outputCommandWithResponse:responseData]; + + [_commandQueue removeObjectAtIndex:0]; //Remove last sent command from our queue array + //Remove the full response from the brsp input buffer + [brspObject flushInputBuffer:response.length]; + + if (_commandQueue.count > 0) + //Send the next command + [self sendCommand:[_commandQueue objectAtIndex:0]]; + else + { + //Done sending commands... + [self enableButtons]; + //Print a footer + [self outputToScreen:@"_________________________________________"]; + } + } + else + { + //The data comming in is not from a sent command + //Just output the response to screen and remove from the input buffer using a readString + [self outputToScreen:[brspObject readString]]; + } + */ + + + [NSThread sleepForTimeInterval:0.5]; + bool isOn=false; + int batt=0; + NSArray *array = [[brspObject readString] componentsSeparatedByString:@"\r\n"]; + if (array && array.count > 1) { + NSString *str=[array objectAtIndex:1]; + NSLog(@": %@",str); + if (str.length>1){ + NSLog(@":: %@",[[array objectAtIndex:1] substringToIndex:1]); + if(![[str substringToIndex:1]isEqualToString:@"OK"]){ + switch (command) { + case 1: + NSLog(@"Weight %@",str); + [self checkBattery]; + break; + case 2: + batt= [str intValue]; + if (batt!=battery) { + NSLog(@"Battery %@",str); + battery=batt; + } + [self checkButton]; + break; + case 3: + if (str.length>1) { + NSString *val=[[str substringFromIndex:str.length-1] substringToIndex:1]; + isOn= [val isEqualToString:@"1"]; + } + if (isOn!=button){ + NSLog(@"Button %@",isOn?@"on":@"off"); + button=isOn; + } + [self checkWeight]; + break; + } + } + + } + //NSLog(@"Weight %@",str); + } + + +} +- (void)brsp:(Brsp*)brsp ErrorReceived:(NSError*)error { + NSLog(@"%@", error.description); +} +- (void)brspModeChanged:(Brsp*)brsp BRSPMode:(BrspMode)mode { + // NSLog(@"BRSP Mode changed to %d", mode); + switch (mode) { + case BrspModeData: + //[self.buttonChangeMode setTitle:[NSString stringWithFormat:@"Data"] forState:UIControlStateNormal]; + break; + case BrspModeRemoteCommand: + //[self.buttonChangeMode setTitle:[NSString stringWithFormat:@"Command"] forState:UIControlStateNormal]; + break; + + default: + break; + } +} + +#pragma mark CBCentralManagerDelegate +- (void)centralManagerDidUpdateState:(CBCentralManager *)central { + +} + +- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { + //call the open function to prepare the brsp service + [self.brspObject open]; +} + +- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { + [self.brspObject close]; + //[self.navigationController popViewControllerAnimated:YES]; + // + // + //!!!!!!deal with this somehow- need to indicate to the connectioncontroller that the device disconnected +} - (id)initWithPeripheral:(CBPeripheral*)peripheral { + + [AppDelegate app].cbCentral.delegate = self; + + self.brspObject = [[Brsp alloc] initWithPeripheral:[AppDelegate app].activePeripheral InputBufferSize:512 OutputBufferSize:512]; + self.brspObject.delegate = self; + [[AppDelegate app].cbCentral connectPeripheral:[AppDelegate app].activePeripheral options:nil]; return self; } @@ -29,7 +198,7 @@ @note The default input and output buffer sizes are 1024 bytes */ - (id)initWithPeripheral:(CBPeripheral*)peripheral InputBufferSize:(NSUInteger)in_size OutputBufferSize:(NSUInteger)out_size { - // + self.brspObject = [[Brsp alloc] initWithPeripheral:[AppDelegate app].activePeripheral]; return self; } @@ -38,35 +207,60 @@ } -/** - Opens a Waydio connection. (Prepares peipheral for using the Waydio service, characteristics, and notifications) +/* + // Opens a Waydio connection. (Prepares peipheral for using the Waydio service, characteristics, and notifications) + + - (void)open { + self.brspObject.delegate = self; + [self.brspObject open]; + //[[self delegate] OpenStatusChanged:YES]; + + }; + */ -- (void)open { - [self.brspObject open]; - //[[self delegate] OpenStatusChanged:YES]; - [NSTimer scheduledTimerWithTimeInterval:0.1 target:self - selector:@selector(checkWeight) userInfo:nil repeats:YES]; - [NSTimer scheduledTimerWithTimeInterval:1.0 target:self - selector:@selector(checkBattery) userInfo:nil repeats:YES]; - [NSTimer scheduledTimerWithTimeInterval:0.1 target:self - selector:@selector(checkButton) userInfo:nil repeats:YES]; -}; - (void)checkWeight { + //NSLog(@"checking weight"); + [self sendCommand:@"ATADC?,0"]; + command=1; } - (void)checkBattery { + //NSLog(@"checking battery"); + [self sendCommand:@"ATBL?"]; + command=2; } - (void)checkButton { + //NSLog(@"checking button"); + [self sendCommand:@"ATSPIO?,3"]; + command=3; } -/** - Closes a Waydio connection. (Turns off notifications, etc) - */ +-(void)sendCommand:(NSString *)str { + + _lastMode = self.brspObject.brspMode; + if (brspObject.brspMode != BrspModeRemoteCommand) { + //Switch to remote command mode + NSError *modeChangeError = [brspObject changeBrspMode:BrspModeRemoteCommand]; + if (modeChangeError) + NSLog(@"%@", modeChangeError); + } + + if (![[str substringFromIndex:str.length-1] isEqualToString:@"\r"]) + str = [NSString stringWithFormat:@"%@\r", str]; //Append a carriage return + //Write as string + NSError *writeError = [self.brspObject writeString:str]; + if (writeError) + NSLog(@"%@: Waydio", writeError.description); +} + + +// Closes a Waydio connection. (Turns off notifications, etc) + - (void)close { - [self.brspObject close]; + [[AppDelegate app].cbCentral cancelPeripheralConnection:[AppDelegate app].activePeripheral]; } @end \ No newline at end of file diff --git a/sampleclient/ConnectionController.h b/sampleclient/ConnectionController.h index d8a7755..31e217d 100755 --- a/sampleclient/ConnectionController.h +++ b/sampleclient/ConnectionController.h @@ -10,7 +10,7 @@ #import "AppDelegate.h" #import "Waydio.h" -@interface ConnectionController : UIViewController { +@interface ConnectionController : UIViewController { UITextField *_inputText; NSMutableString *_outputText; //used as string data for textView to make string concatenations more efficient NSArray *_allCommands; //All commands sent by the get settings button diff --git a/sampleclient/ConnectionController.m b/sampleclient/ConnectionController.m index b56f898..c14ca97 100755 --- a/sampleclient/ConnectionController.m +++ b/sampleclient/ConnectionController.m @@ -13,6 +13,13 @@ @implementation ConnectionController +@synthesize textView; +//@synthesize inputText = _inputText; +//@synthesize buttonChangeMode; +//@synthesize buttonSend100; +//@synthesize buttonGetSettings; +@synthesize waydioObject; + #pragma mark waydioDelegate - (void)waydio:(Waydio*)waydio OpenStatusChanged:(BOOL)isOpen { @@ -42,7 +49,7 @@ - (void)waydio:(Waydio*)waydio ButtonChanged:(BOOL)isPressed { } - +/* #pragma mark CBCentralManagerDelegate - (void)centralManagerDidUpdateState:(CBCentralManager *)central { @@ -57,6 +64,7 @@ [self.waydioObject close]; [self.navigationController popViewControllerAnimated:YES]; } + */ - (void)viewDidLoad { [super viewDidLoad]; @@ -119,17 +127,20 @@ - (void)viewWillAppear:(BOOL)animated { [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; //[self disableButtons]; - [AppDelegate app].cbCentral.delegate = self; + //-------->[AppDelegate app].cbCentral.delegate = self; //init the object with default buffer sizes of 1024 bytes -// self.brspObject = [[Brsp alloc] initWithPeripheral:[AppDelegate app].activePeripheral]; + //self.brspObject = [[Brsp alloc] initWithPeripheral:[AppDelegate app].activePeripheral]; //init with custom buffer sizes //self.brspObject = [[Brsp alloc] initWithPeripheral:[AppDelegate app].activePeripheral InputBufferSize:512 OutputBufferSize:512]; + self.waydioObject = [[Waydio alloc] initWithPeripheral:[AppDelegate app].activePeripheral]; + //[self.waydioObject open]; + //It is important to set this delegate before calling [Brsp open] //self.brspObject.delegate = self; //Use CBCentral Manager to connect this peripheral - [[AppDelegate app].cbCentral connectPeripheral:[AppDelegate app].activePeripheral options:nil]; + //--------->[[AppDelegate app].cbCentral connectPeripheral:[AppDelegate app].activePeripheral options:nil]; _outputText = [NSMutableString stringWithCapacity:MAX_TEXT_VIEW_CHARACTERS]; [super viewWillAppear:animated]; @@ -142,9 +153,9 @@ - (void)viewWillDisappear:(BOOL)animated { //call close to disable notifications etc (Not required) - //[brspObject close]; + [waydioObject close]; //Use CBCentralManager to close the connection to this peripheral - [[AppDelegate app].cbCentral cancelPeripheralConnection:[AppDelegate app].activePeripheral]; + //[[AppDelegate app].cbCentral cancelPeripheralConnection:[AppDelegate app].activePeripheral]; [super viewWillDisappear:animated]; } @@ -160,6 +171,38 @@ } } +#pragma mark - UITextFieldDelegate methods +-(BOOL) textFieldShouldReturn:(UITextField *)textField { + //Write whatever user typed in textfield to brsp peripheral + //NSError *error = [self.brspObject writeString:[NSString stringWithFormat:@"%@\r", textField.text]]; + //if (error) + /// NSLog(@"%@", error.description); + //textField.text = @""; + //[textField resignFirstResponder]; + return YES; +} + +- (void)textFieldDidBeginEditing:(UITextField *)textField { + [self animateTextField:textField up: YES]; +} + +- (void)textFieldDidEndEditing:(UITextField *)textField { + [self animateTextField:textField up: NO]; +} + +- (void) animateTextField:(UITextField*)textField up:(BOOL)up { + const int movementDistance = 170; //will only work on iPhones + const float movementDuration = 0.3f; // + + int movement = (up ? -movementDistance : movementDistance); + + [UIView beginAnimations: @"anim" context: nil]; + [UIView setAnimationBeginsFromCurrentState: YES]; + [UIView setAnimationDuration: movementDuration]; + self.view.frame = CGRectOffset(self.view.frame, 0, movement); + [UIView commitAnimations]; +} + #pragma mark - UI - (IBAction)disconnectButton:(id)sender { @@ -175,6 +218,7 @@ NSLog(@"%@", error.description); } */ + NSLog(@"disconnecting"); } @end diff --git a/sampleclient/MainStoryboard.storyboard b/sampleclient/MainStoryboard.storyboard index 908994a..2ced530 100755 --- a/sampleclient/MainStoryboard.storyboard +++ b/sampleclient/MainStoryboard.storyboard @@ -98,7 +98,7 @@ - + @@ -159,7 +159,6 @@ - @@ -187,6 +186,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/waydio_comms_demo.xcodeproj/project.xcworkspace/xcuserdata/tim.xcuserdatad/UserInterfaceState.xcuserstate b/waydio_comms_demo.xcodeproj/project.xcworkspace/xcuserdata/tim.xcuserdatad/UserInterfaceState.xcuserstate index cde4f07..ea2ace8 100644 Binary files a/waydio_comms_demo.xcodeproj/project.xcworkspace/xcuserdata/tim.xcuserdatad/UserInterfaceState.xcuserstate and b/waydio_comms_demo.xcodeproj/project.xcworkspace/xcuserdata/tim.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/waydio_comms_demo.xcodeproj/xcuserdata/tim.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist b/waydio_comms_demo.xcodeproj/xcuserdata/tim.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist index ae3d289..85feb58 100644 --- a/waydio_comms_demo.xcodeproj/xcuserdata/tim.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist +++ b/waydio_comms_demo.xcodeproj/xcuserdata/tim.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist @@ -12,9 +12,20 @@ startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "50" - endingLineNumber = "50" - landmarkName = "@interface ConnectionController" - landmarkType = "2"> + endingLineNumber = "50"> + + -- cgit v1.2.3