Add pin_factory param to all devices

And some docs ...
This commit is contained in:
Dave Jones
2017-07-14 10:44:24 +01:00
parent 1ca017fc6d
commit 8958874a77
16 changed files with 619 additions and 196 deletions

View File

@@ -16,35 +16,35 @@ everyday components. Components must be wired up correctly before use in code.
Button
======
.. autoclass:: Button(pin, pull_up=True, bounce_time=None, hold_time=1, hold_repeat=False)
.. autoclass:: Button(pin, pull_up=True, bounce_time=None, hold_time=1, hold_repeat=False, pin_factory=None)
:members: wait_for_press, wait_for_release, pin, is_pressed, is_held, hold_time, held_time, hold_repeat, pull_up, when_pressed, when_released, when_held
Line Sensor (TRCT5000)
======================
.. autoclass:: LineSensor(pin, queue_len=5, sample_rate=100, threshold=0.5, partial=False)
.. autoclass:: LineSensor(pin, queue_len=5, sample_rate=100, threshold=0.5, partial=False, pin_factory=None)
:members: wait_for_line, wait_for_no_line, pin, line_detected, when_line, when_no_line
Motion Sensor (D-SUN PIR)
=========================
.. autoclass:: MotionSensor(pin, queue_len=1, sample_rate=10, threshold=0.5, partial=False)
.. autoclass:: MotionSensor(pin, queue_len=1, sample_rate=10, threshold=0.5, partial=False, pin_factory=None)
:members: wait_for_motion, wait_for_no_motion, pin, motion_detected, when_motion, when_no_motion
Light Sensor (LDR)
==================
.. autoclass:: LightSensor(pin, queue_len=5, charge_time_limit=0.01, threshold=0.1, partial=False)
.. autoclass:: LightSensor(pin, queue_len=5, charge_time_limit=0.01, threshold=0.1, partial=False, pin_factory=None)
:members: wait_for_light, wait_for_dark, pin, light_detected, when_light, when_dark
Distance Sensor (HC-SR04)
=========================
.. autoclass:: DistanceSensor(echo, trigger, queue_len=30, max_distance=1, threshold_distance=0.3, partial=False)
.. autoclass:: DistanceSensor(echo, trigger, queue_len=30, max_distance=1, threshold_distance=0.3, partial=False, pin_factory=None)
:members: wait_for_in_range, wait_for_out_of_range, trigger, echo, when_in_range, when_out_of_range, max_distance, distance, threshold_distance
Base Classes
@@ -63,7 +63,7 @@ to construct classes for their own devices.
DigitalInputDevice
==================
.. autoclass:: DigitalInputDevice(pin, pull_up=False, bounce_time=None)
.. autoclass:: DigitalInputDevice(pin, pull_up=False, bounce_time=None, pin_factory=None)
:members:
SmoothedInputDevice
@@ -75,12 +75,12 @@ SmoothedInputDevice
InputDevice
===========
.. autoclass:: InputDevice(pin, pull_up=False)
.. autoclass:: InputDevice(pin, pull_up=False, pin_factory=None)
:members:
GPIODevice
==========
.. autoclass:: GPIODevice(pin)
.. autoclass:: GPIODevice(pin, pin_factory=None)
:members:

View File

