Skip to content

Commit fd55975

Browse files
committed
bricks/ev3: Add NXT ultrasonic sensor on Portview
1 parent 22116c1 commit fd55975

2 files changed

Lines changed: 142 additions & 3 deletions

File tree

bricks/ev3/modules/_ev3_port_view.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
ColorSensor as EV3ColorSensor,
55
TouchSensor as EV3TouchSensor,
66
GyroSensor,
7-
UltrasonicSensor,
7+
UltrasonicSensor as EV3UltrasonicSensor,
88
InfraredSensor,
99
)
10-
from pybricks.nxtdevices import ColorSensor as NXTColorSensor, LightSensor, SoundSensor
10+
from pybricks.nxtdevices import (
11+
ColorSensor as NXTColorSensor,
12+
LightSensor,
13+
SoundSensor,
14+
UltrasonicSensor as NXTUltrasonicSensor,
15+
)
1116
from pybricks.parameters import Button, Port, ImageFile
1217
from pybricks.tools import wait
1318

@@ -30,6 +35,7 @@
3035
IMG_NXT_LIGHT_AMBIENT = ImageFile._PORT_VIEW_NXT_LIGHT_AMBIENT
3136
IMG_NXT_LIGHT_REFLECTION = ImageFile._PORT_VIEW_NXT_LIGHT_REFLECTION
3237
IMG_NXT_SOUND = ImageFile._PORT_VIEW_NXT_SOUND
38+
IMG_NXT_ULTRASONIC = ImageFile._PORT_VIEW_NXT_ULTRASONIC
3339
PORT_NONE_TOP = ImageFile._PORT_VIEW_P0_TOP
3440
PORT_NONE_BOTTOM = ImageFile._PORT_VIEW_P0_BOTTOM
3541
PORT_IMG = [
@@ -94,7 +100,7 @@ def port_process(index):
94100

95101
# EV3 Ultrasonic Sensor
96102
try:
97-
sensor = UltrasonicSensor(PORTS[index])
103+
sensor = EV3UltrasonicSensor(PORTS[index])
98104
while True:
99105
try:
100106
distance = sensor.distance()
@@ -227,6 +233,19 @@ def port_process(index):
227233
except OSError:
228234
pass
229235

236+
# NXT Ultrasonic Sensor
237+
try:
238+
sensor = NXTUltrasonicSensor(PORTS[index])
239+
while True:
240+
try:
241+
distance = sensor.distance()
242+
detail = f"{distance:>4} mm"
243+
yield IMG_NXT_ULTRASONIC, distance, detail
244+
except OSError:
245+
break
246+
except OSError:
247+
pass
248+
230249
# Nothing on this port.
231250
yield None, "---", "No device"
232251

Lines changed: 120 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)