Hardware Reference
In-Depth Information
you want to synchronise the changing of the lights to the beat of the music. he sequence
can either be stepped at a regular rate determined by the speed controls, or locked into the
beat of the music. his last feature will require a little bit of extra hardware and is optional -
you can skip it for now and add it later if you like.
Finally it would be good if all the colours used in the sequence software were customisable;
that is, it should be easy to change by changing a single value at one point of the code only.
his means that whenever you use a colour you do not hard code it in by putting the colour
numbers into a function call, but rather use a variable to deine that colour. hose variables
for all the colours should be grouped in one place in the code for easy access.
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. hat means
starting at the lowest possible function and working your way up. Of course, if you just look
at the inished code, you don't see that. I started by taking the window test program and
writing the functions that showed the columns representing the LEDs in the sequence. hen
I expanded it so that I could click on each LED to turn the box on or of. Next came the con-
trols to clear and invert, followed by the step indicator. his 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. his might not be the sequence
of building up a program that irst 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 inal
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 11-3 shows the sequencer application.
Listing 11-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
Search WWH ::




Custom Search