@@ -11,10 +11,23 @@ are concerned with. However, some users may wish to take advantage of the
capabilities of alternative GPIO implementations or (in future) use GPIO
extender chips. This is the purpose of the pins portion of the library.
When you construct a device, you pass in a pin specification. However, what the
library actually expects is a :class:`Pin` implementation. If it finds anything
else, it uses the existing ``Device.pin_factory`` to construct a :class:`Pin`
implementation based on the specification.
When you construct a device, you pass in a pin specification. This is passed to
a pin :class:`Factory` which turns it into a :class:`Pin` implementation. The
default factory can be queried (and changed) with ``Device.pin_factory``, i.e.
the ``pin_factory`` attribute of the :class:`Device` class. However, all
classes accept a ``pin_factory`` keyword argument to their constructors
permitting the factory to be overridden on a per-device basis (the reason for
allowing per-device factories is made apparent later in the :doc:`remote_gpio`
chapter).
This is illustrated in the following flow-chart:
.. image:: images/device_pin_flowchart.*
The default factory is constructed when GPIO Zero is first imported; if no
default factory can be constructed (e.g. because no GPIO implementations are
installed, or all of them fail to load for whatever reason), an
:exc:`ImportError` will be raised.
Changing the pin factory
========================
@@ -24,7 +37,7 @@ The default pin factory can be replaced by specifying a value for the
.. code-block:: console
pi@raspberrypi $ GPIOZERO_PIN_FACTORY=native python
$ GPIOZERO_PIN_FACTORY=native python
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
@@ -37,8 +50,8 @@ export this value:
.. code-block:: console
pi@raspberrypi $ export GPIOZERO_PIN_FACTORY=native
pi@raspberrypi $ python
$ export GPIOZERO_PIN_FACTORY=native
$ python
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
@@ -46,7 +59,7 @@ export this value:
>>> gpiozero.Device.pin_factory
<gpiozero.pins.native.NativeFactory object at 0x762c26b0>
>>> quit()
pi@raspberrypi $ python
$ python
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
@@ -73,17 +86,30 @@ they are tried by default.
| native | :class:`gpiozero.pins.native.NativeFactory` | :class:`gpiozero.pins.native.NativePin` |
+---------+-----------------------------------------------+-------------------------------------------+
If you need to change the default pin factory from within a script, set
If you need to change the default pin factory from within a script, either set
``Device.pin_factory`` to the new factory instance to use::
from gpiozero.pins.native import NativeFactory
from gpiozero import *
from gpiozero import Device, LED
Device.pin_factory = NativeFactory()
# These will now implicitly use NativePin instead of
# RPiGPIOPin
led1 = LED(16)
led2 = LED(17)
Or use the ``pin_factory`` keyword parameter mentioned above::
from gpiozero.pins.native import NativeFactory
from gpiozero import LED
# This will now use NativePin instead of RPiGPIOPin
led = LED(16)
my_factory = NativeFactory()
# This will use NativePin instead of RPiGPIOPin for led1
# but led2 will continue to use RPiGPIOPin
led1 = LED(16, pin_factory=my_factory)
led2 = LED(17)
Certain factories may take default information from additional sources.
For example, to default to creating pins with
@@ -100,11 +126,13 @@ Like the ``GPIOZERO_PIN_FACTORY`` value, these can be exported from your
.. warning::
The astute and mischievous reader may note that it is possible to mix pin
implementations, e.g. using ``RPiGPIOPin`` for one pin, and ``NativePin``
for another. This is unsupported, and if it results in your script
crashing, your components failing, or your Raspberry Pi turning into an
actual raspberry pie, you have only yourself to blame.
The astute and mischievous reader may note that it is possible to mix
strictly local pin implementations, e.g. using ``RPiGPIOPin`` for one pin,
and ``NativePin`` for another. This is unsupported, and if it results in
your script crashing, your components failing, or your Raspberry Pi turning
into an actual raspberry pie, you have only yourself to blame.
Sensible uses of multiple pin factories are given in :doc:`remote_gpio`.
RPi.GPIO

View File

