Hardware Reference
In-Depth Information
rectangle that encompasses the whole of this small bitmap. hen you have to position this
rectangle to the correct part of the screen. Finally you transfer that small screen bufer to the
main one with the screen_blit call giving it the parameters of the screen bufer 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. his 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, deined by what box you are drawing. he AND operation then separates out just the
least signiicant 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.
If it is, it works out what byte or value this location represents in the sequence and what bit
within that byte it is. hen this line toggles the bit:
seq[byte] ^= 1 << bit
It will look a bit odd and so calls for some explanation. he right side of the equals sign makes a
number with a one in the bit position that you want to change. his is done by taking the value
1 and shifting it to the left the number of times you calculated when you worked out what bit
was clicked. he equals sign is preceded by a caret symbol ^ and means the exclusive OR opera-
tion. So this number you have created by shifting is exclusive OR ed with the value of the sequence
at this point, and it is then put back into the sequence list. It is a shorthand way of saying this:
seq[byte] = seg[byte] ^ (1 << bit)
When you exclusive OR , or XOR as it is sometimes called, two numbers, the result is that you
set bits that are only set to a logic one in one of the numbers and you invert the bits that are
a set to a logic one in both numbers. So doing this operation simply inverts the bit corre-
sponding to the bit you have clicked. You can then go and draw the whole column; again you
update the screen image when you have inished all the drawing.
Next, the mouseGet function looks to see if the mouse has been clicked in any of the control
boxes. If it has, it does the appropriate action. Clearing the sequence writes zero in every value
in the sequence, while inverting applies an exclusive OR operation to all the bits in every value.
Search WWH ::




Custom Search