Skip to content

Commit

Permalink
fix drawing of graphs and draw data after orientation change
Browse files Browse the repository at this point in the history
  • Loading branch information
marcnause committed Nov 19, 2024
1 parent caf0c60 commit c8639ec
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 94 deletions.
12 changes: 8 additions & 4 deletions app/src/main/java/io/pslab/sensors/AbstractSensorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,20 @@ public boolean onOptionsItemSelected(MenuItem item) {

protected abstract int getTitleResId();

protected interface SensorDataFetch {
protected abstract class SensorDataFetch {

default void execute() {
protected float getTimeElapsed() {
return (System.currentTimeMillis() - getStartTime()) / 1000f;
}

protected void execute() {
getSensorData();
updateUi();
}

void getSensorData();
abstract void getSensorData();

void updateUi();
abstract void updateUi();

}

Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/io/pslab/sensors/SensorADS1115.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
y.setLabelCount(10);

y2.setDrawGridLines(false);

if (savedInstanceState != null) {
sensorDataFetch.updateUi();
}
}

@Override
Expand All @@ -114,9 +118,11 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
outState.putParcelableArrayList(KEY_ENTRIES, entries);
}

private class SensorDataFetch implements AbstractSensorActivity.SensorDataFetch {
private class SensorDataFetch extends AbstractSensorActivity.SensorDataFetch {

private int dataADS1115;
/* Initialization required if updateUi is executed before getSensorData */
private float timeElapsed = getTimeElapsed();

@Override
public void getSensorData() {
Expand All @@ -128,8 +134,8 @@ public void getSensorData() {
Log.e(TAG, "Error getting sensor data.", e);
}

long timeElapsed = (System.currentTimeMillis() - getStartTime()) / 1000;
entries.add(new Entry((float) timeElapsed, dataADS1115));
timeElapsed = getTimeElapsed();
entries.add(new Entry(timeElapsed, dataADS1115));
}

@Override
Expand All @@ -142,8 +148,7 @@ public void updateUi() {
mChart.setData(data);
mChart.notifyDataSetChanged();
mChart.setVisibleXRangeMaximum(10);
mChart.moveViewToX(data.getEntryCount());
mChart.invalidate();
mChart.moveViewToX(timeElapsed);
}
}

Expand Down
20 changes: 12 additions & 8 deletions app/src/main/java/io/pslab/sensors/SensorAPDS9960.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
yProximity.setLabelCount(10);

yProximity2.setDrawGridLines(false);

if (savedInstanceState != null) {
sensorDataFetch.updateUi();
}
}

@Override
Expand All @@ -156,12 +160,14 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
outState.putParcelableArrayList(KEY_ENTRIES_PROXIMITY, entriesProximity);
}

