summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Redfern <tim@Adaptics.local>2013-10-11 17:46:32 -0700
committerTim Redfern <tim@Adaptics.local>2013-10-11 17:46:32 -0700
commite89f9947ae51eee20902432c4758b2d8e24185ea (patch)
tree3b4661b0277019e568648ce6ab44592494e552e7
parentcb75fd2a2f258a1a4e9f307840842f582838250b (diff)
talking to the deviceHEADmaster
-rwxr-xr-x.DS_Storebin6148 -> 6148 bytes
-rw-r--r--Waydio.h16
-rw-r--r--Waydio.m228
-rwxr-xr-xsampleclient/ConnectionController.h2
-rwxr-xr-xsampleclient/ConnectionController.m56
-rwxr-xr-xsampleclient/MainStoryboard.storyboard27
-rw-r--r--waydio_comms_demo.xcodeproj/project.xcworkspace/xcuserdata/tim.xcuserdatad/UserInterfaceState.xcuserstatebin41393 -> 53532 bytes
-rw-r--r--waydio_comms_demo.xcodeproj/xcuserdata/tim.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist17
8 files changed, 314 insertions, 32 deletions
diff --git a/.DS_Store b/.DS_Store
index cd8be86..085faa4 100755
--- a/.DS_Store
+++ b/.DS_Store
Binary files differ
diff --git a/Waydio.h b/Waydio.h
index 33dbecc..346d687 100644
--- a/Waydio.h
+++ b/Waydio.h
@@ -17,6 +17,7 @@
#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
+#import "AppDelegate.h"
#import "Brsp.h"
@class Waydio;
@@ -63,9 +64,16 @@
/**
Class used to interact with the Waydio peripheral
*/
-@interface Waydio : NSObject <CBPeripheralDelegate> {
+@interface Waydio : NSObject <CBPeripheralDelegate, BrspDelegate, CBCentralManagerDelegate> {
id <WaydioDelegate> 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 <UITextFieldDelegate, WaydioDelegate, CBCentralManagerDelegate> {
+@interface ConnectionController : UIViewController <UITextFieldDelegate, WaydioDelegate> {
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 @@
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
- <action selector="getSettings:" destination="6Co-CY-QOi" eventType="touchUpInside" id="ul8-L4-zrT"/>
+ <action selector="disconnectButton:" destination="6Co-CY-QOi" eventType="touchUpInside" id="F4J-bI-SgR"/>
</connections>
</button>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" indicatorStyle="black" editable="NO" id="cGV-n4-9ji" userLabel="textView">
@@ -159,7 +159,6 @@
<connections>
<outlet property="batteryLabel" destination="5km-Vi-7hx" id="zkS-Uk-m5d"/>
<outlet property="batteryReading" destination="ydm-ls-dqM" id="FbC-49-5sj"/>
- <outlet property="buttonGetSettings" destination="ecM-M5-w4n" id="V6s-Cg-nxq"/>
<outlet property="buttonReading" destination="Ea1-A6-mdk" id="vab-Ze-c7Z"/>
<outlet property="disconnectButton" destination="ecM-M5-w4n" id="xbP-mt-QDZ"/>
<outlet property="textView" destination="cGV-n4-9ji" id="7k2-1Y-0W7"/>
@@ -187,6 +186,30 @@
<point key="canvasLocation" x="33" y="-143"/>
</scene>
</scenes>
+ <classes>
+ <class className="ConnectionController" superclassName="UIViewController">
+ <source key="sourceIdentifier" type="project" relativePath="./Classes/ConnectionController.h"/>
+ <relationships>
+ <relationship kind="action" name="disconnectButton:"/>
+ <relationship kind="outlet" name="batteryLabel" candidateClass="UILabel"/>
+ <relationship kind="outlet" name="batteryReading" candidateClass="UILabel"/>
+ <relationship kind="outlet" name="buttonReading" candidateClass="UILabel"/>
+ <relationship kind="outlet" name="disconnectButton" candidateClass="UIButton"/>
+ <relationship kind="outlet" name="textView" candidateClass="UITextView"/>
+ <relationship kind="outlet" name="weightLabel" candidateClass="UILabel"/>
+ <relationship kind="outlet" name="weightReading" candidateClass="UILabel"/>
+ </relationships>
+ </class>
+ <class className="ScanController" superclassName="UIViewController">
+ <source key="sourceIdentifier" type="project" relativePath="./Classes/ScanController.h"/>
+ <relationships>
+ <relationship kind="action" name="startScanButton:"/>
+ <relationship kind="action" name="stopScanButton:"/>
+ <relationship kind="outlet" name="_deviceTableView" candidateClass="UITableView"/>
+ <relationship kind="outlet" name="_scanButton" candidateClass="UIButton"/>
+ </relationships>
+ </class>
+ </classes>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
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
--- a/waydio_comms_demo.xcodeproj/project.xcworkspace/xcuserdata/tim.xcuserdatad/UserInterfaceState.xcuserstate
+++ b/waydio_comms_demo.xcodeproj/project.xcworkspace/xcuserdata/tim.xcuserdatad/UserInterfaceState.xcuserstate
Binary files 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">
+ </FileBreakpoint>
+ <FileBreakpoint
+ shouldBeEnabled = "Yes"
+ ignoreCount = "0"
+ continueAfterRunningActions = "No"
+ filePath = "Waydio.m"
+ timestampString = "403230070.567564"
+ startingColumnNumber = "9223372036854775807"
+ endingColumnNumber = "9223372036854775807"
+ startingLineNumber = "99"
+ endingLineNumber = "99"
+ landmarkName = "-brspDataReceived:"
+ landmarkType = "5">
</FileBreakpoint>
</FileBreakpoints>
</Bucket>