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 | #define STATUS_DEFAULT 0 |
---|
18 | #define STATUS_CONNECTING 1 |
---|
19 | #define STATUS_CONNECTED 2 |
---|
20 | #define STATUS_PROCESSING 3 |
---|
21 | #define STATUS_ACTIVE 4 |
---|
22 | |
---|
23 | @implementation FlightViewController { |
---|
24 | SignalConverter *signalConverter; |
---|
25 | int deviceStatus; |
---|
26 | } |
---|
27 | |
---|
28 | @synthesize status, attention, meditation, signal, power, attentionThreshold, meditationThreshold, connectButton, testButton, statusImage, signalPercent, attentionPercent, meditationPercent, powerPercent; |
---|
29 | |
---|
30 | - (void)viewDidLoad |
---|
31 | { |
---|
32 | [super viewDidLoad]; |
---|
33 | AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; |
---|
34 | signalConverter = appDelegate.signalConverter; |
---|
35 | |
---|
36 | signalConverter.delegate = self; |
---|
37 | [self.attentionThreshold addTarget:self action:@selector(sliderChanged) forControlEvents:UIControlEventValueChanged]; |
---|
38 | [self.meditationThreshold addTarget:self action:@selector(sliderChanged) forControlEvents:UIControlEventValueChanged]; |
---|
39 | } |
---|
40 | |
---|
41 | #pragma mark view updates |
---|
42 | |
---|
43 | |
---|
44 | - (void) resetViews |
---|
45 | { |
---|
46 | signal.progress = 0; |
---|
47 | signalPercent.text = @"0%"; |
---|
48 | attention.progress = 0; |
---|
49 | attentionPercent.text = @"0%"; |
---|
50 | meditation.progress = 0; |
---|
51 | meditationPercent.text = @"0%"; |
---|
52 | power.progress = 0; |
---|
53 | powerPercent.text = @"0%"; |
---|
54 | [self logMessage:@"Disconnected"]; |
---|
55 | [self updateStatusImage:STATUS_DEFAULT]; |
---|
56 | connectButton.title = @"Connect"; |
---|
57 | } |
---|
58 | |
---|
59 | - (void) updateStatusImage:(int) statusNo |
---|
60 | { |
---|
61 | if (statusNo != deviceStatus) { |
---|
62 | [statusImage setImage: [UIImage imageNamed:[NSString stringWithFormat:@"status_%u", statusNo]]]; |
---|
63 | deviceStatus = statusNo; |
---|
64 | if (statusNo == STATUS_CONNECTED) { |
---|
65 | [self logMessage:@"Check headset fitted properly"]; |
---|
66 | } else if (statusNo == STATUS_PROCESSING) { |
---|
67 | [self logMessage:@"Increase your concentration"]; |
---|
68 | } else if (statusNo == STATUS_ACTIVE) { |
---|
69 | [self logMessage:@"Flying!"]; |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | - (void) sliderChanged |
---|
75 | { |
---|
76 | [signalConverter setValuesForAttention:attentionThreshold.value meditation:meditationThreshold.value]; |
---|
77 | } |
---|
78 | |
---|
79 | - (void) logMessage:(NSString *) str |
---|
80 | { |
---|
81 | status.text = str; |
---|
82 | } |
---|
83 | |
---|
84 | #pragma mark - SignalConverterDelegate methods |
---|
85 | |
---|
86 | - (void) updatedValuesForSignal: (float)signalStrength |
---|
87 | attention: (float)attentionLevel |
---|
88 | meditation: (float)meditationLevel |
---|
89 | power: (float)powerLevel |
---|
90 | { |
---|
91 | self.signal.progress = signalStrength; |
---|
92 | self.signalPercent.text = [NSString stringWithFormat:@"%i%%", (int)(signalStrength * 100)]; |
---|
93 | self.attention.progress = attentionLevel; |
---|
94 | self.attentionPercent.text = [NSString stringWithFormat:@"%i%%", (int)(attentionLevel * 100)]; |
---|
95 | self.meditation.progress = meditationLevel; |
---|
96 | self.meditationPercent.text = [NSString stringWithFormat:@"%i%%", (int)(meditationLevel * 100)]; |
---|
97 | self.power.progress = powerLevel; |
---|
98 | self.powerPercent.text = [NSString stringWithFormat:@"%i%%", (int)(powerLevel * 100)]; |
---|
99 | [self updateStatusImage:[self statusFromSignal:signalStrength power:powerLevel]]; |
---|
100 | } |
---|
101 | |
---|
102 | - (int) statusFromSignal: (float)signalLevel power:(float)powerLevel |
---|
103 | { |
---|
104 | if (signalLevel < 0.9) { |
---|
105 | return STATUS_CONNECTED; |
---|
106 | } else if (powerLevel == 0.0) { |
---|
107 | return STATUS_PROCESSING; |
---|
108 | } else { |
---|
109 | return STATUS_ACTIVE; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | - (void) notifyHeadsetConnect |
---|
114 | { |
---|
115 | [self logMessage:@"Headset connected"]; |
---|
116 | [self updateStatusImage:STATUS_CONNECTING]; |
---|
117 | } |
---|
118 | |
---|
119 | - (void) notifyHeadsetDisconnect |
---|
120 | { |
---|
121 | |
---|
122 | [self resetViews]; |
---|
123 | } |
---|
124 | |
---|
125 | - (void) notifyDeviceConnected:(NSString *)name |
---|
126 | { |
---|
127 | [self logMessage:[NSString stringWithFormat:@"EEG device %@ connected", name]]; |
---|
128 | [self updateStatusImage:STATUS_CONNECTING]; |
---|
129 | connectButton.title = @"Disconnect"; |
---|
130 | } |
---|
131 | |
---|
132 | - (void) appStopped |
---|
133 | { |
---|
134 | [self resetViews]; |
---|
135 | } |
---|
136 | |
---|
137 | #pragma mark - button press events |
---|
138 | |
---|
139 | - (IBAction) connectButtonPressed:(id) sender { |
---|
140 | |
---|
141 | if (signalConverter.running) { |
---|
142 | [signalConverter stopProcessing]; |
---|
143 | [self resetViews]; |
---|
144 | } else { |
---|
145 | BOOL volumeMax = [signalConverter isVolumeMax]; |
---|
146 | BOOL headphones = [signalConverter isAudioJackPlugged]; |
---|
147 | |
---|
148 | if (!volumeMax && !headphones) { |
---|
149 | 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]; |
---|
150 | [alert show]; |
---|
151 | } else if (!volumeMax) { |
---|
152 | 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]; |
---|
153 | [alert show]; |
---|
154 | } else if (!headphones) { |
---|
155 | 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]; |
---|
156 | [alert show]; |
---|
157 | } else if (!signalConverter.isBluetoothReady) { |
---|
158 | 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]; |
---|
159 | [alert show]; |
---|
160 | } else if ([signalConverter startProcessing]) { |
---|
161 | [self logMessage:@"Searching for device"]; |
---|
162 | connectButton.title = @"Disconnect"; |
---|
163 | } else { |
---|
164 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to connect" message:@"Check the device is correctly paired on Bluetooth" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; |
---|
165 | [alert show]; |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | - (IBAction) testButtonPressed:(id) sender { |
---|
171 | [signalConverter isAudioJackPlugged]; |
---|
172 | if (signalConverter.running) { |
---|
173 | UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Stop test sound" message:@"Press Stop first to end the test" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; |
---|
174 | [alert show]; |
---|
175 | } else { |
---|
176 | if (signalConverter.testing) { |
---|
177 | testButton.title = @"Test"; |
---|
178 | [signalConverter stopTestSound]; |
---|
179 | } else { |
---|
180 | testButton.title = @"Stop"; |
---|
181 | [signalConverter playTestSound]; |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | @end |
---|