| ❌ | ✅ | ❌ | ❌ | ❌ | ❌ |
|---|
City Hub¶
- class CityHub¶
LEGO® City Hub.
Using the hub status light
- light.on(color)¶
Turns on the light at the specified color.
- Parameters:
color (Color) – Color of the light.
- light.off()¶
Turns off the light.
- light.blink(color, durations)¶
Blinks the light at a given color by turning it on and off for given durations.
The light keeps blinking indefinitely while the rest of your program keeps running.
This method provides a simple way to make basic but useful patterns. For more generic and multi-color patterns, use
animate()instead.
- light.animate(colors, interval)¶
Animates the light with a sequence of colors, shown one by one for the given interval.
The animation runs in the background while the rest of your program keeps running. When the animation completes, it repeats.
Using the battery
- battery.voltage() int: mV¶
Gets the voltage of the battery.
- Returns:
Battery voltage.
- battery.current() int: mA¶
Gets the current supplied by the battery.
- Returns:
Battery current.
Button and system control
- buttons.pressed() Set[Button]¶
Checks which buttons are currently pressed.
- Returns:
Set of pressed buttons.
- system.info() dict¶
Gets information about the hub as a dictionary with the following keys:
"name": The hub name. This is the name you see when connecting via Bluetooth."reset_reason": Why the hub (re)booted. It is0if the hub was previously powered off normally. It is1if the hub rebooted automatically, like after a firmware update. It is2if the hub previously crashed due to a watchdog timeout, which indicates a firmware issue."host_connected_ble":Trueif the hub is connected to a computer, tablet, or phone via Bluetooth, andFalseotherwise."host_connected_usb":Trueif the hub is connected to a computer via USB and activated in the app.Falseotherwise."program_start_type": It is1if the program started automatically when the hub was powered on. It is2if the program was started with the hub buttons. It is3if the program was started from your connected computer.“program_id”: Program (slot) number of the currently running program.
- Returns:
A dictionary with system info.
Changed in version 3.6: The name and reset reason where previously available as separate methods. Now they are included in the info dictionary. The methods are still available for backwards compatibility.
- system.set_stop_button(button)¶
Sets the button or button combination that stops a running script.
Normally, the center button is used to stop a running script. You can change or disable this behavior in order to use the button for other purposes.
- Parameters:
button (Button) – A button such as
Button.CENTER, or a tuple of multiple buttons. ChooseNoneto disable the stop button altogether. If you do, you can still turn the hub off by holding the center button for three seconds.
- system.storage(offset, write=)¶
- system.storage(offset, read=) bytes
Reads or writes binary data to persistent storage.
This lets you store data that can be used the next time you run the program.
The data will be saved to flash memory when you turn the hub off normally. It will not be saved if the batteries are removed while the hub is still running.
Once saved, the data will remain available even after you remove the batteries.
- Parameters:
- Returns:
The bytes read if reading, otherwise
None.- Raises:
ValueError – If you try to read or write data outside of the allowed range.
You can store up to 128 bytes of data on this hub. The data is cleared when you update the Pybricks firmware or if you restore the original firmware.
- system.reset_storage()¶
Resets all user settings to default values and erases user programs.
- system.shutdown()¶
Stops your program and shuts the hub down.
Status light examples¶
Turning the light on and off¶
from pybricks.hubs import CityHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = CityHub()
# Turn the light on and off 5 times.
for i in range(5):
hub.light.on(Color.RED)
wait(1000)
hub.light.off()
wait(500)
Changing brightness and using custom colors¶
from pybricks.hubs import CityHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub.
hub = CityHub()
# Show the color at 30% brightness.
hub.light.on(Color.RED * 0.3)
wait(2000)
# Use your own custom color.
hub.light.on(Color(h=30, s=100, v=50))
wait(2000)
# Go through all the colors.
for hue in range(360):
hub.light.on(Color(hue))
wait(10)
Making the light blink¶
from pybricks.hubs import CityHub
from pybricks.parameters import Color
from pybricks.tools import wait
# Initialize the hub
hub = CityHub()
# Keep blinking red on and off.
hub.light.blink(Color.RED, [500, 500])
wait(10000)
# Keep blinking green slowly and then quickly.
hub.light.blink(Color.GREEN, [500, 500, 50, 900])
wait(10000)
Creating light animations¶
from pybricks.hubs import CityHub
from pybricks.parameters import Color
from pybricks.tools import wait
from umath import sin, pi
# Initialize the hub.
hub = CityHub()
# Make an animation with multiple colors.
hub.light.animate([Color.RED, Color.GREEN, Color.NONE], interval=500)
wait(10000)
# Make the color RED grow faint and bright using a sine pattern.
hub.light.animate([Color.RED * (0.5 * sin(i / 15 * pi) + 0.5) for i in range(30)], 40)
wait(10000)
# Cycle through a rainbow of colors.
hub.light.animate([Color(h=i * 8) for i in range(45)], interval=40)
wait(10000)