/*
* Copyright 2011 Greg Milette and Adam Stroud
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package root.gast.playground.sensor;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import root.gast.playground.R;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.widget.TextView;
/**
* When the camera is taking a picture of north, it changes green
* @author David Hutchinson <<a href="mailto:david.n.hutch@gmail.com">david.n.hutch@gmail.com</a>>
*/
public class NorthFinder extends Activity implements SensorEventListener
{
private static final int ANGLE = 20;
private TextView tv;
private GLSurfaceView mGLSurfaceView;
private MyRenderer mRenderer;
private SensorManager mSensorManager;
private Sensor mRotVectSensor;
private float[] orientationVals = new float[3];
private final float[] mRotationMatrix = new float[16];
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sensors_north_main);
mRenderer = new MyRenderer();
mGLSurfaceView = (GLSurfaceView) findViewById(R.id.glsurfaceview);
mGLSurfaceView.setRenderer(mRenderer);
tv = (TextView) findViewById(R.id.tv);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mRotVectSensor =
mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
}
@Override
protected void onResume()
{
super.onResume();
mSensorManager.registerListener(this, mRotVectSensor, 10000);
}
@Override
protected void onPause()
{
super.onPause();
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event)
{
// It is good practice to check that we received the proper sensor event
if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR)
{
// Convert the rotation-vector to a 4x4 matrix.
SensorManager.getRotationMatrixFromVector(mRotationMatrix,
event.values);
SensorManager
.remapCoordinateSystem(mRotationMatrix,
SensorManager.AXIS_X, SensorManager.AXIS_Z,
mRotationMatrix);
SensorManager.getOrientation(mRotationMatrix, orientationVals);
// Optionally convert the result from radians to degrees
orientationVals[0] = (float) Math.toDegrees(orientationVals[0]);
orientationVals[1] = (float) Math.toDegrees(orientationVals[1]);
orientationVals[2] = (float) Math.toDegrees(orientationVals[2]);
tv.setText(" Yaw: " + orientationVals[0] + "\n Pitch: "
+ orientationVals[1] + "\n Roll (not used): "
+ orientationVals[2]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// no-op
}
class MyRenderer implements GLSurfaceView.Renderer
{
public void onDrawFrame(GL10 gl)
{
// Clear screen
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// Detect if the device is pointing within +/- ANGLE of north
if (orientationVals[0] < ANGLE && orientationVals[0] > -ANGLE
&& orientationVals[1] < ANGLE
&& orientationVals[1] > -ANGLE)
{
gl.glClearColor(0, 1, 0, 1); // Make background green
}
else
{
gl.glClearColor(1, 0, 0, 1); // Make background red
}
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
// no-op
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
// no-op
}
}
}