Skip to content

API

go_wait()

Generate random wait time before light out.

Returns:

Type Description
int

Random wait time between 2000-3000ms

Source code in startsignal.py
10
11
12
13
14
15
16
17
def go_wait() -> int:
    """
    Generate random wait time before light out.

    Returns:
        Random wait time between 2000-3000ms
    """
    return random.randint(2000, 3000)

light_up(column)

Light up LEDs in specified column.

Parameters:

Name Type Description Default
column int

Column index (0-4)

required
Source code in startsignal.py
38
39
40
41
42
43
44
45
46
47
def light_up(column: int) -> None:
    """
    Light up LEDs in specified column.

    Args:
        column: Column index (0-4)
    """
    mb.display.set_pixel(column, 3, LED_BRIGHTNESS)
    mb.display.set_pixel(column, 4, LED_BRIGHTNESS)
    music.pitch(150, 150, wait=False)

run_game()

Run one game cycle.

Returns:

Type Description
int

Reaction time in ms, or negative value if a jump start detected.

Source code in startsignal.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def run_game() -> int:
    """
    Run one game cycle.

    Returns:
        Reaction time in ms, or negative value if a jump start detected.
    """
    if not start_sequence():
        mb.display.show(mb.Image.NO)
        return -1

    start_time = time.ticks_ms()  # type: ignore[attr-defined]
    while not mb.button_a.is_pressed():
        time.sleep_ms(1)  # type: ignore[attr-defined]
    return time.ticks_diff(time.ticks_ms(), start_time)  # type: ignore[attr-defined]

start_sequence()

Execute the start light sequence.

Returns:

Type Description
bool

False if jump start detected, True otherwise

Source code in startsignal.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def start_sequence() -> bool:
    """
    Execute the start light sequence.

    Returns:
        False if jump start detected, True otherwise
    """
    mb.display.clear()

    # Light up the subsequent column
    for seq in range(5):
        if seq != 0 and not wait_for(LIGHT_INTERVAL):
            return False
        light_up(seq)

    # Lights out
    if not wait_for(go_wait()):
        return False
    mb.display.clear()
    return True

wait_for(duration)

Wait for duration(ms) time.

Parameters:

Name Type Description Default
duration int

Wait time(ms)

required

Returns:

Type Description
bool

False if jump start detected, True otherwise

Source code in startsignal.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def wait_for(duration: int) -> bool:
    """
    Wait for duration(ms) time.

    Args:
        duration: Wait time(ms)

    Returns:
        False if jump start detected, True otherwise
    """
    wait_time = time.ticks_ms() + duration  # type: ignore[attr-defined]
    while wait_time > time.ticks_ms():  # type: ignore[attr-defined]
        if mb.button_a.is_pressed():
            return False
        time.sleep_ms(1)  # type: ignore[attr-defined]
    return True