source: orbit/iOS/Orbit/Orbit/controllers/FlightViewController.m @ 812f274

Servo
Last change on this file since 812f274 was 812f274, checked in by Steve Castellotti <sc@…>, 6 years ago
  • Fixes to display correct status image
  • Property mode set to 100644
File size: 6.9 KB
Line 
1//
2//  FlightViewController.m
3//  Orbit
4//
5//  Copyright (c) 2013 Puzzlebox Productions, LLC. All rights reserved.
6//  Originally created by Jonathon Horsman.
7//
8//  This code is released under the GNU Public License (GPL) version 2
9//  For more information please refer to http://www.gnu.org/copyleft/gpl.html
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   BOOL warned;
27}
28
29@synthesize attention, meditation, signal, power, attentionThreshold, meditationThreshold, connectButton, testButton, statusImage, signalPercent, attentionPercent, meditationPercent, powerPercent;
30
31- (void)viewDidLoad
32{
33   [super viewDidLoad];
34   
35   attention.progressTintColor = [UIColor redColor];
36   self.attentionThreshold.minimumTrackTintColor = [UIColor grayColor];
37   meditation.progressTintColor = [UIColor blueColor];
38   self.meditationThreshold.minimumTrackTintColor = [UIColor grayColor];
39   self.meditationThreshold.value = 0;
40   
41   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
42   signalConverter = appDelegate.signalConverter;
43   
44   signalConverter.delegate = self;
45   [self setThresholds];
46   [self.attentionThreshold addTarget:self action:@selector(setThresholds) forControlEvents:UIControlEventValueChanged];
47   [self.meditationThreshold addTarget:self action:@selector(setThresholds) forControlEvents:UIControlEventValueChanged];
48   
49}
50
51#pragma mark view updates
52
53// Set all levels back to zero
54- (void) resetViews
55{
56   signal.progress = 0;
57   signalPercent.text = @"0%";
58   attention.progress = 0;
59   attentionPercent.text = @"0%";
60   meditation.progress = 0;
61   meditationPercent.text = @"0%";
62   power.progress = 0;
63   powerPercent.text = @"0%";
64   [self updateStatusImage:STATUS_DEFAULT];
65   connectButton.title = @"Connect";
66}
67
68- (void) updateStatusImage:(int) statusNo
69{
70   if (statusNo != deviceStatus) {
71      [statusImage setImage: [UIImage imageNamed:[NSString stringWithFormat:@"status_%u", statusNo]]];
72      deviceStatus = statusNo;
73   }
74}
75
76- (void) setThresholds
77{
78   [signalConverter setValuesForAttention:attentionThreshold.value meditation:meditationThreshold.value];
79}
80
81#pragma mark - SignalConverterDelegate methods
82
83- (void) updatedValuesForSignal: (float)signalStrength
84                      attention: (float)attentionLevel
85                     meditation: (float)meditationLevel
86                          power: (float)powerLevel
87{
88 
89   // Set Attention and Meditation to zero if we've lost signal
90   if (signalStrength < 100.0) {
91      attentionLevel = 0.000000;
92      meditationLevel = 0.000000;
93      powerLevel = 0.000000;
94   }
95   
96   self.signal.progress = signalStrength / 100;
97   self.signalPercent.text = [NSString stringWithFormat:@"%i%%", (int)(signalStrength)];
98   self.attention.progress = attentionLevel;
99   self.attentionPercent.text = [NSString stringWithFormat:@"%i%%", (int)(attentionLevel * 100)];
100   self.meditation.progress = meditationLevel;
101   self.meditationPercent.text = [NSString stringWithFormat:@"%i%%", (int)(meditationLevel * 100)];
102   self.power.progress = powerLevel;
103   self.powerPercent.text = [NSString stringWithFormat:@"%i%%", (int)(powerLevel * 100)];
104   [self updateStatusImage:[self statusFromSignal:signalStrength power:powerLevel]];
105   
106}
107
108- (int) statusFromSignal: (float)signalLevel power:(float)powerLevel
109{
110   
111//   NSLog(@"signalLevel: %f | powerLevel: %f", signalLevel, powerLevel);
112   
113   if (signalLevel == 0.0) { // no signal
114      return STATUS_CONNECTING;
115   }
116   
117   if (signalLevel < 100.0) { // poor signal
118      return STATUS_CONNECTED;
119   }
120   
121   if (powerLevel == 0.0) { // strong signal but thresholds not met
122      return STATUS_PROCESSING;
123   } else { // flight command sent to orbit
124      return STATUS_ACTIVE;
125   }
126   
127}
128
129- (void) notifyHeadsetConnect
130{
131   [self updateStatusImage:STATUS_CONNECTING];
132}
133
134- (void) notifyHeadsetDisconnect
135{
136   // Reset the outputs back to zero
137   [self resetViews];
138}
139
140- (void) notifyDeviceConnected:(NSString *)name
141{
142   [self updateStatusImage:STATUS_CONNECTING];
143   connectButton.title = @"Disconnect";
144}
145
146- (void) appStopped
147{
148   [self resetViews]; // just in case it didn't happen on close
149}
150
151#pragma mark - button press events
152
153- (IBAction) connectButtonPressed:(id) sender {
154   
155   if (signalConverter.running) {
156      [signalConverter stopProcessing];
157      [self resetViews];
158   } else {
159      BOOL volumeMax  = [signalConverter isVolumeMax];
160      BOOL headphones = [signalConverter isAudioJackPlugged];
161     
162      if (!warned && !volumeMax && !headphones) {
163         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];
164         [alert show];
165      } else if (!warned && !volumeMax) {
166         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];
167         [alert show];
168      } else if (!warned && !headphones) {
169         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];
170         [alert show];
171      } else if (!signalConverter.isBluetoothReady) {
172         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];
173         [alert show];
174      } else if ([signalConverter startProcessing]) {
175         connectButton.title = @"Disconnect";
176      } else {
177         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to connect" message:@"Check the device is correctly paired on Bluetooth" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
178         [alert show];
179      }
180      warned = YES;
181   }
182}
183
184- (IBAction) testButtonPressed:(id) sender {
185   
186//   NSLog(@"Available fonts: %@", [UIFont familyNames]);
187   
188   [signalConverter isAudioJackPlugged];
189   if (signalConverter.running) {
190      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];
191      [alert show];
192   } else {
193      if (signalConverter.testing) {
194         testButton.title = @"Test";
195         [signalConverter stopTestSound];
196      } else {
197         testButton.title = @"Stop";
198         [signalConverter playTestSound];
199      }
200   }
201}
202
203@end
Note: See TracBrowser for help on using the repository browser.