Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import serial
- import sys
- import Xlib
- import Xlib.display
- import time
- #requirements:
- # linux (or manually remove xlib and put in win32 equivalent
- # load extmod in your xorg.conf modules
- # python-serial
- # python-xlib
- #MODIFY THE NEXT LINE TO REFLECT THE CORRECT SERIAL PORT NUMBER!
- port=3
- #set this variable to [] and the calibration will run
- calibration=[32591, 32638, 32603, 32639, 8196, 8009]
- s = serial.Serial(port)
- s.timeout=1
- MOUSE_DOWN = 0x80
- MOUSE_HELD = 0x81
- MOUSE_UP = 0x82
- display = Xlib.display.Display()
- screen = display.screen()
- def mouse_down():
- Xlib.ext.xtest.fake_input(display,Xlib.X.ButtonPress,1)
- display.sync()
- def mouse_up():
- Xlib.ext.xtest.fake_input(display,Xlib.X.ButtonRelease,1)
- display.sync()
- def mouse_move(x,y):
- screen.root.warp_pointer(x,y)
- display.sync()
- def wait_for_quiet():
- '''Waits for the touchscreen to stop sending data'''
- f = ''
- while True:
- f = s.read(1)
- if f == None or f == '':
- sys.stdout.write('\n')
- break
- def get_touched():
- '''Get a valid touch from the touchscreen'''
- touch = []
- while touch == []:
- try:
- e = ord(s.read(1))
- if (e & 0x80) == 0:
- print "out of sync..."
- continue
- u1 = ord(s.read(1))
- x = ord(s.read(1))
- touch_x = (x<<8 | u1)
- u2 = ord(s.read(1))
- y = ord(s.read(1))
- touch_y = (y<<8 | u2)
- touch = (e,touch_x,touch_y)
- except KeyboardInterrupt:
- raise
- except:
- pass
- return touch
- def do_init():
- '''Wait for the touchscreen to become quiet and let the user know they can start using it'''
- print("Initializing touchscreen... (Don't touch it!)")
- wait_for_quiet()
- print("Touchscreen initialized. (Go ahead and touch it!)")
- def do_fancy_callibration():
- smallX = None
- bigX = None
- smallY = None
- bigY = None
- sBigX = None
- sBigY = None
- biggest_possible_x = 9000
- biggest_possible_y = 9000
- print("Drag the stylus from the center of the screen into each corner. You will 30 seconds (from the time the first touch is registered) to get some initial values, if this is successful, then a new timer will start and when the values have stabillized for 10 seconds, calibration will end. (You may need to tap the screen again to finish calibration)")
- initial_time = time.time()
- good_values = False
- last_change = time.time()
- o = get_touched()
- while time.time() - initial_time < 30:
- if good_values and time.time() - last_change > 10:
- break
- e = get_touched()
- if e[2]!=o[2] or e[1] !=o[1]:
- if e[2] < biggest_possible_y:
- if sBigY == None:
- sBigY = e[2]
- last_change = time.time()
- if e[2] > sBigY:
- sBigY = e[2]
- last_change = time.time()
- if e[1] < biggest_possible_x:
- if sBigX == None:
- sBigX = e[1]
- last_change = time.time()
- if e[1] > sBigX:
- sBigX = e[1]
- last_change = time.time()
- if e[2] > biggest_possible_y:
- if bigY == None:
- bigY = e[2]
- last_change = time.time()
- if smallY == None:
- smallY = e[2]
- last_change = time.time()
- if e[2] < smallY:
- smallY = e[2]
- last_change = time.time()
- elif e[2] > bigY:
- bigY = e[2]
- last_change = time.time()
- if e[1] > biggest_possible_x:
- if bigX == None:
- bigX=e[1]
- last_change = time.time()
- if smallX == None:
- smallX = e[1]
- last_change = time.time()
- if e[1] < smallX:
- smallX = e[1]
- last_change = time.time()
- elif e[1] > bigX:
- bigX = e[1]
- last_change = time.time()
- if smallX != None and smallY != None and sBigX != None and sBigY != None and bigX != None and bigY != None:
- print("Good values acquired, stop once you've visited the corners. Then wait 10 seconds and tap the screen.")
- good_values = True
- o = e
- if not good_values: print("Calibraion failed!")
- else: print("Calibration complete!")
- print [ smallX, bigX, smallY, bigY, sBigX, sBigY ]
- return [ smallX, bigX, smallY, bigY, sBigX, sBigY ]
- def touch_to_screen(touch,calibration):
- #not sure if the resolution changes then it will be reflected in the screen object, might have to keep fetching a new screen or something...
- res_x = float(screen['width_in_pixels'])
- res_y = float(screen['height_in_pixels'])
- return [ touch[0], float(touch[1]) / float(calibration[4]) * res_x, float(touch[2]) / float(calibration[5]) * res_y ]
- def get_good_touch(calibration):
- '''Returns a touch event within the calibrated area'''
- while True:
- event = get_touched()
- if event[1] >= calibration[0] and event[1] <= calibration[1]:
- event = [ event[0], event[1] - calibration[0] , event[2] ]
- elif event[1] >= 0 and event[1] <= calibration[4]:
- event = [ event[0], event[1] + calibration[1] - calibration[0], event[2] ]
- else:
- #bad X value
- print("Bad X value for calibration range: %d" % event[1])
- continue
- if event[2] >= calibration[2] and event[2] <= calibration[3]:
- event = [ event[0], event[1], event[2] - calibration[2] ]
- elif event[2] >= 0 and event[2] <= calibration[5]:
- event = [ event[0], event[1], event[2] + calibration[3] - calibration[2] ]
- else:
- #bad Y value
- print("Bad Y value for calibration range: %d" % event[2])
- continue
- break
- return event
- def save_calibration(calibration):
- '''Modifies this script to include saved calibration data'''
- file = open(sys.argv[0],'r+w')
- lines = file.readlines()
- replace_line = -1
- for i in range(0,len(lines)):
- if lines[i].startswith('calibration='):
- replace_line = i
- file.seek(0)
- if replace_line == -1:
- print("Calibration line not found in script! Unable to save calibration data!")
- else:
- for i in range(0, len(lines)):
- if i != replace_line:
- file.write(lines[i])
- else:
- file.write('calibration = %s' % calibration)
- print("Calibration data saved to script.")
- file.close()
- if __name__ == "__main__":
- do_init()
- if calibration == []:
- calibration = do_fancy_callibration()
- save_calibration(calibration)
- while True:
- touch = get_good_touch(calibration)
- touch = touch_to_screen(touch,calibration)
- if touch[0] == MOUSE_DOWN:
- mouse_move(touch[1], touch[2])
- mouse_down()
- elif touch[0] == MOUSE_HELD:
- mouse_move(touch[1], touch[2])
- elif touch[0] == MOUSE_UP:
- mouse_move(touch[1], touch[2])
- mouse_up()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement