Skip to content

Commit

Permalink
Further Cadence invalidation changes
Browse files Browse the repository at this point in the history
Invalidate current results in more situations when input data is bad.
  • Loading branch information
gerhardol committed Oct 2, 2021
1 parent 7b5416d commit b74b272
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions app/src/main/org/runnerup/tracker/component/TrackerCadence.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public String getName() {
private Float mPrevVal = null;
private long mPrevTime = -1;
private Float mCurrentCadence = null;
final int cutOffTime = 3;

public Float getValue() {
if (!isSportEnabled) {
Expand All @@ -63,7 +64,6 @@ public Float getValue() {

// It can take seconds between sensor updates
// Cut-off at 3s corresponds to 60/2/3 => 10 rpm (or 20 steps per minute)
final int cutOffTime = 3;
final long nanoSec = 1000000000L;
long now;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Expand All @@ -84,21 +84,24 @@ public Float getValue() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values == null || event.values.length < 1) {
mCurrentCadence = null;
return;
}

float latestVal = event.values[0];
long latestTime = event.timestamp;
long timeDiff = latestTime - mPrevTime;
final long nanoSec = 1000000000L;

if (mPrevTime == latestTime || mPrevTime < 0 || mPrevVal == null) {
if (timeDiff > cutOffTime * nanoSec || mPrevTime < 0 || mPrevVal == null || latestVal <= mPrevVal) {
mCurrentCadence = null;
} else {
if (latestVal <= mPrevVal) {
float val = (latestVal - mPrevVal) / 2 * 60 * nanoSec / timeDiff;
if (val > 300) {
// ignore this reading, use previous point next time
return;
}

final long nanoSec = 1000000000L;
float val = (latestVal - mPrevVal) / 2 * 60 * nanoSec / (latestTime - mPrevTime);
if (mCurrentCadence == null) {
mCurrentCadence = val;
} else {
Expand All @@ -113,6 +116,7 @@ public void onSensorChanged(SensorEvent event) {

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
mCurrentCadence = null;
}

/**
Expand All @@ -123,19 +127,18 @@ public static boolean isAvailable(final Context context) {
}

private Sensor getSensor(final Context context) {
Sensor sensor = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (mSensorManager == null) {
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
}
//noinspection InlinedApi
sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (sensor == null) {
mSensorManager = null;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return null;
}

if (mSensorManager == null) {
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
}
//noinspection InlinedApi
Sensor sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
if (sensor == null) {
mSensorManager = null;

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
isMockSensor = prefs.getBoolean(context.getString(org.runnerup.R.string.pref_bt_mock), false);
}
Expand Down

0 comments on commit b74b272

Please sign in to comment.