Skip to content

Instantly share code, notes, and snippets.

@joshuajnoble
Created February 19, 2013 13:57
Show Gist options
  • Save joshuajnoble/4986115 to your computer and use it in GitHub Desktop.
Save joshuajnoble/4986115 to your computer and use it in GitHub Desktop.
Simple sensor logging application for Processing Android
import ketai.sensors.*;
import android.content.Context;
import android.hardware.SensorManager;
import android.os.Environment;
import java.io.File;
ArrayList<String> logString;
KetaiSensor sensor;
float[] accelerometer = new float[3];
float[] magneticField = new float[3];
float[] gyro = new float[3];
boolean log;
SensorManager sensorManager;
void setup()
{
logString = new ArrayList();
sensorManager = (SensorManager) getApplicationContext().getSystemService(Context.SENSOR_SERVICE);
sensor = new KetaiSensor(this);
sensor.start();
orientation(LANDSCAPE);
textSize(36);
}
void draw()
{
if(log)
{
String s = accelerometer[0] + " " + accelerometer[1] + " " + accelerometer[2] + "\n";
s += magneticField[0] + " " + magneticField[1] + " " + magneticField[2] + "\n";
s += gyro[0] + " " + gyro[1] + " " + gyro[2] + "\n";
s += "---------------------";
logString.add(s);
}
}
void mousePressed()
{
if(log){
String[] s = new String[logString.size()];
int i = 0;
while ( i < logString.size() ) {
s[i] = logString.get(i);
i++;
}
String SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
println( SDCARD );
saveStrings(SDCARD + File.separator + "circle_log.txt", s);
}
log = !log;
}
void onAccelerometerEvent(float x, float y, float z, long time, int accuracy)
{
accelerometer[0] = x;
accelerometer[1] = y;
accelerometer[2] = z;
}
void onMagneticFieldEvent(float x, float y, float z, long time, int accuracy)
{
magneticField[0] = x;
magneticField[1] = y;
magneticField[2] = z;
}
void onOrientationEvent(float x, float y, float z)
{
gyro[0] = x;
gyro[1] = y;
gyro[2] = z;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment