summaryrefslogtreecommitdiff
path: root/sampleclient/ConnectionController.m
blob: c14ca978825e568b3658af7409a1b1b73c44bb77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//
//  ConnectionController.m
//  sampleterm
//
//  Created by Michael Testa on 11/1/12.
//  Copyright (c) Blueradios, Inc. All rights reserved.
//

#import "ConnectionController.h"

//Make this number larger or smaller to see more or less output in the textview
#define MAX_TEXT_VIEW_CHARACTERS 800

@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 {
    
}
/**
 Called when the measured weight changes
 @param waydio    The Waydio object generating this event
 @param weight
 */
- (void)waydio:(Waydio*)waydio WeightChanged:(Float32)weight {
    
}
/**
 Called when the battery level changes
 @param waydio    The Waydio object generating this event
 @param batteryPercent
 */
- (void)waydio:(Waydio*)waydio BatteryChanged:(Float32)batteryPercent {
    
}
/**
 Called when the isSending changes
 @param waydio    The Waydio object generating this event
 @param isOpen  YES == Pressed
 */
- (void)waydio:(Waydio*)waydio ButtonChanged:(BOOL)isPressed {
    
}
/*
#pragma mark CBCentralManagerDelegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
    
}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
    //call the open function to prepare the brsp service
    [self.waydioObject open];
}

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    [self.waydioObject close];
    [self.navigationController popViewControllerAnimated:YES];
}
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = [AppDelegate app].activePeripheral.name;
    [_inputText setDelegate:self];
    _allCommands = [NSMutableArray new];
    [self loadCommandArray];
    //_lastMode = BrspModeData; //Default brsp mode
}

-(void)loadCommandArray {
    _allCommands = [NSArray arrayWithObjects:
                    @"ATMT?",
                    @"ATV?",
                    @"ATA?",
                    @"ATSN?",
                    @"ATSZ?",
                    @"ATSFC?",
                    @"ATSCL?",
                    @"ATSRM?",
                    @"ATSDIF?",
                    @"ATSPL?",
                    @"ATSUART?",
                    @"ATSPIO?,0",
                    @"ATSPIO?,1",
                    @"ATSPIO?,2",
                    @"ATSPIO?,3",
                    @"ATSPIO?,4",
                    @"ATSPIO?,5",
                    @"ATSPIO?,6",
                    @"ATSPIO?,7",
                    @"ATSPIO?,8",
                    @"ATSPIO?,9",
                    @"ATSPIO?,10",
                    @"ATSPIO?,11",
                    @"ATSPIO?,12",
                    @"ATSPIO?,13",
                    @"ATSPIO?,14",
                    @"ATSLED?,0",
                    @"ATSLED?,1",
                    @"ATSSP?",
                    @"ATSPK?",
                    @"ATSDBLE?",
                    @"ATSBRSP?",
                    @"ATSDSLE?",
                    @"ATSDSTLE?",
                    @"ATSDILE?",
                    @"ATSDITLE?",
                    @"ATSDMTLE?",
                    @"ATSDCP?",
                    @"ATSPLE?",
                    //D2 Modules only
//                    @"ATS?",
//                    @"ATLCA?",
//                    @"ATSP?",
//                    @"ATSCOD?",
                    nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    //[self disableButtons];
    //-------->[AppDelegate app].cbCentral.delegate = self;
    
    //init the object with default buffer sizes of 1024 bytes
    //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];
    _outputText = [NSMutableString stringWithCapacity:MAX_TEXT_VIEW_CHARACTERS];
    [super viewWillAppear:animated];

    //_presses=0;
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)viewWillDisappear:(BOOL)animated {
    //call close to disable notifications etc (Not required)
    [waydioObject close];
    //Use CBCentralManager to close the connection to this peripheral
    //[[AppDelegate app].cbCentral cancelPeripheralConnection:[AppDelegate app].activePeripheral];
	[super viewWillDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    switch(interfaceOrientation)
    {
        case UIInterfaceOrientationLandscapeLeft:
            return NO;
        case UIInterfaceOrientationLandscapeRight:
            return NO;
        default:
            return YES;
    }
}

#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 {
    /*
    //Save the brsp mode so it can be switched back when this process is complete
    _lastMode = self.brspObject.brspMode;
    if (brspObject.brspMode != BrspModeData)
        [self.brspObject changeBrspMode:BrspModeData]; //change brsp mode to data
    for (int i=1; i <= 10; i++) {
        //Write numbers 1-10 to the device
        NSError *error = [self.brspObject writeString:[NSString stringWithFormat:@"%i\r\n", i%10]];
        if (error)
            NSLog(@"%@", error.description);
    }
     */
    NSLog(@"disconnecting");
}

@end