diff --git a/lib/utils/keymap/apps/training_peaks.dart b/lib/utils/keymap/apps/training_peaks.dart index 2af8c109..5748c9ff 100644 --- a/lib/utils/keymap/apps/training_peaks.dart +++ b/lib/utils/keymap/apps/training_peaks.dart @@ -35,6 +35,10 @@ class TrainingPeaks extends SupportedApp { 0x14, // Select/Confirm 0x15, // Back/Cancel 0x16, // Menu + // TPV advertises Steer Left/Right over OBP (seen in a live app-info frame); + // without them here the button editor hides steering before a connection. + 0x18, // Steer Left + 0x19, // Steer Right 0x21, // Push to Talk 0x30, // Increase Difficulty 0x31, // Decrease Difficulty @@ -129,32 +133,42 @@ class TrainingPeaks extends SupportedApp { buttons: [ZwiftButtons.navigationUp], physicalKey: PhysicalKeyboardKey.arrowUp, logicalKey: LogicalKeyboardKey.arrowUp, + inGameAction: InGameAction.up, touchPosition: Offset(42.28406293368177, 92.61854987939971), ), - // Face buttons with touch positions and keyboard fallbacks where sensible + // Face buttons with touch positions and keyboard fallbacks where sensible. + // Every entry needs its [InGameAction] too: a key/touch-only default is + // strictly worse than no entry at all, because it shadows the action the + // button would otherwise inherit from [ControllerButton.action] via + // [Keymap.addNewButtons] — and only in-game actions reach TPV over OBP + // (BLE / DirCon), which is the whole point on a second device. KeyPair( buttons: [ZwiftButtons.z, EliteSquareButtons.z], physicalKey: null, logicalKey: null, + inGameAction: InGameAction.pause, touchPosition: Offset(33.993890038715456, 92.43667306401531), ), KeyPair( buttons: [ZwiftButtons.a, EliteSquareButtons.a], physicalKey: null, logicalKey: null, + inGameAction: InGameAction.select, touchPosition: Offset(47.37191097597044, 92.86963594239016), ), KeyPair( buttons: [ZwiftButtons.b, EliteSquareButtons.b], physicalKey: null, logicalKey: null, + inGameAction: InGameAction.back, touchPosition: Offset(41.12364102683652, 83.72743323236598), ), KeyPair( buttons: [ZwiftButtons.y, EliteSquareButtons.y], physicalKey: null, logicalKey: null, + inGameAction: InGameAction.menu, touchPosition: Offset(58.52936866684111, 84.31131200977018), ), @@ -166,6 +180,7 @@ class TrainingPeaks extends SupportedApp { buttons: [b], physicalKey: PhysicalKeyboardKey.keyH, logicalKey: LogicalKeyboardKey.keyH, + inGameAction: InGameAction.toggleUi, ), ), ...ControllerButton.values @@ -175,6 +190,7 @@ class TrainingPeaks extends SupportedApp { buttons: [b], physicalKey: PhysicalKeyboardKey.pageUp, logicalKey: LogicalKeyboardKey.pageUp, + inGameAction: InGameAction.increaseResistance, ), ), ...ControllerButton.values @@ -184,6 +200,7 @@ class TrainingPeaks extends SupportedApp { buttons: [b], physicalKey: PhysicalKeyboardKey.pageDown, logicalKey: LogicalKeyboardKey.pageDown, + inGameAction: InGameAction.decreaseResistance, ), ), ], diff --git a/test/utils/keymap/training_peaks_keymap_test.dart b/test/utils/keymap/training_peaks_keymap_test.dart new file mode 100644 index 00000000..08964d26 --- /dev/null +++ b/test/utils/keymap/training_peaks_keymap_test.dart @@ -0,0 +1,65 @@ +import 'package:bike_control/bluetooth/devices/elite/elite_square.dart'; +import 'package:bike_control/bluetooth/devices/zwift/constants.dart'; +import 'package:bike_control/utils/keymap/apps/training_peaks.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_test/flutter_test.dart'; + +/// TrainingPeaks Virtual is reached over OpenBikeControl (BLE / DirCon), and +/// `BaseActions._handleDirectConnect` only forwards a [KeyPair] that carries an +/// [InGameAction]. A default that maps a button to a keyboard key or a touch +/// position *only* is therefore worse than shipping no default at all: it +/// shadows the action the button would otherwise inherit from +/// [ControllerButton.action] via [Keymap.addNewButtons], and the press can +/// never leave the phone. +void main() { + final app = TrainingPeaks(); + final supportedActions = app.defaultObpSupportedButtons.mapNotNull((b) => b.action).toSet(); + + test('every default key pair carries an in-game action', () { + final actionless = app.keymap.keyPairs + .where((k) => k.inGameAction == null) + .expand((k) => k.buttons) + .map((b) => b.name) + .toList(); + + expect(actionless, isEmpty, reason: 'these buttons can never reach TPV over OBP: $actionless'); + }); + + test('every default in-game action is one TPV accepts over OBP', () { + for (final keyPair in app.keymap.keyPairs) { + expect( + supportedActions, + contains(keyPair.inGameAction), + reason: '${keyPair.buttons.first.name} → ${keyPair.inGameAction} is not in defaultObpSupportedButtons', + ); + } + }); + + test('navigation and face buttons keep their conventional actions', () { + InGameAction? actionFor(ControllerButton button) => + app.keymap.getKeyPair(button, trigger: ButtonTrigger.singleClick)?.inGameAction; + + expect(actionFor(ZwiftButtons.navigationUp), InGameAction.up); + expect(actionFor(ZwiftButtons.navigationLeft), InGameAction.steerLeft); + expect(actionFor(ZwiftButtons.navigationRight), InGameAction.steerRight); + expect(actionFor(ZwiftButtons.a), InGameAction.select); + expect(actionFor(ZwiftButtons.b), InGameAction.back); + expect(actionFor(ZwiftButtons.y), InGameAction.menu); + expect(actionFor(ZwiftButtons.z), InGameAction.pause); + + // The Elite Square shares the face-button key pairs. + expect(actionFor(EliteSquareButtons.a), InGameAction.select); + expect(actionFor(EliteSquareButtons.y), InGameAction.menu); + }); + + test('navigation down inherits its default action instead of being shadowed', () { + // Not in the profile at all — [Keymap.addNewButtons] seeds it from the + // button itself when a controller connects. This is the behaviour the + // explicit defaults above have to match. + expect(app.keymap.getKeyPair(ZwiftButtons.navigationDown), isNull); + expect(ZwiftButtons.navigationDown.action, InGameAction.down); + expect(supportedActions, contains(InGameAction.down)); + }); +}