libstp.step.logic.do_while

Classes

DoWhileActive

Run one task step only while a reference step is still active.

Functions

do_while_active(reference_step, task)

Run a task concurrently with a reference step, cancelling the task when the reference finishes.

Module Contents

class libstp.step.logic.do_while.DoWhileActive(reference_step: libstp.step.Step, task: libstp.step.Step)

Bases: libstp.step.Step

Run one task step only while a reference step is still active.

reference_step
task
libstp.step.logic.do_while.do_while_active(reference_step: libstp.step.Step, task: libstp.step.Step)

Run a task concurrently with a reference step, cancelling the task when the reference finishes.

Both steps start executing at the same time. When reference_step completes (either normally or via exception), task is immediately cancelled. This is useful for running a background activity (e.g. sensor polling, motor oscillation) only for as long as a primary action is running.

If task finishes before reference_step, the reference step continues running until it completes on its own.

Parameters:
  • reference_step – The primary step whose lifetime controls the task. When this step finishes, task is cancelled.

  • task – The secondary step that runs concurrently and is cancelled once reference_step completes.

Returns:

A step that manages the concurrent execution.

Return type:

DoWhileActive

Example:

from libstp.step.logic import do_while_active, loop_forever

# Flash an LED while the robot drives forward
flash_led = loop_forever(seq([
    set_digital(0, True),
    wait(0.25),
    set_digital(0, False),
    wait(0.25),
]))
do_while_active(drive_forward(50), flash_led)