Hardware Reference
In-Depth Information
print “Joystick test press the joystick button”
print “Ctrl - C to quit”
while True :
buttons = pfio.read_input()
if buttons != lastInput and buttons != 0:
print “bits are”,hex(buttons)
print “player 1”,positions[buttons & 0xf]
print “player 2”,positions[buttons >> 4]
print “ “
leds = (buttons & 0xC) | ((buttons >> 2) & 0x30)
pfio.write_output(leds)
lastInput = buttons
if __name__ == '__main__':
main()
When you irst run this you will get the introductory message. hen any press on the joystick
will print out the bit pattern, in hex, read from the PiFace inputs, and followed by a message
telling you the state of each player's buttons. hese are displayed as the points of the compass
with the colours used in the slot car racing game in brackets. If you press in the east or south
direction, the red or green LED comes on and stays on until there is a change in either button.
he program is quite simple. First the inputs are polled as fast as possible, and then an if state-
ment is used to see if the input has changed since the last time it was looked at and also that the
input has a nonzero value. his ensures that you get only one message per key press and you don't
get a message when the key is released. he bit pattern is then printed in hex, and the joystick
button positions for each player are printed out. hese position messages are held in a list called
positions , and there is an entry for each of the possible 16 diferent combinations. Player 1 has
the four least signiicant bits of the input, and a simple bitwise AND operation just leaves player 1's
input bits to use to look up what to print. For player 2 the top four bits in the input are shifted
down to the bottom four bits so that the same lookup table list can be used. Finally a bit pattern
is calculated to see what LEDs to light. his takes bits 6 and 7 and shifts them into the position of
bits 4 and 5. hen this is merged with bits 2 and 3 to give the output you need.
Notice that there are some positions with question marks; it is quite hard for example to press
both east and west without pressing either north or south - it is easier without the table tennis
ball attached, but that is not something you want to require when using the joystick button in
an application. hese intermediate positions have to be included in the positions list because
you have to cater for all combinations, physically possible or not. here are also positions such
as a “big south”, which is one where east, west and south are all being pressed at one time. One
thing you might notice when testing some positions is that you get two messages such as one
saying south followed by another saying south west. his is because it is almost impossible to
Search WWH ::




Custom Search