Skip to content

Commit

Permalink
Merge pull request #925 from gpiozero/863-cleanup-docs
Browse files Browse the repository at this point in the history
Explain how to cleanup after exception, close #863
  • Loading branch information
bennuttall committed Feb 22, 2021
2 parents a6f7efe + c271d70 commit bdfb40c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ This means that you can reuse the pin for another device, and that despite
turning the LED on (and hence, the pin high), after calling
:meth:`~Device.close` it is restored to its previous state (LED off, pin low).

Read more about :ref:`migrating_from_rpigpio`.


How do I use button.when_pressed and button.when_held together?
===============================================================
Expand Down
16 changes: 16 additions & 0 deletions docs/migrating_from_rpigpio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,22 @@ Pin state cleanup is explicit in RPi.GPIO, and is done manually with
pin used, at the end of the script. Manual cleanup is possible by use of the
:meth:`~Device.close` method on the device.

Note that cleanup only occurs at the point of normal termination of the script.
If the script exits due to a program error, cleanup will not be performed. To
ensure that cleanup is performed after an exception is raised, the exception
must be handled, for example::

from gpiozero import Button

btn = Button(4)

while True:
try:
if btn.is_pressed:
print("Pressed")
except KeyboardInterrupt:
print("Ending program")

Read more in the relevant FAQ: :ref:`gpio-cleanup`


Expand Down

0 comments on commit bdfb40c

Please sign in to comment.