source: orbit/iOS/Orbit/Orbit/controllers/FlightViewController.m @ 359a504

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

Add check for headphones being plugged in

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