forked from OpenTracksApp/OpenTracks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sensor: adding Running Speed and Cadence.
Fixes OpenTracksApp#891.
- Loading branch information
1 parent
eba7a7f
commit 242147e
Showing
16 changed files
with
295 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/de/dennisguse/opentracks/content/sensor/SensorDataRunning.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package de.dennisguse.opentracks.content.sensor; | ||
|
||
import androidx.annotation.Nullable; | ||
import androidx.annotation.VisibleForTesting; | ||
|
||
import de.dennisguse.opentracks.content.data.Distance; | ||
import de.dennisguse.opentracks.content.data.Speed; | ||
|
||
/** | ||
* Provides cadence in rpm and speed in milliseconds from Bluetooth LE Running Speed and Cadence sensors. | ||
*/ | ||
public final class SensorDataRunning extends SensorData<SensorDataRunning.Data> { | ||
|
||
private static final String TAG = SensorDataRunning.class.getSimpleName(); | ||
|
||
private final Speed speed; | ||
|
||
private final Float cadence; | ||
|
||
private final Distance totalDistance; | ||
|
||
public SensorDataRunning(String sensorAddress) { | ||
super(sensorAddress); | ||
this.speed = null; | ||
this.cadence = null; | ||
this.totalDistance = null; | ||
} | ||
|
||
public SensorDataRunning(String sensorAddress, String sensorName, Speed speed, Float cadence, Distance totalDistance) { | ||
super(sensorAddress, sensorName); | ||
this.speed = speed; | ||
this.cadence = cadence; | ||
this.totalDistance = totalDistance; | ||
} | ||
|
||
private boolean hasTotalDistance() { | ||
return totalDistance != null; | ||
} | ||
|
||
|
||
public Float getCadence() { | ||
return cadence; | ||
} | ||
|
||
@VisibleForTesting | ||
public Speed getSpeed() { | ||
return speed; | ||
} | ||
|
||
@VisibleForTesting | ||
public Distance getTotalDistance() { | ||
return totalDistance; | ||
} | ||
|
||
public void compute(SensorDataRunning previous) { | ||
Distance overallDistance = null; | ||
if (hasTotalDistance() && previous != null && previous.hasTotalDistance()) { | ||
overallDistance = this.totalDistance.minus(previous.totalDistance); | ||
if (previous.hasValue() && previous.getValue().getDistance() != null) { | ||
overallDistance = overallDistance.plus(previous.getValue().getDistance()); | ||
} | ||
} | ||
|
||
value = new Data(speed, cadence, overallDistance); | ||
} | ||
|
||
@Override | ||
public void reset() { | ||
if (value != null) { | ||
value = new Data(value.speed, value.cadence, Distance.of(0)); | ||
} | ||
} | ||
|
||
public static class Data { | ||
private final Speed speed; | ||
private final Float cadence; | ||
|
||
@Nullable | ||
private final Distance distance; | ||
|
||
public Data(Speed speed, Float cadence, @Nullable Distance distance) { | ||
this.speed = speed; | ||
this.cadence = cadence; | ||
this.distance = distance; | ||
} | ||
|
||
public Speed getSpeed() { | ||
return speed; | ||
} | ||
|
||
public Float getCadence() { | ||
return cadence; | ||
} | ||
|
||
public Distance getDistance() { | ||
return distance; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Data{" + | ||
"speed=" + speed + | ||
", cadence=" + cadence + | ||
", distance=" + distance + | ||
'}'; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.