Skip to content

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

License

Notifications You must be signed in to change notification settings

vsergeev/python-periphery

Repository files navigation

python-periphery Tests StatusDocs StatusGitHub releaseLicense

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

fromperipheryimportGPIO# Open GPIO /dev/gpiochip0 line 10 with input directiongpio_in=GPIO("/dev/gpiochip0", 10, "in") # Open GPIO /dev/gpiochip0 line 12 with output directiongpio_out=GPIO("/dev/gpiochip0", 12, "out") value=gpio_in.read() gpio_out.write(notvalue) gpio_in.close() gpio_out.close()

Go to GPIO documentation.

LED

fromperipheryimportLED# Open LED "led0" with initial state offled0=LED("led0", False) # Open LED "led1" with initial state onled1=LED("led1", True) value=led0.read() led1.write(value) # Set custom brightness levelled1.write(led1.max_brightness/2) led0.close() led1.close()

Go to LED documentation.

PWM

fromperipheryimportPWM# Open PWM chip 0, channel 10pwm=PWM(0, 10) # Set frequency to 1 kHzpwm.frequency=1e3# Set duty cycle to 75%pwm.duty_cycle=0.75pwm.enable() # Change duty cycle to 50%pwm.duty_cycle=0.50pwm.close()

Go to PWM documentation.

SPI

fromperipheryimportSPI# Open spidev1.0 with mode 0 and max speed 1MHzspi=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

fromperipheryimportI2C# Open i2c-0 controlleri2c=I2C("/dev/i2c-0") # Read byte at address 0x100 of EEPROM at 0x50msgs= [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

fromperipheryimportMMIO# Open am335x real-time clock subsystem pagertc_mmio=MMIO(0x44E3E000, 0x1000) # Read current timertc_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 pagectrl_mmio=MMIO(0x44E10000, 0x1000) # Read MAC addressmac_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

fromperipheryimportSerial# Open /dev/ttyUSB0 with baudrate 115200, and defaults of 8N1, no flow controlserial=Serial("/dev/ttyUSB0", 115200) serial.write(b"Hello World!") # Read up to 128 bytes with 500ms timeoutbuf=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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 8

Languages