Not Logged In

pifaceio 1.22

This package provides a Python interface to the [PiFaceDigital][pifaceboard] peripheral I/O board for the [Raspberry Pi][rpi].A [PiFace Digital][pifaceboard] board offers 8 digital inputs and 8digital outputs. This package allows a Python program to read the inputsand write the outputs on the board via the Raspberry Pi SPI bus.

Multiple [PiFace Digital][pifaceboard] boards are supported.This pifaceio package is focussed on simplicity and performance forpolled implementations and is an alternative to the [pifacedigitalio][]and [piface][] (now depreciated) Python packages for the [PiFaceDigital][pifaceboard] board.

Interrupts are not supported. See [pifacedigitalio][] for interrupt andother functionality.

The pifaceio package is implemented in pure Python code using only thePython standard library, uses no external 3rd party packages, and iscompatible with both Python versions 2 and 3.

### INSTALLATION

Install necessary packages on your Raspberry Pi for build etc:

sudo apt-get install git python-setuptools

Get this package:

git clone http://github.com/bulletmark/pifaceio

Install (can alternately do this as ordinary user in a virtualenvof course):

sudo python ./setup.py install

Set up permissions, groups, udev etc for spidev device on RPi:

sudo ./install-spidev.sh

Note that the [pifaceio pypi package][pifaceio] is also available from[PyPi][pypi] so alternatively you can install it using [pip][].

In general, you start with a once-off allocation of a PiFace boardinstance at startup with:

pf = pifaceio.PiFace()

Default is first PiFace board (0). Optionally takes an argument 0 to 7for up to 8 PiFace board addresses. Create multiple PiFace() instancesif you want to use multiple boards in parallel.

There are also other (rarely needed) options to disable the input pullup resistors, and to invert the input and output bit polarities. Seepifaceio.py for details.

At each poll time, e.g. every part second, read all the inputs (i.e. thesingle input byte) with:

pf.read() # returns the input byte you can use directly if you prefer

Then 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 thesingle output byte) with:

pf.write() # optionally, takes an output byte to write directly

Note that `read_pin()` is just a convenience method wrapping a bittest around the previously read input byte from `read()` and`write_pin()` is just a convenience method wrapping a bit set/cleararound the output byte pending it being written by `write()`. You don'thave to use `read_pin()` or `write_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 use `read()`, and `write()` only in yourapplication.

### EXAMPLES

Simple example to just reflect all PiFace 8 inputs to the 8 outputsevery 10 msec, on the default first PiFace board:

import pifaceio, time pf = pifaceio.PiFace()

while True: pf.write(pf.read()) time.sleep(.01)

Same example, but do it across 4 PiFace boards:

import pifaceio, time pifaces = [pifaceio.PiFace(n) for n in range(4)]

while True: for pf in pifaces: 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:

# Do processing only on change if last != data: last = data process_change() pf.write() # note write() only writes if output changes

time.sleep(.01)

### PIFACE PACKAGE BACKWARDS COMPATIBILITY

The following [piface][] API will work compatibly, but performance isslightly degraded compared to reading and writing the single input andoutput bytes using the canonical new pifaceio API described above.

#import piface.pfio as pf (change this to next line) import pifaceio as pf

# 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.

Copyright (C) 2013 Mark Blakeney. This program is distributed under theterms of the GNU General Public License.This program is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or any laterversion.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU GeneralPublic License at <http: www.gnu.org="" licenses=""/> for more details.