Commit 7ffabe07 authored by Loris's avatar Loris
Browse files

sensor clean

parent 446b489e
package de.tonifetzer.sensorrecorder;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
......@@ -15,21 +14,11 @@ import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.androidplot.util.Redrawer;
import com.androidplot.xy.BoundaryMode;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.StepMode;
import com.androidplot.xy.XYPlot;
import java.util.concurrent.TimeUnit;
import co.nstant.in.cbor.CborException;
import de.tonifetzer.sensorrecorder.sensors.Entry;
import de.tonifetzer.sensorrecorder.sensors.Logger;
import de.tonifetzer.sensorrecorder.sensors.MySensor;
import de.tonifetzer.sensorrecorder.sensors.PhoneSensors;
import de.tonifetzer.sensorrecorder.sensors.SensorType;
public class MainActivity extends AppCompatActivity {
......@@ -75,8 +64,6 @@ public class MainActivity extends AppCompatActivity {
//set the listener
phoneSensors.setListener(new MySensor.SensorListener(){
@Override public void onData(final String csv) {}
@Override public void onData(final SensorType id, final String csv) throws CborException {
}
@Override
public void onData(final Entry entry) throws CborException {
addDataToFile(entry);
......
......@@ -15,8 +15,6 @@ public abstract class MySensor {
void onData(final String csv);
/** received data from the given sensor */
void onData(final SensorType id, final String csv) throws CborException;
void onData(final Entry entry) throws CborException;
}
......
......@@ -13,6 +13,7 @@ import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import co.nstant.in.cbor.CborException;
......@@ -24,21 +25,20 @@ import co.nstant.in.cbor.CborException;
public class PhoneSensors extends MySensor implements SensorEventListener{
private SensorManager sensorManager;
private Sensor acc;
private Sensor lin_acc;
private Sensor gyro;
private List<Sensor> all;
private int DELAY = 10000; // 100Hz in µs
/** ctor */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public PhoneSensors(final Activity act){
// fetch the sensor manager from the activity
sensorManager = (SensorManager) act.getSystemService(Context.SENSOR_SERVICE);
// try to get each sensor
acc = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
lin_acc = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
gyro = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
// dump sensor-vendor info to file
all = sensorManager.getSensorList(Sensor.TYPE_ALL);
// dump sensor-vendor info to file
dumpVendors(act);
}
......@@ -63,9 +63,9 @@ public class PhoneSensors extends MySensor implements SensorEventListener{
sb.append(NL);
// construct sensor details
dumpSensor(sb, SensorType.ACCELEROMETER, acc);
dumpSensor(sb, SensorType.LINEAR_ACCELERATION, lin_acc);
dumpSensor(sb, SensorType.GYROSCOPE, gyro);
for (Sensor s: all) {
dumpSensor(sb, s.getType(), s);
}
// write
fos.write(sb.toString().getBytes());
fos.close();
......@@ -77,10 +77,9 @@ public class PhoneSensors extends MySensor implements SensorEventListener{
}
/** dump all details of the given sensor into the provided stringbuilder */
private void dumpSensor(final StringBuilder sb, final SensorType type, final Sensor sensor) {
private void dumpSensor(final StringBuilder sb, final int type, final Sensor sensor) {
sb.append("[Sensor]").append(NL);
sb.append("\tour_id: ").append(type.id()).append(NL);
sb.append("\ttype: ").append(type).append(NL);
sb.append("\tid: ").append(type).append(NL);
if (sensor != null) {
sb.append("\tVendor: ").append(sensor.getVendor()).append(NL);
......@@ -105,38 +104,20 @@ public class PhoneSensors extends MySensor implements SensorEventListener{
long now = System.currentTimeMillis();
Log.d("PhoneSensors", "onSensorChanged: " + event.sensor.getType());
if(event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
// inform listeners
if (listener != null){
try {
listener.onData(new Entry(now, event.values[0], event.values[1], event.values[2],
Sensor.TYPE_GYROSCOPE));
} catch (CborException e) {
e.printStackTrace();
}
}
}
else if(event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
// inform listeners
if (listener != null){
try {
listener.onData(new Entry(now, event.values[0], event.values[1], event.values[2],
Sensor.TYPE_LINEAR_ACCELERATION));
} catch (CborException e) {
e.printStackTrace();
}
try {
if (event.values.length == 1) {
listener.onData(new Entry(now, event.values[0], 0, 0,
event.sensor.getType()));
}
}
else if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// inform listeners
if (listener != null){
try {
listener.onData(new Entry(now, event.values[0], event.values[1], event.values[2],
Sensor.TYPE_ACCELEROMETER));
} catch (CborException e) {
e.printStackTrace();
}
else if (event.values.length == 2) {
listener.onData(new Entry(now, event.values[0], event.values[1], 0,
event.sensor.getType()));
} else {
listener.onData(new Entry(now, event.values[0], event.values[1], event.values[2],
event.sensor.getType()));
}
} catch (CborException e) {
e.printStackTrace();
}
}
......@@ -145,16 +126,16 @@ public class PhoneSensors extends MySensor implements SensorEventListener{
// nothing to-do here
}
@Override
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onResume(final Activity act) {
// attach as listener to each of the available sensors
// for now we just user delay fastest, but we'll adjust that.
registerIfPresent(acc, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(gyro, SensorManager.SENSOR_DELAY_FASTEST);
registerIfPresent(lin_acc, SensorManager.SENSOR_DELAY_FASTEST);
for (Sensor s: all) {
registerIfPresent(s, DELAY);
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void registerIfPresent(final Sensor sens, final int delay) {
if (sens != null) {
sensorManager.registerListener(this, sens, delay);
......
package de.tonifetzer.sensorrecorder.sensors;
/**
* The different sensor types and their id's
*/
public enum SensorType {
ACCELEROMETER(0),
// GRAVITY(1),
LINEAR_ACCELERATION(2),
GYROSCOPE(3),
/*MAGNETIC_FIELD(4),
PRESSURE(5),
ORIENTATION_NEW(6),
ROTATION_MATRIX(7),
WIFI(8),
BEACON(9),
RELATIVE_HUMIDITY(10),
ORIENTATION_OLD(11),
ROTATION_VECTOR(12),
LIGHT(13),
AMBIENT_TEMPERATURE(14),
HEART_RATE(15),*/
;
private int id;
SensorType(final int id) {
this.id = id;
}
public final int id() {return id;}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment