Hardware Reference
In-Depth Information
Initialisation
After the function deinitions come the statements that set things up for the main part of
the program. he IR LED emitters are controlled by the outputs of PiFace Digital, so they
need to be turned on. his is done with the
digital_write
function that sets a pin to
value 1, that is, turn on.
INNER_BEAM
and
OUTER_BEAM
specify the pin numbers the IR
LEDs are connected to:
#turn on IR LED emitters
p.digital_write(INNER_BEAM,1)
p.digital_write(OUTER_BEAM,1)
Working with Files
Before a Python program (and programs written in most other languages) can write data to a
ile it needs to open it irst. Python does this with the
open
function, which takes a ilename,
mode and bufer length as arguments and returns a ile object. he ile object provides an
easy way of keeping track of the ile that has been opened.
It is worth studying the
open
function in more detail, looking at the example in the program:
#open a file for appending, do not buffer
logFile = open('birdlog.txt', 'a', 0)
he function returns a ile object that is stored in the
logFile
variable. his variable is used
in your
logEvent
function to identify which ile the data about the event is to be written to.
Opening Files - Filename
he irst argument of the function,
'birdlog.txt'
, is the name of the ile that will be
opened. It is
relative
to where the program was run from. his means that
birdlog.txt
will
be opened in the same directory where the command was issued to run your program. If you
wanted to create the ile somewhere else in the ilesystem, you would need to give the appro-
priate ile path.
Linux File Paths
Linux, like most operating systems, has a hierarchical ilesystem; that is, it has a tree type struc-
ture with iles in directories (sometimes called
folders
), directories within other directories and
so on. It provides a means to describe the location of a ile within the tree of directories as a
ile
path.
The top of the tree is the
root,
where it is not possible to have directories above it.