| 1 | <!-- SPDX-License-Identifier: CC-BY-SA-4.0 --> |
| 2 | <!-- SPDX-FileCopyrightText: 2023 Phil Howard <[email protected]> --> |
| 3 | |
| 4 | # gpiod |
| 5 | |
| 6 | These are the official Python bindings for [libgpiod](https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/about/). |
| 7 | |
| 8 | The gpiod library has been vendored into this package for your convenience and |
| 9 | this version of gpiod is independent from your system package. |
| 10 | |
| 11 | Binary wheels are not provided. The source package requires python3-dev. |
| 12 | |
| 13 | ## Rationale |
| 14 | |
| 15 | The new character device interface guarantees all allocated resources are |
| 16 | freed after closing the device file descriptor and adds several new features |
| 17 | that are not present in the obsolete sysfs interface (like event polling, |
| 18 | setting/reading multiple values at once or open-source and open-drain GPIOs). |
| 19 | |
| 20 | Unfortunately interacting with the linux device file can no longer be done |
| 21 | using only standard command-line tools. This is the reason for creating a |
| 22 | library encapsulating the cumbersome, ioctl-based kernel-userspace interaction |
| 23 | in a set of convenient functions and opaque data structures. |
| 24 | |
| 25 | ## Breaking Changes |
| 26 | |
| 27 | As of v2.0.2 we have replaced the unofficial, pure-Python "gpiod". The official |
| 28 | gpiod is not backwards compatible. |
| 29 | |
| 30 | You should ensure you specify at least v2.0.2 for the official API. Versions |
| 31 | 1.5.4 and prior are the deprecated, unofficial, pure-Python bindings. |
| 32 | |
| 33 | ## Installing |
| 34 | |
| 35 | You will need `python3-dev`, on Debian/Ubuntu you can install this with: |
| 36 | |
| 37 | ``` |
| 38 | sudo apt install python3-dev |
| 39 | ``` |
| 40 | |
| 41 | And then install gpiod with: |
| 42 | |
| 43 | ``` |
| 44 | pip install gpiod |
| 45 | ``` |
| 46 | |
| 47 | You can optionally depend upon your system gpiod by installing with: |
| 48 | |
| 49 | ``` |
| 50 | LINK_SYSTEM_LIBGPIOD=1 pip install gpiod |
| 51 | ``` |
| 52 | |
| 53 | If you still need the deprecated pure-Python bindings, install with: |
| 54 | |
| 55 | ``` |
| 56 | pip install gpiod==1.5.4 |
| 57 | ``` |
| 58 | |
| 59 | ## Examples |
| 60 | |
| 61 | Check a GPIO chip character device exists: |
| 62 | |
| 63 | ```python |
| 64 | import gpiod |
| 65 | |
| 66 | gpiod.is_gpiochip_device("/dev/gpiochip0") |
| 67 | |
| 68 | ``` |
| 69 | |
| 70 | Get information about a GPIO chip character device: |
| 71 | |
| 72 | ```python |
| 73 | import gpiod |
| 74 | |
| 75 | with gpiod.Chip("/dev/gpiochip0") as chip: |
| 76 | info = chip.get_info() |
| 77 | print(f"{info.name} [{info.label}] ({info.num_lines} lines)") |
| 78 | ``` |
| 79 | |
| 80 | Blink an LED, or toggling a GPIO line: |
| 81 | |
| 82 | ```python |
| 83 | import time |
| 84 | |
| 85 | from gpiod.line import Direction, Value |
| 86 | |
| 87 | LINE = 5 |
| 88 | |
| 89 | with gpiod.request_lines( |
| 90 | "/dev/gpiochip0", |
| 91 | consumer="blink-example", |
| 92 | config={ |
| 93 | LINE: gpiod.LineSettings( |
| 94 | direction=Direction.OUTPUT, output_value=Value.ACTIVE |
| 95 | ) |
| 96 | }, |
| 97 | ) as request: |
| 98 | while True: |
| 99 | request.set_value(LINE, Value.ACTIVE) |
| 100 | time.sleep(1) |
| 101 | request.set_value(LINE, Value.INACTIVE) |
| 102 | time.sleep(1) |
| 103 | ``` |
| 104 | |
| 105 | ## Testing |
| 106 | |
| 107 | The test suite for the python bindings can be run by calling: |
| 108 | |
| 109 | ``` |
| 110 | make python-tests-run |
| 111 | ``` |
| 112 | |
| 113 | from the `libgpiod/bindings/python` directory as root (necessary to be able |
| 114 | to create the **gpio-sims** used for testing). |