Last active
August 1, 2017 12:00
-
-
Save bennuttall/d6bfddafcd46f2cc8954f8614f272d76 to your computer and use it in GitHub Desktop.
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
# run with GPIOZERO_PIN_FACTORY=mock python3 sense_robot_mock.py | |
import gpiozero | |
from gpiozero import Robot | |
from gpiozero.pins.mock import MockPWMPin | |
from sense_emu import SenseHat | |
from time import sleep | |
gpiozero.Device.pin_factory.pin_class = MockPWMPin | |
sense = SenseHat() | |
robot = Robot(left=(2, 3), right=(4, 5)) | |
sense.stick.direction_up = robot.forward | |
sense.stick.direction_down = robot.backward | |
sense.stick.direction_left = robot.left | |
sense.stick.direction_right = robot.right | |
sense.stick.direction_middle = robot.stop | |
while True: | |
print(tuple(robot.value)) | |
sleep(1) |
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
# run with GPIOZERO_PIN_FACTORY=mock python3 sense_robot_mock_alt.py | |
import gpiozero | |
from gpiozero import Robot | |
from gpiozero.pins.mock import MockPWMPin | |
from sense_emu import SenseHat | |
gpiozero.Device.pin_factory.pin_class = MockPWMPin | |
sense = SenseHat() | |
robot = Robot(left=(2, 3), right=(4, 5)) | |
while True: | |
event = sense.stick.wait_for_event() | |
if event.action in ('pressed', 'held'): | |
if event.direction == 'up': | |
robot.forward() | |
elif event.direction == 'down': | |
robot.backward() | |
elif event.direction == 'left': | |
robot.left() | |
elif event.direction == 'right': | |
robot.right() | |
else: | |
robot.stop() | |
print(tuple(robot.value)) |
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
from gpiozero import Robot | |
from gpiozero.pins.pigpio import PiGPIOFactory | |
from sense_hat import SenseHat | |
from signal import pause | |
factory = PiGPIOFactory(host='192.168.1.5') | |
sense = SenseHat() | |
robot = Robot(left=(2, 3), right=(4, 5), pin_factory=factory) | |
sense.stick.direction_up = robot.forward | |
sense.stick.direction_down = robot.backward | |
sense.stick.direction_left = robot.left | |
sense.stick.direction_right = robot.right | |
sense.stick.direction_middle = 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
from gpiozero import Robot | |
from gpiozero.pins.pigpio import PiGPIOFactory | |
from sense_hat import SenseHat | |
factory = PiGPIOFactory(host='192.168.1.5') | |
sense = SenseHat() | |
robot = Robot(left=(2, 3), right=(4, 5), pin_factory=factory) | |
while True: | |
event = sense.stick.wait_for_event() | |
if event.action in ('pressed', 'held'): | |
if event.direction == 'up': | |
robot.forward() | |
elif event.direction == 'down': | |
robot.backward() | |
elif event.direction == 'left': | |
robot.left() | |
elif event.direction == 'right': | |
robot.right() | |
else: | |
robot.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment