1 | |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | #import <QuartzCore/QuartzCore.h> |
---|
13 | #import "FlightViewController.h" |
---|
14 | #import "AppDelegate.h" |
---|
15 | #import "SignalConverter.h" |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | #define STATUS_DEFAULT 0 |
---|
23 | #define STATUS_CONNECTING 1 |
---|
24 | #define STATUS_CONNECTED 2 |
---|
25 | #define STATUS_PROCESSING 3 |
---|
26 | #define STATUS_ACTIVE 4 |
---|
27 | |
---|
28 | @implementation FlightViewController { |
---|
29 | SignalConverter *signalConverter; |
---|
30 | int deviceStatus; |
---|
31 | BOOL warned; |
---|
32 | } |
---|
33 | |
---|
34 | @synthesize attention, meditation, signal, power, attentionThreshold, meditationThreshold, connectButton, testButton, statusImage, signalPercent, attentionPercent, meditationPercent, powerPercent; |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | - (void)viewDidLoad |
---|
45 | { |
---|
46 | [super viewDidLoad]; |
---|
47 | |
---|
48 | attention.progressTintColor = [UIColor redColor]; |
---|
49 | self.attentionThreshold.minimumTrackTintColor = [UIColor grayColor]; |
---|
50 | meditation.progressTintColor = [UIColor blueColor]; |
---|
51 | self.meditationThreshold.minimumTrackTintColor = [UIColor grayColor]; |
---|
52 | self.meditationThreshold.value = 0; |
---|
53 | |
---|
54 | AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; |
---|
55 | signalConverter = appDelegate.signalConverter; |
---|
56 | |
---|
57 | signalConverter.delegate = self; |
---|
58 | [self setThresholds]; |
---|
59 | [self.attentionThreshold addTarget:self action:@selector(setThresholds) forControlEvents:UIControlEventValueChanged]; |
---|
60 | [self.meditationThreshold addTarget:self action:@selector(setThresholds) forControlEvents:UIControlEventValueChanged]; |
---|
61 | |
---|
62 | } |
---|
63 | |
---|
64 | #pragma mark view updates |
---|
65 | |
---|
66 | |
---|
67 | - (void) resetViews |
---|
68 | { |
---|
69 | signal.progress = 0; |
---|
70 | signalPercent.text = @"0%"; |
---|
71 | attention.progress = 0; |
---|
72 | attentionPercent.text = @"0%"; |
---|
73 | meditation.progress = 0; |
---|
74 | meditationPercent.text = @"0%"; |
---|
75 | power.progress = 0; |
---|
76 | powerPercent.text = @"0%"; |
---|
77 | [self updateStatusImage:STATUS_DEFAULT]; |
---|
78 | connectButton.title = @"Connect"; |
---|
79 | } |
---|
80 | |
---|
81 | - (void) updateStatusImage:(int) statusNo |
---|
82 | { |
---|
83 | if (statusNo != deviceStatus) { |
---|
84 | [statusImage setImage: [UIImage imageNamed:[NSString stringWithFormat:@"status_%u", statusNo]]]; |
---|
85 | deviceStatus = statusNo; |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | - (void) setThresholds |
---|
90 | { |
---|
91 | [signalConverter setValuesForAttention:attentionThreshold.value meditation:meditationThreshold.value]; |
---|
92 | } |
---|
93 | |
---|
94 | #pragma mark - SignalConverterDelegate methods |
---|
95 | |
---|
96 | - (void) updatedValuesForSignal: (float)signalStrength |
---|
97 | attention: (float)attentionLevel |
---|
98 | meditation: (float)meditationLevel |
---|
99 | power: (float)powerLevel |
---|
100 | { |
---|
101 | |
---|
102 | |
---|
103 | if (signalStrength < 100.0) { |
---|
104 | attentionLevel = 0.000000; |
---|
105 | meditationLevel = 0.000000; |
---|
106 | powerLevel = 0.000000; |
---|
107 | } |
---|
108 | |
---|
109 | self.signal.progress = signalStrength / 100; |
---|
110 | self.signalPercent.text = [NSString stringWithFormat:@"%i%%", (int)(signalStrength)]; |
---|
111 | self.attention.progress = attentionLevel; |
---|
112 | self.attentionPercent.text = [NSString stringWithFormat:@"%i%%", (int)(attentionLevel * 100)]; |
---|
113 | self.meditation.progress = meditationLevel; |
---|
114 | self.meditationPercent.text = [NSString stringWithFormat:@"%i%%", (int)(meditationLevel * 100)]; |
---|
115 | self.power.progress = powerLevel; |
---|
116 | self.powerPercent.text = [NSString stringWithFormat:@"%i%%", (int)(powerLevel * 100)]; |
---|
117 | [self updateStatusImage:[self statusFromSignal:signalStrength power:powerLevel]]; |
---|
118 | |
---|
119 | [self updateScores]; |
---|
120 | |
---|
121 | } |
---|
122 | |
---|
123 | - (int) statusFromSignal: (float)signalLevel power:(float)powerLevel |
---|
124 | { |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | if (signalLevel == 0.0) { |
---|
129 | return STATUS_CONNECTING; |
---|
130 | } |
---|
131 | |
---|
132 | if (signalLevel < 100.0) { |
---|
133 | return STATUS_CONNECTED; |
---|
134 | } |
---|
135 | |
---|
136 | if (powerLevel == 0.0) { |
---|
137 | return STATUS_PROCESSING; |
---|
138 | } else { |
---|
139 | return STATUS_ACTIVE; |
---|
140 | } |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | - (void) notifyHeadsetConnect |
---|
145 | { |
---|
146 | [self updateStatusImage:STATUS_CONNECTING]; |
---|
147 | } |
---|
148 | |
---|
149 | - (void) notifyHeadsetDisconnect |
---|
150 | { |
---|
151 | |
---|
152 | [self resetViews]; |
---|
153 | } |
---|
154 | |
---|
155 | - (void) notifyDeviceConnected:(NSString *)name |
---|
156 | { |
---|
157 | [self updateStatusImage:STATUS_CONNECTING]; |
---|
158 | connectButton.title = @"Disconnect"; |
---|
159 | } |
---|
160 | |
---|
161 | - (void) appStopped |
---|
162 | { |
---|
163 | [self resetViews]; |
---|
164 | } |
---|
165 | |
---|
166 | #pragma mark - button press events |
---|
167 | |
---|
168 | - (IBAction) connectButtonPressed:(id) sender { |
---|
169 | |
---|
170 | if (signalConverter.running) { |
---|
171 | [signalConverter stopProcessing]; |
---|
172 | [self resetViews]; |
---|
173 | } else { |
---|
174 | BOOL volumeMax = [signalConverter isVolumeMax]; |
---|
175 | BOOL headphones = [signalConverter isAudioJackPlugged]; |
---|
176 | |
---|
177 | if (!warned && !volumeMax && !headphones) { |
---|
178 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Check Volume & Transmitter" message:@"Ensure the volume is at max and the transmitter is plugged into the headphones" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; |
---|
179 | [alert show]; |
---|
180 | } else if (!warned && !volumeMax) { |
---|
181 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Turn Up The Volume" message:@"Your device volume must be at the maximum for proper operation" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; |
---|
182 | [alert show]; |
---|
183 | } else if (!warned && !headphones) { |
---|
184 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Plug In The Transmitter" message:@"Ensure the infrared transmitter is plugged into the headphone jack" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; |
---|
185 | [alert show]; |
---|
186 | } else if (!signalConverter.isBluetoothReady) { |
---|
187 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Device not found" message:@"No Bluetooth device detected. Ensure Bluetooth is on and the Mindwave headset is paired" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; |
---|
188 | [alert show]; |
---|
189 | } else if ([signalConverter startProcessing]) { |
---|
190 | connectButton.title = @"Disconnect"; |
---|
191 | } else { |
---|
192 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to connect" message:@"Check the device is correctly paired on Bluetooth" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; |
---|
193 | [alert show]; |
---|
194 | } |
---|
195 | warned = YES; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | - (IBAction) testButtonPressed:(id) sender { |
---|
200 | |
---|
201 | |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | [signalConverter isAudioJackPlugged]; |
---|
207 | if (signalConverter.running) { |
---|
208 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Signal Processing Active" message:@"Please press the Disconnect button before sending a Test signal" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; |
---|
209 | [alert show]; |
---|
210 | } else { |
---|
211 | if (signalConverter.testing) { |
---|
212 | testButton.title = @"Test"; |
---|
213 | [signalConverter stopTestSound]; |
---|
214 | } else { |
---|
215 | testButton.title = @"Stop"; |
---|
216 | [signalConverter playTestSound]; |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | - (void) updateScores |
---|
222 | { |
---|
223 | |
---|
224 | |
---|
225 | |
---|
226 | |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | int eegAttentionScore = 0; |
---|
245 | int eegAttention = self.attention.progress * 100; |
---|
246 | int eegAttentionTarget = self.attentionThreshold.value * 100; |
---|
247 | |
---|
248 | int eegMeditationScore = 0; |
---|
249 | int eegMeditation = self.meditation.progress * 100; |
---|
250 | int eegMeditationTarget = self.meditationThreshold.value * 100; |
---|
251 | |
---|
252 | |
---|
253 | if ((eegAttention >= eegAttentionTarget) && |
---|
254 | (eegAttentionTarget > minimumScoreTarget)) |
---|
255 | eegAttentionScore = eegAttentionTarget - minimumScoreTarget; |
---|
256 | |
---|
257 | if ((eegMeditation >= eegMeditationTarget) && |
---|
258 | (eegMeditationTarget > minimumScoreTarget)) |
---|
259 | eegMeditationScore = eegMeditationTarget - minimumScoreTarget; |
---|
260 | |
---|
261 | if (eegAttentionScore > eegMeditationScore) |
---|
262 | scoreCurrent = scoreCurrent + eegAttentionScore; |
---|
263 | else |
---|
264 | scoreCurrent = scoreCurrent + eegMeditationScore; |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | if (scoreCurrent > scoreHigh) { |
---|
269 | scoreHigh = scoreCurrent; |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | if (power.progress == 0) |
---|
274 | [self resetCurrentScore]; |
---|
275 | |
---|
276 | |
---|
277 | |
---|
278 | |
---|
279 | |
---|
280 | |
---|
281 | if ((eegAttention < eegAttentionTarget) && (eegMeditation < minimumScoreTarget)) |
---|
282 | [self resetCurrentScore]; |
---|
283 | |
---|
284 | if ((eegMeditation < eegMeditationTarget) && (eegAttention < minimumScoreTarget)) |
---|
285 | [self resetCurrentScore]; |
---|
286 | |
---|
287 | if ((eegAttention < minimumScoreTarget) && (eegMeditation < minimumScoreTarget)) |
---|
288 | [self resetCurrentScore]; |
---|
289 | |
---|
290 | |
---|
291 | [self displayScore]; |
---|
292 | |
---|
293 | } |
---|
294 | |
---|
295 | - (void) displayScore |
---|
296 | { |
---|
297 | |
---|
298 | NSString* newScore = [NSString stringWithFormat:@"Scores Current: %i Last: %i High: %i", (int)scoreCurrent, (int)scoreLast, (int)scoreHigh]; |
---|
299 | |
---|
300 | if (newScore.length >= 50) |
---|
301 | newScore = [NSString stringWithFormat:@"Scores Current: %i Last: %i High: %i", (int)scoreCurrent, (int)scoreLast, (int)scoreHigh]; |
---|
302 | |
---|
303 | if (newScore.length >= 50) |
---|
304 | newScore = [NSString stringWithFormat:@"Scores Current: %i Last: %i High: %i", (int)scoreCurrent, (int)scoreLast, (int)scoreHigh]; |
---|
305 | |
---|
306 | self.scoresDisplay.text = newScore; |
---|
307 | } |
---|
308 | |
---|
309 | - (void) resetCurrentScore |
---|
310 | { |
---|
311 | if (scoreCurrent > 0) |
---|
312 | scoreLast = scoreCurrent; |
---|
313 | |
---|
314 | scoreCurrent = 0; |
---|
315 | |
---|
316 | } |
---|
317 | |
---|
318 | @end |
---|