From 70063662436ce6ba742a73bccbe253721e4734de Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Fri, 2 Dec 2016 00:02:24 +0000 Subject: [PATCH] Update sourcevalues docs as suggested in lurch's review --- docs/examples/led_button_loop.py | 2 ++ docs/examples/negated.py | 1 + docs/examples/random_values.py | 1 + docs/examples/source_value_processing.py | 13 +++++++++++++ docs/source_values.rst | 18 ++++++------------ 5 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 docs/examples/source_value_processing.py diff --git a/docs/examples/led_button_loop.py b/docs/examples/led_button_loop.py index 8e28368..0b01872 100644 --- a/docs/examples/led_button_loop.py +++ b/docs/examples/led_button_loop.py @@ -1,7 +1,9 @@ from gpiozero import LED, Button +from time import sleep led = LED(17) button = Button(2) while True: led.value = button.value + sleep(0.01) diff --git a/docs/examples/negated.py b/docs/examples/negated.py index e7cc2cf..2e5285e 100644 --- a/docs/examples/negated.py +++ b/docs/examples/negated.py @@ -4,6 +4,7 @@ from signal import pause led = LED(4) btn = Button(17) + led.source = negated(btn.values) pause() diff --git a/docs/examples/random_values.py b/docs/examples/random_values.py index 99a2572..524e456 100644 --- a/docs/examples/random_values.py +++ b/docs/examples/random_values.py @@ -4,5 +4,6 @@ from signal import pause led = PWMLED(4) led.source = random_values() +led.source_delay = 0.1 pause() diff --git a/docs/examples/source_value_processing.py b/docs/examples/source_value_processing.py new file mode 100644 index 0000000..bd2b845 --- /dev/null +++ b/docs/examples/source_value_processing.py @@ -0,0 +1,13 @@ +from gpiozero import Button, LED +from signal import pause + +def opposite(values): + for value in values: + yield not value + +led = LED(4) +btn = Button(17) + +led.source = opposite(btn.values) + +pause() diff --git a/docs/source_values.rst b/docs/source_values.rst index 45d49e5..4b108d9 100644 --- a/docs/source_values.rst +++ b/docs/source_values.rst @@ -35,14 +35,8 @@ provided, setting the device's value to each element at a rate specified in the .. image:: images/source_values.* The most common use case for this is to set the source of an output device to -the values of an input device, like the example above. It is also possible to -set the source of an output device to the values of another output device (so -they match): - -.. literalinclude:: examples/led_button.py - -A more interesting example would be a potentiometer controlling the brightness -of an LED: +the values of an input device, like the example above. A more interesting +example would be a potentiometer controlling the brightness of an LED: .. literalinclude:: examples/pwmled_pot.py @@ -58,7 +52,7 @@ The device's values can also be processed before they are passed to the For example: -.. literalinclude:: examples/custom_generator.py +.. literalinclude:: examples/source_value_processing.py Alternatively, a custom generator can be used to provide values from an artificial source: @@ -81,11 +75,11 @@ Composite devices ----------------- Most devices have a ``value`` range between 0 and 1. Some have a range between --1 and 1 (e.g. ``Robot``). The ``value`` of a composite device is a namedtuple -of such values. For example:: +-1 and 1 (e.g. ``Motor``). The ``value`` of a composite device is a namedtuple +of such values. For example, the ``Robot`` class:: >>> from gpiozero import Robot - >>> robot = Robot(left=(14, 15), right=(17, 18) + >>> robot = Robot(left=(14, 15), right=(17, 18)) >>> robot.value RobotValue(left_motor=0.0, right_motor=0.0) >>> tuple(robot.value)