Commit fe1ab10b authored by mail@toni-fetzer.de's avatar mail@toni-fetzer.de
Browse files

fixed bug that crashes phones below sdk 24

added simple plotting functionality
parent 0184bb81
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -6,8 +6,8 @@ android {
applicationId "de.tonifetzer.sensorrecorder"
minSdkVersion 14
targetSdkVersion 28
versionCode 3
versionName "1.1"
versionCode 5
versionName "1.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
......@@ -25,4 +25,5 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "com.androidplot:androidplot-core:1.5.6"
}
......@@ -20,7 +20,10 @@
android:roundIcon="@drawable/icon"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......
package de.tonifetzer.sensorrecorder;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
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 de.tonifetzer.sensorrecorder.sensors.Logger;
......@@ -18,10 +27,32 @@ import de.tonifetzer.sensorrecorder.sensors.SensorType;
public class MainActivity extends AppCompatActivity {
private static final int HISTORY_SIZE = 1500;
//ui in main layout
private View mainView;
private ToggleButton recButton;
private Button plotButton;
private TextView txtViewTimeCounter;
private TextView txtViewFilename;
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 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);
......@@ -31,7 +62,11 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//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
MainActivity.context = getApplicationContext();
......@@ -39,12 +74,46 @@ public class MainActivity extends AppCompatActivity {
//init the sensors of the phone
phoneSensors = new PhoneSensors(this);
//find ui elements
//find ui elements in main
recButton = findViewById(R.id.toggleButton);
plotButton = findViewById(R.id.buttonPlotter);
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);
//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) {}
......@@ -65,6 +134,74 @@ public class MainActivity extends AppCompatActivity {
}
});
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");
}
});
}
private void startRecording() {
......@@ -78,11 +215,13 @@ public class MainActivity extends AppCompatActivity {
private void stopRecording() {
phoneSensors.onPause(this);
dataLogger.stop();
cleanPlotterData();
}
private void addDataToFile(final SensorType id, final String csv) {
dataLogger.addCSV(id, csv);
provideDataToPlotter(id, csv);
runOnUiThread(new Runnable() {
@Override public void run() {
......@@ -112,4 +251,37 @@ public class MainActivity extends AppCompatActivity {
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();
}
}
......@@ -250,7 +250,7 @@ public class PhoneSensors extends MySensor implements SensorEventListener{
listener.onData(SensorType.LINEAR_ACCELERATION,
event.values[0] + ";" +
event.values[1] + ";" +
event.values[2] + ";"
event.values[2]
);
}
......
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@android:color/holo_green_light" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@android:color/holo_green_light" />
<item android:drawable="@android:color/holo_green_dark" />
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@android:color/holo_red_light" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@android:color/holo_red_light" />
<item android:drawable="@android:color/holo_red_dark" />
</selector>
\ No newline at end of file
......@@ -4,8 +4,8 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:background="#333"
android:keepScreenOn="true"
tools:context=".MainActivity">
<ToggleButton
......@@ -18,11 +18,11 @@
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginBottom="83dp"
android:background="@drawable/btnstartcolor"
android:padding="10dp"
android:text="ToggleButton"
android:textOff="Start Recording"
android:textOn="Stop Recording"
android:background="@drawable/btnstartcolor"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
......@@ -64,4 +64,16 @@
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.941" />
<Button
android:id="@+id/buttonPlotter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="88dp"
android:text="Show Dataplot"
app:layout_constraintStart_toStartOf="@+id/toggleButton"
app:layout_constraintTop_toBottomOf="@+id/toggleButton" />
</android.support.constraint.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#333"
xmlns:ap="http://schemas.android.com/apk/res-auto">
<com.androidplot.xy.XYPlot
style="@style/APDefacto.Dark"
android:id="@+id/plot"
android:layout_width="fill_parent"
android:layout_height="400dp"
ap:title="Accelerometer Data"
ap:rangeTitle="range"
ap:domainTitle="size"
ap:lineLabels="left|bottom"
ap:lineLabelRotationBottom="-45"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/buttonAcc"
android:layout_width="120dp"
android:layout_height="36dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Accelerometer" />
<Button
android:id="@+id/buttonLinearAcc"
android:layout_width="120dp"
android:layout_height="36dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Linear Acc" />
<Button
android:id="@+id/buttonGravity"
android:layout_width="120dp"
android:layout_height="36dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Gravity" />
<Button
android:id="@+id/buttonGyro"
android:layout_width="120dp"
android:layout_height="36dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Gyroscope" />
<Button
android:id="@+id/buttonBarometer"
android:layout_width="120dp"
android:layout_height="36dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:text="Barometer" />
<Button
android:id="@+id/buttonBack"
android:layout_width="120dp"
android:layout_height="36dp"
android:layout_marginLeft="250dp"
android:layout_marginTop="10dp"
android:text="Back" />
</LinearLayout>
</android.support.v7.widget.LinearLayoutCompat>
\ No newline at end of file
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