From 32803a7988b45c9667453702e3f795dbfdaa3faa Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Mon, 29 Aug 2016 20:41:11 +0100 Subject: [PATCH] Convert recipe examples to includes Makes it much easier to test things - no copying'n'pasting just run the examples straight from the dir (after wiring stuff up) --- docs/conf.py | 1 + docs/examples/all_on_1.py | 9 + docs/examples/all_on_2.py | 9 + docs/examples/all_on_3.py | 23 + docs/examples/button_1.py | 9 + docs/examples/button_2.py | 6 + docs/examples/button_3.py | 11 + docs/examples/button_4.py | 15 + docs/examples/button_camera_1.py | 15 + docs/examples/button_camera_2.py | 18 + docs/examples/button_led_1.py | 10 + docs/examples/button_led_2.py | 9 + docs/examples/button_shutdown.py | 11 + docs/examples/button_stop_motion.py | 12 + docs/examples/distance_sensor_1.py | 8 + docs/examples/distance_sensor_2.py | 10 + docs/examples/led_1.py | 10 + docs/examples/led_2.py | 8 + docs/examples/led_bargraph_1.py | 15 + docs/examples/led_bargraph_2.py | 15 + docs/examples/led_board_1.py | 15 + docs/examples/led_board_2.py | 5 + docs/examples/led_builtin.py | 9 + docs/examples/led_pulse.py | 8 + docs/examples/led_travis.py | 19 + docs/examples/led_variable_brightness.py | 12 + docs/examples/light_sensor_1.py | 9 + docs/examples/light_sensor_2.py | 10 + docs/examples/light_sensor_3.py | 9 + docs/examples/motion_sensor.py | 10 + docs/examples/motor.py | 10 + docs/examples/music_box.py | 18 + docs/examples/pot_1.py | 6 + docs/examples/pot_2.py | 7 + docs/examples/reaction_game.py | 22 + docs/examples/rgbled.py | 28 + docs/examples/rgbled_pot_1.py | 11 + docs/examples/rgbled_pot_2.py | 11 + docs/examples/robot_1.py | 10 + docs/examples/robot_2.py | 9 + docs/examples/robot_buttons.py | 23 + docs/examples/robot_keyboard_1.py | 34 + docs/examples/robot_keyboard_2.py | 21 + docs/examples/robot_motion_1.py | 10 + docs/examples/robot_motion_2.py | 9 + docs/examples/thermometer.py | 12 + docs/examples/traffic_lights_1.py | 20 + docs/examples/traffic_lights_2.py | 20 + docs/examples/traffic_lights_3.py | 24 + docs/recipes.rst | 802 ++++------------------- 50 files changed, 745 insertions(+), 692 deletions(-) create mode 100644 docs/examples/all_on_1.py create mode 100644 docs/examples/all_on_2.py create mode 100644 docs/examples/all_on_3.py create mode 100644 docs/examples/button_1.py create mode 100644 docs/examples/button_2.py create mode 100644 docs/examples/button_3.py create mode 100644 docs/examples/button_4.py create mode 100644 docs/examples/button_camera_1.py create mode 100644 docs/examples/button_camera_2.py create mode 100644 docs/examples/button_led_1.py create mode 100644 docs/examples/button_led_2.py create mode 100644 docs/examples/button_shutdown.py create mode 100644 docs/examples/button_stop_motion.py create mode 100644 docs/examples/distance_sensor_1.py create mode 100644 docs/examples/distance_sensor_2.py create mode 100644 docs/examples/led_1.py create mode 100644 docs/examples/led_2.py create mode 100644 docs/examples/led_bargraph_1.py create mode 100644 docs/examples/led_bargraph_2.py create mode 100644 docs/examples/led_board_1.py create mode 100644 docs/examples/led_board_2.py create mode 100644 docs/examples/led_builtin.py create mode 100644 docs/examples/led_pulse.py create mode 100644 docs/examples/led_travis.py create mode 100644 docs/examples/led_variable_brightness.py create mode 100644 docs/examples/light_sensor_1.py create mode 100644 docs/examples/light_sensor_2.py create mode 100644 docs/examples/light_sensor_3.py create mode 100644 docs/examples/motion_sensor.py create mode 100644 docs/examples/motor.py create mode 100644 docs/examples/music_box.py create mode 100644 docs/examples/pot_1.py create mode 100644 docs/examples/pot_2.py create mode 100644 docs/examples/reaction_game.py create mode 100644 docs/examples/rgbled.py create mode 100644 docs/examples/rgbled_pot_1.py create mode 100644 docs/examples/rgbled_pot_2.py create mode 100644 docs/examples/robot_1.py create mode 100644 docs/examples/robot_2.py create mode 100644 docs/examples/robot_buttons.py create mode 100644 docs/examples/robot_keyboard_1.py create mode 100644 docs/examples/robot_keyboard_2.py create mode 100644 docs/examples/robot_motion_1.py create mode 100644 docs/examples/robot_motion_2.py create mode 100644 docs/examples/thermometer.py create mode 100644 docs/examples/traffic_lights_1.py create mode 100644 docs/examples/traffic_lights_2.py create mode 100644 docs/examples/traffic_lights_3.py diff --git a/docs/conf.py b/docs/conf.py index fea2f1c..6fb6ea2 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -74,6 +74,7 @@ autodoc_member_order = 'groupwise' intersphinx_mapping = { 'python': ('http://docs.python.org/3.4', None), + 'picamera': ('http://picamera.readthedocs.io/en/latest', None), } # -- Options for HTML output ---------------------------------------------- diff --git a/docs/examples/all_on_1.py b/docs/examples/all_on_1.py new file mode 100644 index 0000000..978e4e1 --- /dev/null +++ b/docs/examples/all_on_1.py @@ -0,0 +1,9 @@ +from gpiozero import FishDish +from signal import pause + +fish = FishDish() + +fish.button.when_pressed = fish.on +fish.button.when_released = fish.off + +pause() diff --git a/docs/examples/all_on_2.py b/docs/examples/all_on_2.py new file mode 100644 index 0000000..0dccc78 --- /dev/null +++ b/docs/examples/all_on_2.py @@ -0,0 +1,9 @@ +from gpiozero import TrafficHat +from signal import pause + +th = TrafficHat() + +th.button.when_pressed = th.on +th.button.when_released = th.off + +pause() diff --git a/docs/examples/all_on_3.py b/docs/examples/all_on_3.py new file mode 100644 index 0000000..682adb4 --- /dev/null +++ b/docs/examples/all_on_3.py @@ -0,0 +1,23 @@ +from gpiozero import LED, Buzzer, Button +from signal import pause + +button = Button(2) +buzzer = Buzzer(3) +red = LED(4) +amber = LED(5) +green = LED(6) + +things = [red, amber, green, buzzer] + +def things_on(): + for thing in things: + thing.on() + +def things_off(): + for thing in things: + thing.off() + +button.when_pressed = things_on +button.when_released = things_off + +pause() diff --git a/docs/examples/button_1.py b/docs/examples/button_1.py new file mode 100644 index 0000000..5ea7e49 --- /dev/null +++ b/docs/examples/button_1.py @@ -0,0 +1,9 @@ +from gpiozero import Button + +button = Button(2) + +while True: + if button.is_pressed: + print("Button is pressed") + else: + print("Button is not pressed") diff --git a/docs/examples/button_2.py b/docs/examples/button_2.py new file mode 100644 index 0000000..4067627 --- /dev/null +++ b/docs/examples/button_2.py @@ -0,0 +1,6 @@ +from gpiozero import Button + +button = Button(2) + +button.wait_for_press() +print("Button was pressed") diff --git a/docs/examples/button_3.py b/docs/examples/button_3.py new file mode 100644 index 0000000..55694e1 --- /dev/null +++ b/docs/examples/button_3.py @@ -0,0 +1,11 @@ +from gpiozero import Button +from signal import pause + +def say_hello(): + print("Hello!") + +button = Button(2) + +button.when_pressed = say_hello + +pause() diff --git a/docs/examples/button_4.py b/docs/examples/button_4.py new file mode 100644 index 0000000..d3f9d34 --- /dev/null +++ b/docs/examples/button_4.py @@ -0,0 +1,15 @@ +from gpiozero import Button +from signal import pause + +def say_hello(): + print("Hello!") + +def say_goodbye(): + print("Goodbye!") + +button = Button(2) + +button.when_pressed = say_hello +button.when_released = say_goodbye + +pause() diff --git a/docs/examples/button_camera_1.py b/docs/examples/button_camera_1.py new file mode 100644 index 0000000..0affb17 --- /dev/null +++ b/docs/examples/button_camera_1.py @@ -0,0 +1,15 @@ +from gpiozero import Button +from picamera import PiCamera +from datetime import datetime +from signal import pause + +button = Button(2) +camera = PiCamera() + +def capture(): + datetime = datetime.now().isoformat() + camera.capture('/home/pi/%s.jpg' % datetime) + +button.when_pressed = capture + +pause() diff --git a/docs/examples/button_camera_2.py b/docs/examples/button_camera_2.py new file mode 100644 index 0000000..18552d4 --- /dev/null +++ b/docs/examples/button_camera_2.py @@ -0,0 +1,18 @@ +from gpiozero import Button +from picamera import PiCamera +from datetime import datetime +from signal import pause + +left_button = Button(2) +right_button = Button(3) +camera = PiCamera() + +def capture(): + datetime = datetime.now().isoformat() + camera.capture('/home/pi/%s.jpg' % datetime) + +left_button.when_pressed = camera.start_preview +left_button.when_released = camera.stop_preview +right_button.when_pressed = capture + +pause() diff --git a/docs/examples/button_led_1.py b/docs/examples/button_led_1.py new file mode 100644 index 0000000..3dce993 --- /dev/null +++ b/docs/examples/button_led_1.py @@ -0,0 +1,10 @@ +from gpiozero import LED, Button +from signal import pause + +led = LED(17) +button = Button(2) + +button.when_pressed = led.on +button.when_released = led.off + +pause() diff --git a/docs/examples/button_led_2.py b/docs/examples/button_led_2.py new file mode 100644 index 0000000..ba66df5 --- /dev/null +++ b/docs/examples/button_led_2.py @@ -0,0 +1,9 @@ +from gpiozero import LED, Button +from signal import pause + +led = LED(17) +button = Button(2) + +led.source = button.values + +pause() diff --git a/docs/examples/button_shutdown.py b/docs/examples/button_shutdown.py new file mode 100644 index 0000000..2a91a40 --- /dev/null +++ b/docs/examples/button_shutdown.py @@ -0,0 +1,11 @@ +from gpiozero import Button +from subprocess import check_call +from signal import pause + +def shutdown(): + check_call(['sudo', 'poweroff']) + +shutdown_btn = Button(17, hold_time=2) +shutdown_btn.when_held = shutdown + +pause() diff --git a/docs/examples/button_stop_motion.py b/docs/examples/button_stop_motion.py new file mode 100644 index 0000000..8111b48 --- /dev/null +++ b/docs/examples/button_stop_motion.py @@ -0,0 +1,12 @@ +from gpiozero import Button +from picamera import PiCamera + +button = Button(2) +camera = PiCamera() + +camera.start_preview() +frame = 1 +while True: + button.wait_for_press() + camera.capture('/home/pi/frame%03d.jpg' % frame) + frame += 1 diff --git a/docs/examples/distance_sensor_1.py b/docs/examples/distance_sensor_1.py new file mode 100644 index 0000000..c777aa9 --- /dev/null +++ b/docs/examples/distance_sensor_1.py @@ -0,0 +1,8 @@ +from gpiozero import DistanceSensor +from time import sleep + +sensor = DistanceSensor(23, 24) + +while True: + print('Distance to nearest object is', sensor.distance, 'm') + sleep(1) diff --git a/docs/examples/distance_sensor_2.py b/docs/examples/distance_sensor_2.py new file mode 100644 index 0000000..86dfe74 --- /dev/null +++ b/docs/examples/distance_sensor_2.py @@ -0,0 +1,10 @@ +from gpiozero import DistanceSensor, LED +from signal import pause + +sensor = DistanceSensor(23, 24, max_distance=1, threshold_distance=0.2) +led = LED(16) + +sensor.when_in_range = led.on +sensor.when_out_of_range = led.off + +pause() diff --git a/docs/examples/led_1.py b/docs/examples/led_1.py new file mode 100644 index 0000000..5e031b9 --- /dev/null +++ b/docs/examples/led_1.py @@ -0,0 +1,10 @@ +from gpiozero import LED +from time import sleep + +red = LED(17) + +while True: + red.on() + sleep(1) + red.off() + sleep(1) diff --git a/docs/examples/led_2.py b/docs/examples/led_2.py new file mode 100644 index 0000000..c09e5dd --- /dev/null +++ b/docs/examples/led_2.py @@ -0,0 +1,8 @@ +from gpiozero import LED +from signal import pause + +red = LED(17) + +red.blink() + +pause() diff --git a/docs/examples/led_bargraph_1.py b/docs/examples/led_bargraph_1.py new file mode 100644 index 0000000..96534b6 --- /dev/null +++ b/docs/examples/led_bargraph_1.py @@ -0,0 +1,15 @@ +from gpiozero import LEDBarGraph +from time import sleep + +graph = LEDBarGraph(5, 6, 13, 19, 26, 20) + +graph.value = 1 # (1, 1, 1, 1, 1, 1) +sleep(1) +graph.value = 1/2 # (1, 1, 1, 0, 0, 0) +sleep(1) +graph.value = -1/2 # (0, 0, 0, 1, 1, 1) +sleep(1) +graph.value = 1/4 # (1, 0, 0, 0, 0, 0) +sleep(1) +graph.value = -1 # (1, 1, 1, 1, 1, 1) +sleep(1) diff --git a/docs/examples/led_bargraph_2.py b/docs/examples/led_bargraph_2.py new file mode 100644 index 0000000..9544990 --- /dev/null +++ b/docs/examples/led_bargraph_2.py @@ -0,0 +1,15 @@ +from gpiozero import LEDBarGraph +from time import sleep + +graph = LEDBarGraph(5, 6, 13, 19, 26, pwm=True) + +graph.value = 1/10 # (0.5, 0, 0, 0, 0) +sleep(1) +graph.value = 3/10 # (1, 0.5, 0, 0, 0) +sleep(1) +graph.value = -3/10 # (0, 0, 0, 0.5, 1) +sleep(1) +graph.value = 9/10 # (1, 1, 1, 1, 0.5) +sleep(1) +graph.value = 95/100 # (1, 1, 1, 1, 0.75) +sleep(1) diff --git a/docs/examples/led_board_1.py b/docs/examples/led_board_1.py new file mode 100644 index 0000000..e4a3a68 --- /dev/null +++ b/docs/examples/led_board_1.py @@ -0,0 +1,15 @@ +from gpiozero import LEDBoard +from time import sleep +from signal import pause + +leds = LEDBoard(5, 6, 13, 19, 26) + +leds.on() +sleep(1) +leds.off() +sleep(1) +leds.value = (1, 0, 1, 0, 1) +sleep(1) +leds.blink() + +pause() diff --git a/docs/examples/led_board_2.py b/docs/examples/led_board_2.py new file mode 100644 index 0000000..2b01a6d --- /dev/null +++ b/docs/examples/led_board_2.py @@ -0,0 +1,5 @@ +from gpiozero import LEDBoard + +leds = LEDBoard(5, 6, 13, 19, 26, pwm=True) + +leds.value = (0.2, 0.4, 0.6, 0.8, 1.0) diff --git a/docs/examples/led_builtin.py b/docs/examples/led_builtin.py new file mode 100644 index 0000000..80a06ff --- /dev/null +++ b/docs/examples/led_builtin.py @@ -0,0 +1,9 @@ +from gpiozero import LED +from signal import pause + +power = LED(35) # /sys/class/leds/led1 +activity = LED(47) # /sys/class/leds/led0 + +activity.blink() +power.blink() +pause() diff --git a/docs/examples/led_pulse.py b/docs/examples/led_pulse.py new file mode 100644 index 0000000..bc6ac49 --- /dev/null +++ b/docs/examples/led_pulse.py @@ -0,0 +1,8 @@ +from gpiozero import PWMLED +from signal import pause + +led = PWMLED(17) + +led.pulse() + +pause() diff --git a/docs/examples/led_travis.py b/docs/examples/led_travis.py new file mode 100644 index 0000000..c5b237d --- /dev/null +++ b/docs/examples/led_travis.py @@ -0,0 +1,19 @@ +from travispy import TravisPy +from gpiozero import LED +from gpiozero.tools import negated +from time import sleep +from signal import pause + +def build_passed(repo='RPi-Distro/python-gpiozero', delay=3600): + t = TravisPy() + r = t.repo(repo) + while True: + yield r.last_build_state == 'passed' + sleep(delay) # Sleep an hour before hitting travis again + +red = LED(12) +green = LED(16) + +red.source = negated(green.values) +green.source = build_passed() +pause() diff --git a/docs/examples/led_variable_brightness.py b/docs/examples/led_variable_brightness.py new file mode 100644 index 0000000..4b9c232 --- /dev/null +++ b/docs/examples/led_variable_brightness.py @@ -0,0 +1,12 @@ +from gpiozero import PWMLED +from time import sleep + +led = PWMLED(17) + +while True: + led.value = 0 # off + sleep(1) + led.value = 0.5 # half brightness + sleep(1) + led.value = 1 # full brightness + sleep(1) diff --git a/docs/examples/light_sensor_1.py b/docs/examples/light_sensor_1.py new file mode 100644 index 0000000..48ed945 --- /dev/null +++ b/docs/examples/light_sensor_1.py @@ -0,0 +1,9 @@ +from gpiozero import LightSensor + +sensor = LightSensor(18) + +while True: + sensor.wait_for_light() + print("It's light! :)") + sensor.wait_for_dark() + print("It's dark :(") diff --git a/docs/examples/light_sensor_2.py b/docs/examples/light_sensor_2.py new file mode 100644 index 0000000..b618365 --- /dev/null +++ b/docs/examples/light_sensor_2.py @@ -0,0 +1,10 @@ +from gpiozero import LightSensor, LED +from signal import pause + +sensor = LightSensor(18) +led = LED(16) + +sensor.when_dark = led.on +sensor.when_light = led.off + +pause() diff --git a/docs/examples/light_sensor_3.py b/docs/examples/light_sensor_3.py new file mode 100644 index 0000000..fb22dec --- /dev/null +++ b/docs/examples/light_sensor_3.py @@ -0,0 +1,9 @@ +from gpiozero import LightSensor, PWMLED +from signal import pause + +sensor = LightSensor(18) +led = PWMLED(16) + +led.source = sensor.values + +pause() diff --git a/docs/examples/motion_sensor.py b/docs/examples/motion_sensor.py new file mode 100644 index 0000000..0ff122a --- /dev/null +++ b/docs/examples/motion_sensor.py @@ -0,0 +1,10 @@ +from gpiozero import MotionSensor, LED +from signal import pause + +pir = MotionSensor(4) +led = LED(16) + +pir.when_motion = led.on +pir.when_no_motion = led.off + +pause() diff --git a/docs/examples/motor.py b/docs/examples/motor.py new file mode 100644 index 0000000..a0d6fd3 --- /dev/null +++ b/docs/examples/motor.py @@ -0,0 +1,10 @@ +from gpiozero import Motor +from time import sleep + +motor = Motor(forward=4, backward=14) + +while True: + motor.forward() + sleep(5) + motor.backward() + sleep(5) diff --git a/docs/examples/music_box.py b/docs/examples/music_box.py new file mode 100644 index 0000000..872b08e --- /dev/null +++ b/docs/examples/music_box.py @@ -0,0 +1,18 @@ +from gpiozero import Button +import pygame.mixer +from pygame.mixer import Sound +from signal import pause + +pygame.mixer.init() + +sound_pins = { + 2: Sound("samples/drum_tom_mid_hard.wav"), + 3: Sound("samples/drum_cymbal_open.wav"), +} + +buttons = [Button(pin) for pin in sound_pins] +for button in buttons: + sound = sound_pins[button.pin.number] + button.when_pressed = sound.play + +pause() diff --git a/docs/examples/pot_1.py b/docs/examples/pot_1.py new file mode 100644 index 0000000..1a8e703 --- /dev/null +++ b/docs/examples/pot_1.py @@ -0,0 +1,6 @@ +from gpiozero import MCP3008 + +pot = MCP3008(channel=0) + +while True: + print(pot.value) diff --git a/docs/examples/pot_2.py b/docs/examples/pot_2.py new file mode 100644 index 0000000..b770edf --- /dev/null +++ b/docs/examples/pot_2.py @@ -0,0 +1,7 @@ +from gpiozero import LEDBarGraph, MCP3008 +from signal import pause + +graph = LEDBarGraph(5, 6, 13, 19, 26, pwm=True) +pot = MCP3008(channel=0) +graph.source = pot.values +pause() diff --git a/docs/examples/reaction_game.py b/docs/examples/reaction_game.py new file mode 100644 index 0000000..f8e3a52 --- /dev/null +++ b/docs/examples/reaction_game.py @@ -0,0 +1,22 @@ +from gpiozero import Button, LED +from time import sleep +import random + +led = LED(17) + +player_1 = Button(2) +player_2 = Button(3) + +time = random.uniform(5, 10) +sleep(time) +led.on() + +while True: + if player_1.is_pressed: + print("Player 1 wins!") + break + if player_2.is_pressed: + print("Player 2 wins!") + break + +led.off() diff --git a/docs/examples/rgbled.py b/docs/examples/rgbled.py new file mode 100644 index 0000000..b80c9dc --- /dev/null +++ b/docs/examples/rgbled.py @@ -0,0 +1,28 @@ +from gpiozero import RGBLED +from time import sleep + +led = RGBLED(red=9, green=10, blue=11) + +led.red = 1 # full red +sleep(1) +led.red = 0.5 # half red +sleep(1) + +led.color = (0, 1, 0) # full green +sleep(1) +led.color = (1, 0, 1) # magenta +sleep(1) +led.color = (1, 1, 0) # yellow +sleep(1) +led.color = (0, 1, 1) # cyan +sleep(1) +led.color = (1, 1, 1) # white +sleep(1) + +led.color = (0, 0, 0) # off +sleep(1) + +# slowly increase intensity of blue +for n in range(100): + led.blue = n/100 + sleep(0.1) diff --git a/docs/examples/rgbled_pot_1.py b/docs/examples/rgbled_pot_1.py new file mode 100644 index 0000000..1efa5d0 --- /dev/null +++ b/docs/examples/rgbled_pot_1.py @@ -0,0 +1,11 @@ +from gpiozero import RGBLED, MCP3008 + +led = RGBLED(red=2, green=3, blue=4) +red_pot = MCP3008(channel=0) +green_pot = MCP3008(channel=1) +blue_pot = MCP3008(channel=2) + +while True: + led.red = red_pot.value + led.green = green_pot.value + led.blue = blue_pot.value diff --git a/docs/examples/rgbled_pot_2.py b/docs/examples/rgbled_pot_2.py new file mode 100644 index 0000000..a5d49cb --- /dev/null +++ b/docs/examples/rgbled_pot_2.py @@ -0,0 +1,11 @@ +from gpiozero import RGBLED, MCP3008 +from signal import pause + +led = RGBLED(2, 3, 4) +red_pot = MCP3008(0) +green_pot = MCP3008(1) +blue_pot = MCP3008(2) + +led.source = zip(red_pot.values, green_pot.values, blue_pot.values) + +pause() diff --git a/docs/examples/robot_1.py b/docs/examples/robot_1.py new file mode 100644 index 0000000..8c0a791 --- /dev/null +++ b/docs/examples/robot_1.py @@ -0,0 +1,10 @@ +from gpiozero import Robot +from time import sleep + +robot = Robot(left=(4, 14), right=(17, 18)) + +for i in range(4): + robot.forward() + sleep(10) + robot.right() + sleep(1) diff --git a/docs/examples/robot_2.py b/docs/examples/robot_2.py new file mode 100644 index 0000000..55107be --- /dev/null +++ b/docs/examples/robot_2.py @@ -0,0 +1,9 @@ +from gpiozero import Robot, DistanceSensor +from signal import pause + +sensor = DistanceSensor(23, 24, max_distance=1, threshold_distance=0.2) +robot = Robot(left=(4, 14), right=(17, 18)) + +sensor.when_in_range = robot.backward +sensor.when_out_of_range = robot.stop +pause() diff --git a/docs/examples/robot_buttons.py b/docs/examples/robot_buttons.py new file mode 100644 index 0000000..58a285c --- /dev/null +++ b/docs/examples/robot_buttons.py @@ -0,0 +1,23 @@ +from gpiozero import Robot, Button +from signal import pause + +robot = Robot(left=(4, 14), right=(17, 18)) + +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() diff --git a/docs/examples/robot_keyboard_1.py b/docs/examples/robot_keyboard_1.py new file mode 100644 index 0000000..366993a --- /dev/null +++ b/docs/examples/robot_keyboard_1.py @@ -0,0 +1,34 @@ +import curses +from gpiozero import Robot + +robot = Robot(left=(4, 14), right=(17, 18)) + +actions = { + curses.KEY_UP: robot.forward, + curses.KEY_DOWN: robot.backward, + curses.KEY_LEFT: robot.left, + curses.KEY_RIGHT: robot.right, + } + +def main(window): + next_key = None + while True: + curses.halfdelay(1) + if next_key is None: + key = window.getch() + else: + key = next_key + next_key = None + if key != -1: + # KEY DOWN + curses.halfdelay(3) + action = actions.get(key) + if action is not None: + action() + next_key = key + while next_key == key: + next_key = window.getch() + # KEY UP + robot.stop() + +curses.wrapper(main) diff --git a/docs/examples/robot_keyboard_2.py b/docs/examples/robot_keyboard_2.py new file mode 100644 index 0000000..e956338 --- /dev/null +++ b/docs/examples/robot_keyboard_2.py @@ -0,0 +1,21 @@ +from gpiozero import Robot +from evdev import InputDevice, list_devices, ecodes + +robot = Robot(left=(4, 14), right=(17, 18)) + +devices = [InputDevice(device) for device in list_devices()] +keyboard = devices[0] # this may vary + +keypress_actions = { + ecodes.KEY_UP: robot.forward, + ecodes.KEY_DOWN: robot.backward, + ecodes.KEY_LEFT: robot.left, + ecodes.KEY_RIGHT: robot.right, +} + +for event in keyboard.read_loop(): + if event.type == ecodes.EV_KEY: + if event.value == 1: # key down + keypress_actions[event.code]() + if event.value == 0: # key up + robot.stop() diff --git a/docs/examples/robot_motion_1.py b/docs/examples/robot_motion_1.py new file mode 100644 index 0000000..11ae2a4 --- /dev/null +++ b/docs/examples/robot_motion_1.py @@ -0,0 +1,10 @@ +from gpiozero import Robot, MotionSensor +from signal import pause + +robot = Robot(left=(4, 14), right=(17, 18)) +pir = MotionSensor(5) + +pir.when_motion = robot.forward +pir.when_no_motion = robot.stop + +pause() diff --git a/docs/examples/robot_motion_2.py b/docs/examples/robot_motion_2.py new file mode 100644 index 0000000..35580cb --- /dev/null +++ b/docs/examples/robot_motion_2.py @@ -0,0 +1,9 @@ +from gpiozero import Robot, MotionSensor +from signal import pause + +robot = Robot(left=(4, 14), right=(17, 18)) +pir = MotionSensor(5) + +robot.source = zip(pir.values, pir.values) + +pause() diff --git a/docs/examples/thermometer.py b/docs/examples/thermometer.py new file mode 100644 index 0000000..d18b6f7 --- /dev/null +++ b/docs/examples/thermometer.py @@ -0,0 +1,12 @@ +from gpiozero import MCP3008 +from time import sleep + +def convert_temp(gen): + for value in gen: + yield (value * 3.3 - 0.5) * 100 + +adc = MCP3008(channel=0) + +for temp in convert_temp(adc.values): + print('The temperature is', temp, 'C') + sleep(1) diff --git a/docs/examples/traffic_lights_1.py b/docs/examples/traffic_lights_1.py new file mode 100644 index 0000000..d102b0b --- /dev/null +++ b/docs/examples/traffic_lights_1.py @@ -0,0 +1,20 @@ +from gpiozero import TrafficLights +from time import sleep + +lights = TrafficLights(2, 3, 4) + +lights.green.on() + +while True: + sleep(10) + lights.green.off() + lights.amber.on() + sleep(1) + lights.amber.off() + lights.red.on() + sleep(10) + lights.amber.on() + sleep(1) + lights.green.on() + lights.amber.off() + lights.red.off() diff --git a/docs/examples/traffic_lights_2.py b/docs/examples/traffic_lights_2.py new file mode 100644 index 0000000..a3f866c --- /dev/null +++ b/docs/examples/traffic_lights_2.py @@ -0,0 +1,20 @@ +from gpiozero import TrafficLights +from time import sleep +from signal import pause + +lights = TrafficLights(2, 3, 4) + +def traffic_light_sequence(): + while True: + yield (0, 0, 1) # green + sleep(10) + yield (0, 1, 0) # amber + sleep(1) + yield (1, 0, 0) # red + sleep(10) + yield (1, 1, 0) # red+amber + sleep(1) + +lights.source = traffic_light_sequence() + +pause() diff --git a/docs/examples/traffic_lights_3.py b/docs/examples/traffic_lights_3.py new file mode 100644 index 0000000..8bf0aa5 --- /dev/null +++ b/docs/examples/traffic_lights_3.py @@ -0,0 +1,24 @@ +from gpiozero import LED +from time import sleep + +red = LED(2) +amber = LED(3) +green = LED(4) + +green.on() +amber.off() +red.off() + +while True: + sleep(10) + green.off() + amber.on() + sleep(1) + amber.off() + red.on() + sleep(10) + amber.on() + sleep(1) + green.on() + amber.off() + red.off() diff --git a/docs/recipes.rst b/docs/recipes.rst index eea1602..1f0e071 100644 --- a/docs/recipes.rst +++ b/docs/recipes.rst @@ -23,34 +23,19 @@ components: .. image:: images/pin_layout.* + LED === .. image:: images/led.* -Turn an :class:`LED` on and off repeatedly:: +Turn an :class:`LED` on and off repeatedly: - from gpiozero import LED - from time import sleep +.. literalinclude:: examples/led_1.py - red = LED(17) +Alternatively: - while True: - red.on() - sleep(1) - red.off() - sleep(1) - -Alternatively:: - - from gpiozero import LED - from signal import pause - - red = LED(17) - - red.blink() - - pause() +.. literalinclude:: examples/led_2.py .. note:: @@ -62,35 +47,16 @@ Alternatively:: LED with variable brightness ============================ -Any regular LED can have its brightness value set using PWM (pulse-width-modulation). -In GPIO Zero, this can be achieved using :class:`PWMLED` using values between 0 -and 1:: - - from gpiozero import PWMLED - from time import sleep - - led = PWMLED(17) - - while True: - led.value = 0 # off - sleep(1) - led.value = 0.5 # half brightness - sleep(1) - led.value = 1 # full brightness - sleep(1) +Any regular LED can have its brightness value set using PWM +(pulse-width-modulation). In GPIO Zero, this can be achieved using +:class:`PWMLED` using values between 0 and 1: +.. literalinclude:: examples/led_variable_brightness.py Similarly to blinking on and off continuously, a PWMLED can pulse (fade in and -out continuously):: +out continuously): - from gpiozero import PWMLED - from signal import pause - - led = PWMLED(17) - - led.pulse() - - pause() +.. literalinclude:: examples/led_pulse.py Button @@ -98,40 +64,18 @@ Button .. image:: images/button.* -Check if a :class:`Button` is pressed:: +Check if a :class:`Button` is pressed: - from gpiozero import Button +.. literalinclude:: examples/button_1.py - button = Button(2) +Wait for a button to be pressed before continuing: - while True: - if button.is_pressed: - print("Button is pressed") - else: - print("Button is not pressed") +.. literalinclude:: examples/button_2.py -Wait for a button to be pressed before continuing:: +Run a function every time the button is pressed: - from gpiozero import Button - - button = Button(2) - - button.wait_for_press() - print("Button was pressed") - -Run a function every time the button is pressed:: - - from gpiozero import Button - from signal import pause - - def say_hello(): - print("Hello!") - - button = Button(2) - - button.when_pressed = say_hello - - pause() +.. literalinclude:: examples/button_3.py + :emphasize-lines: 9 .. note:: @@ -142,23 +86,9 @@ Run a function every time the button is pressed:: to ``None`` (the return value of this function) which would mean nothing happens when the button is pressed. -Similarly, functions can be attached to button releases:: +Similarly, functions can be attached to button releases: - from gpiozero import Button - from signal import pause - - def say_hello(): - print("Hello!") - - def say_goodbye(): - print("Goodbye!") - - button = Button(2) - - button.when_pressed = say_hello - button.when_released = say_goodbye - - pause() +.. literalinclude:: examples/button_4.py Button controlled LED @@ -166,77 +96,31 @@ Button controlled LED .. image:: images/led_button_bb.* -Turn on an :class:`LED` when a :class:`Button` is pressed:: +Turn on an :class:`LED` when a :class:`Button` is pressed: - from gpiozero import LED, Button - from signal import pause +.. literalinclude:: examples/button_led_1.py - led = LED(17) - button = Button(2) +Alternatively: - button.when_pressed = led.on - button.when_released = led.off - - pause() - -Alternatively:: - - from gpiozero import LED, Button - from signal import pause - - led = LED(17) - button = Button(2) - - led.source = button.values - - pause() +.. literalinclude:: examples/button_led_2.py Button controlled camera ======================== -Using the button press to trigger picamera to take a pitcure using -``button.when_pressed = camera.capture`` would not work because it requires an -``output`` parameter. However, this can be achieved using a custom function -which requires no parameters:: - - from gpiozero import Button - from picamera import PiCamera - from datetime import datetime - from signal import pause +Using the button press to trigger :class:`~picamera.PiCamera` to take a picture +using ``button.when_pressed = camera.capture`` would not work because the +:meth:`~picamera.PiCamera.capture` method requires an ``output`` parameter. +However, this can be achieved using a custom function which requires no +parameters: - button = Button(2) - camera = PiCamera() - - def capture(): - datetime = datetime.now().isoformat() - camera.capture('/home/pi/%s.jpg' % datetime) - - button.when_pressed = capture - - pause() +.. literalinclude:: examples/button_camera_1.py + :emphasize-lines: 9-11 Another example could use one button to start and stop the camera preview, and -another to capture:: +another to capture: - from gpiozero import Button - from picamera import PiCamera - from datetime import datetime - from signal import pause - - left_button = Button(2) - right_button = Button(3) - camera = PiCamera() - - def capture(): - datetime = datetime.now().isoformat() - camera.capture('/home/pi/%s.jpg' % datetime) - - left_button.when_pressed = camera.start_preview - left_button.when_released = camera.stop_preview - right_button.when_pressed = capture - - pause() +.. literalinclude:: examples/button_camera_2.py Shutdown button @@ -244,93 +128,39 @@ Shutdown button The :class:`Button` class also provides the ability to run a function when the button has been held for a given length of time. This example will shut down -the Raspberry Pi when the button is held for 2 seconds:: +the Raspberry Pi when the button is held for 2 seconds: - from gpiozero import Button - from subprocess import check_call - from signal import pause +.. literalinclude:: examples/button_shutdown.py - def shutdown(): - check_call(['sudo', 'poweroff']) - - shutdown_btn = Button(17, hold_time=2) - shutdown_btn.when_held = shutdown - - pause() LEDBoard ======== -A collection of LEDs can be accessed using :class:`LEDBoard`:: +A collection of LEDs can be accessed using :class:`LEDBoard`: - from gpiozero import LEDBoard - from time import sleep - from signal import pause - - leds = LEDBoard(5, 6, 13, 19, 26) - - leds.on() - sleep(1) - leds.off() - sleep(1) - leds.value = (1, 0, 1, 0, 1) - sleep(1) - leds.blink() - - pause() +.. literalinclude:: examples/led_board_1.py Using :class:`LEDBoard` with ``pwm=True`` allows each LED's brightness to be -controlled:: +controlled: - from gpiozero import LEDBoard +.. literalinclude:: examples/led_board_2.py - leds = LEDBoard(5, 6, 13, 19, 26, pwm=True) - - leds.value = (0.2, 0.4, 0.6, 0.8, 1.0) LEDBarGraph =========== A collection of LEDs can be treated like a bar graph using -:class:`LEDBarGraph`:: +:class:`LEDBarGraph`: - from gpiozero import LEDBarGraph - from time import sleep - - graph = LEDBarGraph(5, 6, 13, 19, 26, 20) - - graph.value = 1 # (1, 1, 1, 1, 1, 1) - sleep(1) - graph.value = 1/2 # (1, 1, 1, 0, 0, 0) - sleep(1) - graph.value = -1/2 # (0, 0, 0, 1, 1, 1) - sleep(1) - graph.value = 1/4 # (1, 0, 0, 0, 0, 0) - sleep(1) - graph.value = -1 # (1, 1, 1, 1, 1, 1) - sleep(1) +.. literalinclude:: examples/led_bargraph_2.py Note values are essentially rounded to account for the fact LEDs can only be on or off when ``pwm=False`` (the default). However, using :class:`LEDBarGraph` with ``pwm=True`` allows more precise -values using LED brightness:: +values using LED brightness: - from gpiozero import LEDBarGraph - from time import sleep - - graph = LEDBarGraph(5, 6, 13, 19, 26, pwm=True) - - graph.value = 1/10 # (0.5, 0, 0, 0, 0) - sleep(1) - graph.value = 3/10 # (1, 0.5, 0, 0, 0) - sleep(1) - graph.value = -3/10 # (0, 0, 0, 0.5, 1) - sleep(1) - graph.value = 9/10 # (1, 1, 1, 1, 0.5) - sleep(1) - graph.value = 95/100 # (1, 1, 1, 1, 0.75) - sleep(1) +.. literalinclude:: examples/led_bargraph_2.py Traffic Lights @@ -340,126 +170,37 @@ Traffic Lights A full traffic lights system. -Using a :class:`TrafficLights` kit like Pi-Stop:: +Using a :class:`TrafficLights` kit like Pi-Stop: - from gpiozero import TrafficLights - from time import sleep +.. literalinclude:: examples/traffic_lights_1.py - lights = TrafficLights(2, 3, 4) +Alternatively: - lights.green.on() +.. literalinclude:: examples/traffic_lights_2.py - while True: - sleep(10) - lights.green.off() - lights.amber.on() - sleep(1) - lights.amber.off() - lights.red.on() - sleep(10) - lights.amber.on() - sleep(1) - lights.green.on() - lights.amber.off() - lights.red.off() +Using :class:`LED` components: -Alternatively:: - - from gpiozero import TrafficLights - from time import sleep - from signal import pause - - lights = TrafficLights(2, 3, 4) - - def traffic_light_sequence(): - while True: - yield (0, 0, 1) # green - sleep(10) - yield (0, 1, 0) # amber - sleep(1) - yield (1, 0, 0) # red - sleep(10) - yield (1, 1, 0) # red+amber - sleep(1) - - lights.source = traffic_light_sequence() - - pause() - -Using :class:`LED` components:: - - from gpiozero import LED - from time import sleep - - red = LED(2) - amber = LED(3) - green = LED(4) - - green.on() - amber.off() - red.off() - - while True: - sleep(10) - green.off() - amber.on() - sleep(1) - amber.off() - red.on() - sleep(10) - amber.on() - sleep(1) - green.on() - amber.off() - red.off() +.. literalinclude:: examples/traffic_lights_3.py Travis build LED indicator ========================== Use LEDs to indicate the status of a Travis build. A green light means the -tests are passing, a red light means the build is broken:: +tests are passing, a red light means the build is broken: - from travispy import TravisPy - from gpiozero import LED - from gpiozero.tools import negated - from time import sleep - from signal import pause +.. literalinclude:: examples/led_travis.py - def build_passed(repo='RPi-Distro/python-gpiozero', delay=3600): - t = TravisPy() - r = t.repo(repo) - while True: - yield r.last_build_state == 'passed' - sleep(delay) # Sleep an hour before hitting travis again +Note this recipe requires `travispy`_. Install with ``sudo pip3 install +travispy``. - red = LED(12) - green = LED(16) - - red.source = negated(green.values) - green.source = build_passed() - pause() - - -Note this recipe requires travispy. Install with ``sudo pip3 install travispy``. Push button stop motion ======================= -Capture a picture with the camera module every time a button is pressed:: +Capture a picture with the camera module every time a button is pressed: - from gpiozero import Button - from picamera import PiCamera - - button = Button(2) - camera = PiCamera() - - camera.start_preview() - frame = 1 - while True: - button.wait_for_press() - camera.capture('/home/pi/frame%03d.jpg' % frame) - frame += 1 +.. literalinclude:: examples/button_stop_motion.py See `Push Button Stop Motion`_ for a full resource. @@ -471,30 +212,7 @@ Reaction Game When you see the light come on, the first person to press their button wins! -:: - - from gpiozero import Button, LED - from time import sleep - import random - - led = LED(17) - - player_1 = Button(2) - player_2 = Button(3) - - time = random.uniform(5, 10) - sleep(time) - led.on() - - while True: - if player_1.is_pressed: - print("Player 1 wins!") - break - if player_2.is_pressed: - print("Player 2 wins!") - break - - led.off() +.. literalinclude:: examples/reaction_game.py See `Quick Reaction Game`_ for a full resource. @@ -504,26 +222,7 @@ GPIO Music Box Each button plays a different sound! -:: - - from gpiozero import Button - import pygame.mixer - from pygame.mixer import Sound - from signal import pause - - pygame.mixer.init() - - sound_pins = { - 2: Sound("samples/drum_tom_mid_hard.wav"), - 3: Sound("samples/drum_cymbal_open.wav"), - } - - buttons = [Button(pin) for pin in sound_pins] - for button in buttons: - sound = sound_pins[button.pin.number] - button.when_pressed = sound.play - - pause() +.. literalinclude:: examples/music_box.py See `GPIO Music Box`_ for a full resource. @@ -533,55 +232,17 @@ All on when pressed While the button is pressed down, the buzzer and all the lights come on. -:class:`FishDish`:: +:class:`FishDish`: - from gpiozero import FishDish - from signal import pause +.. literalinclude:: examples/all_on_1.py - fish = FishDish() +Ryanteck :class:`TrafficHat`: - fish.button.when_pressed = fish.on - fish.button.when_released = fish.off +.. literalinclude:: examples/all_on_2.py - pause() +Using :class:`LED`, :class:`Buzzer`, and :class:`Button` components: -Ryanteck :class:`TrafficHat`:: - - from gpiozero import TrafficHat - from signal import pause - - th = TrafficHat() - - th.button.when_pressed = th.on - th.button.when_released = th.off - - pause() - -Using :class:`LED`, :class:`Buzzer`, and :class:`Button` components:: - - from gpiozero import LED, Buzzer, Button - from signal import pause - - button = Button(2) - buzzer = Buzzer(3) - red = LED(4) - amber = LED(5) - green = LED(6) - - things = [red, amber, green, buzzer] - - def things_on(): - for thing in things: - thing.on() - - def things_off(): - for thing in things: - thing.off() - - button.when_pressed = things_on - button.when_released = things_off - - pause() +.. literalinclude:: examples/all_on_3.py Full color LED @@ -589,36 +250,9 @@ Full color LED .. image:: images/rgb_led_bb.* -Making colours with an :class:`RGBLED`:: +Making colours with an :class:`RGBLED`: - from gpiozero import RGBLED - from time import sleep - - led = RGBLED(red=9, green=10, blue=11) - - led.red = 1 # full red - sleep(1) - led.red = 0.5 # half red - sleep(1) - - led.color = (0, 1, 0) # full green - sleep(1) - led.color = (1, 0, 1) # magenta - sleep(1) - led.color = (1, 1, 0) # yellow - sleep(1) - led.color = (0, 1, 1) # cyan - sleep(1) - led.color = (1, 1, 1) # white - sleep(1) - - led.color = (0, 0, 0) # off - sleep(1) - - # slowly increase intensity of blue - for n in range(100): - led.blue = n/100 - sleep(0.1) +.. literalinclude:: examples/rgbled.py Motion sensor @@ -626,18 +260,9 @@ Motion sensor .. image:: images/motion_sensor_bb.* -Light an :class:`LED` when a :class:`MotionSensor` detects motion:: +Light an :class:`LED` when a :class:`MotionSensor` detects motion: - from gpiozero import MotionSensor, LED - from signal import pause - - pir = MotionSensor(4) - led = LED(16) - - pir.when_motion = led.on - pir.when_no_motion = led.off - - pause() +.. literalinclude:: examples/motion_sensor.py Light sensor @@ -645,43 +270,18 @@ Light sensor .. image:: images/light_sensor_bb.* -Have a :class:`LightSensor` detect light and dark:: +Have a :class:`LightSensor` detect light and dark: - from gpiozero import LightSensor +.. literalinclude:: examples/light_sensor_1.py - sensor = LightSensor(18) +Run a function when the light changes: - while True: - sensor.wait_for_light() - print("It's light! :)") - sensor.wait_for_dark() - print("It's dark :(") - -Run a function when the light changes:: - - from gpiozero import LightSensor, LED - from signal import pause - - sensor = LightSensor(18) - led = LED(16) - - sensor.when_dark = led.on - sensor.when_light = led.off - - pause() +.. literalinclude:: examples/light_sensor_2.py Or make a :class:`PWMLED` change brightness according to the detected light -level:: +level: - from gpiozero import LightSensor, PWMLED - from signal import pause - - sensor = LightSensor(18) - led = PWMLED(16) - - led.source = sensor.values - - pause() +.. literalinclude:: examples/light_sensor_3.py Distance sensor @@ -689,29 +289,13 @@ Distance sensor .. IMAGE TBD -Have a :class:`DistanceSensor` detect the distance to the nearest object:: +Have a :class:`DistanceSensor` detect the distance to the nearest object: - from gpiozero import DistanceSensor - from time import sleep +.. literalinclude:: examples/distance_sensor_1.py - sensor = DistanceSensor(23, 24) +Run a function when something gets near the sensor: - while True: - print('Distance to nearest object is', sensor.distance, 'm') - sleep(1) - -Run a function when something gets near the sensor:: - - from gpiozero import DistanceSensor, LED - from signal import pause - - sensor = DistanceSensor(23, 24, max_distance=1, threshold_distance=0.2) - led = LED(16) - - sensor.when_in_range = led.on - sensor.when_out_of_range = led.off - - pause() +.. literalinclude:: examples/distance_sensor_2.py Motors @@ -719,18 +303,9 @@ Motors .. image:: images/motor_bb.* -Spin a :class:`Motor` around forwards and backwards:: +Spin a :class:`Motor` around forwards and backwards: - from gpiozero import Motor - from time import sleep - - motor = Motor(forward=4, backward=14) - - while True: - motor.forward() - sleep(5) - motor.backward() - sleep(5) +.. literalinclude:: examples/motor.py Robot @@ -738,163 +313,54 @@ Robot .. IMAGE TBD -Make a :class:`Robot` drive around in (roughly) a square:: +Make a :class:`Robot` drive around in (roughly) a square: - from gpiozero import Robot - from time import sleep - - robot = Robot(left=(4, 14), right=(17, 18)) - - for i in range(4): - robot.forward() - sleep(10) - robot.right() - sleep(1) +.. literalinclude:: examples/robot_1.py Make a robot with a distance sensor that runs away when things get within -20cm of it:: +20cm of it: - from gpiozero import Robot, DistanceSensor - from signal import pause - - sensor = DistanceSensor(23, 24, max_distance=1, threshold_distance=0.2) - robot = Robot(left=(4, 14), right=(17, 18)) - - sensor.when_in_range = robot.backward - sensor.when_out_of_range = robot.stop - pause() +.. literalinclude:: examples/robot_2.py Button controlled robot ======================= -Use four GPIO buttons as forward/back/left/right controls for a robot:: +Use four GPIO buttons as forward/back/left/right controls for a robot: - from gpiozero import Robot, Button - from signal import pause - - robot = Robot(left=(4, 14), right=(17, 18)) - - 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() +.. literalinclude:: examples/robot_buttons.py Keyboard controlled robot ========================= -Use up/down/left/right keys to control a robot:: +Use up/down/left/right keys to control a robot: - import curses - from gpiozero import Robot - - robot = Robot(left=(4, 14), right=(17, 18)) - - actions = { - curses.KEY_UP: robot.forward, - curses.KEY_DOWN: robot.backward, - curses.KEY_LEFT: robot.left, - curses.KEY_RIGHT: robot.right, - } - - def main(window): - next_key = None - while True: - curses.halfdelay(1) - if next_key is None: - key = window.getch() - else: - key = next_key - next_key = None - if key != -1: - # KEY DOWN - curses.halfdelay(3) - action = actions.get(key) - if action is not None: - action() - next_key = key - while next_key == key: - next_key = window.getch() - # KEY UP - robot.stop() - - curses.wrapper(main) +.. literalinclude:: examples/robot_keyboard_1.py .. note:: - This recipe uses the ``curses`` module. This module requires that Python is - running in a terminal in order to work correctly, hence this recipe will - *not* work in environments like IDLE. + This recipe uses the standard :mod:`curses` module. This module requires + that Python is running in a terminal in order to work correctly, hence this + recipe will *not* work in environments like IDLE. If you prefer a version that works under IDLE, the following recipe should suffice, but will require that you install the evdev library with ``sudo pip3 -install evdev`` first:: +install evdev`` first: - from gpiozero import Robot - from evdev import InputDevice, list_devices, ecodes - - robot = Robot(left=(4, 14), right=(17, 18)) - - devices = [InputDevice(device) for device in list_devices()] - keyboard = devices[0] # this may vary - - keypress_actions = { - ecodes.KEY_UP: robot.forward, - ecodes.KEY_DOWN: robot.backward, - ecodes.KEY_LEFT: robot.left, - ecodes.KEY_RIGHT: robot.right, - } - - for event in keyboard.read_loop(): - if event.type == ecodes.EV_KEY: - if event.value == 1: # key down - keypress_actions[event.code]() - if event.value == 0: # key up - robot.stop() +.. literalinclude:: examples/robot_keyboard_2.py Motion sensor robot =================== -Make a robot drive forward when it detects motion:: +Make a robot drive forward when it detects motion: - from gpiozero import Robot, MotionSensor - from signal import pause +.. literalinclude:: examples/robot_motion_1.py - robot = Robot(left=(4, 14), right=(17, 18)) - pir = MotionSensor(5) +Alternatively: - pir.when_motion = robot.forward - pir.when_no_motion = robot.stop - - pause() - -Alternatively:: - - from gpiozero import Robot, MotionSensor - from signal import pause - - robot = Robot(left=(4, 14), right=(17, 18)) - pir = MotionSensor(5) - - robot.source = zip(pir.values, pir.values) - - pause() +.. literalinclude:: examples/robot_motion_2.py Potentiometer @@ -903,25 +369,14 @@ Potentiometer .. image:: images/potentiometer_bb.* Continually print the value of a potentiometer (values between 0 and 1) -connected to a :class:`MCP3008` analog to digital converter:: +connected to a :class:`MCP3008` analog to digital converter: - from gpiozero import MCP3008 - - pot = MCP3008(channel=0) - - while True: - print(pot.value) +.. literalinclude:: examples/pot_1.py Present the value of a potentiometer on an LED bar graph using PWM to represent -states that won't "fill" an LED:: +states that won't "fill" an LED: - from gpiozero import LEDBarGraph, MCP3008 - from signal import pause - - graph = LEDBarGraph(5, 6, 13, 19, 26, pwm=True) - pot = MCP3008(channel=0) - graph.source = pot.values - pause() +.. literalinclude:: examples/pot_2.py Measure temperature with an ADC @@ -930,54 +385,24 @@ Measure temperature with an ADC .. IMAGE TBD Wire a TMP36 temperature sensor to the first channel of an :class:`MCP3008` -analog to digital converter:: +analog to digital converter: - from gpiozero import MCP3008 - from time import sleep - - def convert_temp(gen): - for value in gen: - yield (value * 3.3 - 0.5) * 100 - - adc = MCP3008(channel=0) - - for temp in convert_temp(adc.values): - print('The temperature is', temp, 'C') - sleep(1) +.. literalinclude:: examples/thermometer.py Full color LED controlled by 3 potentiometers ============================================= Wire up three potentiometers (for red, green and blue) and use each of their -values to make up the colour of the LED:: +values to make up the colour of the LED: - from gpiozero import RGBLED, MCP3008 - - led = RGBLED(red=2, green=3, blue=4) - red_pot = MCP3008(channel=0) - green_pot = MCP3008(channel=1) - blue_pot = MCP3008(channel=2) - - while True: - led.red = red_pot.value - led.green = green_pot.value - led.blue = blue_pot.value +.. literalinclude:: examples/rgbled_pot_1.py Alternatively, the following example is identical, but uses the -:attr:`~SourceMixin.source` property rather than a :keyword:`while` loop:: +:attr:`~SourceMixin.source` property rather than a :keyword:`while` loop: - from gpiozero import RGBLED, MCP3008 - from signal import pause - - led = RGBLED(2, 3, 4) - red_pot = MCP3008(0) - green_pot = MCP3008(1) - blue_pot = MCP3008(2) - - led.source = zip(red_pot.values, green_pot.values, blue_pot.values) - - pause() +.. literalinclude:: examples/rgbled_pot_2.py + :emphasize-lines: 8 Please note the example above requires Python 3. In Python 2, :func:`zip` doesn't support lazy evaluation so the script will simply hang. @@ -997,17 +422,9 @@ be done from the terminal with the following commands:: $ echo none | sudo tee /sys/class/leds/led0/trigger $ echo gpio | sudo tee /sys/class/leds/led1/trigger -Now you can control the LEDs with gpiozero like so:: +Now you can control the LEDs with gpiozero like so: - from gpiozero import LED - from signal import pause - - power = LED(35) # /sys/class/leds/led1 - activity = LED(47) # /sys/class/leds/led0 - - activity.blink() - power.blink() - pause() +.. literalinclude:: examples/led_builtin.py To revert the LEDs to their usual purpose you can either reboot your Pi or run the following commands:: @@ -1030,6 +447,7 @@ run the following commands:: accessible from gpiozero (yet). +.. _travispy: https://travispy.readthedocs.io/ .. _Push Button Stop Motion: https://www.raspberrypi.org/learning/quick-reaction-game/ .. _Quick Reaction Game: https://www.raspberrypi.org/learning/quick-reaction-game/ .. _GPIO Music Box: https://www.raspberrypi.org/learning/gpio-music-box/