// // FlightViewController.m // Orbit // // Copyright (c) 2013 Puzzlebox Productions, LLC. All rights reserved. // Originally created by Jonathon Horsman. // // This code is released under the GNU Public License (GPL) version 2 // For more information please refer to http://www.gnu.org/copyleft/gpl.html // #import #import "FlightViewController.h" #import "AppDelegate.h" #import "SignalConverter.h" #define STATUS_DEFAULT 0 #define STATUS_CONNECTING 1 #define STATUS_CONNECTED 2 #define STATUS_PROCESSING 3 #define STATUS_ACTIVE 4 @implementation FlightViewController { SignalConverter *signalConverter; int deviceStatus; BOOL warned; } @synthesize attention, meditation, signal, power, attentionThreshold, meditationThreshold, connectButton, testButton, statusImage, signalPercent, attentionPercent, meditationPercent, powerPercent; - (void)viewDidLoad { [super viewDidLoad]; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; signalConverter = appDelegate.signalConverter; signalConverter.delegate = self; [self setThresholds]; [self.attentionThreshold addTarget:self action:@selector(setThresholds) forControlEvents:UIControlEventValueChanged]; [self.meditationThreshold addTarget:self action:@selector(setThresholds) forControlEvents:UIControlEventValueChanged]; } #pragma mark view updates // Set all levels back to zero - (void) resetViews { signal.progress = 0; signalPercent.text = @"0%"; attention.progress = 0; attentionPercent.text = @"0%"; meditation.progress = 0; meditationPercent.text = @"0%"; power.progress = 0; powerPercent.text = @"0%"; [self updateStatusImage:STATUS_DEFAULT]; connectButton.title = @"Connect"; } - (void) updateStatusImage:(int) statusNo { if (statusNo != deviceStatus) { [statusImage setImage: [UIImage imageNamed:[NSString stringWithFormat:@"status_%u", statusNo]]]; deviceStatus = statusNo; } } - (void) setThresholds { [signalConverter setValuesForAttention:attentionThreshold.value meditation:meditationThreshold.value]; } #pragma mark - SignalConverterDelegate methods - (void) updatedValuesForSignal: (float)signalStrength attention: (float)attentionLevel meditation: (float)meditationLevel power: (float)powerLevel { self.signal.progress = signalStrength; self.signalPercent.text = [NSString stringWithFormat:@"%i%%", (int)(signalStrength * 100)]; self.attention.progress = attentionLevel; self.attentionPercent.text = [NSString stringWithFormat:@"%i%%", (int)(attentionLevel * 100)]; self.meditation.progress = meditationLevel; self.meditationPercent.text = [NSString stringWithFormat:@"%i%%", (int)(meditationLevel * 100)]; self.power.progress = powerLevel; self.powerPercent.text = [NSString stringWithFormat:@"%i%%", (int)(powerLevel * 100)]; [self updateStatusImage:[self statusFromSignal:signalStrength power:powerLevel]]; } - (int) statusFromSignal: (float)signalLevel power:(float)powerLevel { if (signalLevel < 0.9) { // weak signal return STATUS_CONNECTED; } else if (powerLevel == 0.0) { // strong signal but thresholds not met return STATUS_PROCESSING; } else { // flight command sent to orbit return STATUS_ACTIVE; } } - (void) notifyHeadsetConnect { [self updateStatusImage:STATUS_CONNECTING]; } - (void) notifyHeadsetDisconnect { // Reset the outputs back to zero [self resetViews]; } - (void) notifyDeviceConnected:(NSString *)name { [self updateStatusImage:STATUS_CONNECTING]; connectButton.title = @"Disconnect"; } - (void) appStopped { [self resetViews]; // just in case it didn't happen on close } #pragma mark - button press events - (IBAction) connectButtonPressed:(id) sender { if (signalConverter.running) { [signalConverter stopProcessing]; [self resetViews]; } else { BOOL volumeMax = [signalConverter isVolumeMax]; BOOL headphones = [signalConverter isAudioJackPlugged]; if (!warned && !volumeMax && !headphones) { 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]; [alert show]; } else if (!warned && !volumeMax) { 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]; [alert show]; } else if (!warned && !headphones) { 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]; [alert show]; } else if (!signalConverter.isBluetoothReady) { 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]; [alert show]; } else if ([signalConverter startProcessing]) { connectButton.title = @"Disconnect"; } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to connect" message:@"Check the device is correctly paired on Bluetooth" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; } warned = YES; } } - (IBAction) testButtonPressed:(id) sender { [signalConverter isAudioJackPlugged]; if (signalConverter.running) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Stop test sound" message:@"Press Stop first to end the test" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; } else { if (signalConverter.testing) { testButton.title = @"Test"; [signalConverter stopTestSound]; } else { testButton.title = @"Stop"; [signalConverter playTestSound]; } } } @end