Skip to main content

Library to operate GPIO pins via character device

Project description

Managing GPIO pins via character device

Basics

For the set of GPIO lines (pins) we create an object called GPIOHandle, which manages their state. State is a tuple of 0's and 1's, one number per line.

This library doesn't require root access to the system, but it needs a read-write access to the gpiochip device. By default it uses /dev/gpiochip0.

To allow read-write access to the device for the user, run:

$ sudo chmod a+rw /dev/gpiochip0

Note that the system might have several GPIO chips, some of them can be exposed to the user (as /dev/gpiochip0 on Raspberry Pi) and some of them might be responsible for system functions, like WakeOnLan or system LED lights. Be carefull when choosing the device and allowing user access to it.

You can check the info on the GPIOChip device, by accessing its info() method. See example below.

Example

from gpiodev import GPIOHandle
import time

# Request handle for lines 12 and 23 from default /dev/gpiochip0 

DoubleLED = GPIOHandle((12,23))

# Define states of the Double LED

all = (1, 1)
none = (0, 0)
first = (1, 0)
second = (0, 1)

# Loop through the states

for state in [all, none, first, second, none, all, none]:
    DoubleLED.set_values(state)
    print(DoubleLED.get_values())
    time.sleep(1)

Use another device

To use another gpiochip device, for example /dev/gpiochip1, you can use a different way to setup a handle:

from gpiodev import GPIOChip

GPIO = GPIOChip("/dev/gpiochip1")

# Check info on the gpio chip

print(GPIO.info())

# Request handle for lines 12 and 23 from /dev/gpiochip1

DoubleLED = GPIO.get_handle((12,23)) # This will fail on RaspberryPi as /dev/gpiochip1 is a system gpio chip, with only 8 lines.

Background

New GPIO interface has been introduced in the kernel.

It exposes GPIO interface a character device(/dev/gpiochip0) and provides several ioctl syscalls for bulk operations on sets of GPIO pins.

Unlike Python bindings to the libgpiod C-library, we work with the kernel interface (ioctl calls) directly.

In gpiodev/src/gpioctl.c we wrap the ioctl calls into C-functions suitable for later use.

In gpiodev/gpio.py the ctypes bindings are created and then used to define the main GPIOHandle class.


Tested on Fedora 26+ armhfp, Raspberry Pi 3 Model B.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gpiodev-0.0.1.tar.gz (10.6 kB view details)

Uploaded Source

File details

Details for the file gpiodev-0.0.1.tar.gz.

File metadata

  • Download URL: gpiodev-0.0.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.2 pkginfo/1.4.2 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.5

File hashes

Hashes for gpiodev-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e49b9b75b118c7d433822dab72f0a45e83ef901005a825ddc36d88108e54e52d
MD5 ed2b0bb79c09174b6b0f0923463a7562
BLAKE2b-256 b8c88bbc19413ae5f0eec83f641f12c3517ed17aad9eecf85bb2e7580ceac537

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page