Commit 12460262 authored by Loris's avatar Loris
Browse files

start to work

parent 42390af8
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<codeStyleSettings language="XML">
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="4" />
</indentOptions>
<arrangement>
<rules>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:android</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>xmlns:.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:id</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*:name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>name</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>style</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>^$</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>http://schemas.android.com/apk/res/android</XML_NAMESPACE>
</AND>
</match>
<order>ANDROID_ATTRIBUTE_ORDER</order>
</rule>
</section>
<section>
<rule>
<match>
<AND>
<NAME>.*</NAME>
<XML_ATTRIBUTE />
<XML_NAMESPACE>.*</XML_NAMESPACE>
</AND>
</match>
<order>BY_NAME</order>
</rule>
</section>
</rules>
</arrangement>
</codeStyleSettings>
</code_scheme>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="BintrayJCenter" />
<option name="name" value="BintrayJCenter" />
<option name="url" value="https://jcenter.bintray.com/" />
</remote-repository>
<remote-repository>
<option name="id" value="Google" />
<option name="name" value="Google" />
<option name="url" value="https://dl.google.com/dl/android/maven2/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -20,6 +20,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'co.nstant.in:cbor:0.9'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
......
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
......@@ -20,6 +20,8 @@ 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;
......@@ -38,24 +40,6 @@ public class MainActivity extends AppCompatActivity {
private TextView txtViewFilesize;
private boolean isMainView = true;
//ui in plotter layout
private View plotView;
private Button backButton;
private Button accButton;
private Button linearAccButton;
private Button gravityButton;
private Button gyroscopeButton;
private Button barometerButton;
private Button orientationButton;
private ToggleButton pausePlotButton;
private SimpleXYSeries xBuffer;
private SimpleXYSeries yBuffer;
private SimpleXYSeries zBuffer;
private Redrawer redrawer;
private XYPlot plotClass;
private SensorType currentSensorToPlot = SensorType.ACCELEROMETER;
private PhoneSensors phoneSensors;
private final Logger dataLogger = new Logger(this);
......@@ -67,7 +51,6 @@ public class MainActivity extends AppCompatActivity {
//this is super ugly. normally we would use fragments to switch ui
mainView = getLayoutInflater().inflate(R.layout.activity_main, null);
plotView = getLayoutInflater().inflate(R.layout.simple_1d_plot, null);
setContentView(mainView);
//this is a small hack to get a static context
......@@ -78,50 +61,20 @@ public class MainActivity extends AppCompatActivity {
//find ui elements in main
recButton = findViewById(R.id.toggleButton);
plotButton = findViewById(R.id.buttonPlotter);
txtViewTimeCounter = findViewById(R.id.textViewTimeCounter);
// txtViewTimeCounter = findViewById(R.id.textViewTimeCounter);
txtViewFilename = findViewById(R.id.textViewFilename);
txtViewFilesize = findViewById(R.id.textViewFilesize);
//find ui elements in plotter
backButton = plotView.findViewById(R.id.buttonBack);
accButton = plotView.findViewById(R.id.buttonAcc);
linearAccButton = plotView.findViewById(R.id.buttonLinearAcc);
gravityButton = plotView.findViewById(R.id.buttonGravity);
gyroscopeButton = plotView.findViewById(R.id.buttonGyro);
barometerButton = plotView.findViewById(R.id.buttonBarometer);
orientationButton = plotView.findViewById(R.id.buttonOrientation);
pausePlotButton = plotView.findViewById(R.id.buttonPausePlotting);
//init the plotter data
xBuffer = new SimpleXYSeries("x");
xBuffer.useImplicitXVals();
yBuffer = new SimpleXYSeries("y");
yBuffer.useImplicitXVals();
zBuffer = new SimpleXYSeries("z");
zBuffer.useImplicitXVals();
//format the plotter window
plotClass = plotView.findViewById(R.id.plot);
plotClass.addSeries(xBuffer, new LineAndPointFormatter(
Color.rgb(100, 100, 200), null, null, null));
plotClass.addSeries(yBuffer, new LineAndPointFormatter(
Color.rgb(100, 200, 100), null, null, null));
plotClass.addSeries(zBuffer, new LineAndPointFormatter(
Color.rgb(200, 100, 100), null, null, null));
plotClass.setDomainStepMode(StepMode.INCREMENT_BY_VAL);
plotClass.setDomainStepValue(HISTORY_SIZE/10);
plotClass.setLinesPerRangeLabel(3);
plotClass.setDomainLabel("index");
plotClass.setDomainBoundaries(0, HISTORY_SIZE, BoundaryMode.FIXED);
//redrawer for dynamic data in plotter
redrawer = new Redrawer( plotClass, 100, false);
//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) {addDataToFile(id, csv); }
@Override public void onData(final SensorType id, final String csv) throws CborException {
}
@Override
public void onData(final Entry entry) throws CborException {
addDataToFile(entry);
}
});
recButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
......@@ -133,103 +86,14 @@ public class MainActivity extends AppCompatActivity {
}
else{
buttonView.setBackgroundColor(Color.parseColor("#FF669900"));
stopRecording();
try {
stopRecording();
} catch (CborException e) {
e.printStackTrace();
}
}
}
});
plotButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(plotView);
isMainView = false;
redrawer.start();
}
});
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(mainView);
isMainView = true;
redrawer.pause();
}
});
accButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cleanPlotterData();
currentSensorToPlot = SensorType.ACCELEROMETER;
plotClass.setTitle("Accelerometer Data");
}
});
linearAccButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cleanPlotterData();
currentSensorToPlot = SensorType.LINEAR_ACCELERATION;
plotClass.setTitle("Linear Accelerometer Data");
}
});
gravityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cleanPlotterData();
currentSensorToPlot = SensorType.GRAVITY;
plotClass.setTitle("Gravity Data");
}
});
gyroscopeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cleanPlotterData();
currentSensorToPlot = SensorType.GYROSCOPE;
plotClass.setTitle("Gyroscope Data");
}
});
barometerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cleanPlotterData();
currentSensorToPlot = SensorType.PRESSURE;
plotClass.setTitle("Barometer Data");
}
});
orientationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cleanPlotterData();
currentSensorToPlot = SensorType.ORIENTATION_NEW;
plotClass.setTitle("Orientation Data");
}
});
pausePlotButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
pausePlotButton.setTextOn("Resume Plot");
redrawer.pause();
}
else{
pausePlotButton.setTextOn("Pause Plot");
redrawer.start();
}
}
});
}
private void startRecording() {
......@@ -240,16 +104,14 @@ public class MainActivity extends AppCompatActivity {
txtViewFilename.setText(path.substring(path.length()-17));
}
private void stopRecording() {
private void stopRecording() throws CborException {
phoneSensors.onPause(this);
dataLogger.stop();
cleanPlotterData();
}
private void addDataToFile(final SensorType id, final String csv) {
private void addDataToFile(Entry entry) throws CborException {
dataLogger.addCSV(id, csv);
provideDataToPlotter(id, csv);
dataLogger.addEntry(entry);
runOnUiThread(new Runnable() {
@Override public void run() {
......@@ -264,7 +126,6 @@ public class MainActivity extends AppCompatActivity {
//set time (of course, this is not perfectly accurate, however for this purpose its okay)
long minutes = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - dataLogger.getStartTS());
long seconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - dataLogger.getStartTS());
txtViewTimeCounter.setText(minutes + ":" + (seconds - (minutes * 60)));
}
}
......@@ -272,44 +133,10 @@ public class MainActivity extends AppCompatActivity {
}
//This is also part of the hack to get a static context
private static Context context;
public static Context getAppContext() {
return MainActivity.context;
}
public void provideDataToPlotter(final SensorType sensorID, final String csv){
if(currentSensorToPlot == sensorID){
String[] data = csv.split(";");
// get rid the oldest sample in history:
if (xBuffer.size() > HISTORY_SIZE) {
xBuffer.removeFirst();
yBuffer.removeFirst();
zBuffer.removeFirst();
}
if(data.length > 0){
xBuffer.addLast(null, Float.parseFloat(data[0]));
}
if(data.length > 1){
yBuffer.addLast(null, Float.parseFloat(data[1]));
}
if(data.length > 2){
zBuffer.addLast(null, Float.parseFloat(data[2]));
}
}
}
public void cleanPlotterData(){
xBuffer.clear();
yBuffer.clear();
zBuffer.clear();
}
}
package de.tonifetzer.sensorrecorder.sensors;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import co.nstant.in.cbor.CborBuilder;
import co.nstant.in.cbor.CborEncoder;
import co.nstant.in.cbor.CborException;
public class Entry implements Serializable {
private String sensorName;
private int sensorId;
private Long ts;
private float x;
private float y;
private float z;
private int prediction;
public Entry(long ts, float x, float y, float z, int sId) {
this.ts = ts;
this.sensorId = sId;
this.x = x;
this.y = y;
this.z = z;
}
public int getSensorId() {
return sensorId;
}
public long getDelta(long start) {
return this.ts - start;
}
public Long getTs() {
return ts;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getZ() {
return z;
}
public int getPrediction() {
return prediction;
}
public String getSensorName() {
return this.sensorName;
}
}
......@@ -4,8 +4,18 @@ import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import co.nstant.in.cbor.CborBuilder;
import co.nstant.in.cbor.CborEncoder;
import co.nstant.in.cbor.CborException;
import co.nstant.in.cbor.builder.ArrayBuilder;
import co.nstant.in.cbor.model.DataItem;
import android.provider.Settings.Secure;
/**
* log sensor data to file
......@@ -15,10 +25,17 @@ public final class Logger {
private static final int FLUSH_LIMIT = 2*1024*1024;
private StringBuilder sb = new StringBuilder();
final CborBuilder cborBuilder = new CborBuilder();
ArrayBuilder arrayBuilder;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CborEncoder cborEncoder = new CborEncoder(baos);
CborEncoder encoder;
private File file;
private FileOutputStream fos;
private Context context;
private String androidId;
private int entries = 0;
private int sizeCurrent = 0;
private int sizeTotal = 0;
......@@ -33,19 +50,23 @@ public final class Logger {
/** start logging (into RAM) */
public final void start() {
androidId = Secure.getString(context.getContentResolver(),
Secure.ANDROID_ID);
// start empty
sb.setLength(0);
entries = 0;
sizeTotal = 0;
sizeCurrent = 0;
// starting timestamp
startTS = System.currentTimeMillis();
// open the output-file immeditaly (to get permission errors)
// but do NOT yet write anything to the file
final DataFolder folder = new DataFolder(context, "sensorOutFiles");
file = new File(folder.getFolder(), startTS + ".csv");
file = new File(folder.getFolder(), startTS + ".cbor");
try {
fos = new FileOutputStream(file);
......@@ -54,11 +75,15 @@ public final class Logger {
throw new MyException("error while opening log-file", e);
}
arrayBuilder = cborBuilder.addArray();
arrayBuilder.add(startTS);
arrayBuilder.add(androidId);
}
/** stop logging and flush RAM-data to the flash-chip */
public final void stop() {
public final void stop() throws CborException {
synchronized (this) {
arrayBuilder.end();
flush(true);
close();
}
......@@ -74,7 +99,7 @@ public final class Logger {
public int getNumEntries() {return entries;}
/** add a new CSV entry for the given sensor number to the internal buffer */
public final void addCSV(final SensorType sensorNr, final String csv) {
public final void addCSV(final SensorType sensorNr, final String csv) throws CborException {
synchronized (this) {
final long relTS = System.currentTimeMillis() - startTS;
sb.append(relTS); // relative timestamp (uses less space)
......@@ -82,6 +107,7 @@ public final class Logger {
sb.append(sensorNr.id());
sb.append(';');
sb.append(csv);
Log.d("TAG", "addCSV: " + csv);
sb.append('\n');
++entries;