Skip to content

Commit

Permalink
change range to ODO
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamuel committed Aug 6, 2024
1 parent 1ca572e commit 7f880dc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 32 deletions.
10 changes: 5 additions & 5 deletions lib/bike.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class BikePageState extends ConsumerState<BikePage> {
mainAxisAlignment: MainAxisAlignment.end,
children: [ConnectionWidget(bike: bike)],
),
BatteryRangeControlWidget(bike: bike),
BatteryODOControlWidget(bike: bike),
LightControlWidget(bike: bike),
ModeControlWidget(bike: bike),
AssistControlWidget(bike: bike),
Expand Down Expand Up @@ -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
Expand All @@ -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: () {
Expand Down
19 changes: 15 additions & 4 deletions lib/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand All @@ -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<int> data) {
Expand All @@ -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<int> data) {
const total1Idx = 6;
const total2Idx = 7;
final totalodometer = _getodometer(data[total1Idx], data[total2Idx]);
return copyWith(odometer: totalodometer);
}

BikeRegion _guessRegion(int mode) {
Expand Down Expand Up @@ -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;
Expand Down
43 changes: 22 additions & 21 deletions lib/models.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> toJson() => throw _privateConstructorUsedError;
Expand All @@ -59,7 +59,7 @@ abstract class $BikeStateCopyWith<$Res> {
bool modeLock,
int color,
double battery,
int range,
double odometer,
String speedMetric});
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -173,7 +173,7 @@ abstract class _$$BikeStateImplCopyWith<$Res>
bool modeLock,
int color,
double battery,
int range,
double odometer,
String speedMetric});
}

Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand All @@ -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._();

Expand Down Expand Up @@ -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
Expand All @@ -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));
}
Expand All @@ -380,7 +381,7 @@ class _$BikeStateImpl extends _BikeState {
modeLock,
color,
battery,
range,
odometer,
speedMetric);

@JsonKey(ignore: true)
Expand Down Expand Up @@ -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._();

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/models.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7f880dc

Please sign in to comment.