source: orbit/iOS/Orbit/Orbit/controllers/FlightViewController.m @ 8a0f920f

ServoTab_Interfacepyramid
Last change on this file since 8a0f920f was 8a0f920f, checked in by Jonathon Horsman <jonathon@…>, 8 years ago

Set thresholds when first loaded

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