source: orbit/iOS/Orbit/Orbit/controllers/FlightViewController.m @ 9015b1e

RawEEGServoTab_Interfacepyramid
Last change on this file since 9015b1e was 9015b1e, checked in by Jonathon Horsman <jonathon@…>, 7 years ago

UI working with headset correctly now.
Check Bluetooth enabled and volume at full

  • Property mode set to 100644
File size: 5.5 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}
27
28@synthesize status, attention, meditation, signal, power, attentionThreshold, meditationThreshold, connectButton, statusImage, signalPercent, attentionPercent, meditationPercent, powerPercent;
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    [signalConverter prepare];
40}
41
42#pragma mark view updates
43
44// Set all levels back to zero
45- (void) resetViews
46{
47    signal.progress = 0;
48    signalPercent.text = @"0%";
49    attention.progress = 0;
50    attentionPercent.text = @"0%";
51    meditation.progress = 0;
52    meditationPercent.text = @"0%";
53    power.progress = 0;
54    powerPercent.text = @"0%";
55    [self logMessage:@"Disconnected"];
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        if (statusNo == STATUS_CONNECTED) {
66            [self logMessage:@"Check headset fitted properly"];
67        } else if (statusNo == STATUS_PROCESSING) {
68            [self logMessage:@"Increase your concentration"];
69        } else if (statusNo == STATUS_ACTIVE) {
70            [self logMessage:@"Flying!"];
71        }
72    }
73}
74
75- (void) sliderChanged
76{
77    [signalConverter setValuesForAttention:attentionThreshold.value meditation:meditationThreshold.value];
78}
79
80- (void) logMessage:(NSString *) str
81{
82    status.text = str;
83}
84
85#pragma mark - SignalConverterDelegate methods
86
87- (void) updatedValuesForSignal: (float)signalStrength
88                      attention: (float)attentionLevel
89                     meditation: (float)meditationLevel
90                          power: (float)powerLevel
91{
92    self.signal.progress = signalStrength;
93    self.signalPercent.text = [NSString stringWithFormat:@"%i%%", (int)(signalStrength * 100)];
94    self.attention.progress = attentionLevel;
95    self.attentionPercent.text = [NSString stringWithFormat:@"%i%%", (int)(attentionLevel * 100)];
96    self.meditation.progress = meditationLevel;
97    self.meditationPercent.text = [NSString stringWithFormat:@"%i%%", (int)(meditationLevel * 100)];
98    self.power.progress = powerLevel;
99    self.powerPercent.text = [NSString stringWithFormat:@"%i%%", (int)(powerLevel * 100)];
100    [self updateStatusImage:[self statusFromSignal:signalStrength power:powerLevel]];
101}
102
103- (int) statusFromSignal: (float)signalLevel power:(float)powerLevel
104{
105    if (signalLevel < 0.9) { // weak signal
106        return STATUS_CONNECTED;
107    } else if (powerLevel == 0.0) { // strong signal but thresholds not met
108        return STATUS_PROCESSING;
109    } else { // flight command sent to orbit
110        return STATUS_ACTIVE;
111    }
112}
113
114- (void) notifyHeadsetConnect
115{
116    [self logMessage:@"Headset connected"];
117    [self updateStatusImage:STATUS_CONNECTING];
118}
119
120- (void) notifyHeadsetDisconnect
121{
122    // Reset the outputs back to zero
123    [self resetViews];
124}
125
126- (void) notifyDeviceConnected:(NSString *)name
127{
128    [self logMessage:[NSString stringWithFormat:@"EEG device %@ connected", name]];
129    [self updateStatusImage:STATUS_CONNECTING];
130    connectButton.title = @"Disconnect";
131}
132
133- (void) appStopped
134{
135    [self resetViews]; // just in case it didn't happen on close
136}
137
138#pragma mark - button press events
139
140- (IBAction) connectButtonPressed:(id) sender {
141   
142    if (signalConverter.running) {
143        [signalConverter stopProcessing];
144        [self resetViews];
145    } else if (!signalConverter.isBluetoothReady) {
146        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];
147        [alert show];
148    } else if (!signalConverter.isVolumeMax) {
149        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];
150        [alert show];
151    } else if ([signalConverter startProcessing]) {
152        [self logMessage:@"Searching for device"];
153//        [self updateStatusImage:STATUS_CONNECTING];
154        connectButton.title = @"Disconnect";
155    } else {
156        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed to connect" message:@"Check the device is correctly paired on Bluetooth" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
157        [alert show];
158    }
159}
160
161@end
Note: See TracBrowser for help on using the repository browser.