-
Notifications
You must be signed in to change notification settings - Fork 28
/
NES2USB.py
75 lines (63 loc) · 2.4 KB
/
NES2USB.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
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.gamepad import Gamepad
from adafruit_hid.keycode import Keycode
from time import sleep
import board
gamepad = Gamepad(usb_hid.devices)
button_status = {'up' : 0, 'down' : 0, 'left' : 0, 'right' : 0, 'select' : 0, 'start' : 0, 'a' : 0, 'b' : 0}
buttons = {0 : 'a', 1 : 'b', 2 : 'select', 3 : 'start', 4 : 'up', 5 : 'down', 6 : 'left', 7 : 'right'}
keyboard_buttons = {'up' : Keycode.UP_ARROW, 'down' : Keycode.DOWN_ARROW, 'left' : Keycode.LEFT_ARROW, 'right' : Keycode.RIGHT_ARROW,
'a' : Keycode.LEFT_CONTROL, 'b' : Keycode.SPACE, 'select' : Keycode.LEFT_ALT, 'start' : Keycode.ENTER}
mode = 1 # 1 for Keyboard Emulation, 2 for Joystick
latch = digitalio.DigitalInOut(board.GP5)
clock = digitalio.DigitalInOut(board.GP4)
data = digitalio.DigitalInOut(board.GP6)
latch.direction = digitalio.Direction.OUTPUT
clock.direction = digitalio.Direction.OUTPUT
data.direction = digitalio.Direction.INPUT
data.pull = digitalio.Pull.UP
keyboard = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(keyboard)
latch.value = False
clock.value = False
previousState = False
delaytime = 0.0001
while True:
latch.value = True
sleep(delaytime)
latch.value = False
sleep(delaytime)
button_status[buttons[0]] = data.value
for x in range(0, 7, 1):
clock.value = True
sleep(delaytime)
clock.value = False
sleep(delaytime)
button_status[buttons[x + 1]] = data.value
if button_status['select'] == False and button_status['start'] == False and button_status['up'] == False:
if mode == 2:
mode = 1
else:
mode = 2
keyboard.release_all()
print(mode)
sleep(0.3)
press_buttons = []
release_buttons = []
for x in buttons:
if mode == 2:
if button_status[buttons[x]] == True:
gamepad.release_buttons(x + 1)
else:
gamepad.press_buttons(x + 1)
else:
if button_status[buttons[x]] == True:
release_buttons.append(keyboard_buttons[buttons[x]])
else:
press_buttons.append(keyboard_buttons[buttons[x]])
if mode == 1:
keyboard.press(*press_buttons)
keyboard.release(*release_buttons)