@@ -0,0 +1,19 @@
/* vim: set et sw=4 sts=4: */
digraph device_pins {
graph [rankdir=TB];
node [shape=rect, shape=filled, fontname=Sans, fontsize=10];
edge [fontname=Sans, fontsize=10];
constructor [label="LED(pin_spec, ...,\npin_factory=None)"];
pin_factory_kwarg [shape=diamond,label="pin_factory == None?"];
default_factory [label="self.pin_factory = Device.pin_factory"];
override_factory [label="self.pin_factory = pin_factory"];
factory_pin [label="self.pin = self.pin_factory.pin(pin_spec)"];
constructor->pin_factory_kwarg;
pin_factory_kwarg->default_factory [label="yes"];
pin_factory_kwarg->override_factory [label="no"];
default_factory->factory_pin;
override_factory->factory_pin;
}

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generated by graphviz version 2.38.0 (20140413.2041)
-->
<!-- Title: device_pins Pages: 1 -->
<svg width="372pt" height="273pt"
viewBox="0.00 0.00 371.50 273.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 269)">
<title>device_pins</title>
<polygon fill="white" stroke="none" points="-4,4 -4,-269 367.5,-269 367.5,4 -4,4"/>
<!-- constructor -->
<g id="node1" class="node"><title>constructor</title>
<polygon fill="none" stroke="black" points="243.5,-265 136.5,-265 136.5,-229 243.5,-229 243.5,-265"/>
<text text-anchor="middle" x="190" y="-250" font-family="Sans" font-size="10.00">LED(pin_spec, ...,</text>
<text text-anchor="middle" x="190" y="-239" font-family="Sans" font-size="10.00">pin_factory=None)</text>
</g>
<!-- pin_factory_kwarg -->
<g id="node2" class="node"><title>pin_factory_kwarg</title>
<polygon fill="none" stroke="black" points="190,-192 88.3711,-174 190,-156 291.629,-174 190,-192"/>
<text text-anchor="middle" x="190" y="-171.5" font-family="Sans" font-size="10.00">pin_factory == None?</text>
</g>
<!-- constructor&#45;&gt;pin_factory_kwarg -->
<g id="edge1" class="edge"><title>constructor&#45;&gt;pin_factory_kwarg</title>
<path fill="none" stroke="black" d="M190,-228.813C190,-220.789 190,-211.047 190,-202.069"/>
<polygon fill="black" stroke="black" points="193.5,-202.029 190,-192.029 186.5,-202.029 193.5,-202.029"/>
</g>
<!-- default_factory -->
<g id="node3" class="node"><title>default_factory</title>
<polygon fill="none" stroke="black" points="190,-109 0,-109 0,-73 190,-73 190,-109"/>
<text text-anchor="middle" x="95" y="-88.5" font-family="Sans" font-size="10.00">self.pin_factory = Device.pin_factory</text>
</g>
<!-- pin_factory_kwarg&#45;&gt;default_factory -->
<g id="edge2" class="edge"><title>pin_factory_kwarg&#45;&gt;default_factory</title>
<path fill="none" stroke="black" d="M173.452,-158.891C159.473,-146.971 139.158,-129.65 122.703,-115.621"/>
<polygon fill="black" stroke="black" points="124.861,-112.861 114.98,-109.036 120.319,-118.187 124.861,-112.861"/>
<text text-anchor="middle" x="157.5" y="-130" font-family="Sans" font-size="10.00">yes</text>
</g>
<!-- override_factory -->
<g id="node4" class="node"><title>override_factory</title>
<polygon fill="none" stroke="black" points="363.5,-109 208.5,-109 208.5,-73 363.5,-73 363.5,-109"/>
<text text-anchor="middle" x="286" y="-88.5" font-family="Sans" font-size="10.00">self.pin_factory = pin_factory</text>
</g>
<!-- pin_factory_kwarg&#45;&gt;override_factory -->
<g id="edge3" class="edge"><title>pin_factory_kwarg&#45;&gt;override_factory</title>
<path fill="none" stroke="black" d="M206.722,-158.891C220.849,-146.971 241.377,-129.65 258.005,-115.621"/>
<polygon fill="black" stroke="black" points="260.424,-118.16 265.809,-109.036 255.909,-112.81 260.424,-118.16"/>
<text text-anchor="middle" x="250.5" y="-130" font-family="Sans" font-size="10.00">no</text>
</g>
<!-- factory_pin -->
<g id="node5" class="node"><title>factory_pin</title>
<polygon fill="none" stroke="black" points="311,-36 111,-36 111,-0 311,-0 311,-36"/>
<text text-anchor="middle" x="211" y="-15.5" font-family="Sans" font-size="10.00">self.pin = self.pin_factory.pin(pin_spec)</text>
</g>
<!-- default_factory&#45;&gt;factory_pin -->
<g id="edge4" class="edge"><title>default_factory&#45;&gt;factory_pin</title>
<path fill="none" stroke="black" d="M122.785,-72.9937C138.167,-63.5785 157.484,-51.7554 174.117,-41.5748"/>
<polygon fill="black" stroke="black" points="176.338,-44.3193 183.04,-36.1136 172.683,-38.3489 176.338,-44.3193"/>
</g>
<!-- override_factory&#45;&gt;factory_pin -->
<g id="edge5" class="edge"><title>override_factory&#45;&gt;factory_pin</title>
<path fill="none" stroke="black" d="M267.845,-72.8129C258.448,-63.9174 246.82,-52.9094 236.533,-43.1717"/>
<polygon fill="black" stroke="black" points="238.656,-40.3619 228.988,-36.0288 233.844,-45.4454 238.656,-40.3619"/>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 36 KiB

View File

