From bf76fdafeefabd357bc47956cac090d88e71390a Mon Sep 17 00:00:00 2001 From: Tom Egan Date: Tue, 11 Feb 2020 14:20:28 -0500 Subject: [PATCH 1/4] Fix up for Python 3.x Two issues needed fixed. First spidev is not buffered so we need to open it as an unbuffered binary file. Second ctypes.string_at returns a bytes object instead of a str in python 3.x so we don't want to convert a str to an array of integers when returning from read and transfer, since we already have arrays of ints. --- spi.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/spi.py b/spi.py index 0463d80..38ca84c 100644 --- a/spi.py +++ b/spi.py @@ -29,6 +29,7 @@ import fcntl import array import os.path +import sys def _ioc(direction, number, structure): @@ -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): """ @@ -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 @@ -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 @@ -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)) From 3f964e35fe360ebaf102dc065eedb256b3cab7dd Mon Sep 17 00:00:00 2001 From: Tom Egan Date: Tue, 18 Feb 2020 10:01:52 -0500 Subject: [PATCH 2/4] bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index c7d7098..6e93d6e 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from distutils.core import setup setup(name="spi", - version="0.2.0", + version="0.2.1", description="Pure Python SPI Interface using spidev", long_description="Pure Python SPI interface using spidev", author="Tom Stokes", From 66b2325bcd8e3862f3886d32352b7a64908d48b3 Mon Sep 17 00:00:00 2001 From: "T.K.Egan" Date: Tue, 25 Feb 2020 11:04:09 -0500 Subject: [PATCH 3/4] Update Fork for independant publication to PyPi --- README.md | 6 ++---- setup.py | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 3446301..635f8d8 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,8 @@ # python-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 diff --git a/setup.py b/setup.py index 6e93d6e..121d777 100644 --- a/setup.py +++ b/setup.py @@ -1,13 +1,18 @@ #!/usr/bin/env python +import setuptools from distutils.core import setup -setup(name="spi", - version="0.2.1", - description="Pure Python SPI Interface using spidev", - long_description="Pure Python SPI interface using spidev", - author="Tom Stokes", - author_email="tomstokes@gmail.com", - url="http://github.com/tomstokes/python-spi", +with open("README.md", "r") as f: + long_description = f.read() + +setup(name="python3-spi", + version="0.3.0", + description="Python interface for SPI communications", + long_description=long_description, + long_description_content_type="text/markdown", + author="Tom Stokes, Tom Egan", + author_email="tom@tomegan.tech", + url="https://github.com/tkegan/python-spi", license="MIT", py_modules=['spi']) From 6c45998ad792be773dff63737f892c7f81642807 Mon Sep 17 00:00:00 2001 From: Tom Egan Date: Wed, 26 Feb 2020 09:31:54 -0500 Subject: [PATCH 4/4] Update Metadata to better describe project on PyPi and to pip --- README.md | 4 ++-- setup.py | 28 +++++++++++++++++++--------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 635f8d8..387cac9 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# python-spi +# python3-spi Python 3.x interface for SPI communications using Linux spidev @@ -19,7 +19,7 @@ This is a fork of https://github.com/tomstokes/python-spi which seems to be aban - 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 diff --git a/setup.py b/setup.py index 121d777..3f0e52b 100644 --- a/setup.py +++ b/setup.py @@ -7,12 +7,22 @@ long_description = f.read() setup(name="python3-spi", - version="0.3.0", - description="Python interface for SPI communications", - long_description=long_description, - long_description_content_type="text/markdown", - author="Tom Stokes, Tom Egan", - author_email="tom@tomegan.tech", - url="https://github.com/tkegan/python-spi", - license="MIT", - py_modules=['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="tom@tomegan.tech", + 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'])