Advertisement
tuixte

Wii Nunchuk Mouse

Feb 23rd, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. from pymouse import PyMouse
  2. import serial
  3.  
  4. serialPort = raw_input("Serial port: ")
  5. try:
  6.     ser = serial.Serial(serialPort, 19200)
  7.     ser.timeout = None
  8. except serial.SerialException:
  9.     print "No device connected. Exiting..."
  10.     time.sleep(1)
  11.     sys.exit()
  12.  
  13. sensibility = 10.0
  14. if ((userSens = raw_input("Sensibility (leave blank for default value 10): ")) != ""):
  15.     sensibility = float(userSens)
  16.  
  17. mouse = PyMouse()
  18. zClicked = False
  19.  
  20. while True:
  21.     if (ser.inWaiting() > 0):
  22.         try:
  23.             # Read input
  24.             # Input format: "analogX analogY cBtn zBtn
  25.             nunchukInput = ser.readline().split()
  26.             if(len(nunchukInput) < 4):
  27.                 continue
  28.             try:
  29.                 nX = int(nunchukInput[0])
  30.                 nY = int(nunchukInput[1])
  31.                 cBtn = int(nunchukInput[2])
  32.                 zBtn = int(nunchukInput[3])
  33.             except ValueError:
  34.                 continue
  35.            
  36.             currX = mouse.position()[0]
  37.             currY = mouse.position()[1]
  38.             moveX = moveY = 0
  39.            
  40.             # X coord
  41.             if((nX >= 90) and (nX < 140)):
  42.                 moveX = 0
  43.             elif((nX >= 40) and (nX < 90)):
  44.                 moveX = -0.5
  45.             elif(nX < 40):
  46.                 moveX = -1
  47.             elif((nX >= 140) and (nX < 170)):
  48.                 moveX = 0.5
  49.             elif(nX >= 170):
  50.                 moveX = 1
  51.            
  52.             # Y coord
  53.             if((nY >= 100) and (nY < 150)):
  54.                 moveY = 0
  55.             elif((nY >= 60) and (nY < 100)):
  56.                 moveY = 0.5
  57.             elif(nY < 60):
  58.                 moveY = 1
  59.             elif((nY >= 150) and (nY < 180)):
  60.                 moveY = -0.5
  61.             elif(nY >= 180):
  62.                 moveY = -1
  63.            
  64.             # Move
  65.             newX = currX + sensibility*moveX
  66.             newY = currY + sensibility*moveY
  67.             mouse.move(newX, newY)
  68.            
  69.             # Click
  70.             if(cBtn and zBtn and (not zBtn)):
  71.                 mouse.click(newX, newY, 3)
  72.                 zClicked = False
  73.             elif(cBtn):
  74.                 mouse.click(newX, newY, 1)
  75.                 zClicked = False
  76.             elif(zBtn and (not zClicked)):
  77.                 mouse.click(newX, newY, 2)
  78.                 zClicked = True
  79.         except serial.serialutil.SerialException:
  80.             pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement