Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mouse_control.py to move the mouse pointer on the screen (Linux…
… only for now)
- Loading branch information
Séverin Lemaignan
committed
Sep 15, 2010
1 parent
2d2d676
commit f30b78e
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/python | ||
|
||
import sys, time, logging | ||
from ctypes import cdll | ||
|
||
from emotiv import Emotiv | ||
|
||
class Xlib: | ||
def __init__(self): | ||
self.xlib = cdll.LoadLibrary('libX11.so') | ||
|
||
display = self.xlib.XOpenDisplay(None) | ||
dflt_screen_num = self.xlib.XDefaultScreen(display) | ||
default_screen = self.xlib.XScreenOfDisplay(display, dflt_screen_num) | ||
|
||
self.width = self.xlib.XWidthOfScreen(default_screen) | ||
self.height = self.xlib.XHeightOfScreen(default_screen) | ||
logger.info("Default screen: %d x %d" % (self.width, self.height)) | ||
self.xlib.XCloseDisplay(display) | ||
|
||
def move_mouse(self, x,y): | ||
display = self.xlib.XOpenDisplay(None) | ||
root = self.xlib.XDefaultRootWindow(display) | ||
self.xlib.XWarpPointer(display,None,root,0,0,0,0,x,y) | ||
self.xlib.XCloseDisplay(display) | ||
|
||
def main(debug=False): | ||
|
||
screen = Xlib() | ||
|
||
width = screen.width | ||
height = screen.height | ||
|
||
curX, curY = width/2, height/2 | ||
while True: | ||
updated = False | ||
for packet in emotiv.dequeue(): | ||
updated = True | ||
if abs(packet.gyroX) > 1: | ||
curX -= packet.gyroX - 1 | ||
if abs(packet.gyroY) > 1: | ||
curY += packet.gyroY | ||
curX = max(0, min(curX, width)) | ||
curY = max(0, min(curY, height)) | ||
|
||
if updated: | ||
screen.move_mouse(curX, curY) | ||
time.sleep(1.0/60) | ||
|
||
|
||
emotiv = None | ||
|
||
try: | ||
logger = logging.getLogger('emotiv') | ||
logger.setLevel(logging.INFO) | ||
log_handler = logging.StreamHandler() | ||
logger.addHandler(log_handler) | ||
|
||
emotiv = Emotiv() | ||
|
||
|
||
main(*sys.argv[1:]) | ||
|
||
finally: | ||
if emotiv: | ||
emotiv.close() | ||
|