项目作者: bookwar

项目描述 :
Library to operate GPIO pins via character device
高级语言: Python
项目地址: git://github.com/bookwar/python-gpiodev.git
创建时间: 2017-07-22T19:15:57Z
项目社区:https://github.com/bookwar/python-gpiodev

开源协议:Apache License 2.0

下载


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:

  1. $ 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

  1. from gpiodev import GPIOHandle
  2. import time
  3. # Request handle for lines 12 and 23 from default /dev/gpiochip0
  4. DoubleLED = GPIOHandle((12,23))
  5. # Define states of the Double LED
  6. all = (1, 1)
  7. none = (0, 0)
  8. first = (1, 0)
  9. second = (0, 1)
  10. # Loop through the states
  11. for state in [all, none, first, second, none, all, none]:
  12. DoubleLED.set_values(state)
  13. print(DoubleLED.get_values())
  14. 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:

  1. from gpiodev import GPIOChip
  2. GPIO = GPIOChip("/dev/gpiochip1")
  3. # Check info on the gpio chip
  4. print(GPIO.info())
  5. # Request handle for lines 12 and 23 from /dev/gpiochip1
  6. 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.