| 1 |
package com.ridelogger.listners; |
| 2 |
import android.preference.PreferenceManager; |
| 3 |
|
| 4 |
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc; |
| 5 |
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.CalculatedWheelDistanceReceiver; |
| 6 |
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.CalculatedWheelSpeedReceiver; |
| 7 |
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.DataSource; |
| 8 |
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.ICalculatedCrankCadenceReceiver; |
| 9 |
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.ICalculatedPowerReceiver; |
| 10 |
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.ICalculatedTorqueReceiver; |
| 11 |
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.IInstantaneousCadenceReceiver; |
| 12 |
import com.dsi.ant.plugins.antplus.pcc.AntPlusBikePowerPcc.IRawPowerOnlyDataReceiver; |
| 13 |
import com.dsi.ant.plugins.antplus.pcc.defines.DeviceState; |
| 14 |
import com.dsi.ant.plugins.antplus.pcc.defines.EventFlag; |
| 15 |
import com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult; |
| 16 |
import com.dsi.ant.plugins.antplus.pccbase.AntPluginPcc.IPluginAccessResultReceiver; |
| 17 |
import com.ridelogger.R; |
| 18 |
import com.ridelogger.RideService; |
| 19 |
|
| 20 |
import java.math.BigDecimal; |
| 21 |
import java.util.EnumSet; |
| 22 |
|
| 23 |
/** |
| 24 |
* Power |
| 25 |
* @author Chet Henry |
| 26 |
* Listen to and log Ant+ Power events |
| 27 |
*/ |
| 28 |
public class Power extends Ant |
| 29 |
{ |
| 30 |
public BigDecimal wheelCircumferenceInMeters; //size of wheel to calculate speed |
| 31 |
public IPluginAccessResultReceiver<AntPlusBikePowerPcc> mResultReceiver; |
| 32 |
|
| 33 |
//setup listeners and logging |
| 34 |
public Power(int pDeviceNumber, RideService mContext) { |
| 35 |
super(pDeviceNumber, mContext); |
| 36 |
|
| 37 |
wheelCircumferenceInMeters = new BigDecimal( |
| 38 |
PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.PREF_WHEEL_SIZE), "2.096") |
| 39 |
); |
| 40 |
|
| 41 |
//Handle messages |
| 42 |
mResultReceiver = new IPluginAccessResultReceiver<AntPlusBikePowerPcc>() { |
| 43 |
//Handle the result, connecting to events on success or reporting failure to user. |
| 44 |
@Override |
| 45 |
public void onResultReceived(AntPlusBikePowerPcc result, RequestAccessResult resultCode, DeviceState initialDeviceState) { |
| 46 |
if(resultCode == com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult.SUCCESS) { |
| 47 |
deviceNumber = result.getAntDeviceNumber(); |
| 48 |
|
| 49 |
result.subscribeCalculatedPowerEvent(new ICalculatedPowerReceiver() { |
| 50 |
@Override |
| 51 |
public void onNewCalculatedPower(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedPower) { |
| 52 |
alterCurrentData(RideService.WATTS, calculatedPower.floatValue()); |
| 53 |
} |
| 54 |
} |
| 55 |
); |
| 56 |
|
| 57 |
result.subscribeCalculatedTorqueEvent( |
| 58 |
new ICalculatedTorqueReceiver() { |
| 59 |
@Override |
| 60 |
public void onNewCalculatedTorque(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedTorque) { |
| 61 |
alterCurrentData(RideService.NM, calculatedTorque.floatValue()); |
| 62 |
} |
| 63 |
} |
| 64 |
); |
| 65 |
|
| 66 |
result.subscribeCalculatedCrankCadenceEvent( |
| 67 |
new ICalculatedCrankCadenceReceiver() { |
| 68 |
@Override |
| 69 |
public void onNewCalculatedCrankCadence(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedCrankCadence) { |
| 70 |
alterCurrentData(RideService.CAD, calculatedCrankCadence.floatValue()); |
| 71 |
} |
| 72 |
} |
| 73 |
); |
| 74 |
|
| 75 |
result.subscribeCalculatedWheelSpeedEvent( |
| 76 |
new CalculatedWheelSpeedReceiver(wheelCircumferenceInMeters) { |
| 77 |
@Override |
| 78 |
public void onNewCalculatedWheelSpeed(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedWheelSpeed) |
| 79 |
{ |
| 80 |
alterCurrentData(RideService.KPH, calculatedWheelSpeed.floatValue()); |
| 81 |
} |
| 82 |
} |
| 83 |
); |
| 84 |
|
| 85 |
result.subscribeCalculatedWheelDistanceEvent( |
| 86 |
new CalculatedWheelDistanceReceiver(wheelCircumferenceInMeters) { |
| 87 |
@Override |
| 88 |
public void onNewCalculatedWheelDistance(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final BigDecimal calculatedWheelDistance) |
| 89 |
{ |
| 90 |
alterCurrentData(RideService.KM, calculatedWheelDistance.floatValue()); |
| 91 |
} |
| 92 |
} |
| 93 |
); |
| 94 |
|
| 95 |
result.subscribeInstantaneousCadenceEvent( |
| 96 |
new IInstantaneousCadenceReceiver() { |
| 97 |
@Override |
| 98 |
public void onNewInstantaneousCadence(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final DataSource dataSource, final int instantaneousCadence) |
| 99 |
{ |
| 100 |
alterCurrentData(RideService.CAD, (float) instantaneousCadence); |
| 101 |
} |
| 102 |
} |
| 103 |
); |
| 104 |
|
| 105 |
result.subscribeRawPowerOnlyDataEvent( |
| 106 |
new IRawPowerOnlyDataReceiver() { |
| 107 |
@Override |
| 108 |
public void onNewRawPowerOnlyData(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long powerOnlyUpdateEventCount, final int instantaneousPower, final long accumulatedPower) |
| 109 |
{ |
| 110 |
alterCurrentData(RideService.WATTS, (float) instantaneousPower); |
| 111 |
} |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
/*result.subscribePedalPowerBalanceEvent( |
| 116 |
new IPedalPowerBalanceReceiver() { |
| 117 |
@Override |
| 118 |
public void onNewPedalPowerBalance(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final boolean rightPedalIndicator, final int pedalPowerPercentage) |
| 119 |
{ |
| 120 |
alterCurrentData(RideService.LTE, pedalPowerPercentage); |
| 121 |
} |
| 122 |
} |
| 123 |
); |
| 124 |
|
| 125 |
result.subscribeRawWheelTorqueDataEvent( |
| 126 |
new IRawWheelTorqueDataReceiver() { |
| 127 |
@Override |
| 128 |
public void onNewRawWheelTorqueData(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long wheelTorqueUpdateEventCount, final long accumulatedWheelTicks, final BigDecimal accumulatedWheelPeriod, final BigDecimal accumulatedWheelTorque) |
| 129 |
{ |
| 130 |
alterCurrentData(RideService.NM, accumulatedWheelTorque); |
| 131 |
} |
| 132 |
} |
| 133 |
); |
| 134 |
|
| 135 |
result.subscribeRawCrankTorqueDataEvent( |
| 136 |
new IRawCrankTorqueDataReceiver() { |
| 137 |
@Override |
| 138 |
public void onNewRawCrankTorqueData(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long crankTorqueUpdateEventCount, final long accumulatedCrankTicks, final BigDecimal accumulatedCrankPeriod, final BigDecimal accumulatedCrankTorque) |
| 139 |
{ |
| 140 |
alterCurrentData(RideService.NM, accumulatedCrankTorque); |
| 141 |
} |
| 142 |
} |
| 143 |
); |
| 144 |
|
| 145 |
result.subscribeTorqueEffectivenessEvent( |
| 146 |
new ITorqueEffectivenessReceiver() { |
| 147 |
@Override |
| 148 |
public void onNewTorqueEffectiveness(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long powerOnlyUpdateEventCount, final BigDecimal leftTorqueEffectiveness, final BigDecimal rightTorqueEffectiveness) |
| 149 |
{ |
| 150 |
int[] keys = { |
| 151 |
RideService.LTE, |
| 152 |
RideService.RTE |
| 153 |
}; |
| 154 |
|
| 155 |
float[] values = { |
| 156 |
leftTorqueEffectiveness, |
| 157 |
rightTorqueEffectiveness |
| 158 |
} |
| 159 |
|
| 160 |
alterCurrentData(keys, values); |
| 161 |
} |
| 162 |
|
| 163 |
} |
| 164 |
); |
| 165 |
|
| 166 |
result.subscribePedalSmoothnessEvent(new IPedalSmoothnessReceiver() { |
| 167 |
@Override |
| 168 |
public void onNewPedalSmoothness(final long estTimestamp, final EnumSet<EventFlag> eventFlags, final long powerOnlyUpdateEventCount, final boolean separatePedalSmoothnessSupport, final BigDecimal leftOrCombinedPedalSmoothness, final BigDecimal rightPedalSmoothness) |
| 169 |
{ |
| 170 |
int[] keys = { |
| 171 |
RideService.SNPLC, |
| 172 |
RideService.SNPR |
| 173 |
}; |
| 174 |
|
| 175 |
float[] values = { |
| 176 |
leftOrCombinedPedalSmoothness, |
| 177 |
rightPedalSmoothness |
| 178 |
} |
| 179 |
|
| 180 |
alterCurrentData(map); |
| 181 |
} |
| 182 |
} |
| 183 |
);*/ |
| 184 |
} else if(resultCode == com.dsi.ant.plugins.antplus.pcc.defines.RequestAccessResult.SEARCH_TIMEOUT) { |
| 185 |
if(deviceNumber != 0) { |
| 186 |
requestAccess(); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
requestAccess(); |
| 193 |
} |
| 194 |
|
| 195 |
protected void requestAccess() { |
| 196 |
releaseHandle = AntPlusBikePowerPcc.requestAccess(context, deviceNumber, 0, mResultReceiver, mDeviceStateChangeReceiver); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
@Override |
| 201 |
public void zeroReadings() |
| 202 |
{ |
| 203 |
int[] keys = { |
| 204 |
RideService.WATTS, |
| 205 |
RideService.NM, |
| 206 |
RideService.CAD, |
| 207 |
RideService.KPH, |
| 208 |
RideService.KM |
| 209 |
}; |
| 210 |
|
| 211 |
float[] values = { |
| 212 |
(float) 0.0, |
| 213 |
(float) 0.0, |
| 214 |
(float) 0.0, |
| 215 |
(float) 0.0, |
| 216 |
(float) 0.0 |
| 217 |
}; |
| 218 |
|
| 219 |
alterCurrentData(keys, values); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|