Simple test¶
Ensure your device works with this simple test.
examples/wiichuck_simpletest.py¶
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import time
5import board
6from wiichuck.nunchuk import Nunchuk
7
8nc = Nunchuk(board.I2C())
9
10while True:
11 x, y = nc.joystick
12 ax, ay, az = nc.acceleration
13 print("joystick = {},{}".format(x, y))
14 print("accceleration ax={}, ay={}, az={}".format(ax, ay, az))
15
16 if nc.buttons.C:
17 print("button C")
18 if nc.buttons.Z:
19 print("button Z")
20 time.sleep(0.5)
examples/nunchuk_analog_mouse.py¶
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5import usb_hid
6from adafruit_hid.mouse import Mouse
7from wiichuck.nunchuk import Nunchuk
8
9m = Mouse(usb_hid.devices)
10nc = Nunchuk(board.I2C())
11
12centerX = 128
13centerY = 128
14
15scaleX = 0.15
16scaleY = 0.15
17
18cDown = False
19zDown = False
20
21# This is to allow double checking (only on left click - and it doesn't really work)
22CHECK_COUNT = 0
23
24
25# This is just to show that we're getting back data - uncomment it and hold down the buttons
26# while True:
27# print((0 if nc.button_C else 1, 0 if nc.button_Z else 1))
28
29while True:
30 x, y = nc.joystick
31 # Eliminate spurious reads
32 if x == 255 or y == 255:
33 continue
34 relX = x - centerX
35 relY = centerY - y
36
37 m.move(int(scaleX * relX), int(scaleY * relY), 0)
38
39 buttons = nc.buttons
40 c = buttons.C
41 z = buttons.Z
42
43 if z and not zDown:
44 stillDown = True
45 for n in range(CHECK_COUNT):
46 if nc.button_Z:
47 stillDown = False
48 break
49 if stillDown:
50 m.press(Mouse.LEFT_BUTTON)
51 zDown = True
52 elif not z and zDown:
53 stillDown = True
54 for n in range(CHECK_COUNT):
55 if not nc.button_Z:
56 stillDown = False
57 break
58 if stillDown:
59 m.release(Mouse.LEFT_BUTTON)
60 zDown = False
61 if c and not cDown:
62 m.press(Mouse.RIGHT_BUTTON)
63 cDown = True
64 elif not c and cDown:
65 m.release(Mouse.RIGHT_BUTTON)
66 cDown = False
examples/nunchuk_accel_mouse.py¶
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4import board
5import usb_hid
6from adafruit_hid.mouse import Mouse
7from wiichuck.nunchuk import Nunchuk
8
9m = Mouse(usb_hid.devices)
10nc = Nunchuk(board.I2C())
11
12centerX = 120
13centerY = 110
14
15scaleX = 0.4
16scaleY = 0.5
17
18cDown = False
19zDown = False
20
21# This is to allow double checking (only on left click - and it doesn't really work)
22CHECK_COUNT = 0
23
24
25# This is just to show that we're getting back data - uncomment it and hold down the buttons
26# while True:
27# print((0 if nc.button_C else 1, 0 if nc.button_Z else 1))
28
29while True:
30 accel = nc.acceleration
31 # print(accel)
32 # x, y = nc.joystick
33 # print((x,y))
34 x = accel[0] / 4
35 y = accel[1] / 4
36 print((x, y))
37 # Eliminate spurious reads
38 if x == 255 or y == 255:
39 continue
40 relX = x - centerX
41 relY = y - centerY
42
43 m.move(int(scaleX * relX), int(scaleY * relY), 0)
44 buttons = nc.buttons
45
46 c = buttons.C
47 z = buttons.Z
48
49 if z and not zDown:
50 stillDown = True
51 for n in range(CHECK_COUNT):
52 if nc.button_Z:
53 stillDown = False
54 break
55 if stillDown:
56 m.press(Mouse.LEFT_BUTTON)
57 zDown = True
58 elif not z and zDown:
59 stillDown = True
60 for n in range(CHECK_COUNT):
61 if not nc.button_Z:
62 stillDown = False
63 break
64 if stillDown:
65 m.release(Mouse.LEFT_BUTTON)
66 zDown = False
67 if c and not cDown:
68 m.press(Mouse.RIGHT_BUTTON)
69 cDown = True
70 elif not c and cDown:
71 m.release(Mouse.RIGHT_BUTTON)
72 cDown = False
examples/classic_controller_simpletest.py¶
1# SPDX-FileCopyrightText: 2021 John Furcean
2# SPDX-License-Identifier: MIT
3
4import board
5from wiichuck.classic_controller import ClassicController
6
7controller = ClassicController(board.I2C())
8
9while True:
10 joysticks, buttons, dpad, triggers = controller.values
11
12 # center for the right joystick is (16,16)
13 if (joysticks.rx, joysticks.ry) != (16, 16):
14 print("Right joystick = {},{}".format(joysticks.rx, joysticks.ry))
15
16 # center for the left joystick is (32,32)
17 if (joysticks.lx, joysticks.ly) != (32, 32):
18 print("Left joystick = {},{}".format(joysticks.lx, joysticks.ly))
19
20 # triggers when not pressed is at (0,0)
21 # Classic Controler Pro as no potentiometer and only report 0 or 31
22 if (triggers.left, triggers.right) != (0, 0):
23 print("Trigger left and right = {},{}".format(triggers.left, triggers.right))
24
25 if buttons.A:
26 print("button A")
27 if buttons.B:
28 print("button B")
29 if buttons.X:
30 print("button X")
31 if buttons.Y:
32 print("button Y")
33 if buttons.R:
34 print("button R")
35 if buttons.L:
36 print("button L")
37 if buttons.ZR:
38 print("button ZR")
39 if buttons.ZL:
40 print("button ZL")
41 if buttons.start:
42 print("button start")
43 if buttons.select:
44 print("button select")
45 if buttons.home:
46 print("button home")
47
48 if dpad.up:
49 print("dpad up")
50 if dpad.down:
51 print("dpad down")
52 if dpad.right:
53 print("dpad right")
54 if dpad.left:
55 print("dpad left")
examples/classic_controller_pro_simpletest.py¶
1# SPDX-FileCopyrightText: 2021 John Furcean
2# SPDX-License-Identifier: MIT
3
4import board
5from wiichuck.classic_controller import ClassicController
6
7controller = ClassicController(board.I2C())
8
9while True:
10 # Classic Controller Pro has no trigger/potentiometer and only use L and R
11 joysticks, buttons, dpad, _ = controller.values
12
13 # center for the right joystick is (16,16)
14 if (joysticks.rx, joysticks.ry) != (16, 16):
15 print("Right joystick = {},{}".format(joysticks.rx, joysticks.ry))
16
17 # center for the left joystick is (32,32)
18 if (joysticks.lx, joysticks.ly) != (32, 32):
19 print("Left joystick = {},{}".format(joysticks.lx, joysticks.ly))
20
21 if buttons.A:
22 print("button A")
23 if buttons.B:
24 print("button B")
25 if buttons.X:
26 print("button X")
27 if buttons.Y:
28 print("button Y")
29 if buttons.R:
30 print("button R")
31 if buttons.L:
32 print("button L")
33 if buttons.ZR:
34 print("button ZR")
35 if buttons.ZL:
36 print("button ZL")
37 if buttons.start:
38 print("button start")
39 if buttons.select:
40 print("button select")
41 if buttons.home:
42 print("button home")
43
44 if dpad.up:
45 print("dpad up")
46 if dpad.down:
47 print("dpad down")
48 if dpad.right:
49 print("dpad right")
50 if dpad.left:
51 print("dpad left")
examples/guitar_simpletest.py¶
1# SPDX-FileCopyrightText: Copyright (c) 2021 John Furcean
2#
3# SPDX-License-Identifier: MIT
4import board
5from wiichuck.guitar import Guitar
6
7guitar = Guitar(board.I2C())
8
9while True:
10 joystic, buttons, strum, whammy, touchbar = guitar.values
11
12 # Joystick: (0-63,0-63), middle is (32,32)
13 if joystic != (32, 32):
14 print(f"Joystick (x,y): {joystic}")
15
16 # Whammy Bar: 0-31
17 if whammy > 0:
18 print(f"Whammy Bar: {whammy}")
19
20 # strum
21 if strum.up:
22 print("STRUM UP")
23 if strum.down:
24 print("STRUM DOWN")
25
26 # Neck Buttons
27 if buttons.orange:
28 print("Button Pressed: ORANGE")
29 if buttons.blue:
30 print("Button Pressed: BLUE")
31 if buttons.yellow:
32 print("Button Pressed: YELLOW")
33 if buttons.red:
34 print("Button Pressed: RED")
35 if buttons.green:
36 print("Button Pressed: GREEN")
37
38 if buttons.plus:
39 print("Button Pressed: PLUS")
40 if buttons.minus:
41 print("Button Pressed: MINUS")
examples/udraw_simpletest.py¶
1# SPDX-FileCopyrightText: 2021 John Furcean
2# SPDX-License-Identifier: MIT
3
4import board
5from wiichuck.udraw import UDraw
6
7controller = UDraw(board.I2C())
8
9while True:
10 position, buttons, pressure = controller.values
11
12 # off grid position is (4095,4095)
13 if (position.x, position.y) != (4095, 4095):
14 print("Pen position = {},{}".format(position.x, position.y))
15
16 # off pressure on the tip of the pen is 8
17 if pressure != 8:
18 print("Pen pressure = {}".format(pressure))
19
20 if buttons.C:
21 print("button C")
22 if buttons.Z:
23 print("button Z")
24 if buttons.tip:
25 print("button tip")
examples/udraw_mouse.py¶
1# SPDX-FileCopyrightText: 2021 David Glaude
2# SPDX-License-Identifier: MIT
3
4import board
5import usb_hid
6from adafruit_hid.mouse import Mouse
7from wiichuck.udraw import UDraw
8
9udraw = UDraw(board.I2C())
10
11m = Mouse(usb_hid.devices)
12
13zDown = False
14pDown = False
15
16oldx = 4095
17oldy = 4095
18
19while True:
20 # We don't use the pressure, maybe converting to watcom protocol would permit that
21 position, buttons, _ = udraw.values
22
23 P = buttons.tip or buttons.C # Both the C button and tip work as LEFT mouse click
24
25 # Handeling the button to create mouse click
26 if P and not pDown:
27 m.press(Mouse.LEFT_BUTTON)
28 pDown = True
29 elif not P and pDown:
30 m.release(Mouse.LEFT_BUTTON)
31 pDown = False
32
33 if buttons.Z and not zDown:
34 m.press(Mouse.RIGHT_BUTTON)
35 zDown = True
36 elif not buttons.Z and zDown:
37 m.release(Mouse.RIGHT_BUTTON)
38 zDown = False
39
40 # Values (4095,4095) mean the pen is not near the tablet
41 if position.x == 4095 or position.y == 4095:
42 oldx = 4095
43 oldy = 4095
44 continue # We remember that the pen was raised UP
45
46 # If we reach here, the pen was UP and is now DOWN
47 if oldx == 4095 or oldy == 4095:
48 oldx = position.x
49 oldy = position.y
50 continue
51
52 if (position.x != oldx) or (
53 position.y != oldy
54 ): # PEN has moved we move the HID mouse
55 m.move((position.x - oldx), (oldy - position.y), 0)
56 oldx = position.x
57 oldy = position.y