mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-07-28 04:07:46 +00:00
pybricks.pupdevices.Remote: Light examples.
This commit is contained in:
@@ -26,6 +26,18 @@ Checking which buttons are pressed
|
||||
.. literalinclude::
|
||||
../../../examples/pup/remote/basics.py
|
||||
|
||||
Changing the remote light color
|
||||
**********************************
|
||||
|
||||
.. literalinclude::
|
||||
../../../examples/pup/remote/set_color_basic.py
|
||||
|
||||
Changing the light color using the buttons
|
||||
*******************************************
|
||||
|
||||
.. literalinclude::
|
||||
../../../examples/pup/remote/set_color.py
|
||||
|
||||
|
||||
Using the timeout setting
|
||||
**********************************
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
from pybricks.pupdevices import Remote
|
||||
from pybricks.parameters import Button, Color
|
||||
|
||||
|
||||
def button_to_color(buttons):
|
||||
|
||||
# Return a color depending on the button.
|
||||
if Button.LEFT_PLUS in buttons:
|
||||
return Color.RED
|
||||
if Button.LEFT_MINUS in buttons:
|
||||
return Color.GREEN
|
||||
if Button.LEFT in buttons:
|
||||
return Color.ORANGE
|
||||
if Button.RIGHT_PLUS in buttons:
|
||||
return Color.BLUE
|
||||
if Button.RIGHT_MINUS in buttons:
|
||||
return Color.YELLOW
|
||||
if Button.RIGHT in buttons:
|
||||
return Color.CYAN
|
||||
if Button.CENTER in buttons:
|
||||
return Color.VIOLET
|
||||
|
||||
# Return no color by default.
|
||||
return Color.NONE
|
||||
|
||||
|
||||
# Connect to the remote.
|
||||
remote = Remote()
|
||||
|
||||
while True:
|
||||
# Wait until a button is pressed.
|
||||
pressed = ()
|
||||
while not pressed:
|
||||
pressed = remote.buttons.pressed()
|
||||
|
||||
# Convert button code to color.
|
||||
color = button_to_color(pressed)
|
||||
|
||||
# Set the remote light color.
|
||||
remote.light.on(color)
|
||||
|
||||
# Wait until all buttons are released.
|
||||
while pressed:
|
||||
pressed = remote.buttons.pressed()
|
||||
@@ -0,0 +1,15 @@
|
||||
from pybricks.pupdevices import Remote
|
||||
from pybricks.parameters import Color
|
||||
from pybricks.tools import wait
|
||||
|
||||
# Connect to the remote.
|
||||
remote = Remote()
|
||||
|
||||
while True:
|
||||
# Set the color to red.
|
||||
remote.light.on(Color.RED)
|
||||
wait(1000)
|
||||
|
||||
# Set the color to blue.
|
||||
remote.light.on(Color.BLUE)
|
||||
wait(1000)
|
||||
Reference in New Issue
Block a user