private class SensorDataFetch implements AbstractSensorActivity.SensorDataFetch {
private class SensorDataFetch extends AbstractSensorActivity.SensorDataFetch {

private int[] dataAPDS9960Color;
private double dataAPDS9960Lux;
private int dataAPDS9960Proximity;
private int dataAPDS9960Gesture;
/* Initialization required if updateUi is executed before getSensorData */
private float timeElapsed = getTimeElapsed();

@Override
public void getSensorData() {
Expand All @@ -184,9 +190,9 @@ public void getSensorData() {
} catch (IOException | InterruptedException e) {
Log.e(TAG, "Error getting sensor data.", e);
}
long timeElapsed = (System.currentTimeMillis() - getStartTime()) / 1000;
entriesLux.add(new Entry((float) timeElapsed, (float) dataAPDS9960Lux));
entriesProximity.add(new Entry((float) timeElapsed, dataAPDS9960Proximity));
timeElapsed = getTimeElapsed();
entriesLux.add(new Entry(timeElapsed, (float) dataAPDS9960Lux));
entriesProximity.add(new Entry(timeElapsed, dataAPDS9960Proximity));
}

public void updateUi() {
Expand All @@ -207,15 +213,13 @@ public void updateUi() {
mChartLux.setData(data);
mChartLux.notifyDataSetChanged();
mChartLux.setVisibleXRangeMaximum(10);
mChartLux.moveViewToX(data.getEntryCount());
mChartLux.invalidate();
mChartLux.moveViewToX(timeElapsed);

LineData data2 = new LineData(dataSet2);
mChartProximity.setData(data2);
mChartProximity.notifyDataSetChanged();
mChartProximity.setVisibleXRangeMaximum(10);
mChartProximity.moveViewToX(data2.getEntryCount());
mChartProximity.invalidate();
mChartProximity.moveViewToX(timeElapsed);
} else {
switch (dataAPDS9960Gesture) {
case 1:
Expand Down
25 changes: 14 additions & 11 deletions app/src/main/java/io/pslab/sensors/SensorBMP180.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
yPressure.setLabelCount(10);

yPressure2.setDrawGridLines(false);

if (savedInstanceState != null) {
sensorDataFetch.updateUi();
}
}

@Override
Expand All @@ -192,9 +196,11 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
outState.putParcelableArrayList(KEY_ENTRIES_PRESSURE, entriesPressure);
}

private class SensorDataFetch implements AbstractSensorActivity.SensorDataFetch {
private class SensorDataFetch extends AbstractSensorActivity.SensorDataFetch {

private double[] dataBMP180 = new double[3];
/* Initialization required if updateUi is executed before getSensorData */
private float timeElapsed = getTimeElapsed();

@Override
public void getSensorData() {
Expand All @@ -206,10 +212,10 @@ public void getSensorData() {
Log.e(TAG, "Error getting sensor data.", e);
}

long timeElapsed = (System.currentTimeMillis() - getStartTime()) / 1000;
entriesTemperature.add(new Entry((float) timeElapsed, (float) dataBMP180[0]));
entriesAltitude.add(new Entry((float) timeElapsed, (float) dataBMP180[1]));
entriesPressure.add(new Entry((float) timeElapsed, (float) dataBMP180[2]));
timeElapsed = getTimeElapsed();
entriesTemperature.add(new Entry(timeElapsed, (float) dataBMP180[0]));
entriesAltitude.add(new Entry(timeElapsed, (float) dataBMP180[1]));
entriesPressure.add(new Entry(timeElapsed, (float) dataBMP180[2]));
}

public void updateUi() {
Expand All @@ -229,22 +235,19 @@ public void updateUi() {
mChartTemperature.setData(data);
mChartTemperature.notifyDataSetChanged();
mChartTemperature.setVisibleXRangeMaximum(10);
mChartTemperature.moveViewToX(data.getEntryCount());
mChartTemperature.invalidate();
mChartTemperature.moveViewToX(timeElapsed);

LineData data2 = new LineData(dataSet2);
mChartAltitude.setData(data2);
mChartAltitude.notifyDataSetChanged();
mChartAltitude.setVisibleXRangeMaximum(10);
mChartAltitude.moveViewToX(data.getEntryCount());
mChartAltitude.invalidate();
mChartAltitude.moveViewToX(timeElapsed);

LineData data3 = new LineData(dataSet3);
mChartPressure.setData(data3);
mChartPressure.notifyDataSetChanged();
mChartPressure.setVisibleXRangeMaximum(10);
mChartPressure.moveViewToX(data.getEntryCount());
mChartPressure.invalidate();
mChartPressure.moveViewToX(timeElapsed);
}
}

Expand Down
20 changes: 12 additions & 8 deletions app/src/main/java/io/pslab/sensors/SensorCCS811.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
yTVOC.setLabelCount(10);

yTVOC2.setDrawGridLines(false);

if (savedInstanceState != null) {
sensorDataFetch.updateUi();
}
}

@Override
Expand All @@ -144,10 +148,12 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
outState.putParcelableArrayList(KEY_ENTRIES_TVOC, entriesTVOC);
}

private class SensorDataFetch implements AbstractSensorActivity.SensorDataFetch {
private class SensorDataFetch extends AbstractSensorActivity.SensorDataFetch {

private int dataCCS811eCO2;
private int dataCCS811TVOC;
/* Initialization required if updateUi is executed before getSensorData */
private float timeElapsed = getTimeElapsed();

@Override
public void getSensorData() {
Expand All @@ -160,9 +166,9 @@ public void getSensorData() {
} catch (IOException e) {
Log.e(TAG, "Error getting sensor data.", e);
}
long timeElapsed = (System.currentTimeMillis() - getStartTime()) / 1000;
entrieseCO2.add(new Entry((float) timeElapsed, dataCCS811eCO2));
entriesTVOC.add(new Entry((float) timeElapsed, dataCCS811TVOC));
timeElapsed = getTimeElapsed();
entrieseCO2.add(new Entry(timeElapsed, dataCCS811eCO2));
entriesTVOC.add(new Entry(timeElapsed, dataCCS811TVOC));
}

public void updateUi() {
Expand All @@ -179,15 +185,13 @@ public void updateUi() {
mCharteCO2.setData(data);
mCharteCO2.notifyDataSetChanged();
mCharteCO2.setVisibleXRangeMaximum(10);
mCharteCO2.moveViewToX(data.getEntryCount());
mCharteCO2.invalidate();
mCharteCO2.moveViewToX(timeElapsed);

LineData data2 = new LineData(dataSet2);
mChartTVOC.setData(data2);
mChartTVOC.notifyDataSetChanged();
mChartTVOC.setVisibleXRangeMaximum(10);
mChartTVOC.moveViewToX(data2.getEntryCount());
mChartTVOC.invalidate();
mChartTVOC.moveViewToX(timeElapsed);
}
}

Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/io/pslab/sensors/SensorHMC5883L.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
y.setLabelCount(10);

y2.setDrawGridLines(false);

if (savedInstanceState != null) {
sensorDataFetch.updateUi();
}
}

@Override
Expand All @@ -121,9 +125,11 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
outState.putParcelableArrayList(KEY_ENTRIES_BZ, entriesBz);
}

private class SensorDataFetch implements AbstractSensorActivity.SensorDataFetch {
private class SensorDataFetch extends AbstractSensorActivity.SensorDataFetch {

private List<Double> dataHMC5883L = new ArrayList<>();
/* Initialization required if updateUi is executed before getSensorData */
private float timeElapsed = getTimeElapsed();

@Override
public void getSensorData() {
Expand All @@ -135,10 +141,10 @@ public void getSensorData() {
Log.e(TAG, "Error getting sensor data.", e);
}

long timeElapsed = (System.currentTimeMillis() - getStartTime()) / 1000;
entriesBx.add(new Entry((float) timeElapsed, dataHMC5883L.get(0).floatValue()));
entriesBy.add(new Entry((float) timeElapsed, dataHMC5883L.get(1).floatValue()));
entriesBz.add(new Entry((float) timeElapsed, dataHMC5883L.get(2).floatValue()));
timeElapsed = getTimeElapsed();
entriesBx.add(new Entry(timeElapsed, dataHMC5883L.get(0).floatValue()));
entriesBy.add(new Entry(timeElapsed, dataHMC5883L.get(1).floatValue()));
entriesBz.add(new Entry(timeElapsed, dataHMC5883L.get(2).floatValue()));
}

public void updateUi() {
Expand Down Expand Up @@ -167,8 +173,7 @@ public void updateUi() {
mChart.setData(data);
mChart.notifyDataSetChanged();
mChart.setVisibleXRangeMaximum(10);
mChart.moveViewToX(data.getEntryCount());
mChart.invalidate();
mChart.moveViewToX(timeElapsed);
}
}

Expand Down
20 changes: 12 additions & 8 deletions app/src/main/java/io/pslab/sensors/SensorMLX90614.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
yAmbientTemperature.setLabelCount(10);

yAmbientTemperature2.setDrawGridLines(false);

if (savedInstanceState != null) {
sensorDataFetch.updateUi();
}
}

@Override
Expand Down Expand Up @@ -196,10 +200,12 @@ public void howToConnectDialog(String title, String intro, int imageID, String d
}
}

private class SensorDataFetch implements AbstractSensorActivity.SensorDataFetch {
private class SensorDataFetch extends AbstractSensorActivity.SensorDataFetch {

private Double dataMLX90614ObjectTemp;
private Double dataMLX90614AmbientTemp;
/* Initialization required if updateUi is executed before getSensorData */
private float timeElapsed = getTimeElapsed();

@Override
public void getSensorData() {
Expand All @@ -212,9 +218,9 @@ public void getSensorData() {
Log.e(TAG, "Error getting sensor data.", e);
}

long timeElapsed = (System.currentTimeMillis() - getStartTime()) / 1000;
entriesObjectTemperature.add(new Entry((float) timeElapsed, dataMLX90614ObjectTemp.floatValue()));
entriesAmbientTemperature.add(new Entry((float) timeElapsed, dataMLX90614AmbientTemp.floatValue()));
timeElapsed = getTimeElapsed();
entriesObjectTemperature.add(new Entry(timeElapsed, dataMLX90614ObjectTemp.floatValue()));
entriesAmbientTemperature.add(new Entry(timeElapsed, dataMLX90614AmbientTemp.floatValue()));
}

public void updateUi() {
Expand All @@ -231,15 +237,13 @@ public void updateUi() {
mChartObjectTemperature.setData(data1);
mChartObjectTemperature.notifyDataSetChanged();
mChartObjectTemperature.setVisibleXRangeMaximum(10);
mChartObjectTemperature.moveViewToX(data1.getEntryCount());
mChartObjectTemperature.invalidate();
mChartObjectTemperature.moveViewToX(timeElapsed);

LineData data2 = new LineData(dataSet2);
mChartAmbientTemperature.setData(data2);
mChartAmbientTemperature.notifyDataSetChanged();
mChartAmbientTemperature.setVisibleXRangeMaximum(10);
mChartAmbientTemperature.moveViewToX(data2.getEntryCount());
mChartAmbientTemperature.invalidate();
mChartAmbientTemperature.moveViewToX(timeElapsed);
}
}

Expand Down
Loading

0 comments on commit c8639ec

Please sign in to comment.