source: orbit/iOS/Orbit/Orbit/controllers/FlightViewController.m @ 6bdfe8d

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

UI:

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