Last active
May 1, 2017 21:32
-
-
Save bennuttall/f4f3f564e8a2f79e8fae to your computer and use it in GitHub Desktop.
robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original | |
# 16 lines | |
from gpiozero import RyanteckRobot, Button | |
from signal import pause | |
robot = RyanteckRobot() | |
left = Button(26) | |
right = Button(16) | |
fw = Button(21) | |
bw = Button(20) | |
fw.when_pressed = robot.forward | |
fw.when_released = robot.stop | |
left.when_pressed = robot.left | |
left.when_released = robot.stop | |
right.when_pressed = robot.right | |
right.when_released = robot.stop | |
bw.when_pressed = robot.backward | |
bw.when_released = robot.stop | |
pause() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Zip approach | |
# 8 lines | |
from gpiozero import RyanteckRobot, Button | |
from signal import pause | |
robot = RyanteckRobot() | |
buttons = [Button(btn) for btn in [26, 16, 21, 20]] | |
actions = [robot.forward, robot.left, robot.right, robot.backward] | |
for btn, act in zip(directions, actions): btn.when_pressed = act | |
for btn in [fw, left, right, bw]: btn.when_released = robot.stop | |
pause() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Zip approach with shortened object names | |
# 8 lines | |
from gpiozero import RyanteckRobot as R, Button as B | |
import signal as s | |
r = R() | |
b = [B(n) for n in [26, 16, 21, 20]] | |
a = [r.forward, r.left, r.right, r.backward] | |
for d, c in zip(b, a): d.when_pressed = c | |
for n in b: n.when_released = r.stop | |
s.pause() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dictionary approach | |
# 13 lines | |
from gpiozero import RyanteckRobot, Button | |
from signal import pause | |
robot = RyanteckRobot() | |
button_actions = { | |
Button(26): robot.forward, | |
Button(16): robot.left, | |
Button(21): robot.right, | |
Button(20): robot.backward, | |
} | |
for btn, action in button_actions.items(): | |
btn.when_pressed = action | |
btn.when_released = robot.stop | |
pause() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dictionary approach with one very ling line | |
# 5 lines | |
from gpiozero import RyanteckRobot, Button | |
import signal | |
robot = RyanteckRobot() | |
for btn, action in {Button(26): robot.forward, Button(16): robot.left, Button(21): robot.right, Button(20): robot.backward}.items(): btn.when_pressed, btn.when_released = action, robot.stop | |
pause() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dictionary approach with one-letter object names | |
# 5 lines | |
from gpiozero import RyanteckRobot as R, Button as B | |
import signal as s | |
r = R() | |
for b, a in {B(26): r.forward, B(16): r.left, B(21): r.right, B(20): r.backward}.items(): b.when_pressed, b.when_released = a, r.stop | |
s.pause() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hmm, 5 and 6 are relying on hidden implementation to keep their Button references alive (in other words, if that hidden implementation changes, they'll break). A minimized version of 4 is about the best I can do off the top of my head, but I'll keep thinking about it!