Python Library – spidev
Python spidev library
Informatie (ENG):
Python bindings for Linux SPI access through spidev
Python Spidev
=============
This project contains a python module for interfacing with SPI devices from user space via the spidev linux kernel driver.
This is a modified version of the code originally found [here](http://elk.informatik.fh-augsburg.de/da/da-49/trees/pyap7k/lang/py-spi)
All code is GPLv2 licensed unless explicitly stated otherwise.
Usage
1 2 3 4 5 6 7 |
```python import spidev spi = spidev.SpiDev() spi.open(bus, device) to_send = [0x01, 0x02, 0x03] spi.xfer(to_send) ``` |
Settings
1 2 3 4 5 6 7 8 9 10 |
```python import spidev spi = spidev.SpiDev() spi.open(bus, device) # Settings (for example) spi.max_speed_hz = 5000 spi.mode = 0b01 ... ``` |
* bits_per_word
* cshigh
* loop
* lsbfirst
* max_speed_hz
* mode
– SPI mode as two bit pattern of clock polarity and phase [CPOL|CPHA], min: 0b00 = 0, max: 0b11 = 3
* threewire
– SI/SO signals shared
Methods
——-
open(bus, device) / Connects to the specified SPI device, opening /dev/spidev-bus.device
readbytes(n) / Read n bytes from SPI device.
writebytes(list of values) / Writes a list of values to SPI device.
xfer(list of values[, speed_hz, delay_usec, bits_per_word])
Performs an SPI transaction. Chip-select should be released and reactivated between blocks.
Delay specifies the delay in usec between blocks.
xfer2(list of values[, speed_hz, delay_usec, bits_per_word])
Performs an SPI transaction. Chip-select should be held active between blocks.
close()
Disconnects from the SPI device.
Installatie via Source (TAR.GZ)
Eerst heb je de python DEV dependancies nodig, installeer deze met het commando:
sudo apt-get install python-dev
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: libexpat1-dev libpython-dev libpython2.7-dev python2.7-dev The following NEW packages will be installed: libexpat1-dev libpython-dev libpython2.7-dev python-dev python2.7-dev 0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded. Need to get 18.3 MB of archives. After this operation, 26.3 MB of additional disk space will be used. Do you want to continue? [Y/n] Y Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libexpat1-dev arm hf 2.1.0-6+deb8u1 [114 kB] Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main libpython2.7-dev armhf 2.7.9-2 [17.9 MB] Get:3 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-dev armhf 2.7.9-1 [1,188 B] Get:4 http://mirrordirector.raspbian.org/raspbian/ jessie/main libpython-dev armhf 2.7.9-1 [19.6 kB] Get:5 http://mirrordirector.raspbian.org/raspbian/ jessie/main python2.7-dev armhf 2.7.9-2 [281 kB] Fetched 18.3 MB in 3s (5,672 kB/s) Selecting previously unselected package libexpat1-dev:armhf. (Reading database ... 30300 files and directories currently installed.) Preparing to unpack .../libexpat1-dev_2.1.0-6+deb8u1_armhf.deb ... Unpacking libexpat1-dev:armhf (2.1.0-6+deb8u1) ... Selecting previously unselected package libpython2.7-dev:armhf. Preparing to unpack .../libpython2.7-dev_2.7.9-2_armhf.deb ... Unpacking libpython2.7-dev:armhf (2.7.9-2) ... Selecting previously unselected package libpython-dev:armhf. Preparing to unpack .../libpython-dev_2.7.9-1_armhf.deb ... Unpacking libpython-dev:armhf (2.7.9-1) ... Selecting previously unselected package python2.7-dev. Preparing to unpack .../python2.7-dev_2.7.9-2_armhf.deb ... Unpacking python2.7-dev (2.7.9-2) ... Selecting previously unselected package python-dev. Preparing to unpack .../python-dev_2.7.9-1_armhf.deb ... Unpacking python-dev (2.7.9-1) ... Processing triggers for man-db (2.7.0.2-5) ... Setting up libexpat1-dev:armhf (2.1.0-6+deb8u1) ... Setting up libpython2.7-dev:armhf (2.7.9-2) ... Setting up libpython-dev:armhf (2.7.9-1) ... Setting up python2.7-dev (2.7.9-2) ... Setting up python-dev (2.7.9-1) ... |
Installeer de module met de volgende commando’s:
wget https://pypi.python.org/packages/source/s/spidev/spidev-3.1.tar.gz
1 2 3 4 5 6 7 8 9 10 |
--2016-03-06 17:42:15-- https://pypi.python.org/packages/source/s/spidev/spidev-3.1.tar.gz Resolving pypi.python.org (pypi.python.org)... 23.235.43.223 Connecting to pypi.python.org (pypi.python.org)|23.235.43.223|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 12583 (12K) [application/octet-stream] Saving to: ‘spidev-3.1.tar.gz’ spidev-3.1.tar.gz 100%[========================================================>] 12.29K --.-KB/s in 0s 2016-03-06 17:42:15 (61.7 MB/s) - ‘spidev-3.1.tar.gz’ saved [12583/12583] |
tar -zxvf spidev-3.1.tar.gz
1 2 3 4 5 6 7 |
spidev-3.1/ spidev-3.1/README.md spidev-3.1/CHANGELOG.md spidev-3.1/setup.py spidev-3.1/spidev_module.c spidev-3.1/PKG-INFO spidev-3.1/LICENSE.md |
cd spidev-3.1
sudo python setup.py install
1 2 3 4 5 6 7 8 9 10 11 12 13 |
running install running build running build_ext building 'spidev' extension creating build creating build/temp.linux-armv7l-2.7 arm-linux-gnueabihf-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python2.7 -c spidev_module.c -o build/temp.linux-armv7l-2.7/spidev_module.o creating build/lib.linux-armv7l-2.7 arm-linux-gnueabihf-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wl,-z,relro -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-armv7l-2.7/spidev_module.o -o build/lib.linux-armv7l-2.7/spidev.so running install_lib copying build/lib.linux-armv7l-2.7/spidev.so -> /usr/local/lib/python2.7/dist-packages running install_egg_info Writing /usr/local/lib/python2.7/dist-packages/spidev-3.1.egg-info |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
3.0.1 ===== * Fixed README.md and CHANGELOG.md formatting, hopefully 3.0 === * Memset fix recommended by Dougie Lawson * Fixes for Kernel 3.15+ from https://github.com/chrillomat/py-spidev * Fixes for Python 3/2 compatibility. * Added subclassing support - https://github.com/doceme/py-spidev/issues/10 2.0 === Code sourced from http://elk.informatik.fh-augsburg.de/da/da-49/trees/pyap7k/lang/py-spi and modified. Pre 2.0 ======= spimodule.c originally uathored by Volker Thoms, 2009. |
Download spidev @ pypi.python.org
[#/python/libraries/spidev” ]