@@ -9,42 +9,58 @@ operating systems, including for PCs using the :doc:`remote_gpio` feature.
Raspberry Pi
============
First, update your repositories list::
First, update your repositories list:
sudo apt update
.. code-block:: console
Then install the package for Python 3::
pi@raspberrypi:~$ sudo apt update
sudo apt install python3-gpiozero
Then install the package for Python 3:
or Python 2::
.. code-block:: console
sudo apt install python-gpiozero
pi@raspberrypi:~$ sudo apt install python3-gpiozero
or Python 2:
.. code-block:: console
pi@raspberrypi:~$ sudo apt install python-gpiozero
Linux
=====
First, update your distribution's repositories list. For example::
First, update your distribution's repositories list. For example:
sudo apt update
.. code-block:: console
Then install pip for Python 3::
$ sudo apt update
sudo apt install python3-pip
Then install pip for Python 3:
or Python 3::
.. code-block:: console
sudo apt install python-pip
$ sudo apt install python3-pip
or Python 3:
.. code-block:: console
$ sudo apt install python-pip
(Alternatively, install pip with `get-pip`_.)
Next, install gpiozero for Python 3::
Next, install gpiozero for Python 3:
sudo pip3 install gpiozero
.. code-block:: console
or Python 2::
$ sudo pip3 install gpiozero
sudo pip install gpiozero
or Python 2:
.. code-block:: console
$ sudo pip install gpiozero
.. note::
@@ -55,24 +71,32 @@ or Python 2::
Mac OS
======
First, install pip::
First, install pip:
???
.. code-block:: console
Next, install gpiozero with pip::
$ ???
pip install gpiozero
Next, install gpiozero with pip:
.. code-block:: console
$ pip install gpiozero
Windows
=======
First, install pip::
First, install pip:
???
.. code-block:: doscon
Next, install gpiozero with pip::
C:\Users\user1> ???
pip install gpiozero
Next, install gpiozero with pip:
.. code-block:: doscon
C:\Users\user1> pip install gpiozero
.. _Raspbian Jessie: https://www.raspberrypi.org/downloads/raspbian/

View File

@@ -102,4 +102,4 @@ the ``pip`` utility. This can be done with the following command in Raspbian:
Alternatively, install pip with `get-pip`_.
.. _get_pip: https://pip.pypa.io/en/stable/installing/
.. _get-pip: https://pip.pypa.io/en/stable/installing/

View File

@@ -36,7 +36,9 @@ functionality without the need to wire up your own LEDs (also useful because
the power and activity LEDs are "known good").
Firstly you need to disable the usual triggers for the built-in LEDs. This can
be done from the terminal with the following commands::
be done from the terminal with the following commands:
.. code-block:: console
$ echo none | sudo tee /sys/class/leds/led0/trigger
$ echo gpio | sudo tee /sys/class/leds/led1/trigger
@@ -46,7 +48,9 @@ Now you can control the LEDs with gpiozero like so:
.. literalinclude:: examples/led_builtin.py
To revert the LEDs to their usual purpose you can either reboot your Pi or
run the following commands::
run the following commands:
.. code-block:: console
$ echo mmc0 | sudo tee /sys/class/leds/led0/trigger
$ echo input | sudo tee /sys/class/leds/led1/trigger

View File

