mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-04 20:52:23 +01:00
refactor(incline): extract ManualInclineDevice mixin shared by incline devices
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
import 'package:bike_control/bluetooth/incline/incline_sink.dart';
|
||||
import 'package:bike_control/bluetooth/incline/incline_manual_state.dart';
|
||||
import 'package:bike_control/bluetooth/incline/manual_incline_device.dart';
|
||||
import 'package:bike_control/main.dart';
|
||||
import 'package:bike_control/utils/actions/base_actions.dart';
|
||||
import 'package:bike_control/utils/keymap/buttons.dart';
|
||||
import 'package:bike_control/utils/keymap/keymap.dart';
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:prop/utils/wahoo_climb.dart';
|
||||
@@ -12,25 +8,16 @@ import 'package:universal_ble/universal_ble.dart';
|
||||
import '../bluetooth_device.dart';
|
||||
|
||||
/// Wahoo KICKR Climb — a beta accessory driven by the active bridge's sim grade
|
||||
/// (auto) or by manual button actions (manual hold). Direct-to-Climb BLE sink.
|
||||
class WahooKickrClimb extends BluetoothDevice with Accessory implements InclineSink {
|
||||
/// (auto) or by manual incline actions (manual hold). Direct-to-Climb BLE sink.
|
||||
class WahooKickrClimb extends BluetoothDevice with Accessory, ManualInclineDevice {
|
||||
WahooKickrClimb(super.scanResult) : super(availableButtons: const [], isBeta: true);
|
||||
|
||||
final InclineManualState state = InclineManualState();
|
||||
|
||||
/// Test seam: overridable to capture writes without real BLE.
|
||||
@visibleForTesting
|
||||
Future<void> Function(Uint8List bytes)? debugWriteSink;
|
||||
|
||||
InclineMode get mode => state.mode;
|
||||
|
||||
@override
|
||||
bool get followsGrade => state.mode == InclineMode.auto;
|
||||
|
||||
@override
|
||||
Future<void> handleServices(List<BleService> services) async {
|
||||
// Diagnostics: dump GATT so the first hardware tester can confirm the
|
||||
// proprietary service/char exist on the Climb's own peripheral.
|
||||
for (final s in services) {
|
||||
debugPrint('[Climb] service ${s.uuid} chars=${s.characteristics.map((c) => c.uuid).toList()}');
|
||||
}
|
||||
@@ -74,39 +61,8 @@ class WahooKickrClimb extends BluetoothDevice with Accessory implements InclineS
|
||||
return;
|
||||
}
|
||||
await UniversalBle.write(
|
||||
device.deviceId,
|
||||
wahooClimbServiceUuid,
|
||||
wahooClimbCharacteristicUuid,
|
||||
bytes,
|
||||
device.deviceId, wahooClimbServiceUuid, wahooClimbCharacteristicUuid, bytes,
|
||||
withoutResponse: true,
|
||||
);
|
||||
}
|
||||
|
||||
/// Handles the manual Climb actions. Auto is restored by
|
||||
/// [InGameAction.climbAutoMode]; the InclineController resumes pushing grade.
|
||||
Future<ActionResult> handleKeypair(KeyPair keyPair, {required bool isKeyDown}) async {
|
||||
if (!isKeyDown) return NotHandled('', button: keyPair.buttons.firstOrNull);
|
||||
try {
|
||||
switch (keyPair.inGameAction) {
|
||||
case InGameAction.climbInclineIncrease:
|
||||
state.increase();
|
||||
case InGameAction.climbInclineDecrease:
|
||||
state.decrease();
|
||||
case InGameAction.climbInclineZero:
|
||||
state.zero();
|
||||
case InGameAction.climbAutoMode:
|
||||
state.setAuto();
|
||||
return Success('Climb: follow grade', button: keyPair.buttons.firstOrNull);
|
||||
default:
|
||||
return NotHandled('', button: keyPair.buttons.firstOrNull);
|
||||
}
|
||||
await writeInclineRaw(state.grade001);
|
||||
return Success(
|
||||
'Climb incline ${(state.grade001 / 100).toStringAsFixed(1)}%',
|
||||
button: keyPair.buttons.firstOrNull,
|
||||
);
|
||||
} catch (e) {
|
||||
return Error('Failed to control Climb: $e', button: keyPair.buttons.firstOrNull);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
50
lib/bluetooth/incline/manual_incline_device.dart
Normal file
50
lib/bluetooth/incline/manual_incline_device.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:bike_control/bluetooth/devices/bluetooth_device.dart';
|
||||
import 'package:bike_control/bluetooth/incline/incline_manual_state.dart';
|
||||
import 'package:bike_control/bluetooth/incline/incline_sink.dart';
|
||||
import 'package:bike_control/main.dart';
|
||||
import 'package:bike_control/utils/actions/base_actions.dart';
|
||||
import 'package:bike_control/utils/keymap/buttons.dart';
|
||||
import 'package:bike_control/utils/keymap/keymap.dart';
|
||||
import 'package:dartx/dartx.dart';
|
||||
|
||||
/// Shared manual-incline behavior for incline devices (KICKR Climb, Elite Rizer).
|
||||
/// Holds the auto/manual state and maps the incline in-game actions to it, then
|
||||
/// writes via the device-specific [writeInclineRaw]. `auto` follows the bridge
|
||||
/// grade (the InclineController pushes it); `manual` holds a user-set incline.
|
||||
mixin ManualInclineDevice on BluetoothDevice implements InclineSink {
|
||||
final InclineManualState inclineState = InclineManualState();
|
||||
|
||||
@override
|
||||
bool get followsGrade => inclineState.mode == InclineMode.auto;
|
||||
|
||||
/// Device-specific BLE write of the incline (0.01% signed). Returns true if issued.
|
||||
@override
|
||||
Future<bool> writeInclineRaw(int grade001Pct);
|
||||
|
||||
Future<ActionResult> handleKeypair(KeyPair keyPair, {required bool isKeyDown}) async {
|
||||
if (!isKeyDown) return NotHandled('', button: keyPair.buttons.firstOrNull);
|
||||
try {
|
||||
switch (keyPair.inGameAction) {
|
||||
case InGameAction.climbInclineIncrease:
|
||||
inclineState.increase();
|
||||
case InGameAction.climbInclineDecrease:
|
||||
inclineState.decrease();
|
||||
case InGameAction.climbInclineZero:
|
||||
inclineState.zero();
|
||||
case InGameAction.climbAutoMode:
|
||||
inclineState.setAuto();
|
||||
return Success('Incline: follow grade', button: keyPair.buttons.firstOrNull);
|
||||
default:
|
||||
return NotHandled('', button: keyPair.buttons.firstOrNull);
|
||||
}
|
||||
await writeInclineRaw(inclineState.grade001);
|
||||
return Success(
|
||||
'Incline ${(inclineState.grade001 / 100).toStringAsFixed(1)}%',
|
||||
button: keyPair.buttons.firstOrNull,
|
||||
);
|
||||
} catch (e, s) {
|
||||
recordError(e, s, context: 'ManualInclineDevice: handleKeypair failed');
|
||||
return Error('Failed to control incline: $e', button: keyPair.buttons.firstOrNull);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user