2 Python bindings for the HDAPS interface
5 # Copyright: 2008-2009 Evgeni Golov <sargentd@die-welt.net>
11 __SYSFS_HDAPS
='/sys/devices/platform/hdaps'
12 __SYSFS_HDAPS_CALIBRATION
='%s/calibrate' % __SYSFS_HDAPS
13 __SYSFS_HDAPS_POSITION
='%s/position' % __SYSFS_HDAPS
14 __SYSFS_HDAPS_KEYBOARD_ACTIVITY
='%s/keyboard_activity' % __SYSFS_HDAPS
15 __SYSFS_HDAPS_MOUSE_ACTIVITY
='%s/mouse_activity' % __SYSFS_HDAPS
16 __INPUT_HDAPS_EVENT
='/dev/input/hdaps/accelerometer-event'
18 def readPosition(calibrate
=False):
20 filename
= __SYSFS_HDAPS_CALIBRATION
22 filename
= __SYSFS_HDAPS_POSITION
23 line
= __readSysfs(filename
).strip(')(')
24 posX
,posY
= line
.split(',')
25 return (int(posX
), int(posY
))
27 def readKeyboardActivity():
28 return int(__readSysfs(__SYSFS_HDAPS_KEYBOARD_ACTIVITY
))
30 def readMouseActivity():
31 return int(__readSysfs(__SYSFS_HDAPS_MOUSE_ACTIVITY
))
33 def __readSysfs(filename
):
35 fd
= file(filename
, 'r')
36 line
= fd
.readline().strip()
39 raise IOError, '%s could not be read, is the "hdaps" module loaded?' % filename
42 def hasInputInterface():
43 return os
.path
.exists(__INPUT_HDAPS_EVENT
)
45 def readInputPosition(callback
):
46 if not hasInputInterface():
47 raise IOError, 'You don\'t have %s. Is the "hdaps" module from tp_smapi loaded?' % __INPUT_HDAPS_EVENT
50 input = open(__INPUT_HDAPS_EVENT
,"rb")
51 event
= input.read(16)
53 (tv_sec
, tv_usec
, type, code
, value
) = struct
.unpack('iihhi',event
)
59 if type == 0 and code
== 0:
61 event
= input.read(16)