@@ -7,7 +7,7 @@ Remote GPIO
GPIO Zero supports a number of different pin implementations (low-level pin
libraries which deal with the GPIO pins directly). By default, the `RPi.GPIO`_
library is used (assuming it is installed on your system), but you can
optionally specify one to use. For more information, see the :doc:`pins`
optionally specify one to use. For more information, see the :doc:`api_pins`
documentation page.
One of the pin libraries supported, `pigpio`_, provides the ability to control
@@ -23,9 +23,11 @@ Preparing the Raspberry Pi
If you're using Raspbian Jessie (desktop - not Jessie Lite) then you have
everything you need to use the remote GPIO feature. If you're using Jessie Lite,
or another distribution, you'll need to install pigpio::
or another distribution, you'll need to install pigpio:
sudo apt install pigpio
.. code-block:: console
$ sudo apt install pigpio
Then you just need to enable **Remote GPIO** in the Raspberry Pi configuration
tool:
@@ -34,21 +36,25 @@ tool:
(Alternatively, use ``sudo raspi-config`` on the command line)
Then launch the pigpio daemon::
Then launch the pigpio daemon:
sudo pigpiod
.. code-block:: console
$ sudo pigpiod
To only allow connections from a specific IP address, use the ``-n`` flag. For
example::
example:
sudo pigpiod -n localhost # allow localhost only
sudo pigpiod -n 192.168.1.65 # allow 192.168.1.65 only
sudo pigpiod -n localhost -n 192.168.1.65 # allow localhost and 192.168.1.65 only
.. code-block:: console
$ sudo pigpiod -n localhost # allow localhost only
$ sudo pigpiod -n 192.168.1.65 # allow 192.168.1.65 only
$ sudo pigpiod -n localhost -n 192.168.1.65 # allow localhost and 192.168.1.65 only
You will need to launch the pigpio daemon every time you wish to use this
feature. To automate running the daemon at boot time::
sudo systemctl enable pigpiod
$ sudo systemctl enable pigpiod
Preparing the host computer
===========================
@@ -61,72 +67,100 @@ Python library on the PC.
Raspberry Pi
------------
First, update your repositories list::
First, update your repositories list:
sudo apt update
.. code-block:: console
Then install the pigpio library for Python 3::
$ sudo apt update
sudo apt install python3-pigpio
Then install the pigpio library for Python 3:
or Python 2::
.. code-block:: console
sudo apt install python-pigpio
$ sudo apt install python3-pigpio
Alternatively, install with pip::
or Python 2:
sudo pip3 install pigpio
.. code-block:: console
or::
$ sudo apt install python-pigpio
sudo pip install pigpio
Alternatively, install with pip:
.. code-block:: console
$ sudo pip3 install pigpio
or:
.. code-block:: console
$ sudo pip install pigpio
Linux
-----
First, update your distribution's repositories list. For example::
First, update your distribution's repositories list. For example:
sudo apt update
.. code-block:: console
Then install pip for Python 3::
$ sudo apt update
sudo apt install python3-pip
Then install pip for Python 3:
or Python 2::
.. code-block:: console
sudo apt install python-pip
$ sudo apt install python3-pip
or Python 2:
.. code-block:: console
$ sudo apt install python-pip
(Alternatively, install pip with `get-pip`_.)
Next, install pigpio for Python 3::
Next, install pigpio for Python 3:
sudo pip3 install pigpio
.. code-block:: console
or Python 2::
$ sudo pip3 install pigpio
sudo pip install pigpio
or Python 2:
.. code-block:: console
$ sudo pip install pigpio
Mac OS
------
First, install pip::
First, install pip:
???
.. code-block:: console
Next, install pigpio with pip::
$ ???
pip install pigpio
Next, install pigpio with pip:
.. code-block:: console
$ pip install pigpio
Windows
-------
First install pip::
First install pip:
???
.. code-block:: doscon
Next, install pigpio with pip::
C:\Users\user1> ???
pip install pigpio
Next, install pigpio with pip:
.. code-block:: doscon
C:\Users\user1> pip install pigpio
Environment variables
=====================
@@ -135,7 +169,9 @@ The simplest way to use devices with remote pins is to set the ``PIGPIO_ADDR``
environment variable to the IP address of the desired Raspberry Pi. You must
run your Python script or launch your development environment with the
environment variable set using the command line. For example, one of the
following::
following:
.. code-block:: console
$ PIGPIO_ADDR=192.168.1.3 python3 hello.py
$ PIGPIO_ADDR=192.168.1.3 python3
@@ -147,7 +183,9 @@ pigpio Python library installed, this will work with no further configuration.
However, if you are running this from a Raspberry Pi, you will also need to
ensure the default pin factory is set to ``PiGPIOPin``. If ``RPi.GPIO`` is
installed, this will be selected as the default pin factory, so either uninstall
it, or use another environment variable to set it to ``PiGPIOPin``::
it, or use another environment variable to set it to ``PiGPIOPin``:
.. code-block:: console
$ GPIOZERO_PIN_FACTORY=pigpio PIGPIO_ADDR=192.168.1.3 python3 hello.py
@@ -160,12 +198,16 @@ with no modifications needed. For example:
.. literalinclude:: examples/led_1.py
When run with::
When run with:
.. code-block:: console
$ PIGPIO_ADDR=192.168.1.3 python3 led.py
will flash the LED connected to pin 17 of the Raspberry Pi with the IP address
``192.168.1.3``. And::
``192.168.1.3``. And:
.. code-block:: console
$ PIGPIO_ADDR=192.168.1.4 python3 led.py
@@ -236,11 +278,13 @@ computer using remote pins.
First, configure the boot partition of the SD card:
1. Edit ``config.txt`` and add ``dtoverlay=dwc2`` on a new line, then save the
file.
file.
2. Create an empty file called ``ssh`` (no file extension) and save it in the
boot partition.
boot partition.
3. Edit ``cmdline.txt`` and insert ``modules-load=dwc2,g_ether`` after
``rootwait``.
``rootwait``.
(See `blog.gbaman.info`_ for more information)