Package: pifaceio
PIFACEIOThis package provides a Python interface to thePiFace Digitalperipheral I/O board for theRaspberry Pi.
APiFace Digitalboard offers 8 digital inputs and 8
digital outputs. This package allows a Python program to read the inputs
and write the outputs on the board via the Raspberry Pi SPI bus.The newerPiFace Digital 2board is exactly compatible
with the original board and so is also supported by this package.MultiplePiFace Digitalboards are supported, on either
or both of the RPi SPI bus chip selects. This pifaceio package is
focussed on simplicity and performance for polled implementations and is
an alternative to the officialpifacedigitalioPython package for
thePiFace Digitalboard. In my simple benchmark of
polled reads and writes (see next
section), pifaceio
performs an order of magnitude faster, and with much less overhead thanpifacedigitalio.Interrupts are not supported. Seepifacedigitaliofor interrupt and
other functionality.The pifaceio package is implemented in pure Python code using only the
Python standard library, uses no external 3rd party packages, and is
compatible with Python version 3.6 and later.Performance Benchmarks Against pifacedigitalioA smallbenchmarkprogram is included in the
repository. It produces the following sample of simple polled read and
write performance comparisons betweenpifacedigitalioandpifaceioon a Raspberry Pi2B.FunctionpifacedigitaliopifaceioSpeedupInput reads per second174016317x 9.4Output writes per second88411590x 13.1Input reads per second (classic API)177311128x 6.3Output reads per second (classic API)8899140x 10.3INSTALLATIONThepifaceio pypi packageis available fromPyPiso you
can install it usingpip. Ifpipis not already installed run:sudoapt-getinstallpython3-pipThen use pip to install thepifaceiopackage:sudopip3install-UpifaceioTo set up permissions/groups/udev etc for spidev device on RPi, run the
included script and then reboot.sudopifaceio-install-spidev.shUPGRADEsudopip3install-UpifaceioUSAGEBoard addresses, input pins, and output pins are always numbered from 0.In general, you start with a once-off allocation of a PiFace board
instance at startup with:pf=pifaceio.PiFace()Default is first PiFace board (0). Optionally takes an argument 0 to 7
for up to 8 PiFace board addresses. Create multiple PiFace() instances
if you want to use multiple boards in parallel.There are also other (rarely needed) options to disable the input pull
up resistors, and to invert the input and output bit polarities. See
pifaceio.py for details.At each poll time, e.g. every part second, read all the inputs (i.e. the
single input byte) with:pf.read()# returns the input byte you can use directly if you preferThen read and write individual pins according to your logic with:in_val=pf.read_pin(pin_in)# ..pf.write_pin(pin_out,out_val)# ..Finally, write all the outputs at the end of processing (i.e. write the
single output byte) with:pf.write()# optionally, takes an output byte to write directlyNote thatread_pin()is just a convenience method wrapping a bit
test around the previously read input byte fromread()andwrite_pin()is just a convenience method wrapping a bit set/clear
around the output byte pending it being written bywrite(). You don't
have to useread_pin()orwrite_pin()if you just want to read,
test/manipulate, and write the 8 bit input and/or output byte directly.
In that case you would just useread(), andwrite()only in your
application.EXAMPLESSimple example to just reflect all PiFace 8 inputs to the 8 outputs
every 10 msec, on the default first PiFace board:importpifaceio,timepf=pifaceio.PiFace()whileTrue:pf.write(pf.read())time.sleep(.01)Same example, but do it across 4 PiFace boards:importpifaceio,timepifaces=[pifaceio.PiFace(n)forninrange(4)]whileTrue:forpfinpifaces:pf.write(pf.read())time.sleep(.01)Simple example to test if both input pin 0 and 1 are on at same time,
and then set output pin 7 if true:importpifaceiopf=pifaceio.PiFace()...# Fetch inputs (i.e. single byte)pf.read()first_two_inputs_on=pf.read_pin(0)andpf.read_pin(1)# Now write that state to output pin 7pf.write_pin(7,first_two_inputs_on)# Do final (actual) write when all output pin states are set.pf.write()Simulated "interrupt" processing example by light-weight poll every 10 msecs:importpifaceio,timepf=pifaceio.PiFace()defprocess_change():'On any changed inputs, read inputs and write outputs'pf.write_pin(7,pf.read_pin(0)andpf.read_pin(1))# .. etc .. do logic using pf.read_pin() and pf.write_pin()# Loop forever polling inputs ..last=NonewhileTrue:data=pf.read()# Do processing only on changeiflast!=data:last=dataprocess_change()pf.write()# note write() only writes if output changestime.sleep(.01)PIFACE PACKAGE BACKWARDS COMPATIBILITYAs an alternative API, pifaceio also implements the "classic" API from
thepifacedigitalioand it's predecessorpifacepackages. The
classic API will work compatibly, but performance is slightly degraded
compared to reading and writing the single input and output bytes using
the canonical new and preferred pifaceio API described above. However,
performance is still significantly superior to the originalpifacedigitalioandpifacepackages as shown in the aboveperformance
comparisonsection.#import piface.pfio as pf (change this line ..)#import pifacedigitalio as pf (or this line .., to the following line)importpifaceioaspf# The following calls should be approximately compatible:pf.init()value=pf.digital_read(pin)pf.digital_write(pin,value)pf.deinit()You can also use multiple boards with this compatibility interface, e.g.
as follows where board can be from 0 to 7.value=pf.digital_read(pin,board)pf.digital_write(pin,value,board)LICENSECopyright (C) 2013 Mark Blakeney. This program is distributed under the
terms of the GNU General Public License.
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 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 athttp://www.gnu.org/licenses/for more details.