mirror of
https://github.com/pybricks/pybricks-api.git
synced 2026-09-12 17:46:19 +00:00
These code samples are rendered in the docs, so we include them here. For the git history of these snippets, see: https://github.com/pybricks/pybricks-projects/commits/f4914069aaacfd9ab1e0dd6f05524f62cfc56b29/snippets
26 lines
711 B
Python
26 lines
711 B
Python
#!/usr/bin/env pybricks-micropython
|
|
from pybricks.hubs import EV3Brick
|
|
from pybricks.iodevices import UARTDevice
|
|
from pybricks.parameters import Port
|
|
from pybricks.media.ev3dev import SoundFile
|
|
|
|
# Initialize the EV3
|
|
ev3 = EV3Brick()
|
|
|
|
# Initialize sensor port 2 as a uart device
|
|
ser = UARTDevice(Port.S2, baudrate=115200)
|
|
|
|
# Write some data
|
|
ser.write(b'\r\nHello, world!\r\n')
|
|
|
|
# Play a sound while we wait for some data
|
|
for i in range(3):
|
|
ev3.speaker.play_file(SoundFile.HELLO)
|
|
ev3.speaker.play_file(SoundFile.GOOD)
|
|
ev3.speaker.play_file(SoundFile.MORNING)
|
|
print("Bytes waiting to be read:", ser.waiting())
|
|
|
|
# Read all data received while the sound was playing
|
|
data = ser.read_all()
|
|
print(data)
|