Hardware Reference
In-Depth Information
#now draw the outline of the box
pygame.draw.rect(screen,(0,0,180),(50, 70, 40,40), 3)
pygame.display.update() # refresh the screen
pfio.write_output(state)
def mouseGet() : # see where we have clicked
global box
x,y = pygame.mouse.get_pos()
print “The mouse has been clicked at “,x,y
if x in range(50,90) and y in range(50,110) :
box = not box # toggle the state of the box
drawBox(box)
def terminate(): # close down the program
print (“Closing down please wait”)
pygame.quit()
sys.exit()
def checkForEvent(): # see if we need to quit
# or look at the mouse
#print “checking for quit”
event = pygame.event.poll()
if event.type == pygame.QUIT :
terminate()
elif event.type == pygame.MOUSEBUTTONDOWN :
mouseGet()
elif event.type == pygame.KEYDOWN and event.key == ;
pygame.K_ESCAPE :
terminate()
if __name__ == '__main__':
main()
When you run this, you will get a much smaller window on the desktop with a single square
in it. Click in the square, and four things will happen. First the square will turn from grey to
red, and then you will hear the relay on the PiFace board come on and see one of the LEDs
come on. Finally you will see the position of the mouse when it was clicked printed out in
window coordinates. hat means that the coordinate value will be the same when clicking
the same spot within the window, irrespective of where that window is positioned on the
screen. Let's see what's been done here.
his time you add in the call to import the piface library, which is going to control the relay and
lights. You set a variable called box to be false, which is called a logic or Boolean value and can only
Search WWH ::




Custom Search