Skip to content

brian.buttons

brian

UiEventsListenerAlreadyClosedError class objects

class UiEventsListenerAlreadyClosedError(Exception)

Thrown when trying to access closed UiEventsListener

LedColor class objects

class LedColor()

Color of a button LED.

__init__

def __init__(red: int, green: int, blue: int)

Holds color definition for button LEDs. Color values range from 0 to 255. When outside of this range, the values are clamped

red

Red component of the color, in range from 0 to 255. When set to a value outside of this range, the value is clamped

green

Green component of the color, in range from 0 to 255. When set to a value outside of this range, the value is clamped

blue

Blue component of the color, in range from 0 to 255. When set to a value outside of this range, the value is clamped

LedEffect class objects

class LedEffect(Enum)

Constants for selecting the effect of button LEDs. Only in use when LED effects are handled by Brian OS.

OFF

LED is fully off

LOW

LED is pulsing at low brightness level

MEDIUM

LED is pulsing at medium brightness level

HIGH

LED is pulsing at high brightness level

inherit

LED effect is not set at this level; Effect of a previous (system) level is used.

Button class objects

class Button()

A physical button of the brick. Pre-existing instances are available as brian.buttons.top_left, top_right, bottom_left, bottom_right, any_button and any_button_incl_knob — the class is not instantiable.

is_pressed

def is_pressed() -> bool

Returns:

True if the button is currently pressed by the user.

wait_for_press

def wait_for_press(timeout_ms: Optional[int] = None) -> bool

Waits for next button press event.

This function is blocking.

Arguments:

  • timeout_ms: Maximum number of milliseconds to wait.
  • If the timeout is not provided or is None, the function will wait indefinitely.

Returns:

success: - True: If the desired button event was caught. - False: If the timeout ran out.

wait_for_release

def wait_for_release(timeout_ms: Optional[int] = None) -> bool

Waits for next button release event.

This function is blocking.

Arguments:

  • timeout_ms: Maximum number of milliseconds to wait.
  • If the timeout is not provided or is None, the function will wait indefinitely.

Returns:

success: - True: If the desired button event was caught. - False: If the timeout ran out.

wait_for_press_and_release

def wait_for_press_and_release(timeout_ms: Optional[int] = None) -> bool

Waits for next button press and release event.

This function is blocking.

Arguments:

  • timeout_ms: Maximum number of milliseconds to wait.
  • If the timeout is not provided or is None, the function will wait indefinitely.

Returns:

success: - True: If the desired button event was caught. - False: If the timeout ran out.

set_led

def set_led(effect: LedEffect,
            color: LedColor = 'DEFAULT_COLOR_FROM_SETTINGS') -> None

Sets desired effect and color for the LED of this button. These settings override the effect

requested by the OS, unless LedEffect.inherit is used. For buttons.any_button and buttons.any_button_incl_knob all respective buttons change color/effect.

Arguments:

  • effect: Target LED effect.
  • color: Target color. When unfilled default color from settings is used.

Knob class objects

class Knob(Button)

The physical knob of the brick. The pre-existing instance is available as brian.buttons.knob — the class is not instantiable.

turned_to

def turned_to() -> int

Returns:

Absolute turn indent count (offset-corrected). Can be reset via reset_absolute_rotation().

wait_for_directional_turn

def wait_for_directional_turn(clockwise: bool = True,
                              timeout_ms: Optional[int] = None) -> bool

Waits for next directional turn of the knob.

This function is blocking.

Arguments:

  • clockwise: Whether to wait for clockwise or counterclockwise turn.
  • timeout_ms: Maximum number of milliseconds to wait.
  • If the timeout is not provided or is None, the function will wait indefinitely.

Returns:

success: - True: If the desired button event was caught. - False: If the timeout ran out.

wait_for_any_turn

def wait_for_any_turn(timeout_ms: Optional[int] = None) -> bool

Waits for next any turn of the knob.

This function is blocking.

Arguments:

  • timeout_ms: Maximum number of milliseconds to wait.
  • If the timeout is not provided or is None, the function will wait indefinitely.

Returns:

success: - True: If the desired button event was caught. - False: If the timeout ran out.

reset_absolute_rotation

def reset_absolute_rotation(new_turned_to: int = 0) -> None

Resets turned_to() back to zero (or the provided value).

Arguments:

  • new_turned_to: Value that turned_to() should return immediately after this call. Defaults to 0.

top_left

Top left physical button. Live hardware state — no listener needed.

top_right

Top right physical button. Live hardware state — no listener needed.

bottom_left

Bottom left physical button. Live hardware state — no listener needed.

bottom_right

Bottom right physical button. Live hardware state — no listener needed.

any_button

Virtual button that reacts to any of the four corner buttons (knob excluded).

any_button_incl_knob

Virtual button that reacts to any of the four corner buttons or the knob press.

knob

The physical knob (rotation and press). Live hardware state — no listener needed.

enable_knob_rotation_animation

@staticmethod
def enable_knob_rotation_animation(enabled: bool) -> None

If enabled, OS automatically animates the LEDs under the knob when it rotates. The user program starts in the enabled state.

Arguments:

  • enabled: Whether to animate knob rotation.

use_os_colors

@staticmethod
def use_os_colors() -> None

Returns button LED control to Brian OS (default state). Counterpart of :func:use_raw_colors.

use_raw_colors

@staticmethod
def use_raw_colors() -> None

Takes over full control of the button LEDs. After this call the LEDs show exactly the colors set by :func:set_raw_led_colors — no OS effects are applied.

get_num_of_leds

@staticmethod
def get_num_of_leds() -> int

Returns:

number of individually controllable button LEDs.

set_raw_led_colors

@staticmethod
def set_raw_led_colors(raw_colors: List[LedColor]) -> None

Sets the raw color of every button LED. Only visible after :func:use_raw_colors.

Arguments:

  • raw_colors: List of exactly :func:get_num_of_leds colors.