Changeset 2aabdf3 in orbit for iOS/Orbit/Orbit/ViewController.m


Ignore:
Timestamp:
12/25/12 11:53:41 (9 years ago)
Author:
Steve Castellotti <sc@…>
Branches:
master, RawEEG, Raw_EEG_Plot, Servo, Tab_Interface, pyramid
Children:
daf54d5
Parents:
eac712d
Message:
  • demo mode now cycles faster and repeatedly
  • cleaned up unused and duplicate image files
File:
1 edited

Legend:

Unmodified
Added
Removed
  • iOS/Orbit/Orbit/ViewController.m

    reac712d r2aabdf3  
    44// 
    55//  Created by Jonathon Horsman on 17/12/2012. 
    6 //  Copyright (c) 2012 Jonathon Horsman. All rights reserved. 
     6//  Copyright (c) 2012 Puzzlebox Productions, LLC. All rights reserved. 
    77// 
    88 
     
    1010#import <AVFoundation/AVFoundation.h> 
    1111 
    12 #define LOGGING true // set to false to stop each received signal being logged 
     12//#define LOGGING true // set to false to stop each received signal being logged 
     13#define LOGGING false // set to false to stop each received signal being logged 
    1314 
    1415#define AUDIO_FILE_NAME @"throttle_hover_ios.wav" // @"iOS_noflip.wav" // 
     16//#define AUDIO_FILE_NAME @"iOS_noflip.wav" 
    1517#define POOR_SIGNAL_KEY @"poorSignal" 
    1618#define ATTENTION_KEY @"eSenseAttention" 
     
    1820 
    1921@implementation ViewController { 
    20     // these 2 arrays hold the values of the thrust which should be applied to the helicopter 
    21     // (by way of volume level through the headphones) at each level of attention and meditation. 
    22     // this could be calculated on the fly, but because it happens ~20 times per second, the 
    23     // better option is to store all the values in arrays, and recalculate when the sliders are changed. 
    24     float attentionPower[101]; 
    25     float meditationPower[101]; 
    26      
    27     int currentAttentionLevel, currentMeditationLevel; // the latest readings from the headset 
    28     BOOL demoRunning; 
     22   // these 2 arrays hold the values of the thrust which should be applied to the helicopter 
     23   // (by way of volume level through the headphones) at each level of attention and meditation. 
     24   // this could be calculated on the fly, but because it happens ~20 times per second, the 
     25   // better option is to store all the values in arrays, and recalculate when the sliders are changed. 
     26   float attentionPower[101]; 
     27   float meditationPower[101]; 
     28    
     29   int currentAttentionLevel, currentMeditationLevel; // the latest readings from the headset 
     30   BOOL demoRunning; 
    2931} 
    3032 
     
    3335- (void)viewDidLoad 
    3436{ 
    35     [super viewDidLoad]; 
    36     [self prepareAudio]; 
    37     [self.attentionThreshold addTarget:self action:@selector(calculatePowerValues) forControlEvents:UIControlEventValueChanged]; 
    38     [self.meditationThreshold addTarget:self action:@selector(calculatePowerValues) forControlEvents:UIControlEventValueChanged]; 
    39     [self calculatePowerValues]; 
     37   [super viewDidLoad]; 
     38   [self prepareAudio]; 
     39   [self.attentionThreshold addTarget:self action:@selector(calculatePowerValues) forControlEvents:UIControlEventValueChanged]; 
     40   [self.meditationThreshold addTarget:self action:@selector(calculatePowerValues) forControlEvents:UIControlEventValueChanged]; 
     41   [self calculatePowerValues]; 
    4042} 
    4143 
    4244- (void)didReceiveMemoryWarning 
    4345{ 
    44     [super didReceiveMemoryWarning]; 
    45     // Dispose of any resources that can be recreated. 
    46     [self stopDemo]; 
     46   [super didReceiveMemoryWarning]; 
     47   // Dispose of any resources that can be recreated. 
     48   [self stopDemo]; 
    4749} 
    4850 
     
    5052- (void) appForegrounded 
    5153{ 
    52     EAAccessory *accessory = [[TGAccessoryManager sharedTGAccessoryManager] accessory]; 
    53     if (accessory != nil) { 
    54         [self logMessage:[NSString stringWithFormat:@"App opened with %@ connected. Starting data stream", accessory.name]]; 
    55         [[TGAccessoryManager sharedTGAccessoryManager] startStream]; 
    56     } 
    57     [self resetViews]; // just in case it didn't happen on close 
     54   EAAccessory *accessory = [[TGAccessoryManager sharedTGAccessoryManager] accessory]; 
     55   if (accessory != nil) { 
     56      [self logMessage:[NSString stringWithFormat:@"App opened with %@ connected. Starting data stream", accessory.name]]; 
     57      [[TGAccessoryManager sharedTGAccessoryManager] startStream]; 
     58   } 
     59   [self resetViews]; // just in case it didn't happen on close 
    5860} 
    5961 
     
    6163- (void) resetViews 
    6264{ 
    63     [self resetOutputToZero]; 
    64     self.log.text = @""; 
     65   [self resetOutputToZero]; 
     66   self.log.text = @""; 
    6567} 
    6668 
    6769- (void) resetOutputToZero 
    6870{ 
    69     [self showSignalStrength:[NSNumber numberWithInt:200]]; 
    70     [self setAttentionLevel:[NSNumber numberWithInt:0]]; 
    71     [self setMeditationLevel:[NSNumber numberWithInt:0]]; 
    72     [self showPowerLevel]; 
     71   [self showSignalStrength:[NSNumber numberWithInt:200]]; 
     72   [self setAttentionLevel:[NSNumber numberWithInt:0]]; 
     73   [self setMeditationLevel:[NSNumber numberWithInt:0]]; 
     74   [self showPowerLevel]; 
    7375} 
    7476 
    7577- (void) appClosed 
    7678{ 
    77     [self stopDemo]; 
    78     [self resetViews]; 
    79     if ([[TGAccessoryManager sharedTGAccessoryManager] connected]) { 
    80         [[TGAccessoryManager sharedTGAccessoryManager] stopStream]; 
    81     } 
     79   [self stopDemo]; 
     80   [self resetViews]; 
     81   if ([[TGAccessoryManager sharedTGAccessoryManager] connected]) { 
     82      [[TGAccessoryManager sharedTGAccessoryManager] stopStream]; 
     83   } 
    8284} 
    8385 
    8486- (void) prepareAudio 
    8587{ 
    86     NSURL *audioFilePath = [[NSBundle mainBundle] URLForResource:AUDIO_FILE_NAME withExtension:nil]; 
    87  
    88     NSError *error; 
    89     audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFilePath error:&error]; 
    90     if (!audioPlayer) { 
    91         [self logMessage:[NSString stringWithFormat:@"Failed to initialize audio player: %@", [error debugDescription]]]; 
    92     } else { 
    93         [audioPlayer prepareToPlay]; 
    94     } 
     88   NSURL *audioFilePath = [[NSBundle mainBundle] URLForResource:AUDIO_FILE_NAME withExtension:nil]; 
     89    
     90   NSError *error; 
     91   audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFilePath error:&error]; 
     92   if (!audioPlayer) { 
     93      [self logMessage:[NSString stringWithFormat:@"Failed to initialize audio player: %@", [error debugDescription]]]; 
     94   } else { 
     95      [audioPlayer prepareToPlay]; 
     96   } 
    9597} 
    9698 
    9799- (void) logMessage:(NSString *) str 
    98100{ 
    99     log.text = [[NSString alloc] initWithFormat:@"%@\n%@", str, log.text]; 
     101   log.text = [[NSString alloc] initWithFormat:@"%@\n%@", str, log.text]; 
    100102} 
    101103 
    102104- (void) logDataReceived:(NSDictionary *) data 
    103105{ 
    104     NSNumber *sig = [data valueForKey:POOR_SIGNAL_KEY]; 
    105     if (sig != NULL) { 
    106         int val = (200 - [sig intValue]) / 2; 
    107         [self logMessage:[NSString stringWithFormat:@"Signal strength: %u%%, Attention: %@%%, Meditation: %@%%", 
    108                           val, [data valueForKey:ATTENTION_KEY], [data valueForKey:MEDITATION_KEY]]]; 
    109     } 
    110  
     106   NSNumber *sig = [data valueForKey:POOR_SIGNAL_KEY]; 
     107   if (sig != NULL) { 
     108      int val = (200 - [sig intValue]) / 2; 
     109      [self logMessage:[NSString stringWithFormat:@"Signal strength: %u%%, Attention: %@%%, Meditation: %@%%", 
     110                        val, [data valueForKey:ATTENTION_KEY], [data valueForKey:MEDITATION_KEY]]]; 
     111   } 
     112    
    111113} 
    112114 
     
    114116 
    115117- (void)dataReceived:(NSDictionary *)data { 
    116     [self performSelectorOnMainThread:@selector(updatedSignalReceived:) 
    117                            withObject:data 
    118                         waitUntilDone:NO]; 
     118   [self performSelectorOnMainThread:@selector(updatedSignalReceived:) 
     119                          withObject:data 
     120                       waitUntilDone:NO]; 
    119121} 
    120122 
    121123- (void) updatedSignalReceived:(id) data 
    122124{ 
    123     if (LOGGING) { 
    124         [self logDataReceived: data]; 
    125     } 
    126     [self showSignalStrength:(NSNumber *)[data valueForKey:POOR_SIGNAL_KEY]]; 
    127     [self setAttentionLevel:(NSNumber *)[data valueForKey:ATTENTION_KEY]]; 
    128     [self setMeditationLevel:(NSNumber *)[data valueForKey:MEDITATION_KEY]]; 
    129     [self showPowerLevel]; 
    130     [self playAudio]; 
     125   if (LOGGING) { 
     126      [self logDataReceived: data]; 
     127   } 
     128   [self showSignalStrength:(NSNumber *)[data valueForKey:POOR_SIGNAL_KEY]]; 
     129   [self setAttentionLevel:(NSNumber *)[data valueForKey:ATTENTION_KEY]]; 
     130   [self setMeditationLevel:(NSNumber *)[data valueForKey:MEDITATION_KEY]]; 
     131   [self showPowerLevel]; 
     132   [self playAudio]; 
    131133} 
    132134 
    133135// The headset was switched on, start the data stream 
    134136- (void)accessoryDidConnect:(EAAccessory *)accessory { 
    135     [self logMessage:[NSString stringWithFormat:@"%@ was connected to this device. Starting data stream...", [accessory name]]]; 
    136     if ([[TGAccessoryManager sharedTGAccessoryManager] accessory] != nil) { 
    137         [[TGAccessoryManager sharedTGAccessoryManager] startStream]; 
    138     } 
     137   [self logMessage:[NSString stringWithFormat:@"%@ was connected to this device. Starting data stream...", [accessory name]]]; 
     138   if ([[TGAccessoryManager sharedTGAccessoryManager] accessory] != nil) { 
     139      [[TGAccessoryManager sharedTGAccessoryManager] startStream]; 
     140   } 
    139141} 
    140142 
     
    142144// Reset the outputs back to zero 
    143145- (void)accessoryDidDisconnect { 
    144     [self logMessage:@"Accessory was disconnected."]; 
    145     [self resetOutputToZero]; 
     146   [self logMessage:@"Accessory was disconnected."]; 
     147   [self resetOutputToZero]; 
    146148} 
    147149 
     
    151153- (void) calculatePowerValues 
    152154{ 
    153     float threshold = self.attentionThreshold.value; // this is the user-selected minimum threshold - between 0 and 1 
    154     for (int i = 0; i < 101; i++) { 
    155         attentionPower[i] = [self calculatePowerAt:(float)i/100 withThreshold:threshold]; 
    156     } 
    157     threshold = self.meditationThreshold.value; // this is the user-selected minimum threshold - between 0 and 1 
    158     for (int i = 0; i < 101; i++) { 
    159         meditationPower[i] = [self calculatePowerAt:(float)i/100 withThreshold:threshold]; 
    160     } 
     155   float threshold = self.attentionThreshold.value; // this is the user-selected minimum threshold - between 0 and 1 
     156   for (int i = 0; i < 101; i++) { 
     157      attentionPower[i] = [self calculatePowerAt:(float)i/100 withThreshold:threshold]; 
     158   } 
     159   threshold = self.meditationThreshold.value; // this is the user-selected minimum threshold - between 0 and 1 
     160   for (int i = 0; i < 101; i++) { 
     161      meditationPower[i] = [self calculatePowerAt:(float)i/100 withThreshold:threshold]; 
     162   } 
    161163} 
    162164 
     
    169171- (float) calculatePowerAt:(float)value withThreshold:(float)threshold 
    170172{ 
    171     if (value < threshold) { // threshold not met 
    172         return 0; 
    173     } 
    174     // e.g. if the threshold is 0.55 and the current value is 0.7: 
    175     // there is 0.45 remaining of the threshold slider, and the value is 0.15 past the threshold (0.7 - 0.55), which is 0.33 (0.15 / 0.45) 
    176     return (value - threshold) / (1 - threshold); 
     173   if (value < threshold) { // threshold not met 
     174      return 0; 
     175   } 
     176   // e.g. if the threshold is 0.55 and the current value is 0.7: 
     177   // there is 0.45 remaining of the threshold slider, and the value is 0.15 past the threshold (0.7 - 0.55), which is 0.33 (0.15 / 0.45) 
     178   return (value - threshold) / (1 - threshold); 
    177179} 
    178180 
    179181- (void) showSignalStrength:(NSNumber *) value 
    180182{ 
    181     if (value != nil) { 
    182         self.signal.progress = (200.0 - [value intValue]) / 200.0; 
    183     } 
     183   if (value != nil) { 
     184      self.signal.progress = (200.0 - [value intValue]) / 200.0; 
     185   } 
    184186} 
    185187 
    186188- (void) setAttentionLevel:(NSNumber *) value 
    187189{ 
    188     if (value != nil) { 
    189         currentAttentionLevel = [value intValue]; 
    190         self.attention.progress = (float)currentAttentionLevel / 100; 
    191     }     
     190   if (value != nil) { 
     191      currentAttentionLevel = [value intValue]; 
     192      self.attention.progress = (float)currentAttentionLevel / 100; 
     193   } 
    192194} 
    193195 
    194196- (void) setMeditationLevel:(NSNumber *) value 
    195197{ 
    196     if (value != nil) { 
    197         currentMeditationLevel = [value intValue]; 
    198         self.meditation.progress = (float)currentMeditationLevel / 100; 
    199     } 
     198   if (value != nil) { 
     199      currentMeditationLevel = [value intValue]; 
     200      self.meditation.progress = (float)currentMeditationLevel / 100; 
     201   } 
    200202} 
    201203 
     
    203205- (void) showPowerLevel 
    204206{ 
    205     self.power.progress = [self currentPowerLevel]; 
     207   self.power.progress = [self currentPowerLevel]; 
    206208} 
    207209 
     
    210212- (float) currentPowerLevel 
    211213{ 
    212     float powerLevel = attentionPower[currentAttentionLevel] + meditationPower[currentMeditationLevel]; 
    213     if (powerLevel > 1) { 
    214         return 1.0; 
    215     } 
    216     return powerLevel; 
     214   float powerLevel = attentionPower[currentAttentionLevel] + meditationPower[currentMeditationLevel]; 
     215   if (powerLevel > 1) { 
     216      return 1.0; 
     217   } 
     218   return powerLevel; 
    217219} 
    218220 
    219221- (void) playAudio 
    220222{ 
    221     audioPlayer.volume = [self currentPowerLevel]; 
    222     [audioPlayer play]; 
     223   audioPlayer.volume = [self currentPowerLevel]; 
     224   [audioPlayer play]; 
    223225} 
    224226 
     
    228230- (IBAction) demoButtonPressed:(id) sender 
    229231{ 
    230     if (demoRunning) { 
    231         [self stopDemo]; 
    232     } else { 
    233         [self startDemo]; 
    234     } 
     232   if (demoRunning) { 
     233      [self stopDemo]; 
     234   } else { 
     235      [self startDemo]; 
     236   } 
    235237} 
    236238 
    237239- (void) stopDemo 
    238240{ 
    239     demoRunning = NO; 
    240     [self resetOutputToZero]; 
    241     [demoButton setTitle:@"Demo" forState:UIControlStateNormal]; 
    242     if (audioPlayer != NULL && [audioPlayer isPlaying]) { 
    243         [audioPlayer stop]; 
    244     } 
     241   demoRunning = NO; 
     242   [self resetOutputToZero]; 
     243   [demoButton setTitle:@"Demo" forState:UIControlStateNormal]; 
     244   if (audioPlayer != NULL && [audioPlayer isPlaying]) { 
     245      [audioPlayer stop]; 
     246   } 
    245247} 
    246248 
    247249- (void) startDemo 
    248250{ 
    249     demoRunning = YES; 
    250     [self logMessage:@"Running demo"]; 
    251     [demoButton setTitle:@"Stop" forState:UIControlStateNormal]; 
    252     [self performSelectorInBackground:@selector(generateDemoData) withObject:nil]; 
     251   demoRunning = YES; 
     252   [self logMessage:@"Running demo"]; 
     253   [demoButton setTitle:@"Stop" forState:UIControlStateNormal]; 
     254   [self performSelectorInBackground:@selector(generateDemoData) withObject:nil]; 
    253255} 
    254256 
     
    257259- (void) generateDemoData 
    258260{ 
    259     for (int i = 0; i < 100; i++) { 
    260         if (!demoRunning) { 
     261   while (demoRunning) { 
     262      for (int i = 0; i < 100; i++) { 
     263         if (!demoRunning) { 
    261264            break; // break out if we stop the demo 
    262         } 
    263         NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys: 
    264                                 [NSNumber numberWithInt:i], ATTENTION_KEY, 
    265                                 [NSNumber numberWithInt:i], MEDITATION_KEY, 
    266                                 [NSNumber numberWithInt:0], POOR_SIGNAL_KEY, 
    267                                 nil]; 
    268         [self dataReceived:values]; 
    269         [NSThread sleepForTimeInterval:1]; 
    270     } 
     265         } 
     266         NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys: 
     267                                 [NSNumber numberWithInt:i], ATTENTION_KEY, 
     268                                 [NSNumber numberWithInt:i * 0.8], MEDITATION_KEY, 
     269                                 [NSNumber numberWithInt:0], POOR_SIGNAL_KEY, 
     270                                 nil]; 
     271         [self dataReceived:values]; 
     272         [NSThread sleepForTimeInterval:0.1]; 
     273      } 
     274       
     275      for (int i = 100; i > 0; i--) { 
     276         if (!demoRunning) { 
     277            break; // break out if we stop the demo 
     278         } 
     279         NSDictionary *values = [NSDictionary dictionaryWithObjectsAndKeys: 
     280                                 [NSNumber numberWithInt:i], ATTENTION_KEY, 
     281                                 [NSNumber numberWithInt:i * 0.8], MEDITATION_KEY, 
     282                                 [NSNumber numberWithInt:0], POOR_SIGNAL_KEY, 
     283                                 nil]; 
     284         [self dataReceived:values]; 
     285         [NSThread sleepForTimeInterval:0.1]; 
     286      } 
     287       
     288   } 
    271289} 
    272290@end 
Note: See TracChangeset for help on using the changeset viewer.