Friday, August 1, 2008

Smackbook Pro

Today I noticed on reddit, an article titled "Knock-based Commands for Your Linux Laptop", which showed you how to take advantage of the accelerometer in your (IBM/Lenovo Thinkpad) laptop to allow you to issue commands by physically tapping your laptop. It's a really great idea, and I got to thinking about my laptop (a Macbook Pro,which does indeed have an accelerometer in it) and wondered how I could emulate this idea. It turns out it had already been done. In fact, this guy wrote about it two months before the previously mentioned article. It requires a program which reports data from the sensor and a script which uses this data to issue any particular command. It's slightly complicated (if you're not used to compiling programs) but can be simplified A LOT if you're using Leopard. I rewrote his program in python and added an applescript within the program which allows you to switch virtual desktops (spaces, whatever) by tapping the side of your screen. It's really neat, but it probably needs to be tweaked a good deal.

Anyway, here is the program:
#Detect taps via AMSTracker in order to change
#spaces. Based on the Perl script by Erling Ellingsen (http://blog.medallia.com/2006/05/smacbook_pro.html)
#
#by Sean Brewer (http://seabre986.googlepages.com)

#Note:
#AMSTracker MAY NOT BE DISTRIBUTED WITH THIS PROGRAM.
#The author of AMSTracker does not allow distribution (without his permission anyway)
#so, in order to use this script you must download AMSTracker from:
# http://www.osxbook.com/software/sms/amstracker/
#If anyone actually reads this PLEASE PLEASE PLEASE someone
#come up with a free software alternative to this! If I have time I'll do it!

#License:
#This program is licensed under the GPL.
#See http://www.gnu.org/licenses/gpl.txt for more details.

#Instructions: It's simple really.
#1. Download AMSTracker and put this program and AMSTracker into the
# same directory.
#2. Run this program. If you don't know how, ask a friend.
#3. Tap the side of your screen to change spaces.
from os import waitpid
from subprocess import Popen,PIPE
from signal import SIGTERM

amscheck = Popen (["./AMSTracker","-s","-u0.01"], stdout=PIPE)
stable = 0

#Hopefully you have at least one space, right?
space = 1

#This is the maximum number of spaces
#on my mac, though yours may be more or less, so
#change this accordingly.
space_max = 4

#The actual script to change spaces.
def spaces_script(num):
return """osascript -e 'tell application "System Events"
tell process "Finder"
keystroke\"""" + str(num) + """\" using control down
end tell
end tell'"""
while 1:
i = amscheck.stdout.readline().split()

if len(i) > 3:
continue

i = map(lambda j: int(j),i)
#We get a signed short written as two unsigned bytes.
if i[0] < 0:
i[0] += 256

x = (i[1] * 256) + i[0]

if abs(x) < 20:
stable += 1

if abs(x) > 30 and stable > 30:
stable = 0
#The logic here allows for movement
#to the next specified (by the tap) space.
if x < 0 and space == 1:
space += 1
elif x < 0 and space == space_max:
space -= 1
elif x < 0:
space -= 1
else:
space += 1

#I really need to make this cleaner.
p = Popen (spaces_script(space), shell=True)
sts = waitpid(p.pid,0)




It's really simple to run. All you need to do is download the script: here
and then download the program to get the sensor data: here

Put both programs into the same directory, and then run smack.py, then test it out. It should work.

No comments: