Skip to content

Commit

Permalink
Merge pull request #158 from waveform80/fixup-rest
Browse files Browse the repository at this point in the history
Re-apply #120 and #117 to rest-docs
  • Loading branch information
waveform80 committed Jan 31, 2016
2 parents 96a9112 + 44e943b commit d77ee6e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Keep your script running

The following script looks like it should turn an LED on::

from gpiozero import led
from gpiozero import LED

led = LED(17)
led.on()
Expand All @@ -23,7 +23,7 @@ briefly, then the script would end and it would turn off.
The following file includes an intentional :func:`~signal.pause` to keep the
script alive::

from gpiozero import led
from gpiozero import LED
from signal import pause

led = LED(17)
Expand Down
48 changes: 42 additions & 6 deletions docs/recipes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Alternatively::
red = LED(17)

red.blink()

pause()

.. note::
Expand Down Expand Up @@ -89,6 +90,7 @@ Run a function every time the button is pressed::
button = Button(2)

button.when_pressed = say_hello

pause()

Button controlled LED
Expand Down Expand Up @@ -224,14 +226,10 @@ 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()

def play(pin):
sound = sound_pins[pin]
print("playing note from pin %s" % pin)
sound.play()

sound_pins = {
2: Sound("samples/drum_tom_mid_hard.wav"),
3: Sound("samples/drum_cymbal_open.wav"),
Expand All @@ -242,6 +240,8 @@ Each button plays a different sound!
sound = sound_pins[button.pin]
button.when_pressed = sound.play

pause()

See `GPIO Music Box`_ for a full resource.

All on when pressed
Expand All @@ -252,24 +252,31 @@ While the button is pressed down, the buzzer and all the lights come on.
:class:`FishDish`::

from gpiozero import FishDish
from signal import pause

fish = FishDish()

fish.button.when_pressed = fish.on
fish.button.when_released = fish.off

pause()

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)
Expand All @@ -290,6 +297,8 @@ Using :class:`LED`, :class:`Buzzer`, and :class:`Button` components::
button.when_pressed = things_on
button.when_released = things_off

pause()

RGB LED
=======

Expand Down Expand Up @@ -325,13 +334,16 @@ Motion sensor
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()

Light sensor
============

Expand All @@ -352,13 +364,16 @@ Have a :class:`LightSensor` detect light and 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()

Motors
======

Expand Down Expand Up @@ -457,13 +472,16 @@ Motion sensor robot
Make a robot drive forward when it detects motion::

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()

Potentiometer
=============

Expand All @@ -476,7 +494,7 @@ connected to a :class:`MCP3008` analog to digital converter::

while True:
with MCP3008(channel=0) as pot:
print(pot.read())
print(pot.value)

Full color LED controlled by 3 potentiometers
=============================================
Expand All @@ -496,6 +514,24 @@ values to make up the colour of the LED::
led.green = green_pot.value
led.blue = blue_pot.value

Alternatively, the following example is identical, but uses the
: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()

Please note the example above requires Python 3. In Python 2, :func:`zip`
doesn't support lazy evaluation so the script will simply hang.


.. _Push Button Stop Motion: https://www.raspberrypi.org/learning/quick-reaction-game/
.. _Quick Reaction Game: https://www.raspberrypi.org/learning/quick-reaction-game/
Expand Down

0 comments on commit d77ee6e

Please sign in to comment.