Hardware Reference
In-Depth Information
def checkForEvent():
# see if we need to quit or look at the mouse
#print “checking for quit”
event = pygame.event.poll()
if event.type == pygame.QUIT :
terminate()
elif event.type == pygame.MOUSEBUTTONDOWN :
mouseGet()
elif event.type == pygame.KEYDOWN
and event.key == pygame.K_ESCAPE :
terminate()
if __name__ == '__main__':
main()
So let's walk through the major sections of the code. It starts of by importing the required
libraries and then initialising them and the program's window. Next comes the section where
you can customise the colours for the program. I found a black background looks best when
you are using the program but a white background looks a lot better when viewed in a topic.
The next section defines the few global variables needed by the program.
he main function is simple, just four lines. Set up the screen, and then loop forever check-
ing for events to quit or mouse clicks to change what is happening. Finally check if you need
to advance the sequence. This sequence advance function follows next, although as you
know, the order of the function definitions is not important.
he checkStep function first looks at the variable that defines where the trigger to the next
step is coming from. If this is from the internal timer, the time now is compared to when the
next step should occur and if it is time the updateSeq function is called, and the step vari-
able is incremented and tested to see if it has not gone over the maximum number of steps.
If it has then the step variable is reset to zero. This is known as wrapping around the counter.
Finally, the time for the next change is set up by adding the time now to the time increment
variable. If the system is set up so that the sequence is advanced on a hardware input then
that input is looked at to see if it is different from last time. This indicates a level change or
edge has been detected, and if the input level is now high it is time to advance the sequence
in the same way as before. One line that might puzzle beginners is this:
switchState = pfio.read_input() & 1
What the & operator does is to perform a bitwise AND operation between what is read and the
number 1. The result of this is that the variable switchState just contains the least signifi-
cant bit of the byte that is read from the PiFace board's input lines. This means you can
Search WWH ::




Custom Search