Skip to content

Commit

Permalink
Merge branch 'westphahl-python3'
Browse files Browse the repository at this point in the history
  • Loading branch information
tdicola committed May 7, 2015
2 parents d55fd61 + 23ba591 commit ea023b4
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 63 deletions.
46 changes: 23 additions & 23 deletions Adafruit_I2C.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

# ===========================================================================
# Adafruit_I2C Class
# Adafruit_I2C.py is essentially a fork of the Adafruit Raspberry Pi I2C module.
# Any pull requests for this module should be directed to the following, and I
# Adafruit_I2C.py is essentially a fork of the Adafruit Raspberry Pi I2C module.
# Any pull requests for this module should be directed to the following, and I
# can pull them. I'd rather not deviate from the original:
# https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_I2C
# ===========================================================================
Expand All @@ -28,59 +28,59 @@ def reverseByteOrder(self, data):
return val

def errMsg(self):
print "Error accessing 0x%02X: Check your I2C address" % self.address
print("Error accessing 0x%02X: Check your I2C address" % self.address)
return -1

def write8(self, reg, value):
"Writes an 8-bit value to the specified register/address"
try:
self.bus.write_byte_data(self.address, reg, value)
if self.debug:
print "I2C: Wrote 0x%02X to register 0x%02X" % (value, reg)
except IOError, err:
print("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg))
except IOError as err:
return self.errMsg()

def write16(self, reg, value):
"Writes a 16-bit value to the specified register/address pair"
try:
self.bus.write_word_data(self.address, reg, value)
if self.debug:
print ("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" %
print("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" %
(value, reg, reg+1))
except IOError, err:
except IOError as err:
return self.errMsg()

def writeList(self, reg, list):
"Writes an array of bytes using I2C format"
try:
if self.debug:
print "I2C: Writing list to register 0x%02X:" % reg
print list
print("I2C: Writing list to register 0x%02X:" % reg)
print(list)
self.bus.write_i2c_block_data(self.address, reg, list)
except IOError, err:
except IOError as err:
return self.errMsg()

def readList(self, reg, length):
"Read a list of bytes from the I2C device"
try:
results = self.bus.read_i2c_block_data(self.address, reg, length)
if self.debug:
print ("I2C: Device 0x%02X returned the following from reg 0x%02X" %
print("I2C: Device 0x%02X returned the following from reg 0x%02X" %
(self.address, reg))
print results
print(results)
return results
except IOError, err:
except IOError as err:
return self.errMsg()

def readU8(self, reg):
"Read an unsigned byte from the I2C device"
try:
result = self.bus.read_byte_data(self.address, reg)
if self.debug:
print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
print("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
(self.address, result & 0xFF, reg))
return result
except IOError, err:
except IOError as err:
return self.errMsg()

def readS8(self, reg):
Expand All @@ -89,35 +89,35 @@ def readS8(self, reg):
result = self.bus.read_byte_data(self.address, reg)
if result > 127: result -= 256
if self.debug:
print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
print("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
(self.address, result & 0xFF, reg))
return result
except IOError, err:
except IOError as err:
return self.errMsg()

def readU16(self, reg):
"Reads an unsigned 16-bit value from the I2C device"
try:
result = self.bus.read_word_data(self.address,reg)
if (self.debug):
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
print("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg))
return result
except IOError, err:
except IOError as err:
return self.errMsg()

def readS16(self, reg):
"Reads a signed 16-bit value from the I2C device"
try:
result = self.bus.read_word_data(self.address,reg)
if (self.debug):
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
print("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg))
return result
except IOError, err:
except IOError as err:
return self.errMsg()

if __name__ == '__main__':
try:
bus = Adafruit_I2C(address=0)
print "Default I2C bus is accessible"
print("Default I2C bus is accessible")
except:
print "Error accessing default I2C bus"
print("Error accessing default I2C bus")
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.0.30
---
* Merge Python 3 compatibility fixes from Github user westphahl.
* Moved old Angstrom build fix for missing py_compile from setup.py to separate file.

0.0.20
----
* Fix for SPI not loading spidevX.X correctly based on load order
Expand Down
22 changes: 22 additions & 0 deletions fix_py_compile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python2
# Some Angstrom images are missing the py_compile module; get it if not
# present:
# Fix credit:https://github.com/alexanderhiam/PyBBIO/blob/master/setup.py
import random, os
python_lib_path = random.__file__.split('random')[0]
if not os.path.exists(python_lib_path + 'py_compile.py'):
print "py_compile module missing; installing to %spy_compile.py" %\
python_lib_path
import urllib2
url = "http://hg.python.org/cpython/raw-file/4ebe1ede981e/Lib/py_compile.py"
py_compile = urllib2.urlopen(url)
with open(python_lib_path+'py_compile.py', 'w') as f:
f.write(py_compile.read())
print "testing py_compile..."
try:
import py_compile
print "py_compile installed successfully"
except Exception, e:
print "*py_compile install failed, could not import"
print "*Exception raised:"
raise e
27 changes: 2 additions & 25 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,3 @@
# Some Angstrom images are missing the py_compile module; get it if not
# present:
# Fix credit:https://github.com/alexanderhiam/PyBBIO/blob/master/setup.py
import random, os
python_lib_path = random.__file__.split('random')[0]
if not os.path.exists(python_lib_path + 'py_compile.py'):
print "py_compile module missing; installing to %spy_compile.py" %\
python_lib_path
import urllib2
url = "http://hg.python.org/cpython/raw-file/4ebe1ede981e/Lib/py_compile.py"
py_compile = urllib2.urlopen(url)
with open(python_lib_path+'py_compile.py', 'w') as f:
f.write(py_compile.read())
print "testing py_compile..."
try:
import py_compile
print "py_compile installed successfully"
except Exception, e:
print "*py_compile install failed, could not import"
print "*Exception raised:"
raise e

try:
from overlays import builder
builder.compile()
Expand All @@ -35,15 +13,14 @@
'Operating System :: POSIX :: Linux',
'License :: OSI Approved :: MIT License',
'Intended Audience :: Developers',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Topic :: Software Development',
'Topic :: Home Automation',
'Topic :: System :: Hardware']

setup(name = 'Adafruit_BBIO',
version = '0.0.20',
version = '0.0.30',
author = 'Justin Cooper',
author_email = '[email protected]',
description = 'A module to control BeagleBone IO channels',
Expand All @@ -54,7 +31,7 @@
classifiers = classifiers,
packages = find_packages(),
py_modules = ['Adafruit_I2C'],
ext_modules = [Extension('Adafruit_BBIO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
ext_modules = [Extension('Adafruit_BBIO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
Extension('Adafruit_BBIO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
Extension('Adafruit_BBIO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
Extension('Adafruit_BBIO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
Expand Down
62 changes: 47 additions & 15 deletions source/spimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
#include <sys/ioctl.h>
#include "common.h"

#if PY_MAJOR_VERSION < 3
# define PyLong_AS_LONG(val) PyInt_AS_LONG(val)
# define PyLong_AsLong(val) PyInt_AsLong(val)
# define PyLong_Check(val) PyInt_Check(val)
#endif

PyDoc_STRVAR(SPI_module_doc,
"This module defines an object type that allows SPI transactions\n"
"on hosts running the Linux kernel. The host kernel must have SPI\n"
Expand Down Expand Up @@ -94,7 +100,7 @@ SPI_dealloc(SPI *self)
PyObject *ref = SPI_close(self);
Py_XDECREF(ref);

self->ob_type->tp_free((PyObject *)self);
Py_TYPE(self)->tp_free((PyObject *)self);
}

#define MAXPATH 16
Expand Down Expand Up @@ -131,11 +137,11 @@ SPI_writebytes(SPI *self, PyObject *args)

for (ii = 0; ii < len; ii++) {
PyObject *val = PyList_GET_ITEM(list, ii);
if (!PyInt_Check(val)) {
if (!PyLong_Check(val)) {
PyErr_SetString(PyExc_TypeError, wrmsg);
return NULL;
}
buf[ii] = (__u8)PyInt_AS_LONG(val);
buf[ii] = (__u8)PyLong_AS_LONG(val);
}

status = write(self->fd, &buf[0], len);
Expand Down Expand Up @@ -237,14 +243,14 @@ SPI_xfer(SPI *self, PyObject *args)

for (ii = 0; ii < len; ii++) {
PyObject *val = PyList_GET_ITEM(list, ii);
if (!PyInt_Check(val)) {
if (!PyLong_Check(val)) {
free(txbuf);
free(rxbuf);
free(xferptr);
PyErr_SetString(PyExc_TypeError, wrmsg);
return NULL;
}
txbuf[ii] = (__u8)PyInt_AS_LONG(val);
txbuf[ii] = (__u8)PyLong_AS_LONG(val);
xferptr[ii].tx_buf = (unsigned long)&txbuf[ii];
xferptr[ii].rx_buf = (unsigned long)&rxbuf[ii];
xferptr[ii].len = 1;
Expand Down Expand Up @@ -315,13 +321,13 @@ SPI_xfer2(SPI *self, PyObject *args)

for (ii = 0; ii < len; ii++) {
PyObject *val = PyList_GET_ITEM(list, ii);
if (!PyInt_Check(val)) {
if (!PyLong_Check(val)) {
free(txbuf);
free(rxbuf);
PyErr_SetString(PyExc_TypeError, msg);
return NULL;
}
txbuf[ii] = (__u8)PyInt_AS_LONG(val);
txbuf[ii] = (__u8)PyLong_AS_LONG(val);
}

xfer.tx_buf = (unsigned long)txbuf;
Expand Down Expand Up @@ -447,13 +453,13 @@ SPI_set_mode(SPI *self, PyObject *val, void *closure)
"Cannot delete attribute");
return -1;
}
else if (!PyInt_Check(val)) {
else if (!PyLong_Check(val)) {
PyErr_SetString(PyExc_TypeError,
"The mode attribute must be an integer");
return -1;
}

mode = PyInt_AsLong(val);
mode = PyLong_AsLong(val);

if ( mode > 3 ) {
PyErr_SetString(PyExc_TypeError,
Expand Down Expand Up @@ -601,13 +607,13 @@ SPI_set_bpw(SPI *self, PyObject *val, void *closure)
"Cannot delete attribute");
return -1;
}
else if (!PyInt_Check(val)) {
else if (!PyLong_Check(val)) {
PyErr_SetString(PyExc_TypeError,
"The bpw attribute must be an integer");
return -1;
}

bits = PyInt_AsLong(val);
bits = PyLong_AsLong(val);

if (bits < 8 || bits > 16) {
PyErr_SetString(PyExc_TypeError,
Expand Down Expand Up @@ -642,13 +648,13 @@ SPI_set_msh(SPI *self, PyObject *val, void *closure)
"Cannot delete attribute");
return -1;
}
else if (!PyInt_Check(val)) {
else if (!PyLong_Check(val)) {
PyErr_SetString(PyExc_TypeError,
"The msh attribute must be an integer");
return -1;
}

msh = PyInt_AsLong(val);
msh = PyLong_AsLong(val);
// DAW - 8/12/12 - removed limitation on SPI speed
// if (8000000 < msh) {
// PyErr_SetString(PyExc_TypeError,
Expand Down Expand Up @@ -793,8 +799,7 @@ static PyMethodDef SPI_methods[] = {
};

static PyTypeObject SPI_type = {
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
PyVarObject_HEAD_INIT(NULL, 0)
"SPI", /* tp_name */
sizeof(SPI), /* tp_basicsize */
0, /* tp_itemsize */
Expand Down Expand Up @@ -838,20 +843,47 @@ static PyMethodDef SPI_module_methods[] = {
{NULL}
};

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"SPI", /* m_name */
SPI_module_doc, /* m_doc */
-1, /* m_size */
SPI_module_methods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};
#endif

#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
#if PY_MAJOR_VERSION >= 3
PyInit_SPI(void)
#else
initSPI(void)
#endif
{
PyObject* m;

if (PyType_Ready(&SPI_type) < 0)
return;

#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&moduledef);
#else
m = Py_InitModule3("SPI", SPI_module_methods, SPI_module_doc);
#endif

Py_INCREF(&SPI_type);
PyModule_AddObject(m, "SPI", (PyObject *)&SPI_type);

#if PY_MAJOR_VERSION >= 3
return m;
#endif
}


0 comments on commit ea023b4

Please sign in to comment.