Page 1 of 1

Need help with uinput script.

Posted: Wed Oct 02, 2013 9:29 pm
by Borreltje
Hi all,

I'm trying to get this script to work, I want it to be able to emulate a keyboard press everytime I push on a button connected to a gpio pin (17 in this case)
but now it only emulates the key once, but it does register the button being pressed.
So, I run the script, the terminal screen says "Waiting for input." and when I push the button the terminal screen says "Button 1 Pressed" and the screen connected to the rapberry pi says "n", so far so good. But if I piush the button again, the terminal says "Button 1 Pressed" but the screen on the pi says nothing, so the "n" command only works once.

here is the code:

Code: Select all

#!/usr/bin/env python

import uinput
import os
from time import sleep
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
BUTTON_1 = 17
device = uinput.Device([uinput.KEY_N])
GPIO.setup(BUTTON_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def Input_1(channel):
    device.emit(uinput.KEY_N, 1) 
    status ='Button 1 Pressed'
    print status
    return (status)

GPIO.add_event_detect(BUTTON_1, GPIO.FALLING, callback=Input_1, bouncetime=300)

while True:
   print "Status is ", status , '- Waiting for input.'
   sleep(10);
#   GPIO.cleanup()
I hope someone can help me out, I don't see why this doesn't work the way I thought it would.

Thanks.

Re: Need help with uinput script.

Posted: Wed Oct 02, 2013 9:58 pm
by paddyg
You might need to pass the device to the callback method. Scope of variables is rather confusing in python, mainly because it obligingly tries to guess what you intended when it would be much more useful to just get an error message! Ideally you would pass device as an argument to the callback function but that is (to my mind a serious) shortcoming of the GPIO library. The only way I have found is to set it as a global. Of course the problem might be due to something else but I would try

Code: Select all

..
def Input_1(channel):
    global device
    device.emit(uinput.KEY_N, 1)
    status ='Button 1 Pressed'
..

Re: Need help with uinput script.

Posted: Wed Oct 02, 2013 10:05 pm
by Borreltje
Thanks paddyg, but I just tried that, but it don't work either.

Re: Need help with uinput script.

Posted: Wed Oct 02, 2013 10:25 pm
by paddyg
Hmm. I suppose I would check that uinput.Device.emit() works fine with real keyboard input then I would see if it worked in a thread, some things don't but you can get the thread to set flag variable (global one in this case!) which you can then access in your while loop and do the emit there (and reset the flag). If it worked in a thread I would try to see what bit of GPIO callback it didn't like. Then probably ask on a forum!

Re: Need help with uinput script.

Posted: Wed Oct 02, 2013 10:41 pm
by AndrewS
Before you get too far with re-inventing the wheel, have you looked at http://www.raspberrypi.org/phpBB3/viewt ... 78&t=29962 ? ;)

Re: Need help with uinput script.

Posted: Fri Oct 04, 2013 9:15 am
by Borreltje
AndrewS wrote:Before you get too far with re-inventing the wheel, have you looked at http://www.raspberrypi.org/phpBB3/viewt ... 78&t=29962 ? ;)

I'm reading it now, all of it, seems to be wirking OK, so I ordered some bits and pieces (buttons, resistors etc...) and I'm gonna set it all up over the weekend. I'll report back on monday to let you know if I got it working.

Thanks for the link.

Re: Need help with uinput script.

Posted: Wed Oct 09, 2013 12:14 pm
by Borreltje
Got my supplies yesterday, and got everything working like it should, thanks for the link AndrewS, buttons now act as keyboard as I wanted to.