Accessing the Beagleboard user button from Python

It's useful to have a physical way to control software running on the Bealgeboard, though without a normal keyboard this can be difficult.

There is a small push-button, labeled "user" connected to one of the GPIO inputs, and Linux uses a driver called "gpio-keys" to map this to a standard input device. The user button can be accessed using /dev/input/event0, and it's even possible to map other GPIO inputs (for example, those on the expansion connector) by editing the kernel source to hook them into the gpio-keys driver.

Here's some python code which detects when the user button is pressed. It uses standard file access to read from /dev/input/event0 and then parses the input event structure:

# Beagleboard user button
# Copyright 2009 mechomaniac.com
import struct
inputDevice = "/dev/input/event0"
# format of the event structure (int, int, short, short, int)
inputEventFormat = 'iihhi'
inputEventSize = 16
 
file = open(inputDevice, "rb") # standard binary file input
event = file.read(inputEventSize)
while event:
  (time1, time2, type, code, value) = struct.unpack(inputEventFormat, event)
  if type == 1 and code == 276 and value == 1:
    print "User button pressed!"
  event = file.read(inputEventSize)
file.close()

User login