diff --git a/lib/bike.dart b/lib/bike.dart index 1b0bc43..8b529b1 100644 --- a/lib/bike.dart +++ b/lib/bike.dart @@ -244,7 +244,7 @@ class BikePageState extends ConsumerState { mainAxisAlignment: MainAxisAlignment.end, children: [ConnectionWidget(bike: bike)], ), - BatteryRangeControlWidget(bike: bike), + BatteryODOControlWidget(bike: bike), LightControlWidget(bike: bike), ModeControlWidget(bike: bike), AssistControlWidget(bike: bike), @@ -334,8 +334,8 @@ class LockWidget extends StatelessWidget { ); } } -class BatteryRangeControlWidget extends ConsumerWidget { - const BatteryRangeControlWidget({super.key, required this.bike}); +class BatteryODOControlWidget extends ConsumerWidget { + const BatteryODOControlWidget({super.key, required this.bike}); final BikeState bike; @override @@ -351,8 +351,8 @@ class BatteryRangeControlWidget extends ConsumerWidget { colorIndex: bike.color, title: "", metric: bike.speedMetric == 'metric' - ? '${bike.range.toStringAsFixed(0)} km' - : '${(bike.range * 0.621371).toStringAsFixed(0)} mi', + ? '${bike.odometer.toStringAsFixed(0)} km' + : '${(bike.odometer * 0.621371).toStringAsFixed(0)} mi', titleIcon: Icons.pedal_bike, selected: false, onTap: () { diff --git a/lib/models.dart b/lib/models.dart index e741695..3796365 100644 --- a/lib/models.dart +++ b/lib/models.dart @@ -26,7 +26,7 @@ class BikeState with _$BikeState { @Assert('color >= 0') @Assert('battery >= 0') @Assert('battery <= 100') - @Assert('range >= 0') + @Assert('odometer >= 0') @Assert('speedMetric == "metric" || speedMetric == "imperial"') const factory BikeState( {required String id, @@ -41,7 +41,7 @@ class BikeState with _$BikeState { @Default(false) bool modeLock, @Default(0) int color, @Default(0.0) double battery, - @Default(0) int range, + @Default(0) double odometer, @Default('metric') String speedMetric }) = _BikeState; @@ -50,7 +50,7 @@ class BikeState with _$BikeState { factory BikeState.defaultState(String id) { return BikeState( - id: id, mode: 0, light: false, assist: 0, name: getName(seed: id), battery: 0.0, range: 0); + id: id, mode: 0, light: false, assist: 0, name: getName(seed: id), battery: 0.0, odometer: 0); } BikeState updateFromData(List data) { @@ -70,7 +70,14 @@ class BikeState with _$BikeState { const cadenceIdx = 3; const rangeIdx = 8; final batteryPercentage = _batteryPercentage(data[rangeIdx]); - return copyWith(battery: batteryPercentage, range: data[rangeIdx]); + return copyWith(battery: batteryPercentage); + } + + BikeState updateTotalFromData(List data) { + const total1Idx = 6; + const total2Idx = 7; + final totalodometer = _getodometer(data[total1Idx], data[total2Idx]); + return copyWith(odometer: totalodometer); } BikeRegion _guessRegion(int mode) { @@ -105,6 +112,10 @@ class BikeState with _$BikeState { return (mode + 1) % 4; } + double _getodometer(int total1, int total2) { + return (((total2 * 256) + total1) / 10); + } + double _batteryPercentage(int range) { // All current Super73 max range is 60km double batteryPercentage = range / 60.0 * 100; diff --git a/lib/models.freezed.dart b/lib/models.freezed.dart index a3f4484..898e457 100644 --- a/lib/models.freezed.dart +++ b/lib/models.freezed.dart @@ -32,7 +32,7 @@ mixin _$BikeState { bool get modeLock => throw _privateConstructorUsedError; int get color => throw _privateConstructorUsedError; double get battery => throw _privateConstructorUsedError; - int get range => throw _privateConstructorUsedError; + double get odometer => throw _privateConstructorUsedError; String get speedMetric => throw _privateConstructorUsedError; Map toJson() => throw _privateConstructorUsedError; @@ -59,7 +59,7 @@ abstract class $BikeStateCopyWith<$Res> { bool modeLock, int color, double battery, - int range, + double odometer, String speedMetric}); } @@ -88,7 +88,7 @@ class _$BikeStateCopyWithImpl<$Res, $Val extends BikeState> Object? modeLock = null, Object? color = null, Object? battery = null, - Object? range = null, + Object? odometer = null, Object? speedMetric = null, }) { return _then(_value.copyWith( @@ -140,10 +140,10 @@ class _$BikeStateCopyWithImpl<$Res, $Val extends BikeState> ? _value.battery : battery // ignore: cast_nullable_to_non_nullable as double, - range: null == range - ? _value.range - : range // ignore: cast_nullable_to_non_nullable - as int, + odometer: null == odometer + ? _value.odometer + : odometer // ignore: cast_nullable_to_non_nullable + as double, speedMetric: null == speedMetric ? _value.speedMetric : speedMetric // ignore: cast_nullable_to_non_nullable @@ -173,7 +173,7 @@ abstract class _$$BikeStateImplCopyWith<$Res> bool modeLock, int color, double battery, - int range, + double odometer, String speedMetric}); } @@ -200,7 +200,7 @@ class __$$BikeStateImplCopyWithImpl<$Res> Object? modeLock = null, Object? color = null, Object? battery = null, - Object? range = null, + Object? odometer = null, Object? speedMetric = null, }) { return _then(_$BikeStateImpl( @@ -252,10 +252,10 @@ class __$$BikeStateImplCopyWithImpl<$Res> ? _value.battery : battery // ignore: cast_nullable_to_non_nullable as double, - range: null == range - ? _value.range - : range // ignore: cast_nullable_to_non_nullable - as int, + odometer: null == odometer + ? _value.odometer + : odometer // ignore: cast_nullable_to_non_nullable + as double, speedMetric: null == speedMetric ? _value.speedMetric : speedMetric // ignore: cast_nullable_to_non_nullable @@ -280,7 +280,7 @@ class _$BikeStateImpl extends _BikeState { this.modeLock = false, this.color = 0, this.battery = 0.0, - this.range = 0, + this.odometer = 0, this.speedMetric = 'metric'}) : assert(mode <= 3), assert(mode >= 0), @@ -289,7 +289,7 @@ class _$BikeStateImpl extends _BikeState { assert(color >= 0), assert(battery >= 0), assert(battery <= 100), - assert(range >= 0), + assert(odometer >= 0), assert(speedMetric == "metric" || speedMetric == "imperial"), super._(); @@ -328,14 +328,14 @@ class _$BikeStateImpl extends _BikeState { final double battery; @override @JsonKey() - final int range; + final double odometer; @override @JsonKey() final String speedMetric; @override String toString() { - return 'BikeState(id: $id, mode: $mode, modeLocked: $modeLocked, light: $light, lightLocked: $lightLocked, assist: $assist, assistLocked: $assistLocked, name: $name, region: $region, modeLock: $modeLock, color: $color, battery: $battery, range: $range, speedMetric: $speedMetric)'; + return 'BikeState(id: $id, mode: $mode, modeLocked: $modeLocked, light: $light, lightLocked: $lightLocked, assist: $assist, assistLocked: $assistLocked, name: $name, region: $region, modeLock: $modeLock, color: $color, battery: $battery, odometer: $odometer, speedMetric: $speedMetric)'; } @override @@ -359,7 +359,8 @@ class _$BikeStateImpl extends _BikeState { other.modeLock == modeLock) && (identical(other.color, color) || other.color == color) && (identical(other.battery, battery) || other.battery == battery) && - (identical(other.range, range) || other.range == range) && + (identical(other.odometer, odometer) || + other.odometer == odometer) && (identical(other.speedMetric, speedMetric) || other.speedMetric == speedMetric)); } @@ -380,7 +381,7 @@ class _$BikeStateImpl extends _BikeState { modeLock, color, battery, - range, + odometer, speedMetric); @JsonKey(ignore: true) @@ -411,7 +412,7 @@ abstract class _BikeState extends BikeState { final bool modeLock, final int color, final double battery, - final int range, + final double odometer, final String speedMetric}) = _$BikeStateImpl; const _BikeState._() : super._(); @@ -443,7 +444,7 @@ abstract class _BikeState extends BikeState { @override double get battery; @override - int get range; + double get odometer; @override String get speedMetric; @override diff --git a/lib/models.g.dart b/lib/models.g.dart index 3eb18ac..f393bd3 100644 --- a/lib/models.g.dart +++ b/lib/models.g.dart @@ -20,7 +20,7 @@ _$BikeStateImpl _$$BikeStateImplFromJson(Map json) => modeLock: json['modeLock'] as bool? ?? false, color: (json['color'] as num?)?.toInt() ?? 0, battery: (json['battery'] as num?)?.toDouble() ?? 0.0, - range: (json['range'] as num?)?.toInt() ?? 0, + odometer: (json['odometer'] as num?)?.toDouble() ?? 0, speedMetric: json['speedMetric'] as String? ?? 'metric', ); @@ -38,7 +38,7 @@ Map _$$BikeStateImplToJson(_$BikeStateImpl instance) => 'modeLock': instance.modeLock, 'color': instance.color, 'battery': instance.battery, - 'range': instance.range, + 'odometer': instance.odometer, 'speedMetric': instance.speedMetric, }; diff --git a/lib/repository.dart b/lib/repository.dart index bc084bb..055ac58 100644 --- a/lib/repository.dart +++ b/lib/repository.dart @@ -270,6 +270,7 @@ class BluetoothRepository { break; default: currentStateId = [3, 0]; + break; } // Set the char register to the right mode to get the current state.