Hardware Reference
In-Depth Information
advance the sequence from the lowest switch or the music by attaching the special beat fol-
lowing circuit to it. I will describe that circuit later in this chapter.
he
updateSeq
function basically does two jobs; first it updates the position of the sequence
indicator square by drawing a long thin rectangle in the background colour to erase the old
square, and then drawing a new one. Finally it outputs the next pattern in the sequence with
the following line:
pfio.write_output(seq[n])
This takes the list called
seq
and extracts the value that is next in the list given by the vari-
able in the square braces and then writes it out to the PiFace board. This single line is what
actually does the turning on and of of the lights; everything else just supports this one line.
he
setUpScreen
function simply calls other functions that draw the basic framework of
the screen. So after wiping out everything in the screen buffer and setting it to the back-
ground colour there are five calls to draw control boxes. This call takes the parameters of the
words in the box, its location in the x axis and the width of the box. Finally the
drawCol
, or
draw column, function is called in a loop 32 times, one for each step in the sequencer. The
two parameters it takes is what step in the sequence it is and what bit pattern it is to set it at.
Drawing text under the
pygame
module is a bit complex. First you have to define your font,
which was done at the start of the code; the
None
parameter is the name of the default font file
and the number used is the font size. You then have to render the font into a bitmap, which is
a little bit of a screen buffer that contains only the font characters you want. You then define a
rectangle that encompasses the whole of this small bitmap. Then you have to position this
rectangle to the correct part of the screen. Finally you transfer that small screen buffer to the
main one with the
screen_blit
call giving it the parameters of the screen buffer and where
you want it put. See if you can follow those steps in the
drawControl
function.
he
drawCol
function draws a column of boxes, with one colour if that corresponds to a lit
LED in the sequence or another colour if it is unlit. In order to do this you have to separate
out all the bits from the sequence value. This is done by this line:
if ((value >> boxNum) & 1) != 1 :
What is happening here is that the variable called
value
is shifted to the left a number of
times, defined by what box you are drawing. The
AND
operation then separates out just the
least significant bit of this, as you saw before, and then makes the decision of what to draw
based on this bit.
Finally the
mouseGet
function does all the work of dealing with clicks. First it gets the loca-
tion of the mouse and checks to see if it is in the range of out array of LEDs in the sequence.