Accelerometer support


#1

It seems that Juce doesn’t have a class to support accelerometers on iOS and Android, but maybe I’m missing something ?

And if not, is such a feature planned for future versions of Juce ?


#2

Not sure exactly how much functionality you need but I wrote this simple class for a friend a few years ago as a wrapper around the iOS accelerometer. It’s quick and dirty and largely unchecked (not sure if it will even compile any more), something I just threw together in a few minutes as he didn’t know any Obj-C.

To be honest I think the motion APIs have been improved a lot in iOS since then but this might be enough to give you a nudge in the right direction. If you can get something together that’s fairly complete and documented I’m sure Jules would take a look.

[code]//
// AccelerometerManager.h
// iOS
//
// Created by David Rowland on 22/01/2012.
// Copyright © 2012 dRowAudio. All rights reserved.
//

//==============================================================================
class Accelerometer
{
public:
//==============================================================================
Accelerometer();

virtual ~Accelerometer();

virtual void accelerometerChanged (float x, float y, float z) = 0;

private:
//==============================================================================
void* accelerometerManager;
};[/code]

[code]//
// AccelerometerManager.m
// iOS
//
// Created by David Rowland on 22/01/2012.
// Copyright © 2012 dRowAudio. All rights reserved.
//

#import “AccelerometerManager.h”

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <UIKit/UIKit.h>

//==============================================================================
@interface AccelerometerManager : NSObject
{
Accelerometer* owner;
}

@end

//==============================================================================
@implementation AccelerometerManager

  • (void) accelerometer: (UIAccelerometer *) accelerometer
    didAccelerate: (UIAcceleration *) acceleration
    {
    owner->accelerometerChanged (acceleration.x, acceleration.y, acceleration.z);
    }

  • (void) startAccelerometer
    {
    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    accelerometer.delegate = self;
    accelerometer.updateInterval = 0.25;
    }

  • (void) stopAccelerometer
    {
    UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];
    accelerometer.delegate = nil;
    }

  • (id) initWithOwner: (Accelerometer*) owner_
    {
    if ((self = [super init]) != nil)
    {
    owner = owner_;
    [self startAccelerometer];
    };

    return self;
    }

  • (void) dealloc
    {
    [self stopAccelerometer];
    }

@end

//==============================================================================
Accelerometer::Accelerometer()
{
AccelerometerManager* newManager = [[AccelerometerManager alloc] initWithOwner: this];
[newManager retain];

accelerometerManager = newManager;

}

Accelerometer::~Accelerometer()
{
[((AccelerometerManager*) accelerometerManager) release];
}
[/code]


#3

Great, thanks for you help ! :slight_smile:


#4

Anything new in the accelerometers support on iOS and Android ? Is such a feature planned for future versions of Juce ?

Thanks.