From b239b07a961d62273d7bcab2ced08835dc528522 Mon Sep 17 00:00:00 2001 From: Thomas Preston Date: Fri, 14 Jun 2013 11:10:26 +0100 Subject: [PATCH] reworked structure, updated install and readme, I think this is ready to go --- README.md | 21 ++--- {scripts => bin}/spidev-setup.sh | 0 {scripts => bin}/unblacklist-spi-bcm2708.sh | 0 install.sh | 15 +--- pifacecommon/__init__.py | 86 +++++++++++++++++++ .../asm_generic_ioctl.py | 0 pifacecommon.py => pifacecommon/core.py | 21 +++-- setup.py | 35 ++++---- 8 files changed, 129 insertions(+), 49 deletions(-) rename {scripts => bin}/spidev-setup.sh (100%) rename {scripts => bin}/unblacklist-spi-bcm2708.sh (100%) create mode 100644 pifacecommon/__init__.py rename asm_generic_ioctl.py => pifacecommon/asm_generic_ioctl.py (100%) rename pifacecommon.py => pifacecommon/core.py (97%) diff --git a/README.md b/README.md index 0ed4faa..e0f3d6d 100644 --- a/README.md +++ b/README.md @@ -27,16 +27,17 @@ After a reboot the /dev/spidev* devices should appear but they require special privileges to access them. You'll need to add a udev rule (udev monitors and configures devices) and set up groups by running `spidev-setup.sh'. -### 2. Building and installing the distribution egg -You'll need to install the Python 3 setup tools. +### 2. Building and installing +Python automatically builds the source, generates the egg file and installs. - $ sudo aptitude install python3-setuptools + $ sudo python3 setup.py install -A Python egg can be distributed on it's own but for the time being we'll build -one from source and install it using easy_install3. +Examples +======== - $ python3 setup.py bdist_egg - $ easy_install3 dist/pifacecommon-1.0-py3.2.egg - -Alternatively we could have just run `python3 setup.py install' but I'm hoping -to distribute the eggs on their own (in a .deb) soon. \ No newline at end of file + $ python3 + >>> import pifacecommon + >>> pifacecommon.init() + >>> pifacecommon.write(0xAA, pifacecommon.GPIOA) + >>> pifacecommon.read(pifacecommon.GPIOA) + 0xAA diff --git a/scripts/spidev-setup.sh b/bin/spidev-setup.sh similarity index 100% rename from scripts/spidev-setup.sh rename to bin/spidev-setup.sh diff --git a/scripts/unblacklist-spi-bcm2708.sh b/bin/unblacklist-spi-bcm2708.sh similarity index 100% rename from scripts/unblacklist-spi-bcm2708.sh rename to bin/unblacklist-spi-bcm2708.sh diff --git a/install.sh b/install.sh index d11ef88..b3ec943 100755 --- a/install.sh +++ b/install.sh @@ -9,7 +9,7 @@ then fi # unblacklist spi -./scripts/unblacklist-spi-bcm2708.sh +./bin/unblacklist-spi-bcm2708.sh if [ $? -ne 0 ] then printf "Failed to unblacklist spi-bcm2708.\nExiting...\n" @@ -17,7 +17,7 @@ then fi # set up spidev permissions -./scripts/spidev-setup.sh +./bin/spidev-setup.sh if [ $? -ne 0 ] then printf "Failed to setup spidev.\nExiting...\n" @@ -25,15 +25,6 @@ then fi # install python library - -# install python3 setup tools -#aptitude install python3-setuptools -apt-get install -y python3-setuptools # apt-get is slightly faster - -printf "Building egg...\n" -python3 setup.py bdist_egg - -# tp - ultimately I want to just distribute the egg printf "Installing pifacecommon...\n" -easy_install3 dist/pifacecommon-1.0-py3.2.egg +python3 setup.py install printf "Done!\n" diff --git a/pifacecommon/__init__.py b/pifacecommon/__init__.py new file mode 100644 index 0000000..8aab192 --- /dev/null +++ b/pifacecommon/__init__.py @@ -0,0 +1,86 @@ +# -*- coding: utf-8 -*- +"""Provides common I/O methods for interfacing with PiFace Products +Copyright (C) 2013 Thomas Preston + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +""" +# constants +from .core import ( + MAX_BOARDS, + IODIRA, + IODIRB, + IPOLA, + IPOLB, + GPINTENA, + GPINTENB, + DEFVALA, + DEFVALB, + INTCONA, + INTCONB, + IOCON, + GPPUA, + GPPUB, + INTFA, + INTFB, + INTCAPA, + INTCAPB, + GPIOA, + GPIOB, + BANK_OFF, + BANK_ON, + INT_MIRROR_ON, + INT_MIRROR_OFF, + SEQOP_OFF, + SEQOP_ON, + DISSLW_ON, + DISSLW_OFF, + HAEN_ON, + HAEN_OFF, + ODR_ON, + ODR_OFF, + INTPOL_HIGH, + INTPOL_LOW, + IN_EVENT_DIR_ON, + IN_EVENT_DIR_OFF, + IN_EVENT_DIR_BOTH, +) + +# classes +from .core import ( + DigitalPort, + DigitalInputPort, + DigitalOutputPort, + DigitalItem, + DigitalInputItem, + DigitalOutputItem, + InputFunctionMap, +) + +# functions +from .core import ( + init, + deinit, + get_bit_mask, + get_bit_num, + read_bit, + write_bit, + read, + write, + spisend, + sleep_microseconds, + wait_for_interrupt, + clear_interrupts, + enable_interrupts, + disable_interrupts, +) diff --git a/asm_generic_ioctl.py b/pifacecommon/asm_generic_ioctl.py similarity index 100% rename from asm_generic_ioctl.py rename to pifacecommon/asm_generic_ioctl.py diff --git a/pifacecommon.py b/pifacecommon/core.py similarity index 97% rename from pifacecommon.py rename to pifacecommon/core.py index d26c1c0..92cd166 100644 --- a/pifacecommon.py +++ b/pifacecommon/core.py @@ -23,7 +23,7 @@ import time from abc import ABCMeta from fcntl import ioctl -from asm_generic_ioctl import _IOW +from .asm_generic_ioctl import _IOW # spi stuff requires Python 3 assert sys.version_info.major >= 3, \ @@ -365,7 +365,7 @@ def spisend(bytes_to_send): len=ctypes.sizeof(wbuffer)) # send the spi command (with a little help from asm-generic - iomsg = _IOW(SPI_IOC_MAGIC, 0, ctypes.c_char*ctypes.sizeof(transfer)) + iomsg = _IOW(SPI_IOC_MAGIC, 0, ctypes.c_char * ctypes.sizeof(transfer)) ioctl(spidev_fd, iomsg, ctypes.addressof(transfer)) return ctypes.string_at(rbuffer, ctypes.sizeof(rbuffer)) @@ -436,14 +436,15 @@ def _call_mapped_input_functions(port, input_func_map): Returns whether the wait_for_input function should keep waiting for input """ if port == GPIOA: - intflag = INTFA + intflag = INTFA intcapture = INTCAPA else: intflag = INTFB intcapture = INTCAPB for board_i in range(MAX_BOARDS): - this_board_ifm = [m for m in input_func_map if m['board_num'] == board_i] + this_board_ifm = \ + [m for m in input_func_map if m['board_num'] == board_i] # read the interrupt status of this PiFace board # interrupt bit (int_bit) - bit map showing what caused the interrupt @@ -452,7 +453,7 @@ def _call_mapped_input_functions(port, input_func_map): if int_flag_bit == 0: continue # The interrupt has not been flagged on this board int_bit_num = get_bit_num(int_flag_bit) - + # interrupt byte (int_byte) - snapshot of in port when int occured int_byte = read(intcapture, board_i) @@ -498,7 +499,7 @@ def enable_interrupts(port): _set_gpio_interrupt_edge() except Timeout as e: raise InterruptEnableException( - "There was an error bringing gpio%d into userspace. %s" % \ + "There was an error bringing gpio%d into userspace. %s" % (GPIO_INTERRUPT_PIN, e.message) ) @@ -506,7 +507,8 @@ def enable_interrupts(port): def _bring_gpio_interrupt_into_userspace(): try: # is it already there? - with open(GPIO_INTERRUPT_DEVICE_VALUE): return + with open(GPIO_INTERRUPT_DEVICE_VALUE): + return except IOError: # no, bring it into userspace with open(GPIO_EXPORT_FILE, 'w') as export_file: @@ -533,10 +535,11 @@ def _wait_until_file_exists(filename): time_limit = start_time + FILE_IO_TIMEOUT while time.time() < time_limit: try: - with open(filename): return + with open(filename): + return except IOError: pass - + raise Timeout("Waiting too long for %s." % filename) diff --git a/setup.py b/setup.py index ef448b8..44a0335 100644 --- a/setup.py +++ b/setup.py @@ -9,21 +9,20 @@ author='Thomas Preston', author_email='thomasmarkpreston@gmail.com', url='http://pi.cs.man.ac.uk/interface.htm', - py_modules=['pifacecommon', 'asm_generic_ioctl'], - long_description="""\ - pifacecommon provides common classes, functions and variables to various - piface related modules. - """, - classifiers=[ - "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)", - "Programming Language :: Python", - "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "Topic :: Software Development :: Libraries :: Python Modules", - ], - keywords='piface raspberrypi openlx', - license='GPLv3+', - install_requires=[ - 'setuptools', - ], -) \ No newline at end of file + packages=['pifacecommon'], + long_description="pifacecommon provides common classes, functions and" + "variables to various piface related modules.", + classifiers=[ + "License :: OSI Approved :: GNU Affero General Public License v3 or " + "later (AGPLv3+)", + "Programming Language :: Python", + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "Topic :: Software Development :: Libraries :: Python Modules", + ], + keywords='piface raspberrypi openlx', + license='GPLv3+', + install_requires=[ + 'setuptools', + ], +)