-
Notifications
You must be signed in to change notification settings - Fork 1
/
nunchucks.py
76 lines (61 loc) · 2.02 KB
/
nunchucks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import machine
import time
"""
Connections:
- Nunchuk1:
- 3V3 → Pin 36 (3V3)
- GND → Pin 38 (GND)
- SDA → Pin 21 (GP16)
- SCL → Pin 22 (GP17)
- Nunchuk2:
- 3V3 → Pin 36 (3V3)
- GND → Pin 38 (GND)
- SDA → Pin 24 (GP18)
- SCL → Pin 25 (GP19)
"""
class Nunchuck:
INIT_COMMANDS = [b'\xf0\x55', b'\xfb\x00']
def __init__(self, i2c, poll=True, poll_interval=50):
self.i2c = i2c
self.address = 0x52
self.buffer = bytearray(6)
self.last_poll = time.ticks_ms()
self.polling_threshold = poll_interval if poll else -1
self._initialize_nunchuck()
def _initialize_nunchuck(self):
for command in self.INIT_COMMANDS:
self.i2c.writeto(self.address, command)
def _update(self):
self.i2c.writeto(self.address, b'\x00')
self.i2c.readfrom_into(self.address, self.buffer)
def _poll(self):
if self.polling_threshold > 0 and time.ticks_diff(time.ticks_ms(), self.last_poll) > self.polling_threshold:
self._update()
self.last_poll = time.ticks_ms()
def buttons(self):
self._poll()
return (
not (self.buffer[5] & 0x02), # C button
not (self.buffer[5] & 0x01) # Z button
)
def joystick(self):
self._poll()
return self.buffer[0], self.buffer[1]
def main():
# Initialize the I2C bus
i2c_devices = [
machine.I2C(0, scl=machine.Pin(17), sda=machine.Pin(16), freq=100000),
machine.I2C(1, scl=machine.Pin(19), sda=machine.Pin(18), freq=100000)
]
nunchuks = [Nunchuck(i2c, poll=True, poll_interval=100) for i2c in i2c_devices]
# Infinite loop for continuously querying and displaying data
while True:
for idx, nunchuk in enumerate(nunchuks, 1):
print(f"Nunchuk {idx}:")
print("Joystick:", nunchuk.joystick())
print("Buttons:", nunchuk.buttons())
print("-" * 40)
print("=" * 40)
time.sleep(0.5)
if __name__ == "__main__":
main()