Olimex Support Forum

OLinuXino Android / Linux boards and System On Modules => A20 => Topic started by: ROM on January 14, 2014, 02:16:16 PM

Title: Access buttons from linux
Post by: ROM on January 14, 2014, 02:16:16 PM
Hello!

How can I get access to the keys from Linux?
Title: Re: Access buttons from linux
Post by: bearclaw on January 20, 2014, 10:49:58 PM
All I got so far is:

All Android buttons are connected to an ADC (LRADC) that uses a divider to determine which button is pressed. ADC being labeled "AB23".

Title: Re: Access buttons from linux
Post by: bear on January 21, 2014, 08:04:10 PM
hello on!
try: insmod sun4i-keyboard.ko
(from //lib/modules/3.4.xx+/kernel/drivers/input/keyboard/ )
there xx -you kernel version
evtest (apt-get install evtest) help you test/show any input events (select sun4i-keyboard).
Title: Re: Access buttons from linux
Post by: lauhub on May 27, 2014, 05:39:01 PM
Hello,

Do you think it is possible to call a program when an event is fired ?

As an example, I would like to call a shutdown command when the power button is hold for more than 4 seconds.

But I would also like to call program as soon as a button is pressed.

Would you have leads on how to do this ?
Title: Re: Access buttons from linux
Post by: MBR on May 28, 2014, 09:50:36 PM
You have to read the correct /dev/input/eventX device (look at dmesg output to see what device it is). When the button is pressed or released, you can read the event data. The structure is defined in linux/input.h, but for one button, you will only look for the keypress and release. The program may, for example, wait in read(), branch by event using switch/case, store the timestamp when button is pressed and compare stored value wtih the timestamp when button is released - if the delta is more than four seconds, it will do the shutdown (using the system() call).
Title: Re: Access buttons from linux
Post by: fab on May 31, 2014, 11:14:43 AM
Add "sun4i-keyboard" to /etc/modules

and find hereafter an example to catch events from a python script:


infile_path = "/dev/input/event3"
#long int, long int, unsigned short, unsigned short, unsigned int
FORMAT = 'llHHI'
EVENT_SIZE = struct.calcsize(FORMAT)
#open file in binary mode
in_file = open(infile_path, "rb")
try:
    event = in_file.read(EVENT_SIZE)
    while event:
        (tv_sec, tv_usec, type, code, value) = struct.unpack(FORMAT, event)
        if type != 0 or code != 0 or value != 0:
            if code==217 and value==1:
             # TODO
            if code==114 and value==1:
             # TODO
            if code==139 and value==1:
                print "System halt"
                os.system("halt")
        event = in_file.read(EVENT_SIZE)

finally:
    in_file.close()


Fab