Welcome, guest | Sign In | My Account | Store | Cart

A small program utilizing the ctypes and turtle modules. You can drive the little turtle by turning your Thinkpad (which must have APS).

Requirements:

You need a newer Thinkpad (T41, T42, T43, T6x, R5x, R6x or any newer) and need to have the Active Protection System drivers (now called Lenovo Airbag Protection) installed.

Python, 32 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import ctypes
import time
import turtle

class SensorData(ctypes.Structure):
    _fields_ = [
        ('status', ctypes.c_int),
        ('raw_x', ctypes.c_short),
        ('raw_y', ctypes.c_short),
        ('x', ctypes.c_short),
        ('y', ctypes.c_short),
        ('temp', ctypes.c_byte),
        ('center_x', ctypes.c_short),
        ('center_y', ctypes.c_short),
    ]

def getXY():
    data = SensorData()
    ctypes.windll.Sensor.ShockproofGetAccelerometerData(ctypes.byref(data))
    return data.raw_x / 10.0, data.raw_y / 10.0

def main():
    centerx, centery = getXY()
    turtle.pensize(3)
    while 1:
        x, y = getXY()
        turtle.left(centery-y)
        turtle.forward(centerx-x)
        time.sleep(0.01)

if __name__ == '__main__':
    main()

5 comments

Victor Noagbodji 14 years, 4 months ago  # | flag

please provide more information. i tried to run this program on my laptop and got an exception:

C:\labs>python thinkpad.py
Traceback (most recent call last):
  File "thinkpad.py", line 32, in <module>
    main()
  File "thinkpad.py", line 23, in main
    centerx, centery = getXY()
  File "thinkpad.py", line 19, in getXY
    ctypes.windll.Sensor.ShockproofGetAccelerometerData(ctypes.byref(data))
  File "c:\python26\lib\ctypes\__init__.py", line 423, in __getattr__
    dll = self._dlltype(name)
  File "c:\python26\lib\ctypes\__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found
Michael Grünewald (author) 14 years, 4 months ago  # | flag

You probably don't have a newer Thinkpad (T41, T42, T43, T6x, R5x, R6x or any newer) or don't have the Active Protection System drivers (now called Lenovo Airbag Protection) installed.

Victor Noagbodji 14 years, 4 months ago  # | flag

ahh i don't even have thinkpad :)

Sridhar Ratnakumar 14 years, 4 months ago  # | flag

Linux version is here.

Denis Barmenkov 14 years, 3 months ago  # | flag

My Python 2.4 under windows denied to use turtle attributes, so I fix source for running in text mode:

import ctypes
import time
import turtle

class SensorData(ctypes.Structure):
    _fields_ = [
        ('status', ctypes.c_int),
        ('raw_x', ctypes.c_short),
        ('raw_y', ctypes.c_short),
        ('x', ctypes.c_short),
        ('y', ctypes.c_short),
        ('temp', ctypes.c_byte),
        ('center_x', ctypes.c_short),
        ('center_y', ctypes.c_short),
    ]

def getXY():
    data = SensorData()
    ctypes.windll.Sensor.ShockproofGetAccelerometerData(ctypes.byref(data))
    return data.raw_x / 10.0, data.raw_y / 10.0

def main():
    start = prev = getXY()
    while 1:
        current = getXY()
        if current <> prev:
            print start[0] - current[0], ' - ', start[1] - current[1]
            prev = current
        time.sleep(0.1)

if __name__ == '__main__':
    main()