Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit 7784d91

Browse files
committed
added compass example
1 parent 1bed101 commit 7784d91

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Based on the compass example from Rolf van Gelder.
2+
// http://cagewebdev.com/index.php/android-processing-examples/
3+
4+
import android.content.Context;
5+
import android.hardware.Sensor;
6+
import android.hardware.SensorManager;
7+
import android.hardware.SensorEvent;
8+
import android.hardware.SensorEventListener;
9+
10+
Context context;
11+
SensorManager manager;
12+
SensorListener listener;
13+
Sensor accelerometer;
14+
Sensor magnetometer;
15+
16+
float easing = 0.05;
17+
18+
float azimuth;
19+
float pitch;
20+
float roll;
21+
22+
void setup() {
23+
fullScreen(P2D);
24+
orientation(PORTRAIT);
25+
}
26+
27+
void draw() {
28+
background(255);
29+
30+
float cx = width * 0.5;
31+
float cy = height * 0.4;
32+
float radius = 0.8 * cx;
33+
34+
translate(cx, cy);
35+
36+
noFill();
37+
stroke(0);
38+
strokeWeight(2);
39+
ellipse(0, 0, radius*2, radius*2);
40+
line(0, -cy, 0, -radius);
41+
42+
fill(192, 0, 0);
43+
noStroke();
44+
rotate(-azimuth);
45+
beginShape();
46+
vertex(-30, 40);
47+
vertex(0, 0);
48+
vertex(30, 40);
49+
vertex(0, -radius);
50+
endShape();
51+
}
52+
53+
public void onResume() {
54+
super.onResume();
55+
56+
// This works for regular apps
57+
//Context context = surface.getActivity();
58+
59+
// This for fragments:
60+
context = (Context) surface.getComponent();
61+
62+
listener = new SensorListener();
63+
manager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
64+
accelerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
65+
magnetometer = manager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
66+
67+
manager.registerListener(listener, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
68+
manager.registerListener(listener, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
69+
}
70+
71+
public void onPause() {
72+
super.onPause();
73+
if (manager != null) manager.unregisterListener(listener);
74+
}
75+
76+
class SensorListener implements SensorEventListener {
77+
float[] gravity = new float[3];
78+
float[] geomagnetic = new float[3];
79+
float[] I = new float[16];
80+
float[] R = new float[16];
81+
float orientation[] = new float[3];
82+
83+
public void onSensorChanged(SensorEvent event) {
84+
if (event.accuracy == SensorManager.SENSOR_STATUS_ACCURACY_LOW) return;
85+
86+
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
87+
arrayCopy(event.values, geomagnetic);
88+
}
89+
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
90+
arrayCopy(event.values, gravity);
91+
}
92+
if (SensorManager.getRotationMatrix(R, I, gravity, geomagnetic)) {
93+
SensorManager.getOrientation(R, orientation);
94+
azimuth += easing * (orientation[0] - azimuth);
95+
pitch += easing * (orientation[1] - pitch);
96+
roll += easing * (orientation[2] - roll);;
97+
}
98+
}
99+
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
100+
}

0 commit comments

Comments
 (0)