nunchuk_analog_mouse.py
1 import board 2 import adafruit_nunchuk 3 from adafruit_hid.mouse import Mouse 4 5 m = Mouse() 6 nc = adafruit_nunchuk.Nunchuk(board.I2C()) 7 8 centerX = 128 9 centerY = 128 10 11 scaleX = 0.3 12 scaleY = 0.3 13 14 cDown = False 15 zDown = False 16 17 # This is to allow double checking (only on left click - and it doesn't really work) 18 CHECK_COUNT = 0 19 20 21 # This is just to show that we're getting back data - uncomment it and hold down the buttons 22 # while True: 23 # print((0 if nc.button_C else 1, 0 if nc.button_Z else 1)) 24 25 while True: 26 x, y = nc.joystick 27 # Eliminate spurious reads 28 if x == 255 or y == 255: 29 continue 30 relX = x - centerX 31 relY = centerY - y 32 33 m.move(int(scaleX * relX), int(scaleY * relY), 0) 34 35 c = nc.button_C 36 z = nc.button_Z 37 38 if z and not zDown: 39 stillDown = True 40 for n in range(CHECK_COUNT): 41 if nc.button_Z: 42 stillDown = False 43 break 44 if stillDown: 45 m.press(Mouse.LEFT_BUTTON) 46 zDown = True 47 elif not z and zDown: 48 stillDown = True 49 for n in range(CHECK_COUNT): 50 if not nc.button_Z: 51 stillDown = False 52 break 53 if stillDown: 54 m.release(Mouse.LEFT_BUTTON) 55 zDown = False 56 if c and not cDown: 57 m.press(Mouse.RIGHT_BUTTON) 58 cDown = True 59 elif not c and cDown: 60 m.release(Mouse.RIGHT_BUTTON) 61 cDown = False