At the bottom of this post is a Python script that uses the Lenovo Thinkpad (a T520 in my case) accelerometer as input. That means that you can tilt your laptop to issue commands, in this example controlling your music player.

To get this working, we need to install an extra kernel module and user space daemon:

sudo apt-get install tp-smapi-dkms hdapsd

The first is a dynamic kernel module exposing some extra features of the Thinkpad hardware/firmware, e.g. battery charging. It also provides a improved version of the mainline HDAPS driver. FYI, HDAPS stands for "Hard Drive Active Protection System", which is IBM/Lenovos name for active hard-drive protection.

The second is the hdapsd disk protection user space daemon. Its official use is monitoring the acceleration values through the HDAPS interface and automatically initiating disk protection, but it also allows users to easily read out the values of the HDAPS interface.

Loading the module should show up in your dmesg output:

[ 343.452582] thinkpad_ec: thinkpad_ec 0.41 loaded. [ 343.465315] tp_smapi 0.41 loading... [ 343.465395] tp_smapi successfully loaded (smapi_port=0xb2). [ 350.973926] hdaps: initial mode latch is 0x05 [ 350.974084] hdaps: setting ec_rate=250, filter_order=2 [ 350.974301] hdaps: device successfully initialized. [ 350.974347] input: ThinkPad HDAPS joystick emulation as /devices/virtual/input/input13 [ 350.974494] input: ThinkPad HDAPS accelerometer data as /devices/virtual/input/input14 [ 350.974580] hdaps: driver successfully loaded.

And you now should have a file through the sysfs interface, which contains the current orientation of the laptop in two dimensions.

cat /sys/devices/platform/hdaps/position (471,504)

The Python script (download) below reads in those values, and uses it to provide four "inputs": tilt left, tilt right, tilt forward, tilt backward. These are mapped to four commands for my cmus console music player, i.e. toggle play/pause, next track and volume up/down. But any console command (or Python-function) can be called.

It also does some rudementary averaging, together with threshold detection. This makes sure undeliberate changes are unlikely to trigger the command, and allows for gradual shifts of the baseline position of the laptop.

Starting up the tilt-detection/controller is a simple ./monitor.py . It prints out if/which tilt it detects, and you can comment out some other print statements for debugging.