-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamepad.py
61 lines (53 loc) · 1.3 KB
/
gamepad.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
import board
import struct
import usb_hid
import pew
pew.init()
screen = pew.Pix()
for gamepad in usb_hid.devices:
if gamepad.usage_page == 0x01 and gamepad.usage == 0x05:
break
else:
raise RuntimeError("Gamepad HID device not found")
report = bytearray(6)
while True:
buttons = pew.keys()
report_buttons = 0
if buttons & pew.K_O:
screen.pixel(6, 3, 3)
report_buttons |= 0x01
else:
screen.pixel(6, 3, 1)
if buttons & pew.K_X:
screen.pixel(6, 5, 3)
report_buttons |= 0x02
else:
screen.pixel(6, 5, 1)
if buttons & pew.K_UP:
y = -127
screen.pixel(2, 3, 3)
screen.pixel(2, 5, 1)
elif buttons & pew.K_DOWN:
y = 127
screen.pixel(2, 3, 1)
screen.pixel(2, 5, 3)
else:
y = 0
screen.pixel(2, 3, 1)
screen.pixel(2, 5, 1)
if buttons & pew.K_LEFT:
x = -127
screen.pixel(1, 4, 3)
screen.pixel(3, 4, 1)
elif buttons & pew.K_RIGHT:
x = 127
screen.pixel(1, 4, 1)
screen.pixel(3, 4, 3)
else:
x = 0
screen.pixel(1, 4, 1)
screen.pixel(3, 4, 1)
struct.pack_into('<Hbbbb', report, 0, report_buttons, x, y, 0, 0)
gamepad.send_report(report)
pew.show(screen)
pew.tick(1/12)