Change between workspaces using HP 2140 accelerometer

HP mini 2140 netbook comes with an ST Microeletronics accelerometer and under linux kernel 2.6.27 we found a driver that supports it (LIS3LV02Dx).
With wmctrl and some scripting we can toggle between workspaces when we tilt the machine left or right. This is a useless script but it has some fun :)

First you need to install wmctrl
sudo apt-get install wmctrlbash shell script - Download#!/bin/bash

COUNTER=0

while [ $COUNTER -le 100 ]; do
    sleep 0.5
    POS=`cat /sys/devices/platform/lis3lv02d/position | awk -F "(" '{print$2}' | awk -F , '{print$1}'`
    echo $POS
        if [ $POS -ge 12 ]; then
            wmctrl -o 1024,0
        elif [ $POS -le -12 ]; then
            wmctrl -o 0,0
        fi
    COUNTER=$(( $COUNTER + 1 ))
done
UPDATE Sep 8, 2009: an approach in python - Download#!/usr/bin/python
import time
import os
cnt=0
while cnt <= 100:
    time.sleep(0.5)
    f = open ('/sys/devices/platform/lis3lv02d/position','r')
    data = f.read()
    ar = data.split(',')
    results = ar[0].replace('(',''), ar[1], ar[2].replace(')','')
    print results
    if (ar[0].replace('(','') >= '12'):
        os.system('wmctrl -o 1024,0')
    elif (ar[1] <= '-12'):
        os.system('wmctrl -o 0,0')
    f.close()
    cnt += 1
Note: Scripts will work when System/Preferences/Appearance/Visual Effects is set to Normal or Extra.

4 comments:

  Dean

Saturday, September 19, 2009 4:26:00 AM

Very nice ... but seems like you have it backwards. Make screen change slide in direction that you tilt it.

  J@mBeL

Saturday, September 19, 2009 8:05:00 PM

I did it when it tilts right slide to the right workspace, and when it tilts left to the left.
You can change it for your needs.

Thanks for your comment!

  C. Sharff

Tuesday, October 20, 2009 7:18:00 AM

Sorry for being very noob but I am running ubuntu linux on my 2140 and have installed wmctrl and have downloaded the script but how do I run/use the script?

Do I need to install the drivers for the accelerometer? Thank you

  J@mBeL

Wednesday, October 28, 2009 8:10:00 PM

If you're running 2.6.27 kernel or later you don't need any driver. Also this scripts running if you have the visual effects set to normal or extra.