forked from topikachu/python-ev3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIR-remote.py
More file actions
executable file
·64 lines (52 loc) · 1.58 KB
/
Copy pathIR-remote.py
File metadata and controls
executable file
·64 lines (52 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
"""
Simple remote control.
The motors are controlled according to the following table.
Motor port Channel Buttons
---------- ------- -------
A 1 red
B 1 blue
C 2 red
D 2 blue
The program is stopped by pressing the baecon button.
"""
from ev3.ev3dev import Motor, NoSuchMotorError
from ev3.lego import InfraredSensor
from ev3.event_loop import EventLoop
ir = InfraredSensor()
motors = []
for port in 'ABCD':
try:
motor = Motor(port=port)
motor.regulation_mode = False
except NoSuchMotorError:
motor = None
motors.append(motor)
buttons = [
(ir.REMOTE.RED_UP, ir.REMOTE.RED_DOWN),
(ir.REMOTE.BLUE_UP, ir.REMOTE.BLUE_DOWN),
]
def ir_changed(event):
for channel in range(2):
state = event.evaluation[channel]
if state == ir.REMOTE.BAECON_MODE_ON:
for m in motors:
if m is not None:
m.stop()
loop.stop()
for button in range(2):
n = channel * 2 + button
motor = motors[channel * 2 + button]
if not motor:
pass
elif state == buttons[button][0]:
motor.run_forever(50)
elif state == buttons[button][1]:
motor.run_forever(-50)
else:
motor.stop()
loop = EventLoop()
loop.register_value_change(getter=lambda: ir.remote,
startvalue=ir.remote,
target=ir_changed)
loop.start()