Hardware Reference
In-Depth Information
numbers define 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 define the X and Y coordinates of the rect-
angle, followed by how wide and how high to draw it. The final value tells the computer how wide
a pen to draw this with. A zero is a special case and fills in the whole rectangle. Finally after draw-
ing the rectangle, you have to tell the computer to update what is being shown on the screen.
This 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 flash. The technical name for this is double buffer-
ing, because one buffer, or area of memory, is being used to display the picture, and the other
is being used to construct the next picture. The display update call copies the construction
buffer into the display buffer. Note that at this point the display and construction buffers
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 significant
LED is turned on or off, and all the other LEDs are turned off.
The 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. The mouseGet
function first recovers the coordinates of the mouse pointer when it was clicked. Then 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.
The 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 thinking in bit terms, it is easier if you group these bits and
consider storage in terms of bytes. However, as you will see you sometimes want to manipu-
late individual bits in that byte. In the last program you saw that a Boolean variable could
only have one of two possible values; however it takes a byte to store that one variable, so all
the other bits in it are wasted. If you take a byte you can store the state of eight LEDs in it.
The relationship between the byte, the bits and the LEDs is shown in Figure 3-1.
So by using a byte variable to store the state of all eight LEDs you can then use a list of these
variable to store a sequence of LED patterns. To output each pattern all you have to do is to
write out the next variable in the list to the PiFace board.
Search WWH ::




Custom Search