~ubuntu-branches/ubuntu/precise/csound/precise

« back to all changes in this revision

Viewing changes to android/CsoundAndroid/src/com/csounds/valueCacheable/CachedAccelerometer.java

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2012-04-19 09:26:46 UTC
  • mfrom: (3.2.19 sid)
  • Revision ID: package-import@ubuntu.com-20120419092646-96xbj1n6atuqosk2
Tags: 1:5.17.6~dfsg-1
* New upstream release
 - Do not build the wiimote opcodes (we need wiiuse).
* Add new API function to symbols file
* Disable lua opcodes, they were broken. Requires OpenMP to be enabled.
* Backport fixes from upstream:
  - Link dssi4cs with dl. Backport
  - Fix building of CsoundAC

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 
 
3
 CachedAccelerometer.java:
 
4
 
 
5
 Copyright (C) 2011 Victor Lazzarini, Steven Yi
 
6
 
 
7
 This file is part of Csound Android Examples.
 
8
 
 
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.   
 
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
package com.csounds.valueCacheable;
 
27
 
 
28
import java.util.List;
 
29
 
 
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;
 
36
 
 
37
import com.csounds.CsoundObj;
 
38
 
 
39
import csnd.CsoundMYFLTArray;
 
40
 
 
41
public class CachedAccelerometer extends AbstractValueCacheable implements SensorEventListener {
 
42
 
 
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";
 
46
        
 
47
        CsoundMYFLTArray channelPtrX;
 
48
        CsoundMYFLTArray channelPtrY;
 
49
        CsoundMYFLTArray channelPtrZ;
 
50
        
 
51
        double x, y, z;
 
52
        
 
53
        Context context;
 
54
        
 
55
        SensorManager sensorManager;
 
56
        Sensor sensor;
 
57
        
 
58
        public CachedAccelerometer(Context context) {
 
59
                this.context = context;
 
60
                x = y = z = 0;
 
61
        }
 
62
        
 
63
        @Override
 
64
        public void setup(CsoundObj csoundObj) {
 
65
                
 
66
                 sensorManager = (SensorManager) context.
 
67
                        getSystemService(Context.SENSOR_SERVICE);
 
68
                List<Sensor> sensors = sensorManager.getSensorList(
 
69
                        Sensor.TYPE_ACCELEROMETER);
 
70
                
 
71
            if(sensors.size() > 0) {
 
72
                sensor = sensors.get(0);
 
73
                
 
74
                sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_FASTEST);
 
75
                
 
76
                channelPtrX = csoundObj.getInputChannelPtr(CS_ACCEL_X);
 
77
                        channelPtrY = csoundObj.getInputChannelPtr(CS_ACCEL_Y);
 
78
                        channelPtrZ = csoundObj.getInputChannelPtr(CS_ACCEL_Z); 
 
79
            } else {
 
80
                Log.d("CachedAccelerometer", "Unable to get Accelerometer sensor.");
 
81
            }
 
82
                
 
83
        }
 
84
 
 
85
        @Override
 
86
        public void updateValuesToCsound() {
 
87
                if(channelPtrX != null) {
 
88
                        channelPtrX.SetValue(0, x);
 
89
                        channelPtrY.SetValue(0, y);
 
90
                        channelPtrZ.SetValue(0, z);
 
91
                }
 
92
        }
 
93
        
 
94
        @Override
 
95
        public void cleanup() {
 
96
                if(channelPtrX != null) {
 
97
                        channelPtrX.Clear();
 
98
                        channelPtrY.Clear();
 
99
                        channelPtrZ.Clear();
 
100
                        
 
101
                        channelPtrX = null;
 
102
                        channelPtrY = null;
 
103
                        channelPtrZ = null;
 
104
                        
 
105
                        sensorManager.unregisterListener(this);
 
106
                }
 
107
        }
 
108
 
 
109
        // SENSOR EVENT LISTENER
 
110
        
 
111
        @Override
 
112
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
 
113
                
 
114
        }
 
115
 
 
116
        @Override
 
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();
 
121
        }
 
122
 
 
123
}