Complete three visible speed-limit purple pulses

This commit is contained in:
Dom
2026-11-13 17:27:09 -06:00
parent fa5d114a4c
commit 4479bc679d
3 changed files with 85 additions and 11 deletions
+11 -6
View File
@@ -43,21 +43,26 @@ class SpeedLimitPulse:
if live and not observation.action_enabled and limit != self.last_limit:
self.start_time = now
self.display_pulsing = True
# Finish an already-started highlight for the same visible valid sign.
# Source hold/recovery and automatic adoption are not new animations.
elif (observation.kind != ObservationKind.VALID or limit != self.last_limit or
not self.display_pulsing):
# Finish the highlight while the same sign remains displayed, including
# retained stale/absent observations. Freshness does not time this animation.
elif limit != self.last_limit or not self.display_pulsing:
self.start_time = -math.inf
self.display_pulsing = False
self.manual_pending = False
self.last_limit = limit
return
manual = not observation.automatic_adoption_pending and slc_accept_available(state)
self.display_pulsing = not manual
if observation.pending_since_ns > 0:
if not manual:
# Start the three visible cycles when the UI first sees the new sign.
# The controller timestamp still governs acceptance, but may predate its
# first rendered frame or change while this same sign remains on screen.
if limit != self.last_limit or not self.display_pulsing or not math.isfinite(self.start_time):
self.start_time = now
elif observation.pending_since_ns > 0:
self.start_time = observation.pending_since_ns / 1e9
elif limit != self.last_limit or manual != self.manual_pending or not math.isfinite(self.start_time):
self.start_time = now
self.display_pulsing = not manual
self.manual_pending = manual
self.last_limit = limit
@@ -1,4 +1,4 @@
"""Controller-timed automatic pulses and the stationary manual confirmation rhythm."""
"""Three visible purple pulses and the unchanged manual confirmation rhythm."""
from dataclasses import replace
from openpilot.starpilot.ui.raylib import rl
@@ -18,9 +18,9 @@ def state(automatic=True, **changes):
return replace(OnroadState(True, False, 10., 80., obs, drive_frame=1), **changes)
def test_three_slow_flashes_stop_at_the_controller_deadline():
def test_three_slow_flashes_stop_after_three_visible_seconds():
pulse = SpeedLimitPulse()
pulse.update(state(), 10.25)
pulse.update(state(), 10.)
assert pulse.start_time == 10.
for now in (10., 11., 12., 13., 14.):
assert rgba(pulse.color(rl.WHITE, now)) == rgba(rl.WHITE)
@@ -93,12 +93,14 @@ def test_switching_between_manual_and_automatic_uses_the_current_rhythm():
assert rgba(pulse.color(rl.WHITE, 13.)) == rgba(rl.WHITE)
def test_reappearing_widget_uses_remaining_controller_time():
def test_reappearing_automatic_widget_gets_three_visible_cycles():
pulse = SpeedLimitPulse()
pulse.update(state(), 10., visible=False)
pulse.update(state(), 12.)
assert rgba(pulse.color(rl.WHITE, 12.5)) == (188, 132, 255, 255)
assert rgba(pulse.color(rl.WHITE, 13.)) == rgba(rl.WHITE)
assert rgba(pulse.color(rl.WHITE, 13.5)) == (188, 132, 255, 255)
assert rgba(pulse.color(rl.WHITE, 14.5)) == (188, 132, 255, 255)
assert rgba(pulse.color(rl.WHITE, 15.)) == rgba(rl.WHITE)
def display_state(limit=20., **changes):
@@ -178,3 +180,41 @@ def test_new_held_value_does_not_start_or_inherit_a_highlight(held_fields):
held = replace(display_state(25.), speed_limit=replace(display_state(25.).speed_limit, **held_fields))
pulse.update(held, 10.5)
assert rgba(pulse.color(rl.WHITE, 10.5)) == rgba(rl.WHITE)
@pytest.mark.parametrize('kind', [ObservationKind.STALE, ObservationKind.ABSENT, ObservationKind.UNKNOWN])
def test_same_retained_sign_finishes_all_three_purple_pulses(kind):
pulse = SpeedLimitPulse()
current = display_state()
pulse.update(current, 10.)
retained = replace(current, speed_limit=replace(current.speed_limit, kind=kind, speed_limit_mps=None,
last_received_speed_limit_mps=20.))
for now in (10.5, 11.5, 12.5):
pulse.update(retained, now)
assert rgba(pulse.color(rl.WHITE, now)) == (188, 132, 255, 255)
pulse.update(current, 13.5)
assert rgba(pulse.color(rl.WHITE, 13.5)) == rgba(rl.WHITE)
def test_late_automatic_observation_gets_three_complete_visible_pulses():
pulse = SpeedLimitPulse()
current = state()
pulse.update(current, 11.4)
# Controller timing governs adoption, not the local visual notification.
for now in (11.9, 12.9, 13.9):
pulse.update(current, now)
assert rgba(pulse.color(rl.WHITE, now)) == (188, 132, 255, 255)
pulse.update(current, 14.4)
assert rgba(pulse.color(rl.WHITE, 14.4)) == rgba(rl.WHITE)
def test_repeated_pending_timestamp_changes_do_not_restart_purple_pulse():
pulse = SpeedLimitPulse()
current = state()
pulse.update(current, 10.)
for now in (10.5, 11.5, 12.5):
changed = replace(current, speed_limit=replace(current.speed_limit, pending_since_ns=int(now * 1e9)))
pulse.update(changed, now)
assert rgba(pulse.color(rl.WHITE, now)) == (188, 132, 255, 255)
pulse.update(current, 13.5)
assert rgba(pulse.color(rl.WHITE, 13.5)) == rgba(rl.WHITE)
@@ -584,3 +584,32 @@ def test_contour_accepts_raylib_standard_color_tuples():
draw_text(font, 'Dash', FontRole.NORMAL, 22, 100, 50, rl.WHITE)
color = font.draw.call_args.args[-1]
assert (color.r, color.g, color.b, color.a) == (255, 255, 255, 255)
@pytest.mark.parametrize('profile', list(Profile))
@pytest.mark.parametrize('automatic', [False, True])
def test_purple_sign_renders_three_full_cycles_across_retention(profile, automatic):
font = fonts(profile)
sign = SpeedLimitWidget(font, profile)
current = state(observed_ns=11_400_000_000, drive_frame=1)
obs = replace(current.speed_limit, action_enabled=automatic, automatic_adoption_pending=automatic,
pending_speed_limit_mps=50 / 3.6 if automatic else None)
current = replace(current, speed_limit=obs)
sign.render(current)
# The source can expire/adopt while the same posted sign stays visible.
retained = replace(current, speed_limit=replace(obs, kind=ObservationKind.STALE, speed_limit_mps=None,
last_received_speed_limit_mps=50 / 3.6, last_received_source='map',
pending_speed_limit_mps=None, automatic_adoption_pending=False))
for frame in range(1, 181):
elapsed = frame / 60
font.draw.reset_mock()
sign.render(replace(retained, observed_ns=11_400_000_000 + round(elapsed * 1e9)))
colors = [rgba_tuple(call.args[-1]) for call in foreground_calls(font.draw)]
assert colors
if frame in (30, 90, 150):
assert (188, 132, 255, 255) in colors
if frame in (60, 120, 180):
assert (188, 132, 255, 255) not in colors
font.draw.reset_mock()
sign.render(replace(current, observed_ns=14_900_000_000))
assert all(rgba_tuple(call.args[-1]) != (188, 132, 255, 255) for call in foreground_calls(font.draw))