|
| 1 | +from pybricks.tools import wait |
| 2 | +from pybricks.parameters import ImageFile, Button, Port, Color |
| 3 | +from pybricks.hubs import EV3Brick |
| 4 | +from pybricks.ev3devices import InfraredSensor |
| 5 | + |
| 6 | +from _ev3_motor_dc import motor_set_dc |
| 7 | + |
| 8 | +# Load assets. |
| 9 | +IMG_POS = ImageFile._ROTATE_CW18 |
| 10 | +IMG_NEG = ImageFile._ROTATE_CCW18 |
| 11 | +IMG_12 = ImageFile._APP_IR_CONTROL_12 |
| 12 | +IMG_34 = ImageFile._APP_IR_CONTROL_34 |
| 13 | + |
| 14 | +# Power level for all motors when on. |
| 15 | +POWER = 100 |
| 16 | + |
| 17 | +# Load initial UI. |
| 18 | +center_was_pressed = False |
| 19 | +channel_12_active = True |
| 20 | +ev3 = EV3Brick() |
| 21 | + |
| 22 | + |
| 23 | +def draw_ui(): |
| 24 | + ev3.screen.draw_image(0, 20, IMG_12 if channel_12_active else IMG_34) |
| 25 | + |
| 26 | + |
| 27 | +# Wait for sensor to be attached. |
| 28 | +draw_ui() |
| 29 | +sensor = None |
| 30 | +while sensor is None: |
| 31 | + try: |
| 32 | + sensor = InfraredSensor(Port.S4) |
| 33 | + sensor.buttons(1) |
| 34 | + except OSError: |
| 35 | + wait(10) |
| 36 | + |
| 37 | + |
| 38 | +# Exit if sensor is unplugged or program ends. |
| 39 | +while True: |
| 40 | + # Get button state for selected channel pair. |
| 41 | + channel_x = 1 if channel_12_active else 3 |
| 42 | + channel_y = 2 if channel_12_active else 4 |
| 43 | + pressed_x = sensor.buttons(channel_x) |
| 44 | + pressed_y = sensor.buttons(channel_y) |
| 45 | + |
| 46 | + # Use tank drive to set power levels for each motor. |
| 47 | + power = [ |
| 48 | + POWER |
| 49 | + if Button.LEFT_UP in pressed_y |
| 50 | + else -POWER |
| 51 | + if Button.LEFT_DOWN in pressed_y |
| 52 | + else 0, |
| 53 | + POWER |
| 54 | + if Button.LEFT_UP in pressed_x |
| 55 | + else -POWER |
| 56 | + if Button.LEFT_DOWN in pressed_x |
| 57 | + else 0, |
| 58 | + POWER |
| 59 | + if Button.RIGHT_UP in pressed_x |
| 60 | + else -POWER |
| 61 | + if Button.RIGHT_DOWN in pressed_x |
| 62 | + else 0, |
| 63 | + POWER |
| 64 | + if Button.RIGHT_UP in pressed_y |
| 65 | + else -POWER |
| 66 | + if Button.RIGHT_DOWN in pressed_y |
| 67 | + else 0, |
| 68 | + ] |
| 69 | + |
| 70 | + # Blink if power set, otherwise steady green. |
| 71 | + if any(power): |
| 72 | + ev3.light.blink(Color.GREEN, [100, 100, 800, 100]) |
| 73 | + else: |
| 74 | + ev3.light.on(Color.GREEN) |
| 75 | + |
| 76 | + # Apply power level to each motor, and draw visual indicator if moving. |
| 77 | + ev3.screen.draw_box(0, 0, 177, 16, fill=True, color=Color.WHITE) |
| 78 | + for m in range(len(power)): |
| 79 | + dc = motor_set_dc(m, power[m]) |
| 80 | + if dc: |
| 81 | + ev3.screen.draw_image(20 + m * 40, 2, IMG_POS if dc > 0 else IMG_NEG) |
| 82 | + |
| 83 | + # Keep moving until IR button state changes. |
| 84 | + while pressed_x == sensor.buttons(channel_x) and pressed_y == sensor.buttons( |
| 85 | + channel_y |
| 86 | + ): |
| 87 | + wait(10) |
| 88 | + |
| 89 | + # While we wait, monitor center button to change channels. |
| 90 | + if Button.CENTER not in ev3.buttons.pressed(): |
| 91 | + center_was_pressed = False |
| 92 | + elif not center_was_pressed: |
| 93 | + # Pressed now but not before, so toggle channel and restart. |
| 94 | + center_was_pressed = True |
| 95 | + channel_12_active = not channel_12_active |
| 96 | + draw_ui() |
| 97 | + break |
0 commit comments