3
CachedAccelerometer.java:
5
Copyright (C) 2011 Victor Lazzarini, Steven Yi
7
This file is part of Csound Android Examples.
9
The Csound Android Examples 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.
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.
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
26
package com.csounds.valueCacheable;
28
import java.util.List;
30
import android.content.Context;
31
import android.hardware.Sensor;
32
import android.hardware.SensorEvent;
33
import android.hardware.SensorEventListener;
34
import android.hardware.SensorManager;
35
import android.util.Log;
37
import com.csounds.CsoundObj;
39
import csnd.CsoundMYFLTArray;
41
public class CachedAccelerometer extends AbstractValueCacheable implements SensorEventListener {
43
private static final String CS_ACCEL_X = "accelerometerX";
44
private static final String CS_ACCEL_Y = "accelerometerY";
45
private static final String CS_ACCEL_Z = "accelerometerZ";
47
CsoundMYFLTArray channelPtrX;
48
CsoundMYFLTArray channelPtrY;
49
CsoundMYFLTArray channelPtrZ;
55
SensorManager sensorManager;
58
public CachedAccelerometer(Context context) {
59
this.context = context;
64
public void setup(CsoundObj csoundObj) {
66
sensorManager = (SensorManager) context.
67
getSystemService(Context.SENSOR_SERVICE);
68
List<Sensor> sensors = sensorManager.getSensorList(
69
Sensor.TYPE_ACCELEROMETER);
71
if(sensors.size() > 0) {
72
sensor = sensors.get(0);
74
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
76
channelPtrX = csoundObj.getInputChannelPtr(CS_ACCEL_X);
77
channelPtrY = csoundObj.getInputChannelPtr(CS_ACCEL_Y);
78
channelPtrZ = csoundObj.getInputChannelPtr(CS_ACCEL_Z);
80
Log.d("CachedAccelerometer", "Unable to get Accelerometer sensor.");
86
public void updateValuesToCsound() {
87
if(channelPtrX != null) {
88
channelPtrX.SetValue(0, x);
89
channelPtrY.SetValue(0, y);
90
channelPtrZ.SetValue(0, z);
95
public void cleanup() {
96
if(channelPtrX != null) {
105
sensorManager.unregisterListener(this);
109
// SENSOR EVENT LISTENER
112
public void onAccuracyChanged(Sensor sensor, int accuracy) {
117
public void onSensorChanged(SensorEvent event) {
118
x = event.values[0] / sensor.getMaximumRange();
119
y = event.values[1] / sensor.getMaximumRange();
120
z = event.values[2] / sensor.getMaximumRange();