The GPIO Zero class hierarchy is quite extensive. It contains several base classes (most of which are documented in their corresponding chapters):
There are also several mixin classes for adding important functionality at numerous points in the hierarchy, which is illustrated below:
Represents a single device of any type; GPIO-based, SPI-based, I2C-based, etc. This is the base class of the device hierarchy. It defines the basic services applicable to all devices (specifically thhe is_active property, the value property, and the close() method).
Shut down the device and release all associated resources. This method can be called on an already closed device without raising an exception.
This method is primarily intended for interactive use at the command line. It disables the device and releases its pin(s) for use by another device.
You can attempt to do this simply by deleting an object, but unless you’ve cleaned up all references to the object this may not work (even if you’ve cleaned up all references, there’s still no guarantee the garbage collector will actually delete the object at that point). By contrast, the close method provides a means of ensuring that the object is shut down.
For example, if you have a breadboard with a buzzer connected to pin 16, but then wish to attach an LED instead:
>>> from gpiozero import *
>>> bz = Buzzer(16)
>>> bz.on()
>>> bz.off()
>>> bz.close()
>>> led = LED(16)
>>> led.blink()
Device descendents can also be used as context managers using the with statement. For example:
>>> from gpiozero import *
>>> with Buzzer(16) as bz:
... bz.on()
...
>>> with LED(16) as led:
... led.on()
...
Returns True if the device is closed (see the close() method). Once a device is closed you can no longer use any other methods or properties to control or query the device.
Returns True if the device is currently active and False otherwise. This property is usually derived from value. Unlike value, this is always a boolean.
Returns a value representing the device’s state. Frequently, this is a boolean value, or a number between 0 and 1 but some devices use larger ranges (e.g. -1 to +1) and composite devices usually use tuples to return the states of all their subordinate components.
Adds a values property to the class which returns an infinite generator of readings from the value property. There is rarely a need to use this mixin directly as all base classes in GPIO Zero include it.
Note
Use this mixin first in the parent class list.
An infinite iterator of values read from value.
Adds a source property to the class which, given an iterable, sets value to each member of that iterable until it is exhausted. This mixin is generally included in novel output devices to allow their state to be driven from another device.
Note
Use this mixin first in the parent class list.
The iterable to use as a source of values for value.
Adds edge-detected when_activated() and when_deactivated() events to a device based on changes to the is_active property common to all devices. Also adds wait_for_active() and wait_for_inactive() methods for level-waiting.
Note
Note that this mixin provides no means of actually firing its events; call _fire_events() in sub-classes when device state changes to trigger the events. This should also be called once at the end of initialization to set initial states.
Pause the script until the device is activated, or the timeout is reached.
Parameters: | timeout (float) – Number of seconds to wait before proceeding. If this is None (the default), then wait indefinitely until the device is active. |
---|
Pause the script until the device is deactivated, or the timeout is reached.
Parameters: | timeout (float) – Number of seconds to wait before proceeding. If this is None (the default), then wait indefinitely until the device is inactive. |
---|
The length of time (in seconds) that the device has been active for. When the device is inactive, this is None.
The length of time (in seconds) that the device has been inactive for. When the device is active, this is None.
The function to run when the device changes state from inactive to active.
This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that activated will be passed as that parameter.
Set this property to None (the default) to disable the event.
The function to run when the device changes state from active to inactive.
This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that deactivated will be passed as that parameter.
Set this property to None (the default) to disable the event.
Extends EventsMixin to add the when_held event and the machinery to fire that event repeatedly (when hold_repeat is True) at internals defined by hold_time.
The length of time (in seconds) that the device has been held for. This is counted from the first execution of the when_held event rather than when the device activated, in contrast to active_time. If the device is not currently held, this is None.
If True, when_held will be executed repeatedly with hold_time seconds between each invocation.
The length of time (in seconds) to wait after the device is activated, until executing the when_held handler. If hold_repeat is True, this is also the length of time between invocations of when_held.
The function to run when the device has remained active for hold_time seconds.
This can be set to a function which accepts no (mandatory) parameters, or a Python function which accepts a single mandatory parameter (with as many optional parameters as you like). If the function accepts a single mandatory parameter, the device that activated will be passed as that parameter.
Set this property to None (the default) to disable the event.