From b2e4237a57e3928f4005cefd5562817870b3e4dc Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Fri, 14 Jul 2017 14:01:29 +0100 Subject: [PATCH] Correct remote GPIO recipes --- docs/examples/led_button_remote_1.py | 6 +++-- docs/examples/led_button_remote_2.py | 9 ++++--- docs/examples/led_remote_1.py | 5 ++-- docs/examples/led_remote_2.py | 8 +++--- docs/examples/led_remote_3.py | 5 ++-- docs/examples/led_remote_4.py | 5 ++-- docs/examples/led_remote_5.py | 9 ++++--- docs/examples/pi_zero_remote.py | 5 ++-- docs/examples/sense_hat_remote.py | 5 ++-- docs/examples/sense_hat_remote_2.py | 5 ++-- docs/examples/traffichat_remote_1.py | 2 +- docs/examples/traffichat_remote_2.py | 5 ++-- docs/remote_gpio.rst | 37 ++++++++++++++-------------- 13 files changed, 62 insertions(+), 44 deletions(-) diff --git a/docs/examples/led_button_remote_1.py b/docs/examples/led_button_remote_1.py index 7fb7363..4b75ec2 100644 --- a/docs/examples/led_button_remote_1.py +++ b/docs/examples/led_button_remote_1.py @@ -1,9 +1,11 @@ from gpiozero import LED -from gpiozero.pins.pigpio import PiGPIOPin +from gpiozero.pins.pigpio import PiGPIOFactory from signal import pause +factory = PiGPIOFactory(host='192.168.1.3') + button = Button(2) -led = LED(PiGPIOPin(17, host='192.168.1.3')) +led = LED(17, pin_factory=factory) led.source = button.values diff --git a/docs/examples/led_button_remote_2.py b/docs/examples/led_button_remote_2.py index bacc443..dfe60b8 100644 --- a/docs/examples/led_button_remote_2.py +++ b/docs/examples/led_button_remote_2.py @@ -1,11 +1,14 @@ from gpiozero import LED -from gpiozero.pins.pigpio import PiGPIOPin +from gpiozero.pins.pigpio import PiGPIOFactory from gpiozero.tools import all_values from signal import pause +factory3 = PiGPIOFactory(host='192.168.1.3') +factory4 = PiGPIOFactory(host='192.168.1.3') + led = LED(17) -button_1 = Button(PiGPIOPin(17, host='192.168.1.3')) -button_2 = Button(PiGPIOPin(17, host='192.168.1.4')) +button_1 = Button(17, pin_factory=factory3) +button_2 = Button(17, pin_factory=factory4) led.source = all_values(button_1.values, button_2.values) diff --git a/docs/examples/led_remote_1.py b/docs/examples/led_remote_1.py index 0bfd9f4..68d2135 100644 --- a/docs/examples/led_remote_1.py +++ b/docs/examples/led_remote_1.py @@ -1,8 +1,9 @@ from gpiozero import LED -from gpiozero.pins.pigpio import PiGPIOPin +from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep -led = LED(PiGPIOPin(17, host='192.168.1.3')) +factory = PiGPIOFactory(host='192.168.1.3') +led = LED(17, pin_factory=factory) while True: led.on() diff --git a/docs/examples/led_remote_2.py b/docs/examples/led_remote_2.py index c3bdb02..387646c 100644 --- a/docs/examples/led_remote_2.py +++ b/docs/examples/led_remote_2.py @@ -1,9 +1,11 @@ from gpiozero import LED -from gpiozero.pins.pigpio import PiGPIOPin +from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep -led_1 = LED(PiGPIOPin(17, host='192.168.1.3')) -led_2 = LED(PiGPIOPin(17, host='192.168.1.4')) +factory3 = PiGPIOFactory(host='192.168.1.3') +factory4 = PiGPIOFactory(host='192.168.1.4') +led_1 = LED(17, pin_factory=factory3) +led_2 = LED(17, pin_factory=factory4) while True: led_1.on() diff --git a/docs/examples/led_remote_3.py b/docs/examples/led_remote_3.py index 1e7133b..9e4c49c 100644 --- a/docs/examples/led_remote_3.py +++ b/docs/examples/led_remote_3.py @@ -1,9 +1,10 @@ from gpiozero import LED -from gpiozero.pins.pigpio import PiGPIOPin +from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep +remote_factory = PiGPIOFactory(host='192.168.1.3') led_1 = LED(17) # local pin -led_2 = LED(PiGPIOPin(17, host='192.168.1.3')) # remote pin +led_2 = LED(17, pin_factory=remote_factory) # remote pin while True: led_1.on() diff --git a/docs/examples/led_remote_4.py b/docs/examples/led_remote_4.py index 5d88d8b..a9396f7 100644 --- a/docs/examples/led_remote_4.py +++ b/docs/examples/led_remote_4.py @@ -1,8 +1,9 @@ from gpiozero import LED -from gpiozero.pins.rpigpio import RPiGPIOPin +from gpiozero.pins.rpigpio import RPiGPIOFactory from time import sleep -led_1 = LED(RPiGPIOPin(17)) # local pin +local_factory = RPiGPIOFactory() +led_1 = LED(17, pin_factory=local_factory) # local pin led_2 = LED(17) # remote pin while True: diff --git a/docs/examples/led_remote_5.py b/docs/examples/led_remote_5.py index ab403ee..f498db4 100644 --- a/docs/examples/led_remote_5.py +++ b/docs/examples/led_remote_5.py @@ -1,10 +1,13 @@ from gpiozero import LED -from gpiozero.pins.pigpio import PiGPIOPin +from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep +factory3 = PiGPIOFactory(host='192.168.1.3') +factory4 = PiGPIOFactory(host='192.168.1.4') + led_1 = LED(17) # local pin -led_2 = LED(PiGPIOPin(17, host='192.168.1.3')) # remote pin on one pi -led_3 = LED(PiGPIOPin(17, host='192.168.1.4')) # remote pin on another pi +led_2 = LED(17, pin_factory=factory3) # remote pin on one pi +led_3 = LED(17, pin_factory=factory4) # remote pin on another pi while True: led_1.on() diff --git a/docs/examples/pi_zero_remote.py b/docs/examples/pi_zero_remote.py index 553460d..086683a 100644 --- a/docs/examples/pi_zero_remote.py +++ b/docs/examples/pi_zero_remote.py @@ -1,8 +1,9 @@ from gpiozero import LED -from gpiozero.pins.pigpio import PiGPIOPin +from gpiozero.pins.pigpio import PiGPIOFactory from signal import pause -led = LED(PiGPIOPin(17, host='raspberrypi.local')) +factory = PiGPIOFactory(host='raspberrypi.local') +led = LED(17, pin_factory=factory) led.blink() diff --git a/docs/examples/sense_hat_remote.py b/docs/examples/sense_hat_remote.py index 0b93178..93a18bc 100644 --- a/docs/examples/sense_hat_remote.py +++ b/docs/examples/sense_hat_remote.py @@ -1,8 +1,9 @@ from gpiozero import MotionSensor -from gpiozero.pins.pigpio import PiGPIOPin +from gpiozero.pins.pigpio import PiGPIOFactory from sense_hat import SenseHat -pir = MotionSensor(PiGPIOPin(4, host='192.168.1.4')) # remote motion sensor +remote_factory = PiGPIOFactory(host='192.198.1.4') +pir = MotionSensor(4, pin_factory=remote_factory) # remote motion sensor sense = SenseHat() # local sense hat while True: diff --git a/docs/examples/sense_hat_remote_2.py b/docs/examples/sense_hat_remote_2.py index c13eba9..9bf2527 100644 --- a/docs/examples/sense_hat_remote_2.py +++ b/docs/examples/sense_hat_remote_2.py @@ -1,8 +1,9 @@ from gpiozero import LightSensor -from gpiozero.pins.pigpio import PiGPIOPin +from gpiozero.pins.pigpio import PiGPIOFactory from sense_hat import SenseHat -light = LightSensor(PiGPIOPin(4, host='192.168.1.4')) # remote motion sensor +remote_factory = PiGPIOFactory(host='192.168.1.4') +light = LightSensor(4, pin_factory=remote_factory) # remote motion sensor sense = SenseHat() # local sense hat blue = (0, 0, 255) diff --git a/docs/examples/traffichat_remote_1.py b/docs/examples/traffichat_remote_1.py index 9b08eb7..0bc74d6 100644 --- a/docs/examples/traffichat_remote_1.py +++ b/docs/examples/traffichat_remote_1.py @@ -3,5 +3,5 @@ from gpiozero import TrafficHat from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep -gpiozero.Device._set_pin_factory(PiGPIOFactory(host='192.168.1.3')) +gpiozero.Device.pin_factory = PiGPIOFactory(host='192.168.1.3') th = TrafficHat() # traffic hat on 192.168.1.3 using remote pins diff --git a/docs/examples/traffichat_remote_2.py b/docs/examples/traffichat_remote_2.py index 5795455..b46a147 100644 --- a/docs/examples/traffichat_remote_2.py +++ b/docs/examples/traffichat_remote_2.py @@ -3,6 +3,7 @@ from gpiozero import TrafficHat from gpiozero.pins.pigpio import PiGPIOFactory from time import sleep +remote_factory = PiGPIOFactory(host='192.168.1.3') + th_1 = TrafficHat() # traffic hat using local pins -gpiozero.Device._set_pin_factory(PiGPIOFactory(host='192.168.1.3')) -th_2 = TrafficHat() # traffic hat on 192.168.1.3 using remote pins +th_2 = TrafficHat(pin_factory=remote_factory) # traffic hat on 192.168.1.3 using remote pins diff --git a/docs/remote_gpio.rst b/docs/remote_gpio.rst index 997425d..1686d3d 100644 --- a/docs/remote_gpio.rst +++ b/docs/remote_gpio.rst @@ -12,8 +12,8 @@ documentation page. One of the pin libraries supported, `pigpio`_, provides the ability to control GPIO pins remotely over the network, which means you can use GPIO Zero to -control devices connected to a Raspberry Pi on the network. You can do this from -another Raspberry Pi, or even from a PC. +control devices connected to a Raspberry Pi on the network. You can do this +from another Raspberry Pi, or even from a PC. See the :doc:`recipes_remote_gpio` page for examples on how remote pins can be used. @@ -22,8 +22,8 @@ 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: +everything you need to use the remote GPIO feature. If you're using Jessie +Lite, or another distribution, you'll need to install pigpio:: .. code-block:: console @@ -182,16 +182,16 @@ If you are running this from a PC (not a Raspberry Pi) with gpiozero and the 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``: +installed, this will be selected as the default pin factory, so either +uninstall 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 -This usage will set the pin factory to :class:`PiGPIOPin` with a default host of -``192.168.1.3``. The pin factory can be changed inline in the code, as seen in -the following sections. +This usage will set the pin factory to :class:`PiGPIOFactory` with a default +host of ``192.168.1.3``. The pin factory can be changed inline in the code, as +seen in the following sections. With this usage, you can write gpiozero code like you would on a Raspberry Pi, with no modifications needed. For example: @@ -218,9 +218,9 @@ Pin objects =========== An alternative (or additional) method of configuring gpiozero objects to use -remote pins is to create instances of :class:PiGPIOPin objects, and -instantiating device objects with those pin objects, rather than just numbers. -For example, with no environment variables set: +remote pins is to create instances of :class:`PiGPIOFactory` objects, and use +them when instantiating device objects. For example, with no environment +variables set: .. literalinclude:: examples/led_remote_1.py @@ -230,8 +230,8 @@ This allows devices on multiple Raspberry Pis to be used in the same script: You can, of course, continue to create gpiozero device objects as normal, and create others using remote pins. For example, if run on a Raspberry Pi, the -following script will flash an LED on the host Pi, and also on another Pi on the -network: +following script will flash an LED on the host Pi, and also on another Pi on +the network: .. literalinclude:: examples/led_remote_3.py @@ -249,7 +249,8 @@ Note that these examples use the :class:`LED` class, which takes a ``pin`` argument to initialise. Some classes, particularly those representing HATs and other add-on boards, do not require their pin numbers to be specified. However, it is still possible to use remote pins with these devices, either using -environment variables, or by using :meth:`~Device._set_pin_factory`: +environment variables, :attr:`Device.pin_factory`, or the ``pin_factory`` +keyword argument: .. literalinclude:: examples/traffichat_remote_1.py @@ -299,10 +300,10 @@ from the computer, referencing the host by its hostname, like so: .. note:: - When running code directly on a Raspberry Pi, any pin type can be used + When running code directly on a Raspberry Pi, any pin factory can be used (assuming the relevant library is installed), but when a device is used - remotely, only :class:`PiGPIOPin` can be used, as pigpio is the only pin - library which supports remote GPIO. + remotely, only :class:`PiGPIOFactory` can be used, as pigpio is the only + pin library which supports remote GPIO. .. _RPi.GPIO: https://pypi.python.org/pypi/RPi.GPIO