source: orbit/iOS/Orbit/Orbit/controllers/FlightViewController.m @ e37a889

ServoTab_Interfacepyramid
Last change on this file since e37a889 was e37a889, checked in by Steve Castellotti <sc@…>, 7 years ago

Orbit:

  • Operating on WAV sound
  • Property mode set to 100644
File size: 6.7 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//   [signalConverter prepareAudio];
42   
43   AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
44   signalConverter = appDelegate.signalConverter;
45   
46   signalConverter.delegate = self;
47   [self setThresholds];
48   [self.attentionThreshold addTarget:self action:@selector(setThresholds) forControlEvents:UIControlEventValueChanged];
49   [self.meditationThreshold addTarget:self action:@selector(setThresholds) forControlEvents:UIControlEventValueChanged];
50   
51}
52
53#pragma mark view updates
54
55// Set all levels back to zero
56- (void) resetViews
57{
58   signal.progress = 0;
59   signalPercent.text = @"0%";
60   attention.progress = 0;
61   attentionPercent.text = @"0%";
62   meditation.progress = 0;
63   meditationPercent.text = @"0%";
64   power.progress = 0;
65   powerPercent.text = @"0%";
66   [self updateStatusImage:STATUS_DEFAULT];
67   connectButton.title = @"Connect";
68}
69
70- (void) updateStatusImage:(int) statusNo
71{
72   if (statusNo != deviceStatus) {
73      [statusImage setImage: [UIImage imageNamed:[NSString stringWithFormat:@"status_%u", statusNo]]];
74      deviceStatus = statusNo;
75   }
76}
77
78- (void) setThresholds
79{
80   [signalConverter setValuesForAttention:attentionThreshold.value meditation:meditationThreshold.value];
81}
82
83#pragma mark - SignalConverterDelegate methods
84
85- (void) updatedValuesForSignal: (float)signalStrength
86                      attention: (float)attentionLevel
87                     meditation: (float)meditationLevel
88                          power: (float)powerLevel
89{
90 
91   // Set Attention and Meditation to zero if we've lost signal
92   if (signalStrength < 100.0) {
93      attentionLevel = 0.000000;
94      meditationLevel = 0.000000;
95      powerLevel = 0.000000;
96   }
97   
98   self.signal.progress = signalStrength / 100;
99   self.signalPercent.text = [NSString stringWithFormat:@"%i%%", (int)(signalStrength)];
100   self.attention.progress = attentionLevel;
101   self.attentionPercent.text = [NSString stringWithFormat:@"%i%%", (int)(attentionLevel * 100)];
102   self.meditation.progress = meditationLevel;
103   self.meditationPercent.text = [NSString stringWithFormat:@"%i%%", (int)(meditationLevel * 100)];
104   self.power.progress = powerLevel;
105   self.powerPercent.text = [NSString stringWithFormat:@"%i%%", (int)(powerLevel * 100)];
106   [self updateStatusImage:[self statusFromSignal:signalStrength power:powerLevel]];
107   
108}
109
110- (int) statusFromSignal: (float)signalLevel power:(float)powerLevel
111{
112   if (signalLevel < 0.9) { // weak signal
113      return STATUS_CONNECTED;
114   } else if (powerLevel == 0.0) { // strong signal but thresholds not met
115      return STATUS_PROCESSING;
116   } else { // flight command sent to orbit
117      return STATUS_ACTIVE;
118   }
119}
120
121- (void) notifyHeadsetConnect
122{
123   [self updateStatusImage:STATUS_CONNECTING];
124}
125
126- (void) notifyHeadsetDisconnect
127{
128   // Reset the outputs back to zero
129   [self resetViews];
130}
131
132- (void) notifyDeviceConnected:(NSString *)name
133{
134   [self updateStatusImage:STATUS_CONNECTING];
135   connectButton.title = @"Disconnect";
136}
137
138- (void) appStopped
139{
140   [self resetViews]; // just in case it didn't happen on close
141}
142
143#pragma mark - button press events
144
145- (IBAction) connectButtonPressed:(id) sender {
146   
147   if (signalConverter.running) {
148      [signalConverter stopProcessing];
149      [self resetViews];
150   } else {
151      BOOL volumeMax  = [signalConverter isVolumeMax];
152      BOOL headphones = [signalConverter isAudioJackPlugged];
153     
154      if (!warned && !volumeMax && !headphones) {
155         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];
156         [alert show];
157      } else if (!warned && !volumeMax) {
158         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];
159         [alert show];
160      } else if (!warned && !headphones) {
161         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];
162         [alert show];
163      } else if (!signalConverter.isBluetoothReady) {
164         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];
165         [alert show];
166      } else if ([signalConverter startProcessing]) {
167         connectButton.title = @"Disconnect";
168      } else {
169         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to connect" message:@"Check the device is correctly paired on Bluetooth" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
170         [alert show];
171      }
172      warned = YES;
173   }
174}
175
176- (IBAction) testButtonPressed:(id) sender {
177   
178//   NSLog(@"Available fonts: %@", [UIFont familyNames]);
179   
180   [signalConverter isAudioJackPlugged];
181   if (signalConverter.running) {
182      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Stop test sound" message:@"Press Stop first to end the test" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
183      [alert show];
184   } else {
185      if (signalConverter.testing) {
186         testButton.title = @"Test";
187         [signalConverter stopTestSound];
188      } else {
189         testButton.title = @"Stop";
190         [signalConverter playTestSound];
191      }
192   }
193}
194
195@end
Note: See TracBrowser for help on using the repository browser.