Compass in iPhone

Previous topic - Next topic

Kitty Hello

put that in an external .mm file and include the proper headers. Then make a C function wrapper around it.

... can someone help here?

matchy

#16
 :whistle:

When I test on my iPad 1, there is no gyroscope on that device (but it does have a compass) so I haven't bothered to place parameters in the calls, but this prototype should work (output to NSLog only so the rotationRate will need to be move to gyro_read later).    :noggin:
gyro.h
Code (glbasic) Select

#import <CoreMotion/CoreMotion.h>
@interface gyro: NSObject {
CMMotionManager *motionManager;
NSOperation *opQ;
}
@end


gyro.mm
Code (glbasic) Select

#import "gyro.h"
@implementation gyro
#pragma mark -
#pragma mark Memory management
- (void)dealloc {
[motionManager dealloc];
[super dealloc];
}
#pragma mark -
#pragma mark GLB Gyro
- (void) _glb_gyro_init {
NSLog(@"Gyro init.");
motionManager = [[CMMotionManager alloc] init];
motionManager.gyroUpdateInterval = 1.0/60.0;
if (motionManager.gyroAvailable) {
NSLog(@"Gyroscope is on device.");
[motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
   withHandler: ^(CMGyroData *gyroData, NSError *error)
{
CMRotationRate rotate = gyroData.rotationRate;
NSLog(@"rotation rate = [%f %f %f]", rotate.x, rotate.y, rotate.z);
}];
} else {
NSLog(@"No gyroscope on device.");
[motionManager release];
}
}
- (void) _glb_gyro_read {
NSLog(@"Gyro read.");
}
extern "C" {
gyro *my_gyro;
void glb_gyro_init() {
my_gyro = [[gyro alloc] init];
[my_gyro _glb_gyro_init];
}
void glb_gyro_read() {
[my_gyro _glb_gyro_read];
}
}
@end


gyro.gbas
Code (glbasic) Select

// --------------------------------- //
// Project: Gyroscrope for iOS4 - prototype wrapper
// Start: Tuesday, June 21, 2011
// IDE Version: 9.104

// Wrapper install: In Xcode, drop the gyro.h & gyro.mm files in Classes folder and add CoreMotion.framework

TYPE _mouse
x
y
b1
b2
ENDTYPE

main_gyro()

FUNCTION main_gyro:
LOCAL mo AS _mouse

glb_gyro_init()
WHILE TRUE
MOUSESTATE mo.x, mo.y, mo.b1, mo.b2
IF mo.b1 = 1
glb_gyro_read() // test output in NSLog
PRINT "read: YES", 0, 20
ELSE
PRINT "read: NO", 0, 20
ENDIF
PRINT "Gyro", 0, 0
SHOWSCREEN
WEND
ENDFUNCTION

?IFDEF IPHONE
IMPORT "C" void glb_gyro_init()
IMPORT "C" void glb_gyro_read()
?ELSE
FUNCTION glb_gyro_init:
RETURN 0
ENDFUNCTION

FUNCTION glb_gyro_read:
RETURN 0
ENDFUNCTION
?ENDIF