summaryrefslogtreecommitdiff
path: root/Waydio.m
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 /Waydio.m
parentcb75fd2a2f258a1a4e9f307840842f582838250b (diff)
talking to the deviceHEADmaster
Diffstat (limited to 'Waydio.m')
-rw-r--r--Waydio.m228
1 files changed, 211 insertions, 17 deletions
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