Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pymouse import PyMouse
- import serial
- serialPort = raw_input("Serial port: ")
- try:
- ser = serial.Serial(serialPort, 19200)
- ser.timeout = None
- except serial.SerialException:
- print "No device connected. Exiting..."
- time.sleep(1)
- sys.exit()
- sensibility = 10.0
- if ((userSens = raw_input("Sensibility (leave blank for default value 10): ")) != ""):
- sensibility = float(userSens)
- mouse = PyMouse()
- zClicked = False
- while True:
- if (ser.inWaiting() > 0):
- try:
- # Read input
- # Input format: "analogX analogY cBtn zBtn
- nunchukInput = ser.readline().split()
- if(len(nunchukInput) < 4):
- continue
- try:
- nX = int(nunchukInput[0])
- nY = int(nunchukInput[1])
- cBtn = int(nunchukInput[2])
- zBtn = int(nunchukInput[3])
- except ValueError:
- continue
- currX = mouse.position()[0]
- currY = mouse.position()[1]
- moveX = moveY = 0
- # X coord
- if((nX >= 90) and (nX < 140)):
- moveX = 0
- elif((nX >= 40) and (nX < 90)):
- moveX = -0.5
- elif(nX < 40):
- moveX = -1
- elif((nX >= 140) and (nX < 170)):
- moveX = 0.5
- elif(nX >= 170):
- moveX = 1
- # Y coord
- if((nY >= 100) and (nY < 150)):
- moveY = 0
- elif((nY >= 60) and (nY < 100)):
- moveY = 0.5
- elif(nY < 60):
- moveY = 1
- elif((nY >= 150) and (nY < 180)):
- moveY = -0.5
- elif(nY >= 180):
- moveY = -1
- # Move
- newX = currX + sensibility*moveX
- newY = currY + sensibility*moveY
- mouse.move(newX, newY)
- # Click
- if(cBtn and zBtn and (not zBtn)):
- mouse.click(newX, newY, 3)
- zClicked = False
- elif(cBtn):
- mouse.click(newX, newY, 1)
- zClicked = False
- elif(zBtn and (not zClicked)):
- mouse.click(newX, newY, 2)
- zClicked = True
- except serial.serialutil.SerialException:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement