Hardware Reference
In-Depth Information
It is time to test what you have; as usual, a short piece of code whose only function is to test
the hardware is given in Listing 15-1.
Listing 15-1 Roto-Sketch Control Box Test 1
#!/usr/bin/env python
“””
Rotary Encoder input test 1 - raw inputs
encoders wired to inputs 6 & 7 and 4 & 5
“””
import piface.pfio as pfio # piface library
pfio.init() # initialise piface
print “Single encoder input test Ctrl C to quit”
print “Displays raw input from encoder”
lastEncoder = -1
display = [“00”,”01”,”10”,”11”]
while True:
encoder = pfio.read_input() & 0xF8
if lastEncoder != encoder:
enc2 = (encoder >> 4) & 0x03
enc1 = (encoder >> 6)
print display[enc1],” “,display[enc2],
if encoder & 0x08 != 0 :
print “ box inverted”
else :
print “ “
lastEncoder = encoder
You can see that this code is a bit fancy in the way it displays the input values. Rather than just
have the raw number or even a bit pattern, I have used a list called display to show the bit
pattern in a clear way. You can print it out in binary, but you get a 0b before the number, which
makes it a bit diicult to read. As it is you will get the bit pattern printed out each time there is
a change in the input. You can easily see what sort of detent encoder you have by noting the
transitions between clicks. As the tilt switch is activated you will see a message telling you the
box is inverted. he irst pattern printed out is what you will be using for the X movement.
Next, you'll try to do something with those numbers that are being returned. his is a bit
more tricky than you might at irst think. he program in Listing 15-2 takes just one of the
encoders and keeps track of the clicks.
 
Search WWH ::




Custom Search