Browse Source

Abstract accelerometer sampling behaviour

master
Chris Smith 14 years ago
parent
commit
c13381be3c

BIN
code/ActivityRecorder/dist/ActivityRecorder.apk View File


+ 17
- 38
code/ActivityRecorder/src/uk/co/md87/android/activityrecorder/RecorderService.java View File

@@ -38,6 +38,7 @@ import uk.co.md87.android.common.Aggregator;
38 38
 import uk.co.md87.android.common.ModelReader;
39 39
 import uk.co.md87.android.common.accel.AccelReader;
40 40
 import uk.co.md87.android.common.accel.AccelReaderFactory;
41
+import uk.co.md87.android.common.accel.Sampler;
41 42
 
42 43
 /**
43 44
  *
@@ -63,60 +64,36 @@ public class RecorderService extends Service {
63 64
 
64 65
     };
65 66
 
66
-    private final Runnable sampleRunnable = new Runnable() {
67
+    private final Runnable registerRunnable = new Runnable() {
67 68
 
68 69
         public void run() {
69
-            final float[] values = reader.getSample();
70
-
71
-            data[nextSample * 2] = values[0];
72
-            data[nextSample * 2 + 1] = values[1];
73
-
74
-            if (++nextSample == 128) {
75
-                float[] cache = new float[256];
76
-                System.arraycopy(data, 0, cache, 0, 256);
77
-                analyse(cache);
78
-
79
-                reader.stopSampling();
80
-                return;
81
-            }
82
-
83
-            handler.postDelayed(sampleRunnable, 50);
70
+            //Log.i(getClass().getName(), "Registering");
71
+            sampler.start();
72
+            
73
+            handler.postDelayed(registerRunnable, 30000);
84 74
         }
85
-        
75
+
86 76
     };
87 77
 
88
-    private final Runnable registerRunnable = new Runnable() {
78
+    private final Runnable analyseRunnable = new Runnable() {
89 79
 
90 80
         public void run() {
91
-            //Log.i(getClass().getName(), "Registering");
92
-            nextSample = 0;
93
-
94
-            reader.startSampling();
95
-
96
-            handler.postDelayed(sampleRunnable, 50);
97
-            handler.postDelayed(registerRunnable, 30000);
81
+            final Intent intent = new Intent(RecorderService.this, ClassifierService.class);
82
+            intent.putExtra("data", sampler.getData());
83
+            startService(intent);
98 84
         }
99 85
 
100 86
     };
101 87
 
102 88
     final Handler handler = new Handler();
103 89
 
104
-    float[] data = new float[256];
105
-    volatile int nextSample = 0;
106
-
107 90
     boolean running;
108 91
     final Aggregator aggregator = new Aggregator();
109 92
     AccelReader reader;
93
+    Sampler sampler;
110 94
     public static Map<Float[], String> model;
111 95
     private final List<Classification> classifications = new ArrayList<Classification>();
112 96
 
113
-    public void analyse(float[] data) {
114
-        //Log.i(getClass().getName(), "Analysing");
115
-        final Intent intent = new Intent(this, ClassifierService.class);
116
-        intent.putExtra("data", data);
117
-        startService(intent);
118
-    }
119
-
120 97
     @Override
121 98
     public IBinder onBind(Intent arg0) {
122 99
         return binder;
@@ -129,6 +106,7 @@ public class RecorderService extends Service {
129 106
         running = true;
130 107
 
131 108
         reader = new AccelReaderFactory().getReader(this);
109
+        sampler = new Sampler(handler, reader, analyseRunnable);
132 110
 
133 111
         init();
134 112
     }
@@ -162,10 +140,11 @@ public class RecorderService extends Service {
162 140
         if (running) {
163 141
             running = false;
164 142
 
165
-            handler.removeCallbacks(sampleRunnable);
143
+            if (sampler != null) {
144
+                sampler.stop();
145
+            }
146
+            
166 147
             handler.removeCallbacks(registerRunnable);
167
-
168
-            reader.stopSampling();
169 148
         }
170 149
     }
171 150
 

+ 81
- 0
code/Common/accel/Sampler.java View File

@@ -0,0 +1,81 @@
1
+/*
2
+ * Copyright (c) 2009-2010 Chris Smith
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package uk.co.md87.android.common.accel;
24
+
25
+import android.os.Handler;
26
+
27
+/**
28
+ *
29
+ * @author chris
30
+ */
31
+public class Sampler implements Runnable {
32
+
33
+    private final Handler handler;
34
+    private final AccelReader reader;
35
+    private final Runnable finishedRunnable;
36
+
37
+    private float[] data;
38
+    private int nextSample;
39
+
40
+    public Sampler(final Handler handler, final AccelReader reader,
41
+            final Runnable finishedRunnable) {
42
+        this.handler = handler;
43
+        this.reader = reader;
44
+        this.finishedRunnable = finishedRunnable;
45
+    }
46
+
47
+    public void start() {
48
+        data = new float[256];
49
+        nextSample = 0;
50
+
51
+        reader.startSampling();
52
+
53
+        handler.postDelayed(this, 50);
54
+    }
55
+
56
+    public float[] getData() {
57
+        return data;
58
+    }
59
+
60
+    /** {@inheritDoc} */
61
+    @Override
62
+    public void run() {
63
+        final float[] values = reader.getSample();
64
+
65
+        data[nextSample * 2] = values[0];
66
+        data[nextSample * 2 + 1] = values[1];
67
+
68
+        if (++nextSample == 128) {
69
+            reader.stopSampling();
70
+            finishedRunnable.run();
71
+            return;
72
+        }
73
+
74
+        handler.postDelayed(this, 50);
75
+    }
76
+
77
+    public void stop() {
78
+        handler.removeCallbacks(this);
79
+    }
80
+
81
+}

Loading…
Cancel
Save