• Stars
    star
    529
  • Rank 83,828 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 9 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A pure Python 2/3 library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.

python-periphery Build Status Docs Status GitHub release License

Linux Peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) with Python 2 & 3

python-periphery is a pure Python library for GPIO, LED, PWM, SPI, I2C, MMIO, and Serial peripheral I/O interface access in userspace Linux. It is useful in embedded Linux environments (including Raspberry Pi, BeagleBone, etc. platforms) for interfacing with external peripherals. python-periphery is compatible with Python 2 and Python 3, is written in pure Python, and is MIT licensed.

Using Lua or C? Check out the lua-periphery and c-periphery projects.

Contributed libraries: java-periphery, dart_periphery

Installation

With pip:

pip install python-periphery

With easy_install:

easy_install python-periphery

With setup.py:

git clone https://github.com/vsergeev/python-periphery.git
cd python-periphery
python setup.py install

Examples

GPIO

from periphery import GPIO

# Open GPIO /dev/gpiochip0 line 10 with input direction
gpio_in = GPIO("/dev/gpiochip0", 10, "in")
# Open GPIO /dev/gpiochip0 line 12 with output direction
gpio_out = GPIO("/dev/gpiochip0", 12, "out")

value = gpio_in.read()
gpio_out.write(not value)

gpio_in.close()
gpio_out.close()

Go to GPIO documentation.

LED

from periphery import LED

# Open LED "led0" with initial state off
led0 = LED("led0", False)
# Open LED "led1" with initial state on
led1 = LED("led1", True)

value = led0.read()
led1.write(value)

# Set custom brightness level
led1.write(led1.max_brightness / 2)

led0.close()
led1.close()

Go to LED documentation.

PWM

from periphery import PWM

# Open PWM chip 0, channel 10
pwm = PWM(0, 10)

# Set frequency to 1 kHz
pwm.frequency = 1e3
# Set duty cycle to 75%
pwm.duty_cycle = 0.75

pwm.enable()

# Change duty cycle to 50%
pwm.duty_cycle = 0.50

pwm.close()

Go to PWM documentation.

SPI

from periphery import SPI

# Open spidev1.0 with mode 0 and max speed 1MHz
spi = SPI("/dev/spidev1.0", 0, 1000000)

data_out = [0xaa, 0xbb, 0xcc, 0xdd]
data_in = spi.transfer(data_out)

print("shifted out [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x{:02x}]".format(*data_out))
print("shifted in  [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x{:02x}]".format(*data_in))

spi.close()

Go to SPI documentation.

I2C

from periphery import I2C

# Open i2c-0 controller
i2c = I2C("/dev/i2c-0")

# Read byte at address 0x100 of EEPROM at 0x50
msgs = [I2C.Message([0x01, 0x00]), I2C.Message([0x00], read=True)]
i2c.transfer(0x50, msgs)
print("0x100: 0x{:02x}".format(msgs[1].data[0]))

i2c.close()

Go to I2C documentation.

MMIO

from periphery import MMIO

# Open am335x real-time clock subsystem page
rtc_mmio = MMIO(0x44E3E000, 0x1000)

# Read current time
rtc_secs = rtc_mmio.read32(0x00)
rtc_mins = rtc_mmio.read32(0x04)
rtc_hrs = rtc_mmio.read32(0x08)

print("hours: {:02x} minutes: {:02x} seconds: {:02x}".format(rtc_hrs, rtc_mins, rtc_secs))

rtc_mmio.close()

# Open am335x control module page
ctrl_mmio = MMIO(0x44E10000, 0x1000)

# Read MAC address
mac_id0_lo = ctrl_mmio.read32(0x630)
mac_id0_hi = ctrl_mmio.read32(0x634)

print("MAC address: {:04x}{:08x}".format(mac_id0_lo, mac_id0_hi))

ctrl_mmio.close()

Go to MMIO documentation.

Serial

from periphery import Serial

# Open /dev/ttyUSB0 with baudrate 115200, and defaults of 8N1, no flow control
serial = Serial("/dev/ttyUSB0", 115200)

serial.write(b"Hello World!")

# Read up to 128 bytes with 500ms timeout
buf = serial.read(128, 0.5)
print("read {:d} bytes: _{:s}_".format(len(buf), buf))

serial.close()

Go to Serial documentation.

Documentation

Documentation is hosted at https://python-periphery.readthedocs.io.

To build documentation locally with Sphinx, run:

cd docs
make html

Sphinx will produce the HTML documentation in docs/_build/html/.

Run make help to see other output targets (LaTeX, man, text, etc.).

Testing

The tests located in the tests folder may be run under Python to test the correctness and functionality of python-periphery. Some tests require interactive probing (e.g. with an oscilloscope), the installation of a physical loopback, or the existence of a particular device on a bus. See the usage of each test for more details on the required setup.

