Hardware Reference
In-Depth Information
Although it might be tempting to write a program that cycles through all the colours, what
you actually want to see is if each colour works at each of its intensities. It is also helpful to
know that the switches are working as well. he test program is shown in Listing 8-1.
Listing 8-1 Colour Snap Hardware Test
#!/usr/bin/env python
“””
Colour Snap test
“””
import time # for delays
import piface.pfio as pfio # piface library
pfio.init() # initialise piface
def main():
print”Testing the colour snap hardware Ctrl C to quit”
print”showing the three intensity levels for each colour”
print”& showing buttons pressed”
while True:
for led in range(0,6,2) :
for c in range(1,4) :
pfio.write_output(c << led) # turn on the colour
time.sleep(.5)
switchState = pfio.read_input() & 3
if switchState & 1 :
print “blue button held down”
if switchState & 2 :
print “red button held down”
pfio.write_output(0) # turn off the light
time.sleep(0.8)
if __name__ == '__main__':
main()
Listing 8-1 is simply a loop that turns on each colour at the three intensities in turn. his is
done with two for loops. he irst uses the variable led as an index and counts up in two, so
it gives you the bit position of the irst of the two control bits for each colour of LED in turn.
he next for loop generates the numbers 1 to 3 in a variable called c (for “colour”). hen you
combine these two numbers by shifting c to the left by the led number. hat gives the bit
pattern you need to write out to the PiFace board to light only one LED at a speciic level.
When you run this you should see the red colour irst in three intensities, the green colour in
 
Search WWH ::




Custom Search