| 1 |
/* |
| 2 |
|
| 3 |
CsoundMotion.m: |
| 4 |
|
| 5 |
Copyright (C) 2014 Steven Yi, Victor Lazzarini, Aurelius Prochazka |
| 6 |
|
| 7 |
This file is part of Csound iOS Examples. |
| 8 |
|
| 9 |
The Csound for iOS Library is free software; you can redistribute it |
| 10 |
and/or modify it under the terms of the GNU Lesser General Public |
| 11 |
License as published by the Free Software Foundation; either |
| 12 |
version 2.1 of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
Csound is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU Lesser General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU Lesser General Public |
| 20 |
License along with Csound; if not, write to the Free Software |
| 21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 22 |
02111-1307 USA |
| 23 |
|
| 24 |
*/ |
| 25 |
|
| 26 |
#import "CsoundMotion.h" |
| 27 |
#import "CsoundAccelerometerBinding.h" |
| 28 |
#import "CsoundAttitudeBinding.h" |
| 29 |
#import "CsoundGyroscopeBinding.h" |
| 30 |
|
| 31 |
@interface CsoundMotion () { |
| 32 |
CsoundObj *csoundObj; |
| 33 |
} |
| 34 |
@end |
| 35 |
|
| 36 |
@implementation CsoundMotion |
| 37 |
|
| 38 |
- (instancetype)initWithCsoundObj:(CsoundObj *)csound { |
| 39 |
self = [super init]; |
| 40 |
if (self) { |
| 41 |
csoundObj = csound; |
| 42 |
_motionManager = [[CMMotionManager alloc] init]; |
| 43 |
[csoundObj addListener:self]; |
| 44 |
} |
| 45 |
return self; |
| 46 |
} |
| 47 |
|
| 48 |
- (void)enableAccelerometer { |
| 49 |
|
| 50 |
if (!_motionManager.accelerometerAvailable) { |
| 51 |
NSLog(@"Accelerometer not available"); |
| 52 |
} |
| 53 |
|
| 54 |
CsoundAccelerometerBinding *accelerometer = [[CsoundAccelerometerBinding alloc] init:_motionManager]; |
| 55 |
[csoundObj addBinding:accelerometer]; |
| 56 |
|
| 57 |
|
| 58 |
_motionManager.accelerometerUpdateInterval = 1 / 100.0; // 100 hz |
| 59 |
|
| 60 |
[_motionManager startAccelerometerUpdates]; |
| 61 |
} |
| 62 |
|
| 63 |
- (void)enableGyroscope { |
| 64 |
|
| 65 |
if (!_motionManager.isGyroAvailable) { |
| 66 |
NSLog(@"Gyroscope not available"); |
| 67 |
} |
| 68 |
|
| 69 |
CsoundGyroscopeBinding *gyro = [[CsoundGyroscopeBinding alloc] init:_motionManager]; |
| 70 |
[csoundObj addBinding:gyro]; |
| 71 |
|
| 72 |
_motionManager.gyroUpdateInterval = 1 / 100.0; // 100 hz |
| 73 |
|
| 74 |
[_motionManager startGyroUpdates]; |
| 75 |
} |
| 76 |
|
| 77 |
- (void)enableAttitude { |
| 78 |
if (!_motionManager.isDeviceMotionAvailable) { |
| 79 |
NSLog(@"Attitude not available"); |
| 80 |
} |
| 81 |
|
| 82 |
CsoundAttitudeBinding *attitude = [[CsoundAttitudeBinding alloc] init:_motionManager]; |
| 83 |
[csoundObj addBinding:attitude]; |
| 84 |
|
| 85 |
_motionManager.deviceMotionUpdateInterval = 1 / 100.0; // 100hz |
| 86 |
|
| 87 |
[_motionManager startDeviceMotionUpdates]; |
| 88 |
} |
| 89 |
|
| 90 |
#pragma mark CsoundObjListener |
| 91 |
|
| 92 |
- (void)csoundObjCompleted:(CsoundObj *)csoundObj { |
| 93 |
[_motionManager stopAccelerometerUpdates]; |
| 94 |
[_motionManager stopGyroUpdates]; |
| 95 |
[_motionManager stopDeviceMotionUpdates]; |
| 96 |
} |
| 97 |
|
| 98 |
@end |