License

python-periphery is MIT licensed. See the included LICENSE file.

More Repositories

1

c-periphery

A C library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
C
727
star
2

luaradio

A lightweight, embeddable software-defined radio framework built on LuaJIT
Lua
604
star
3

u-msgpack-python

A portable, lightweight MessagePack serializer and deserializer written in pure Python, compatible with Python 2, Python 3, CPython, PyPy / msgpack.org[Python]
Python
253
star
4

briefsky

A free weather frontend to a variety of weather providers
TypeScript
243
star
5

apfcp

x86 Assembly Primer for C Programmers
Assembly
211
star
6

lua-periphery

A Lua library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
C
181
star
7

btckeygenie

A standalone Bitcoin keypair/address generator, written in Go.
Go
104
star
8

ntgbtminer

A no thrills getblocktemplate Bitcoin miner, written in Python.
Python
83
star
9

vavrdisasm

vAVRdisasm is an 8-bit Atmel AVR disassembler.
C
74
star
10

audioprism

A spectrogram tool for PulseAudio and WAV files, written in C++11.
C++
63
star
11

tinytaptunnel

a point-to-point layer 2 tap interface tunnel over UDP/IP with HMAC-SHA256 authentication, written in Go.
Go
57
star
12

ssterm

A simple console-based serial port terminal, written in Python.
Python
52
star
13

libGIS

libGIS is a collection of utility functions to create, read and write Atmel Generic, Intel HEX8, and Motorola S-Record formatted files.
C
50
star
14

0xtrades.info

A real-time trade viewer for the 0x protocol.
TypeScript
36
star
15

snake.ts

A simple snake implementation in TypeScript and blessed.
TypeScript
22
star
16

3d-gears

3D Parametric Gears made with Fusion 360
18
star
17

arm-bmw-sw

ARM Bare Metal Widget (arm-bmw) software
C
18
star
18

mbed-cmsis

Building your own CMSIS Code for the mbed.
C
17
star
19

vpicdisasm

vPICdisasm is a Microchip PIC disassembler for Baseline, Mid-Range, and Mid-Range Enhanced 8-bit PIC cores.
C
16
star
20

arm-bmw-hw

ARM Bare Metal Widget (arm-bmw) hardware
Eagle
15
star
21

cmsis-templates

CMSIS v3.20 Bootstrapping Templates for GNU ARM Tools
C
14
star
22

v8cpu

v8cpu is a simple multi-cycle von Neumann architecture 8-bit CPU in under 500 lines of Verilog.
Verilog
14
star
23

teatimer

A simple kitchen timer implemented in digital logic on a Lattice MachXO2 CPLD.
Eagle
14
star
24

gardend-lua

A modular, discrete-time control daemon for a hydroponic garden, written in Lua.
Lua
12
star
25

radio-decoders

Python
11
star
26

evolve110

Rule 110 on the Ethereum blockchain
JavaScript
9
star
27

libGISdotnet

libGIS ported to .NET.
C#
8
star
28

wireless-triac

Wireless ZigBee/XBee Controlled TRIAC (Sep 2009)
C
8
star
29

zigradio

A lightweight software-defined radio framework built with Zig
Zig
7
star
30

rigexpert-tool

Dump, plot, and convert impedance sweeps from a RigExpert antenna analyzer.
Python
6
star
31

qrd

Simple QR Code decoder for educational purposes
Python
6
star
32

minifortune

A minimal fortune-mod clone, written in C.
C
6
star
33

basic-makefiles

TeX
6
star
34

3d-simple-iso-thread

A simple, single-file modeled ISO thread module for OpenSCAD
OpenSCAD
4
star
35

3d-holster-claw

A 3D printable holster claw accessory made with Fusion 360
3
star
36

wireless-power-meter

Wireless ZigBee/XBee V-I Power Meter (Sep 2009)
Python
2
star
37

3d-mini-turntable

A miniature motorized turntable for small objects made with OpenSCAD
OpenSCAD
2
star
38

3d-fiber-enclosure

A 3D printable fiber optic project enclosure made with Fusion 360
1
star
39

3d-webcam-mount

3D Printable Webcam Mount made with Fusion 360
1
star
40

3d-jewelry-box

A 3D printable jewelry box made with OpenSCAD
OpenSCAD
1
star
41

3d-flosser-holder

A 3D printable flosser holder made with OpenSCAD
OpenSCAD
1
star
42

template110

Rule 110 implemented with C++11 templates and std::array.
C++
1
star
43

wclock

LED Word Clock (Mar 2012)
Eagle
1
star
44

yatumblr-backup

yet another tumblr backup script
Python
1
star