Files
swiftcontrol/lib/utils/erg_power_stepping.dart
Jonas Bark db5cc74a12 fix(erg,sram): shared ERG stepping with boundary guards; scope §7 guard to shifting-enabled; coalesce iOS echo presses
ERG (review findings on the ±5W change):
- New ErgPowerStepping extension is the single home for the 5W step and
  500W manual cap, replacing 7 inline copies (physical buttons, hero-card
  buttons/slider, three overlay controllers).
- Boundaries are guards, not clamps: up at >=500W is a no-op like the
  disabled UI button (a 600W FTMS target is no longer slashed to 500),
  down from >500W steps relative, and an unknown baseline (hub-commanded
  ERG reports no target) is a no-op with a new localized message instead
  of slamming the trainer to ~150W.
- ERG shift-up Success carries the button again so +5W presses show
  activity feedback (missed conversion from 843f7985).

SRAM gesture:
- Same-lever repeats inside the 90ms combo window coalesce as duplicates:
  a finger can't re-press in 90ms, but iOS re-delivers a colliding read
  response of the 0xFF edge through the notify callback — it must not
  actuate twice (nor break a combo when it misdecodes to the other lever).
- The §7 multishift guard only runs while shifting is enabled — the one
  state where a held paddle actually bursts (per c94b87f0's --observe
  capture). Bonded rapid tapping and both-lever combos are never
  throttled. Timer-driven instead of DateTime.now() (deterministic under
  fakeAsync) and cleared by _resetGesture on (re)connect.
- The "set up SRAM control" warning fires once per connection, not on
  every undecoded press.

Harness:
- EmulationSession.notify drops notifications for removed peripherals so
  delayed notifies (SRAM double-tap) can't land after teardown.
- The 0xFF press edge is SramAxsConstants.triggerEdge at all call sites;
  the emulation test waits on the named TRIGGER_UUID constant.
- New coverage: guard behavior through processCharacteristic in both
  states, burst suppression while unbonded, all-taps-delivered once
  bonded, ERG boundary/game-set-target cases.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 17:51:35 +02:00

44 lines
1.8 KiB
Dart

import 'dart:math' as math;
import 'package:prop/emulators/definitions/fitness_bike_definition.dart';
/// Manual ERG-target stepping shared by every +/- control: the physical
/// shifter buttons (proxy_device), the on-screen buttons and slider
/// (gear_hero_card), and the overlay controls (desktop/android/ios overlay
/// controllers). One home for the step size and the manual range so the
/// controls can't drift apart again (physical buttons once stepped 10 W while
/// the screen stepped 5 W).
extension ErgPowerStepping on FitnessBikeDefinition {
/// Watts added/removed per +/- press.
static const int stepW = 5;
/// Ceiling for MANUALLY set targets. The definition itself accepts up to
/// 2000 W (FTMS setTargetPower from a game may exceed 500 W legitimately);
/// this only caps what the +/- controls do.
static const int maxManualW = 500;
/// Steps the manual ERG target by ±[stepW] and returns the new target, or
/// null when nothing was changed:
///
/// - the current target is unknown (`ergTargetPower.value == null`, e.g.
/// hub-commanded ERG where the game owns the target and never reports it) —
/// stepping from a made-up baseline would slam the trainer;
/// - the step would leave the manual range. A target ABOVE [maxManualW]
/// (set by the game) is never yanked down: up is a no-op there, down steps
/// relative like anywhere else.
int? stepManualErgPower({required bool up}) {
final current = ergTargetPower.value;
if (current == null) return null;
final int next;
if (up) {
if (current >= maxManualW) return null;
next = math.min(current + stepW, maxManualW);
} else {
if (current <= 0) return null;
next = math.max(current - stepW, 0);
}
setManualErgPower(next);
return next;
}
}