Hardware Reference
In-Depth Information
Implementing the Sequencer
After designing the sequencer from the top down, when it comes to implementing the design
it is better to write the code in what is known as a bottom-up implementation. That means
starting at the lowest possible function and working your way up. Of course, if you just look
at the finished code you don't see that. I started by taking the window test program and writ-
ing the functions that showed the columns representing the LEDs in the sequence. Then I
expanded it so that I could click on each LED to turn the box on or off. Next came the con-
trols to clear and invert, followed by the step indicator. This was followed by the speed con-
trols and at this point I added the code to actually output something to the LEDs. Finally the
auto/external step control was added and the code tidied up. This might not be the sequence
of building up a program that first springs to the mind of a beginner, but the idea is to do a
little coding and then test. So you are always looking to do something that can be instantly
tested, even if it means writing the odd line of code that is not going to make it in the final
mix. It also means that if something goes wrong with the test you know you have just writ-
ten the code with the error in it.
Listing 3-3 shows the sequencer application.
Listing 3-3: The Sequencer Application
#!/usr/bin/env python
“””
Disco LED sequence display on the PiFace board
“””
import time # for delays
import piface.pfio as pfio # piface library
import os, pygame, sys
pfio.init() # initialise pfio
pygame.init() # initialise graphics interface
os.environ['SDL_VIDEO_WINDOW_POS'] = 'center'
pygame.display.set_caption(“LED sequence controller”)
screen = pygame.display.set_mode([788,250],0,32)
background = pygame.Surface((788,250))
# define the colours to use for the user interface
cBackground =(255,255,255)
cLEDon = (255,0,0)
cLEDoff = (128,128,128)
cOutline = (255,128,0)
cText = (0,0,0)
cTextBack = (220,220,220)
cStepBlock = (0,255,255)
Search WWH ::




Custom Search