vectraboyv6
Posts: 43
Joined: Fri Aug 17, 2012 11:28 am

mouse click contrlling gpio

Tue Jan 06, 2015 7:12 pm

hi,

im wanting to create a python script that runs in the background of lxde to monitor the left hand mouse click. if the mouse is clicked it turns on gpio pin 4. if after 30 seconds of non clicking it turns off gpio pin 4.

i think i need to use the "time" import and some sort of loop listening for the mouse click.

something like :-

"main"
get time
if mouse click then goto "on"
when time equals 30 gpio 4 off
goto main

"on"
gpio 4 on
goto main

however i lack the knowledge to put this together in python. any guide would be usefull

thanks

User avatar
paddyg
Posts: 2583
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: mouse click contrlling gpio

Wed Jan 07, 2015 12:34 pm

detecting the left mouse button is a bit tricky if you are completely new to python programming. Look at the python curses docs for one way of doing it (or a user interface system such as pygame or tk). This is an outline way of doing it

Code: Select all

last_clicked = 0.0
pin_status = False
while True:
  now = time.time()
  if get_mouse_status(): #function you will have written returning True if LB clicked
    last_clicked = now
    if not pin_status:
      GPIO.output(4, True)
      pin_status = True
  if pin_status and now > (last_clicked + 30.0):
    GPIO.output(4, False)
    pin_status = False
  time.sleep(0.1) #to stop it running flat out
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”