Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
805 | - | 1 | // /////////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) 2010, Frank Blumenberg |
||
3 | // |
||
4 | // See License.txt for complete licensing and attribution information. |
||
5 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
||
6 | // of this software and associated documentation files (the "Software"), to deal |
||
7 | // in the Software without restriction, including without limitation the rights |
||
8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
9 | // copies of the Software, and to permit persons to whom the Software is |
||
10 | // furnished to do so, subject to the following conditions: |
||
11 | // |
||
12 | // The above copyright notice and this permission notice shall be included in all |
||
13 | // copies or substantial portions of the Software. |
||
14 | // |
||
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
||
21 | // THE SOFTWARE. |
||
22 | // |
||
23 | // /////////////////////////////////////////////////////////////////////////////// |
||
24 | |||
25 | #import "LcdViewController.h" |
||
26 | #import "MKConnectionController.h" |
||
27 | #import "NSData+MKCommandEncode.h" |
||
28 | #import "MKDatatypes.h" |
||
29 | #import "MKDataConstants.h" |
||
30 | |||
31 | @implementation LcdViewController |
||
32 | |||
33 | @synthesize label; |
||
34 | @synthesize segment; |
||
35 | @synthesize gestureStartPoint; |
||
36 | |||
37 | - (void) viewDidLoad { |
||
38 | label.text = @"Not connected\r\nNo data\r\n\r\n"; |
||
39 | lcdCount = 0; |
||
40 | [super viewDidLoad]; |
||
41 | } |
||
42 | |||
43 | #pragma mark - |
||
44 | #pragma mark UIViewController delegate methods |
||
45 | |||
46 | |||
47 | |||
48 | - (void) updateSegment { |
||
49 | |||
50 | if ([[MKConnectionController sharedMKConnectionController] hasNaviCtrl]) { |
||
51 | [segment setEnabled:YES forSegmentAtIndex:0]; |
||
52 | [segment setEnabled:YES forSegmentAtIndex:1]; |
||
53 | [segment setEnabled:YES forSegmentAtIndex:2]; |
||
54 | } |
||
55 | else { |
||
56 | [segment setEnabled:NO forSegmentAtIndex:0]; |
||
57 | [segment setEnabled:YES forSegmentAtIndex:1]; |
||
58 | [segment setEnabled:NO forSegmentAtIndex:2]; |
||
59 | } |
||
60 | |||
61 | MKAddress currentDevice=[MKConnectionController sharedMKConnectionController].currentDevice; |
||
62 | switch (currentDevice) { |
||
63 | case MKAddressNC: |
||
64 | segment.selectedSegmentIndex=0; |
||
65 | break; |
||
66 | case MKAddressFC: |
||
67 | segment.selectedSegmentIndex=1; |
||
68 | break; |
||
69 | case MKAddressMK3MAg: |
||
70 | segment.selectedSegmentIndex=2; |
||
71 | break; |
||
72 | default: |
||
73 | break; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | // called after this controller's view will appear |
||
78 | - (void)viewWillAppear:(BOOL)animated |
||
79 | { |
||
80 | [self.navigationController setToolbarHidden:YES animated:NO]; |
||
81 | |||
82 | // for aesthetic reasons (the background is black), make the nav bar black for this particular page |
||
83 | self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque; |
||
84 | |||
85 | // match the status bar with the nav bar |
||
86 | [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque; |
||
87 | |||
88 | [[MKConnectionController sharedMKConnectionController] activateNaviCtrl]; |
||
89 | [self updateSegment]; |
||
90 | } |
||
91 | |||
92 | - (void) viewDidAppear:(BOOL)animated { |
||
93 | |||
94 | canProcessNextGesture=YES; |
||
95 | |||
96 | // for aesthetic reasons (the background is black), make the nav bar black for this particular page |
||
97 | self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque; |
||
98 | |||
99 | // match the status bar with the nav bar |
||
100 | [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleBlackOpaque; |
||
101 | |||
102 | NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; |
||
103 | |||
104 | [nc addObserver:self |
||
105 | selector:@selector(lcdNotification:) |
||
106 | name:MKLcdNotification |
||
107 | object:nil]; |
||
108 | |||
109 | [self performSelector:@selector(sendMenuRefreshRequest) withObject:self afterDelay:0.1]; |
||
110 | } |
||
111 | |||
112 | - (void) viewWillDisappear:(BOOL)animated { |
||
113 | NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; |
||
114 | |||
115 | [nc removeObserver:self]; |
||
116 | |||
117 | // restore the nav bar and status bar color to default |
||
118 | self.navigationController.navigationBar.barStyle = UIBarStyleDefault; |
||
119 | [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; |
||
120 | } |
||
121 | |||
122 | |||
123 | - (void) sendMenuRequestForKeys:(uint8_t)keys; |
||
124 | { |
||
125 | MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController]; |
||
126 | uint8_t lcdReq[2]; |
||
127 | |||
128 | lcdReq[0] = keys; |
||
129 | lcdReq[1] = 50; |
||
130 | |||
131 | NSData * data = [NSData dataWithCommand:MKCommandLcdRequest |
||
132 | forAddress:MKAddressFC |
||
133 | payloadWithBytes:lcdReq |
||
134 | length:2]; |
||
135 | |||
136 | [cCtrl sendRequest:data]; |
||
137 | } |
||
138 | |||
139 | - (void) sendMenuRefreshRequest { |
||
140 | [self sendMenuRequestForKeys:0xFF]; |
||
141 | } |
||
142 | |||
143 | - (void) lcdNotification:(NSNotification *)aNotification { |
||
144 | NSArray * mr = [[aNotification userInfo] objectForKey:kMKDataKeyMenuRows]; |
||
145 | |||
146 | label.text = [mr componentsJoinedByString:@"\r\n"]; |
||
147 | if (lcdCount++ > 4 ) { |
||
148 | [self sendMenuRefreshRequest]; |
||
149 | lcdCount = 0; |
||
150 | } |
||
151 | } |
||
152 | |||
153 | - (IBAction) changeDevice { |
||
154 | |||
155 | [[MKConnectionController sharedMKConnectionController] activateNaviCtrl]; |
||
156 | |||
157 | switch (segment.selectedSegmentIndex) { |
||
158 | case 1: |
||
159 | [[MKConnectionController sharedMKConnectionController] activateFlightCtrl]; |
||
160 | break; |
||
161 | case 2: |
||
162 | [[MKConnectionController sharedMKConnectionController] activateMK3MAG]; |
||
163 | break; |
||
164 | } |
||
165 | |||
166 | MKConnectionController * cCtrl = [MKConnectionController sharedMKConnectionController]; |
||
167 | DLog(@"Device set to %d", cCtrl.currentDevice); |
||
168 | |||
169 | [self performSelector:@selector(sendMenuRefreshRequest) withObject:self afterDelay:0.1]; |
||
170 | } |
||
171 | |||
172 | - (IBAction) nextScreen { |
||
173 | [self sendMenuRequestForKeys:0xFD]; |
||
174 | } |
||
175 | |||
176 | - (IBAction) prevScreen { |
||
177 | [self sendMenuRequestForKeys:0xFE]; |
||
178 | } |
||
179 | |||
180 | - (void) didReceiveMemoryWarning { |
||
181 | [super didReceiveMemoryWarning]; |
||
182 | } |
||
183 | |||
184 | - (void) viewDidUnload { |
||
185 | [super viewDidUnload]; |
||
186 | } |
||
187 | |||
188 | - (void) dealloc { |
||
189 | [super dealloc]; |
||
190 | } |
||
191 | |||
192 | #pragma mark - |
||
193 | |||
194 | #define kMinimumGestureLength 25 |
||
195 | #define kMaximumVariance 5 |
||
196 | |||
197 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { |
||
198 | |||
199 | UITouch *touch = [touches anyObject]; |
||
200 | gestureStartPoint = [touch locationInView:self.view]; |
||
201 | |||
202 | } |
||
203 | - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { |
||
204 | |||
205 | if (!canProcessNextGesture) |
||
206 | return; |
||
207 | |||
208 | UITouch *touch = [touches anyObject]; |
||
209 | CGPoint currentPosition = [touch locationInView:self.view]; |
||
210 | |||
211 | CGFloat deltaX = (gestureStartPoint.x - currentPosition.x); |
||
212 | CGFloat deltaY = (gestureStartPoint.y - currentPosition.y); |
||
213 | |||
214 | BOOL leftToRight=(deltaX<0); |
||
215 | |||
216 | NSLog(@"%f",deltaX); |
||
217 | deltaX = fabsf(deltaX); |
||
218 | deltaY = fabsf(deltaY); |
||
219 | |||
220 | if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) { |
||
221 | if (leftToRight) |
||
222 | [self prevScreen]; |
||
223 | else |
||
224 | [self nextScreen]; |
||
225 | |||
226 | canProcessNextGesture=NO; |
||
227 | |||
228 | } |
||
229 | else if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance){ |
||
230 | } |
||
231 | |||
232 | } |
||
233 | |||
234 | -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { |
||
235 | canProcessNextGesture=YES; |
||
236 | } |
||
237 | |||
238 | @end |