mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-12-08 20:39:01 +00:00
Fix #459 - properly support remote SPI with pigpio
Sorry! Dave's messing around with the pin implementations again. Hopefully the last time. The pin_factory is now really a factory object which can be asked to produce individual pins or pin-based interfaces like SPI (which can be supported properly via pigpio).
This commit is contained in:
@@ -59,6 +59,20 @@ Errors
|
||||
|
||||
.. autoexception:: SPIBadArgs
|
||||
|
||||
.. autoexception:: SPIBadChannel
|
||||
|
||||
.. autoexception:: SPIFixedClockMode
|
||||
|
||||
.. autoexception:: SPIInvalidClockMode
|
||||
|
||||
.. autoexception:: SPIFixedBitOrder
|
||||
|
||||
.. autoexception:: SPIFixedSelect
|
||||
|
||||
.. autoexception:: SPIFixedWordSize
|
||||
|
||||
.. autoexception:: SPIInvalidWordSize
|
||||
|
||||
.. autoexception:: GPIODeviceError
|
||||
|
||||
.. autoexception:: GPIODeviceClosed
|
||||
@@ -83,23 +97,31 @@ Errors
|
||||
|
||||
.. autoexception:: PinInvalidEdges
|
||||
|
||||
.. autoexception:: PinInvalidBounce
|
||||
|
||||
.. autoexception:: PinSetInput
|
||||
|
||||
.. autoexception:: PinFixedPull
|
||||
|
||||
.. autoexception:: PinEdgeDetectUnsupported
|
||||
|
||||
.. autoexception:: PinGPIOUnsupported
|
||||
|
||||
.. autoexception:: PinSPIUnsupported
|
||||
|
||||
.. autoexception:: PinPWMError
|
||||
|
||||
.. autoexception:: PinPWMUnsupported
|
||||
|
||||
.. autoexception:: PinPWMFixedValue
|
||||
|
||||
.. autoexception:: PinUnknownPi
|
||||
|
||||
.. autoexception:: PinMultiplePins
|
||||
|
||||
.. autoexception:: PinNoPins
|
||||
|
||||
.. autoexception:: PinUnknownPi
|
||||
.. autoexception:: PinInvalidPin
|
||||
|
||||
Warnings
|
||||
========
|
||||
@@ -110,3 +132,7 @@ Warnings
|
||||
|
||||
.. autoexception:: SPISoftwareFallback
|
||||
|
||||
.. autoexception:: PinFactoryFallback
|
||||
|
||||
.. autoexception:: PinNonPhysical
|
||||
|
||||
|
||||
@@ -11,68 +11,68 @@ 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 GPIO pin number. However, what the
|
||||
library actually expects is a :class:`Pin` implementation. If it finds a simple
|
||||
integer number instead, it uses one of the following classes to provide the
|
||||
:class:`Pin` implementation (classes are listed in favoured order):
|
||||
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.
|
||||
|
||||
1. :class:`gpiozero.pins.rpigpio.RPiGPIOPin`
|
||||
Changing the pin factory
|
||||
========================
|
||||
|
||||
2. :class:`gpiozero.pins.rpio.RPIOPin`
|
||||
The default pin factory can be replaced by specifying a value for the
|
||||
``GPIOZERO_PIN_FACTORY`` environment variable. For example:
|
||||
|
||||
3. :class:`gpiozero.pins.pigpiod.PiGPIOPin`
|
||||
.. code-block:: console
|
||||
|
||||
4. :class:`gpiozero.pins.native.NativePin`
|
||||
pi@raspberrypi $ 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.
|
||||
>>> import gpiozero
|
||||
>>> gpiozero.Device._pin_factory
|
||||
<gpiozero.pins.native.NativeFactory object at 0x762c26b0>
|
||||
|
||||
You can change the default pin implementation by over-writing the
|
||||
``pin_factory`` global in the ``devices`` module like so::
|
||||
The following values, and the corresponding :class:`Factory` and :class:`Pin`
|
||||
classes are listed in the table below. Factories are listed in the order that
|
||||
they are tried by default.
|
||||
|
||||
from gpiozero.pins.native import NativePin
|
||||
import gpiozero.devices
|
||||
# Force the default pin implementation to be NativePin
|
||||
gpiozero.devices.pin_factory = NativePin
|
||||
+---------+-----------------------------------------------+-------------------------------------------+
|
||||
| Name | Factory class | Pin class |
|
||||
+=========+===============================================+===========================================+
|
||||
| rpigpio | :class:`gpiozero.pins.rpigpio.RPiGPIOFactory` | :class:`gpiozero.pins.rpigpio.RPiGPIOPin` |
|
||||
+---------+-----------------------------------------------+-------------------------------------------+
|
||||
| rpio | :class:`gpiozero.pins.rpio.RPIOFactory` | :class:`gpiozero.pins.rpio.RPIOPin` |
|
||||
+---------+-----------------------------------------------+-------------------------------------------+
|
||||
| pigpio | :class:`gpiozero.pins.pigpiod.PiGPIOFactory` | :class:`gpiozero.pins.pigpiod.PiGPIOPin` |
|
||||
+---------+-----------------------------------------------+-------------------------------------------+
|
||||
| native | :class:`gpiozero.pins.native.NativeFactory` | :class:`gpiozero.pins.native.NativePin` |
|
||||
+---------+-----------------------------------------------+-------------------------------------------+
|
||||
|
||||
If you need to change the default pin factory from within a script, use the
|
||||
``Device._set_pin_factory`` class method, passing in the instance of the new
|
||||
factory to use. This is only supported at script startup (replacing the factory
|
||||
closes all existing pin instances which can have interesting consequences for
|
||||
any devices using them)::
|
||||
|
||||
from gpiozero.pins.native import NativeFactory
|
||||
from gpiozero import *
|
||||
Device._set_pin_factory(NativeFactory())
|
||||
|
||||
from gpiozero import LED
|
||||
|
||||
# This will now use NativePin instead of RPiGPIOPin
|
||||
led = LED(16)
|
||||
|
||||
``pin_factory`` is a concrete descendent of the abstract :class:`Pin` class.
|
||||
The descendent may take additional parameters in its constructor provided they
|
||||
are optional; GPIO Zero will expect to be able to construct instances with
|
||||
nothing more than an integer pin number.
|
||||
|
||||
However, the descendent may take default information from additional sources.
|
||||
Certain factories may take default information from additional sources.
|
||||
For example, to default to creating pins with
|
||||
:class:`gpiozero.pins.pigpiod.PiGPIOPin` on a remote pi called ``remote-pi``
|
||||
you can set the :envvar:`PIGPIO_ADDR` environment variable when running your
|
||||
script::
|
||||
script:
|
||||
|
||||
$ PIGPIO_ADDR=remote-pi python my_script.py
|
||||
.. code-block:: console
|
||||
|
||||
It is worth noting that instead of passing an integer to device constructors,
|
||||
you can pass an object derived from :class:`Pin` itself::
|
||||
|
||||
from gpiozero.pins.native import NativePin
|
||||
from gpiozero import LED
|
||||
|
||||
led = LED(NativePin(16))
|
||||
|
||||
In future, this separation of pins and devices should also permit the library
|
||||
to utilize pins that are part of IO extender chips. For example::
|
||||
|
||||
from gpiozero import IOExtender, LED
|
||||
|
||||
ext = IOExtender()
|
||||
led = LED(ext.pins[0])
|
||||
led.on()
|
||||
|
||||
.. warning::
|
||||
|
||||
While the devices API is now considered stable and won't change in
|
||||
backwards incompatible ways, the pins API is *not* yet considered stable.
|
||||
It is potentially subject to change in future versions. We welcome any
|
||||
comments from testers!
|
||||
$ export GPIOZERO_PIN_FACTORY=pigpio
|
||||
$ PIGPIO_ADDR=remote-pi python3 my_script.py
|
||||
|
||||
.. warning::
|
||||
|
||||
@@ -83,41 +83,48 @@ to utilize pins that are part of IO extender chips. For example::
|
||||
actual raspberry pie, you have only yourself to blame.
|
||||
|
||||
|
||||
RPiGPIOPin
|
||||
==========
|
||||
RPi.GPIO
|
||||
========
|
||||
|
||||
.. autoclass:: gpiozero.pins.rpigpio.RPiGPIOFactory
|
||||
|
||||
.. autoclass:: gpiozero.pins.rpigpio.RPiGPIOPin
|
||||
|
||||
|
||||
RPIOPin
|
||||
=======
|
||||
RPIO
|
||||
====
|
||||
|
||||
.. autoclass:: gpiozero.pins.rpio.RPIOFactory
|
||||
|
||||
.. autoclass:: gpiozero.pins.rpio.RPIOPin
|
||||
|
||||
|
||||
PiGPIOPin
|
||||
=========
|
||||
PiGPIO
|
||||
======
|
||||
|
||||
.. autoclass:: gpiozero.pins.pigpiod.PiGPIOFactory
|
||||
|
||||
.. autoclass:: gpiozero.pins.pigpiod.PiGPIOPin
|
||||
|
||||
|
||||
NativePin
|
||||
=========
|
||||
Native
|
||||
======
|
||||
|
||||
.. autoclass:: gpiozero.pins.native.NativeFactory
|
||||
|
||||
.. autoclass:: gpiozero.pins.native.NativePin
|
||||
|
||||
|
||||
Abstract Pin
|
||||
Base classes
|
||||
============
|
||||
|
||||
.. autoclass:: Factory
|
||||
:members:
|
||||
|
||||
.. autoclass:: Pin
|
||||
:members:
|
||||
|
||||
|
||||
Local Pin
|
||||
=========
|
||||
|
||||
.. autoclass:: LocalPin
|
||||
.. autoclass:: SPI
|
||||
:members:
|
||||
|
||||
|
||||
|
||||
Binary file not shown.
Binary file not shown.
|
Before Width: | Height: | Size: 34 KiB After Width: | Height: | Size: 34 KiB |
@@ -4,6 +4,7 @@ Notes
|
||||
|
||||
.. currentmodule:: gpiozero
|
||||
|
||||
|
||||
.. _keep-your-script-running:
|
||||
|
||||
Keep your script running
|
||||
@@ -46,6 +47,7 @@ events to be detected::
|
||||
button.when_pressed = hello
|
||||
pause()
|
||||
|
||||
|
||||
Importing from GPIO Zero
|
||||
========================
|
||||
|
||||
@@ -70,12 +72,15 @@ In this case, all references to items within GPIO Zero must be prefixed::
|
||||
|
||||
button = gpiozero.Button(2)
|
||||
|
||||
|
||||
How can I tell what version of gpiozero I have installed?
|
||||
=========================================================
|
||||
|
||||
The gpiozero library relies on the setuptools package for installation
|
||||
services. You can use the setuptools ``pkg_resources`` API to query which
|
||||
version of gpiozero is available in your Python environment like so::
|
||||
version of gpiozero is available in your Python environment like so:
|
||||
|
||||
.. code-block:: pycon
|
||||
|
||||
>>> from pkg_resources import require
|
||||
>>> require('gpiozero')
|
||||
@@ -89,7 +94,9 @@ the first entry in the list will be the version that ``import gpiozero`` will
|
||||
import.
|
||||
|
||||
If you receive the error "No module named pkg_resources", you need to install
|
||||
the ``pip`` utility. This can be done with the following command in Raspbian::
|
||||
the ``pip`` utility. This can be done with the following command in Raspbian:
|
||||
|
||||
.. code-block:: console
|
||||
|
||||
$ sudo apt-get install python-pip
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ library. Please note that all recipes are written assuming Python 3. Recipes
|
||||
*may* work under Python 2, but no guarantees!
|
||||
|
||||
|
||||
.. _pin_numbering:
|
||||
.. _pin-numbering:
|
||||
|
||||
Pin Numbering
|
||||
=============
|
||||
@@ -429,7 +429,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
|
||||
@@ -439,7 +441,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
|
||||
|
||||
Reference in New Issue
Block a user