Hardware Reference
In-Depth Information
IN MY YOUTH, during the late 60s, I answered an advertisement in the Manchester Evening
News for someone to turn lights on and of in time to the music in an Ashton-under-Lyne night
club. I went to the interview that Friday evening, which consisted of their showing me the light-
ing control rig and saying I had to be there by 7:30 p.m. on Saturday and Sunday. To be honest,
I didn't fancy ive hours of switching lights on and of for just £1.00, so I arrived the following
evening on my Lambretta with a rucksack full of electronics. I had a large multiway switch used
in telephone exchanges called a uniselector (you can still get these on eBay), which was wired
up to make an on/of control of ive circuits. I started hacking the lighting panel, and before
long, I had ive coloured spotlights lashing away while I put my feet up.
hese days, you cannot go hacking about with mains like that - health and safety would have
a it. And with the Raspberry Pi, you have the opportunity to do something a lot more sophis-
ticated. So in this chapter, you are going to see how to control those disco lights, and change
the pattern with a click of a mouse. Not only that, but you will see how to drive the light
sequence from the beat of the music.
In this chapter, you'll learn how to write a Python program to deine a sequence of lights.
You'll also learn about various aspects of electronics and control.
Deining Your Sequence
So far in this topic, you have written programs that interact through the Python console.
Now you are going to produce a proper desktop application. his would be quite a daunting
prospect if it were not for the help that you can get from a Python package that does a lot of
the under-the-hood hard work for you. his just leaves you to specify exactly what things
should look like. his package also integrates the windows style selected for your whole desk-
top, so the result looks consistent with other applications.
his package is called pygame and comes preloaded in most Raspberry Pi distributions. It
consists of a number of functions to create and update windows, draw in the windows, regis-
ter a mouse click and read the keyboard. It will also handle sound and music, but you will not
be looking at that function this time.
Start IDLE, and select a new window. For a start let's look at Listing 11-1, a very basic piece
of code to open a window and close it down.
Listing 11-1 Windows1 Test Program
#!/usr/bin/env python
“””
Window1 to open up a window on the desktop
“””
Search WWH ::




Custom Search