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 | } |
---|
26 | |
---|
27 | @synthesize status, attention, meditation, signal, power, attentionThreshold, meditationThreshold, connectButton, statusImage; |
---|
28 | |
---|
29 | - (void)viewDidLoad |
---|
30 | { |
---|
31 | [super viewDidLoad]; |
---|
32 | AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; |
---|
33 | signalConverter = appDelegate.signalConverter; |
---|
34 | |
---|
35 | signalConverter.delegate = self; |
---|
36 | [self.attentionThreshold addTarget:self action:@selector(sliderChanged) forControlEvents:UIControlEventValueChanged]; |
---|
37 | [self.meditationThreshold addTarget:self action:@selector(sliderChanged) forControlEvents:UIControlEventValueChanged]; |
---|
38 | [signalConverter prepare]; |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | - (void) appForegrounded |
---|
43 | { |
---|
44 | [self resetViews]; |
---|
45 | } |
---|
46 | |
---|
47 | #pragma mark view updates |
---|
48 | |
---|
49 | |
---|
50 | - (void) resetViews |
---|
51 | { |
---|
52 | signal.progress = 0; |
---|
53 | attention.progress = 0; |
---|
54 | meditation.progress = 0; |
---|
55 | power.progress = 0; |
---|
56 | [self logMessage:@"Disconnected"]; |
---|
57 | } |
---|
58 | |
---|
59 | - (void) updateStatusImage:(int) statusNo |
---|
60 | { |
---|
61 | [statusImage setImage: [UIImage imageNamed:[NSString stringWithFormat:@"status_%u", statusNo]]]; |
---|
62 | } |
---|
63 | |
---|
64 | - (void) sliderChanged |
---|
65 | { |
---|
66 | [signalConverter setValuesForAttention:attentionThreshold.value meditation:meditationThreshold.value]; |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | } |
---|
80 | |
---|
81 | - (void) logMessage:(NSString *) str |
---|
82 | { |
---|
83 | status.text = str; |
---|
84 | } |
---|
85 | |
---|
86 | #pragma mark - SignalConverterDelegate methods |
---|
87 | |
---|
88 | - (void) updatedValuesForSignal: (float)signalStrength |
---|
89 | attention: (float)attentionLevel |
---|
90 | meditation: (float)meditationLevel |
---|
91 | power: (float)powerLevel |
---|
92 | { |
---|
93 | self.signal.progress = signalStrength; |
---|
94 | self.signalPercent.text = [NSString stringWithFormat:@"%i%%", (int)(signalStrength * 100)]; |
---|
95 | self.attention.progress = attentionLevel; |
---|
96 | self.attentionPercent.text = [NSString stringWithFormat:@"%i%%", (int)(attentionLevel * 100)]; |
---|
97 | self.meditation.progress = meditationLevel; |
---|
98 | self.meditationPercent.text = [NSString stringWithFormat:@"%i%%", (int)(meditationLevel * 100)]; |
---|
99 | self.power.progress = powerLevel; |
---|
100 | self.powerPercent.text = [NSString stringWithFormat:@"%i%%", (int)(powerLevel * 100)]; |
---|
101 | } |
---|
102 | |
---|
103 | - (void) notifyHeadsetConnect |
---|
104 | { |
---|
105 | [self logMessage:@"Headet connected"]; |
---|
106 | } |
---|
107 | |
---|
108 | - (void) notifyHeadsetDisconnect |
---|
109 | { |
---|
110 | |
---|
111 | [self resetViews]; |
---|
112 | [self logMessage:@"Headet disconnected"]; |
---|
113 | } |
---|
114 | |
---|
115 | - (void) notifyDeviceConnected:(NSString *)name |
---|
116 | { |
---|
117 | [self logMessage:[NSString stringWithFormat:@"EEG device %@ connected", name]]; |
---|
118 | } |
---|
119 | |
---|
120 | #pragma mark - button press events |
---|
121 | |
---|
122 | - (IBAction) connectButtonPressed:(id) sender { |
---|
123 | |
---|
124 | if (signalConverter.running) { |
---|
125 | [signalConverter stopProcessing]; |
---|
126 | connectButton.title = @"Connect"; |
---|
127 | [self resetViews]; |
---|
128 | } else { |
---|
129 | [self logMessage:@"Searching for device"]; |
---|
130 | [signalConverter startProcessing]; |
---|
131 | connectButton.title = @"Disconnect"; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | @end |
---|