1 | |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | #import "SignalConverter.h" |
---|
10 | |
---|
11 | #define AUDIO_FILE_NAME @"throttle_hover_ios.wav" // @"iOS_noflip.wav" |
---|
12 | #define POOR_SIGNAL_KEY @"poorSignal" |
---|
13 | #define ATTENTION_KEY @"eSenseAttention" |
---|
14 | #define MEDITATION_KEY @"eSenseMeditation" |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | @implementation SignalConverter { |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | float attentionPower[101]; |
---|
24 | float meditationPower[101]; |
---|
25 | |
---|
26 | int signalStrength, attentionLevel, meditationLevel; |
---|
27 | } |
---|
28 | |
---|
29 | @synthesize attentionThreshold, meditationThreshold, running; |
---|
30 | |
---|
31 | - (void) setValuesForAttention:(float) attention meditation:(float) meditation |
---|
32 | { |
---|
33 | attentionThreshold = attention; |
---|
34 | meditationThreshold = meditation; |
---|
35 | [self calculatePowerValues]; |
---|
36 | } |
---|
37 | |
---|
38 | - (void) prepare |
---|
39 | { |
---|
40 | NSURL *audioFilePath = [[NSBundle mainBundle] URLForResource:AUDIO_FILE_NAME withExtension:nil]; |
---|
41 | audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFilePath error:nil]; |
---|
42 | if (audioPlayer) { |
---|
43 | [audioPlayer prepareToPlay]; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | - (void) appClosed |
---|
48 | { |
---|
49 | if ([[TGAccessoryManager sharedTGAccessoryManager] connected]) { |
---|
50 | [[TGAccessoryManager sharedTGAccessoryManager] stopStream]; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | #pragma mark - TGAccessoryDelegate methods |
---|
55 | |
---|
56 | - (void)dataReceived:(NSDictionary *)data { |
---|
57 | [self performSelectorOnMainThread:@selector(updatedSignalReceived:) |
---|
58 | withObject:data |
---|
59 | waitUntilDone:NO]; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | - (void) updatedSignalReceived:(id) data |
---|
64 | { |
---|
65 | [self setValuesFromData:data]; |
---|
66 | |
---|
67 | if (_delegate != nil) |
---|
68 | { |
---|
69 | [_delegate updatedValuesForSignal: signalStrength |
---|
70 | attention: (float)attentionLevel / 100 |
---|
71 | meditation: (float)meditationLevel / 100 |
---|
72 | power: [self currentPowerLevel]]; |
---|
73 | } |
---|
74 | [self playAudio]; |
---|
75 | } |
---|
76 | |
---|
77 | - (void) setValuesFromData:(id) data |
---|
78 | { |
---|
79 | [self setSignalStrength:(NSNumber *)[data valueForKey:POOR_SIGNAL_KEY]]; |
---|
80 | [self setAttentionLevel:(NSNumber *)[data valueForKey:ATTENTION_KEY]]; |
---|
81 | [self setMeditationLevel:(NSNumber *)[data valueForKey:MEDITATION_KEY]]; |
---|
82 | } |
---|
83 | |
---|
84 | - (void) setSignalStrength:(NSNumber *) value |
---|
85 | { |
---|
86 | if (value != nil) { |
---|
87 | signalStrength = (200.0 - [value intValue]) / 200.0; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | - (void) setAttentionLevel:(NSNumber *) value |
---|
92 | { |
---|
93 | if (value != nil) { |
---|
94 | attentionLevel = [value intValue]; |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | - (void) setMeditationLevel:(NSNumber *) value |
---|
99 | { |
---|
100 | if (value != nil) { |
---|
101 | meditationLevel = [value intValue]; |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | - (void)accessoryDidConnect:(EAAccessory *)accessory { |
---|
107 | if (_delegate != nil) |
---|
108 | { |
---|
109 | [_delegate notifyHeadsetConnect]; |
---|
110 | } |
---|
111 | if ([[TGAccessoryManager sharedTGAccessoryManager] accessory] != nil) { |
---|
112 | [[TGAccessoryManager sharedTGAccessoryManager] startStream]; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | - (void)accessoryDidDisconnect { |
---|
118 | if (_delegate != nil) |
---|
119 | { |
---|
120 | [_delegate notifyHeadsetDisconnect]; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | #pragma mark start / stop methods |
---|
125 | |
---|
126 | - (void) startProcessing |
---|
127 | { |
---|
128 | running = YES; |
---|
129 | EAAccessory *accessory = [[TGAccessoryManager sharedTGAccessoryManager] accessory]; |
---|
130 | if (accessory != nil) { |
---|
131 | if (_delegate != nil) |
---|
132 | { |
---|
133 | [_delegate notifyDeviceConnected: accessory.name]; |
---|
134 | } |
---|
135 | [[TGAccessoryManager sharedTGAccessoryManager] startStream]; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | - (void) stopProcessing |
---|
140 | { |
---|
141 | running = NO; |
---|
142 | if ([[TGAccessoryManager sharedTGAccessoryManager] connected]) { |
---|
143 | [[TGAccessoryManager sharedTGAccessoryManager] stopStream]; |
---|
144 | } |
---|
145 | [audioPlayer stop]; |
---|
146 | [self prepare]; |
---|
147 | } |
---|
148 | |
---|
149 | #pragma mark internal processing methods |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | - (float) currentPowerLevel |
---|
154 | { |
---|
155 | float powerLevel = attentionPower[attentionLevel] + meditationPower[meditationLevel]; |
---|
156 | if (powerLevel > 1) { |
---|
157 | return 1.0; |
---|
158 | } |
---|
159 | return powerLevel; |
---|
160 | } |
---|
161 | |
---|
162 | - (void) playAudio |
---|
163 | { |
---|
164 | |
---|
165 | if ([self currentPowerLevel] > 0) { |
---|
166 | audioPlayer.volume = 1.0; |
---|
167 | [audioPlayer play]; |
---|
168 | } else { |
---|
169 | [audioPlayer stop]; |
---|
170 | [self prepare]; |
---|
171 | |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | #pragma mark - display calculation and update methods |
---|
176 | |
---|
177 | |
---|
178 | - (void) calculatePowerValues |
---|
179 | { |
---|
180 | for (int i = 0; i < 101; i++) { |
---|
181 | attentionPower[i] = [self calculatePowerAt:(float)i/100 withThreshold:attentionThreshold]; |
---|
182 | } |
---|
183 | for (int i = 0; i < 101; i++) { |
---|
184 | meditationPower[i] = [self calculatePowerAt:(float)i/100 withThreshold:meditationThreshold]; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | |
---|
194 | - (float) calculatePowerAt:(float)value withThreshold:(float)threshold |
---|
195 | { |
---|
196 | if (value < threshold) { |
---|
197 | return 0; |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | return (value - threshold) / (1 - threshold); |
---|
202 | } |
---|
203 | |
---|
204 | @end |
---|