Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# python-spi
# python3-spi

**Note:** python-spi is still under active development
Python 3.x interface for SPI communications using Linux spidev

## Overview

A pure Python SPI interface using the Linux spidev device
This is a fork of https://github.com/tomstokes/python-spi which seems to be abandoned with the python 3 compatibility patch proposed by Tom Egan applied.

## Features

Expand All @@ -21,7 +19,7 @@ A pure Python SPI interface using the Linux spidev device
- As a last resort, running the python script as root should allow access to the spidev. **Note** This is not recommended. Use the 'spi' group or udev rules whenever possible.

## Example
```python
```python3
import spi
spi = SPI("/dev/spidev1.0")
spi.mode = SPI.MODE_0
Expand Down
33 changes: 24 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
#!/usr/bin/env python

import setuptools
from distutils.core import setup

setup(name="spi",
version="0.2.0",
description="Pure Python SPI Interface using spidev",
long_description="Pure Python SPI interface using spidev",
author="Tom Stokes",
author_email="[email protected]",
url="http://github.com/tomstokes/python-spi",
license="MIT",
py_modules=['spi'])
with open("README.md", "r") as f:
long_description = f.read()

setup(name="python3-spi",
version="0.3.1",
description="Python interface for SPI communications",
long_description=long_description,
long_description_content_type="text/markdown",
author="Tom Stokes, Tom Egan",
author_email="[email protected]",
url="https://github.com/tkegan/python-spi",
license="MIT",
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Operating System :: POSIX :: Linux',
'Topic :: Software Development :: Embedded Systems',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
],
python_requires='>=2.7, !=3.0.*, <4',
py_modules=['spi'])
21 changes: 18 additions & 3 deletions spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import fcntl
import array
import os.path
import sys


def _ioc(direction, number, structure):
Expand Down Expand Up @@ -58,6 +59,20 @@ def _ioc(direction, number, structure):

return direction, op, structure

def bytes2bytes(value):
"""
Convert strings to arrays of integers leaving bytes values alone

Args:
value: a bytes object though on Python < 3.0 that's a string

Returns: An array of integers python <= 2.7 or a bytes object python >= 3.0
"""
if sys.version_info >= (3, 0):
return value
else:
return [ord(byte) for byte in value]


class SPI(object):
"""
Expand Down Expand Up @@ -134,7 +149,7 @@ def __init__(self, device, speed=None, bits_per_word=None, phase=None,
if not os.path.exists(device):
raise IOError("{} does not exist".format(device))

self.handle = open(device, "w+")
self.handle = open(device, "w+b", buffering=0)

if speed is not None:
self.speed = speed
Expand Down Expand Up @@ -404,7 +419,7 @@ def read(self, length, speed=0, bits_per_word=0, delay=0):
length, speed, delay, bits_per_word, 0,
0, 0, 0)
fcntl.ioctl(self.handle, SPI._IOC_MESSAGE, spi_ioc_transfer)
return [ord(byte) for byte in ctypes.string_at(receive_buffer, length)]
return bytes2bytes(ctypes.string_at(receive_buffer, length))

def transfer(self, data, speed=0, bits_per_word=0, delay=0):
"""Perform full-duplex SPI transfer
Expand All @@ -431,4 +446,4 @@ def transfer(self, data, speed=0, bits_per_word=0, delay=0):
length, speed, delay, bits_per_word, 0,
0, 0, 0)
fcntl.ioctl(self.handle, SPI._IOC_MESSAGE, spi_ioc_transfer)
return [ord(byte) for byte in ctypes.string_at(receive_buffer, length)]
return bytes2bytes(ctypes.string_at(receive_buffer, length))