Hardware Reference
In-Depth Information
take on one of two values. You can call these values one and zero or true and false. he main func-
tion calls a drawBox function and then enters an endless loop that simply checks the events.
Take a closer look at the drawBox function. It takes in a variable, called state , that deines
what colour the box is going to be. It is tested and you use the draw rectangle command from
pygame . At irst this looks complex, with lots of parameters or numbers in it, but it is quite
simple. he irst parameter in the command tells where you are going to draw the rectangle, in
this case in an area called screen you deined at the start of the program. he next three num-
bers deine the colour you will use in red, green and blue values - these are in brackets because
they are one entity that could be replaced by a suitable variable later on called a tuple. Next you
have four values bracketed as a tuple that deine the X and Y coordinates of the rectangle, fol-
lowed by how wide and how high to draw it. he inal value tells the computer how wide a pen
to draw this with. A zero is a special case and ills in the whole rectangle. Finally after drawing
the rectangle, you have to tell the computer to update what is being shown on the screen.
his way of working means that no matter how complicated or time consuming it is to draw,
the user always sees the screen change in a lash. he technical name for this is double bufer-
ing because one bufer, or area of memory, is being used to display the picture, and the other
is being used to construct the next picture. he display update call copies the construction
bufer into the display bufer. Note that at this point the display and construction bufers
both contain the same thing. Finally in this function the variable state is written out to the
PiFace board. As this Boolean variable can only be a zero or a one, then the least signiicant
LED is turned on or of, and all the other LEDs are turned of.
he last thing to look at in this program is the mouseGet function, which is called by the
checkForEvent function when it detects a mouse button down event. he mouseGet
function irst recovers the coordinates of the mouse pointer when it was clicked. hen the
compound if statement checks if both the x and y fall within the coordinates of the box. If
it does, then you toggle or invert the state of the variable box and then call the function that
draws it and writes to the outputs.
So with a mouse click, you can control a light.
A Small Detour into Theory
Now you've got a program that doesn't just act like a real window application; you can click
in the window and control an output. However, before you can go on to looking at a com-
plete sequencer you need to look a little bit about how the LEDs on the PiFace board are
related to the value you write to the interface.
he basic unit of storage in a computer is the byte. A byte consists of eight bits, each bit being
a separate logic level. Rather than think in bit terms, it is easier if you group these bits and
Search WWH ::




Custom Search