| 1 |
/* |
| 2 |
|
| 3 |
HardwareTestViewController.m: |
| 4 |
|
| 5 |
Copyright (C) 2014 Steven Yi, Aurelius Prochazka |
| 6 |
Updated in 2017 by Dr. Richard Boulanger, Nikhil Singh |
| 7 |
|
| 8 |
This file is part of Csound iOS Examples. |
| 9 |
|
| 10 |
The Csound for iOS Library is free software; you can redistribute it |
| 11 |
and/or modify it under the terms of the GNU Lesser General Public |
| 12 |
License as published by the Free Software Foundation; either |
| 13 |
version 2.1 of the License, or (at your option) any later version. |
| 14 |
|
| 15 |
Csound is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU Lesser General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU Lesser General Public |
| 21 |
License along with Csound; if not, write to the Free Software |
| 22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 23 |
02111-1307 USA |
| 24 |
|
| 25 |
*/ |
| 26 |
|
| 27 |
#import "HardwareTestViewController.h" |
| 28 |
|
| 29 |
@implementation HardwareTestViewController |
| 30 |
|
| 31 |
-(void)viewDidLoad { |
| 32 |
self.title = @"14. Hardware: Motion Control"; |
| 33 |
[super viewDidLoad]; |
| 34 |
|
| 35 |
motionManager = [[CMMotionManager alloc] init]; |
| 36 |
|
| 37 |
[motionManager startAccelerometerUpdates]; |
| 38 |
[motionManager startGyroUpdates]; |
| 39 |
[motionManager startDeviceMotionUpdates]; |
| 40 |
|
| 41 |
[motionManager setAccelerometerUpdateInterval:.1]; |
| 42 |
[motionManager setGyroUpdateInterval:.1]; |
| 43 |
[motionManager setDeviceMotionUpdateInterval:.1]; |
| 44 |
|
| 45 |
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accData, NSError *error){ |
| 46 |
if(error!= nil) NSLog(@"%@", [error localizedDescription]); |
| 47 |
|
| 48 |
accX.text = [NSString stringWithFormat:@"%.3f", accData.acceleration.x]; |
| 49 |
accY.text = [NSString stringWithFormat:@"%.3f", accData.acceleration.y]; |
| 50 |
accZ.text = [NSString stringWithFormat:@"%.3f", accData.acceleration.z]; |
| 51 |
}]; |
| 52 |
|
| 53 |
[motionManager startGyroUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMGyroData *gyroData, NSError *error){ |
| 54 |
if(error!= nil) NSLog(@"%@", [error localizedDescription]); |
| 55 |
|
| 56 |
gyroX.text = [NSString stringWithFormat:@"%.3f", gyroData.rotationRate.x]; |
| 57 |
gyroY.text = [NSString stringWithFormat:@"%.3f", gyroData.rotationRate.y]; |
| 58 |
gyroZ.text = [NSString stringWithFormat:@"%.3f", gyroData.rotationRate.z]; |
| 59 |
}]; |
| 60 |
|
| 61 |
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motionData, NSError *error) { |
| 62 |
if(error!= nil) NSLog(@"%@", [error localizedDescription]); |
| 63 |
|
| 64 |
roll.text = [NSString stringWithFormat:@"%.3f", motionData.attitude.roll]; |
| 65 |
pitch.text = [NSString stringWithFormat:@"%.3f", motionData.attitude.pitch]; |
| 66 |
yaw.text = [NSString stringWithFormat:@"%.3f", motionData.attitude.yaw]; |
| 67 |
}]; |
| 68 |
} |
| 69 |
|
| 70 |
-(IBAction) toggleOnOff:(id)component { |
| 71 |
UISwitch *uiswitch = (UISwitch *)component; |
| 72 |
NSLog(@"Status: %d", [uiswitch isOn]); |
| 73 |
|
| 74 |
if(uiswitch.on) { |
| 75 |
|
| 76 |
NSString *tempFile = [[NSBundle mainBundle] pathForResource:@"hardwareTest" |
| 77 |
ofType:@"csd"]; |
| 78 |
NSLog(@"FILE PATH: %@", tempFile); |
| 79 |
|
| 80 |
[self.csound stop]; |
| 81 |
|
| 82 |
self.csound = [[CsoundObj alloc] init]; |
| 83 |
[self.csound addListener:self]; |
| 84 |
|
| 85 |
CsoundMotion *csoundMotion = [[CsoundMotion alloc] initWithCsoundObj:self.csound]; |
| 86 |
|
| 87 |
[csoundMotion enableAccelerometer]; |
| 88 |
[csoundMotion enableAttitude]; |
| 89 |
[csoundMotion enableGyroscope]; |
| 90 |
|
| 91 |
[self.csound play:tempFile]; |
| 92 |
|
| 93 |
} else { |
| 94 |
[self.csound stop]; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
- (IBAction)showInfo:(UIButton *)sender { |
| 99 |
UIViewController *infoVC = [[UIViewController alloc] init]; |
| 100 |
infoVC.modalPresentationStyle = UIModalPresentationPopover; |
| 101 |
|
| 102 |
UIPopoverPresentationController *popover = infoVC.popoverPresentationController; |
| 103 |
popover.sourceView = sender; |
| 104 |
popover.sourceRect = sender.bounds; |
| 105 |
[infoVC setPreferredContentSize:CGSizeMake(300, 220)]; |
| 106 |
|
| 107 |
UITextView *infoText = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, infoVC.preferredContentSize.width, infoVC.preferredContentSize.height)]; |
| 108 |
infoText.editable = NO; |
| 109 |
infoText.selectable = NO; |
| 110 |
NSString *description = @"Hardware: Motion Control shows how to use the device's motion sensor data as a set of controllers for Csound, and also displays this data in a set of UILabels. Accelerometer X controls oscillator frequency, Attitude: Yaw controls filter cutoff, Attitude: Pitch controls amplitude, and Attitude: Roll controls filter resonance."; |
| 111 |
[infoText setAttributedText:[[NSAttributedString alloc] initWithString:description]]; |
| 112 |
infoText.font = [UIFont fontWithName:@"Menlo" size:16]; |
| 113 |
[infoVC.view addSubview:infoText]; |
| 114 |
popover.delegate = self; |
| 115 |
|
| 116 |
[popover setPermittedArrowDirections:UIPopoverArrowDirectionUp]; |
| 117 |
|
| 118 |
[self presentViewController:infoVC animated:YES completion:nil]; |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
#pragma mark CsoundObjListener |
| 124 |
|
| 125 |
-(void)csoundObjCompleted:(CsoundObj *)csoundObj { |
| 126 |
[mSwitch setOn:NO animated:YES]; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
@end |