Here is the code of the accelerometer values application for Android. I did this application in a couple of hours to test the Android platform so the features are very basic. I’m currently working on a new version that will include logging of the values and a few other things.
However, here is the code of the two main classes of the application. It will compile with recent versions of the Android SDK even though some methods have been deprecated meanwhile.
package com.arak.sensor.accele;
import android.view.View;
import android.hardware.SensorManager;
import android.hardware.SensorListener;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Paint;
import android.graphics.Color;
import java.util.LinkedList;
import java.text.DecimalFormat;
public class AcceleView extends View implements SensorListener {
public final static int MAX_VALUES = 200;
public final static int SCALING = 3; // for G to graphic
public final static int GSCALING = 2; // for panels
public final static int MARGIN_TOP = 15;
public final static int SEPARATION = 20;
public final static int MARGIN_LEFT = 10;
private long timekeeper;
private final LinkedList<float[]> fifo;
private float height;
private float width;
private float halfRectHeight;
private float xYLine;
private float yYLine;
private float zYLine;
private float pX;
private float pY;
private float pZ;
private float minX;
private float minY;
private float minZ;
private float maxX;
private float maxY;
private float maxZ;
public AcceleView(Context context, SensorManager sma) {
super(context);
timekeeper = android.os.SystemClock.uptimeMillis();
fifo = new LinkedList<float[]>();
int mask = 0;
mask |= SensorManager.SENSOR_ORIENTATION;
mask |= SensorManager.SENSOR_ACCELEROMETER;
sma.registerListener(this, mask, SensorManager.SENSOR_DELAY_FASTEST);
pX = 0f;
pY = 0f;
pZ = 0f;
minX = 0f;
minY = 0f;
minZ = 0f;
maxX = 0f;
maxY = 0f;
maxZ = 0f;
}
protected void onDraw(Canvas canvas) {
height = canvas.getHeight();
width = canvas.getWidth();
halfRectHeight = SCALING * 10 *GSCALING;
xYLine = MARGIN_TOP + SCALING * 10 *GSCALING;
yYLine = xYLine + halfRectHeight * 2 + SEPARATION;
zYLine = yYLine + halfRectHeight * 2 + SEPARATION;
clear(canvas);
drawGridAndLastValues(canvas);
drawValues(canvas);
}
public void onSensorChanged(int sensor, float[] values) {
if (android.os.SystemClock.uptimeMillis() < timekeeper + 20) return;
timekeeper = android.os.SystemClock.uptimeMillis();
final float[] val = new float[]{values[0], values[1], values[2]}; // defensive copy because keeps the same object
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
if (fifo.size() > MAX_VALUES) fifo.poll();
fifo.add(val);
minX = Math.min(minX, val[0]);
minY = Math.min(minY, val[1]);
minZ = Math.min(minZ, val[2]);
maxX = Math.max(maxX, val[0]);
maxY = Math.max(maxY, val[1]);
maxZ = Math.max(maxZ, val[2]);
invalidate();
}
}
public void onAccuracyChanged(int i, int i1) {
/* @todo implement method */
}
private void clear(Canvas canvas) {
Rect rect = new Rect();
rect.set(0, 0, canvas.getWidth(), canvas.getHeight());
Paint p = new Paint();
p.setStyle(Paint.Style.FILL);
p.setColor(Color.WHITE);
canvas.drawRect(rect, p);
}
private void drawGridAndLastValues(Canvas canvas) {
Paint p = new Paint();
// print measured values
DecimalFormat df = new DecimalFormat("##.#");
String xStringVal = df.format(pX);
String yStringVal = df.format(pY);
String zStringVal = df.format(pZ);
String xMinStringVal = df.format(minX);
String yMinStringVal = df.format(minY);
String zMinStringVal = df.format(minZ);
String xMaxStringVal = df.format(maxX);
String yMaxStringVal = df.format(maxY);
String zMaxStringVal = df.format(maxZ);
//x
String xText = "X values [last: " + xStringVal + " - min:" + xMinStringVal + " - max:" + xMaxStringVal + "]";
p.setColor(Color.LTGRAY);
canvas.drawRect(MARGIN_LEFT, xYLine - halfRectHeight, width - MARGIN_LEFT, xYLine + halfRectHeight, p);
p.setColor(Color.BLACK);
canvas.drawLine(MARGIN_LEFT, xYLine, width - MARGIN_LEFT, xYLine, p);
canvas.drawText(xText, MARGIN_LEFT + width / 6, xYLine - halfRectHeight - 2, p);
canvas.drawText("+2G", MARGIN_LEFT, xYLine - halfRectHeight + 10, p);
canvas.drawText("-2G", MARGIN_LEFT, xYLine + halfRectHeight, p);
//y
String yText = "Y values [last: " + yStringVal + " - min:" + yMinStringVal + " - max:" + yMaxStringVal + "]";
p.setColor(Color.LTGRAY);
canvas.drawRect(MARGIN_LEFT, yYLine - halfRectHeight, width - MARGIN_LEFT, yYLine + halfRectHeight, p);
p.setColor(Color.BLACK);
canvas.drawLine(MARGIN_LEFT, yYLine, width - MARGIN_LEFT, yYLine, p);
canvas.drawText(yText, MARGIN_LEFT + width / 6, yYLine - halfRectHeight - 2, p);
canvas.drawText("+2G", MARGIN_LEFT, yYLine - halfRectHeight + 10, p);
canvas.drawText("-2G", MARGIN_LEFT, yYLine + halfRectHeight, p);
//z
String zText = "Z values [last:" + zStringVal + " - min:" + zMinStringVal + " - max:" + zMaxStringVal + "]";
p.setColor(Color.LTGRAY);
canvas.drawRect(MARGIN_LEFT, zYLine - halfRectHeight, width - MARGIN_LEFT, zYLine + halfRectHeight, p);
p.setColor(Color.BLACK);
canvas.drawLine(MARGIN_LEFT, zYLine, width - MARGIN_LEFT, zYLine, p);
canvas.drawText(zText, MARGIN_LEFT + width / 6, zYLine - halfRectHeight - 2, p);
canvas.drawText("+2G", MARGIN_LEFT, zYLine - halfRectHeight + 10, p);
canvas.drawText("-2G", MARGIN_LEFT, zYLine + halfRectHeight, p);
}
private void drawValues(Canvas canvas) {
float len = (width - MARGIN_LEFT * 2) / MAX_VALUES;
Paint p = new Paint();
float xVal = MARGIN_LEFT;
boolean first = true;
for (float[] f : fifo) {
float x = f[0];
float y = f[1];
float z = f[2];
if (first) {
pX = x;
pY = y;
pZ = z;
first = false;
}
p.setColor(Color.GREEN);
canvas.drawLine(xVal, xYLine - pX * SCALING, xVal + len, xYLine - x * SCALING, p);
p.setColor(Color.RED);
canvas.drawLine(xVal, yYLine - pY * SCALING, xVal + len, yYLine - y * SCALING, p);
p.setColor(Color.BLUE);
canvas.drawLine(xVal, zYLine - pZ * SCALING, xVal + len, zYLine - z * SCALING, p);
xVal += len;
pX = x;
pY = y;
pZ = z;
}
}
}// END
package com.arak.sensor.accele;
import android.app.Activity;
import android.os.Bundle;
import android.hardware.SensorManager;
import android.content.Context;
public class ShowSensor extends Activity {
private AcceleView av;
@Override
public void onCreate(Bundle args) {
super.onCreate(args);
av = new AcceleView(this, (SensorManager) getSystemService(Context.SENSOR_SERVICE));
setTitle("Show Sensor Values");
setContentView(av);
}
}
Very helpful, thanks! Do you do any consulting?
hi,
I don’t do any consulting right now, I’m just busy enough!
Regards
B.
[...] This post was mentioned on Twitter by Alan Garcia. Alan Garcia said: Accelerometer Values « karanar http://icio.us/1e2hx3 [...]