-
Notifications
You must be signed in to change notification settings - Fork 1
/
rest_controller_service.py
153 lines (129 loc) · 5.01 KB
/
rest_controller_service.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from enum import Enum
import logging
from joycontrol.controller import Controller
from joycontrol.controller_state import ControllerState, StickState
from joycontrol.memory import FlashMemory
from joycontrol.protocol import ControllerProtocol
from joycontrol.server import create_hid_server
class ControllerStick(str, Enum):
l_stick = "l_stick"
r_stick = "r_stick"
class ControllerAxis(str, Enum):
x_axis = "x_axis"
y_axis = "y_axis"
class ControllerButton(str, Enum):
y = 'y'
x = 'x'
b = 'b'
a = 'a'
r = 'r'
zr = 'zr'
minus = 'minus'
plus = 'plus'
r_stick = 'r_stick'
l_stick = 'l_stick'
home = 'home'
capture = 'capture'
down = 'down'
up = 'up'
right = 'right'
left = 'left'
l = 'l'
zl = 'zl'
sr = 'sr'
sl = 'sl'
class SwitchControllerService:
def __init__(self):
self.controller_state: ControllerState = None
self.transport = None
async def disconnect(self):
if self.is_connected():
await self.transport.close()
self.transport = None
async def connect(self, controller_type: str, reconnect_address: str, spi_firm: bytes):
if spi_firm is None:
spi_flash = FlashMemory()
else:
spi_flash = FlashMemory(spi_flash_memory_data = spi_firm)
lib_controller_type = Controller.from_arg(controller_type)
def create_controller_protocol():
return ControllerProtocol(lib_controller_type, spi_flash=spi_flash)
factory = create_controller_protocol
transport, protocol = await create_hid_server(factory, reconnect_bt_addr=reconnect_address)
controller_state = protocol.get_controller_state()
self.controller_state = controller_state
self.transport = transport
return transport._itr_sock.getpeername()[0]
async def get_status(self):
if not self.is_connected():
return {"connected" : "false"}
else:
peer = self.transport._itr_sock.getpeername()[0]
buttonList = self.controller_state.button_state.get_available_buttons()
button_dict = { b : self.controller_state.button_state.get_button(b) for b in buttonList }
controller_type = self.controller_state.get_controller().name
nfc_active = self.controller_state.get_nfc() is not None
l_stick_dict = convertStickState(self.controller_state.l_stick_state)
r_stick_dict = convertStickState(self.controller_state.r_stick_state)
return { "connected" : "true",
"peer" : peer,
"controller_type" : controller_type ,
"buttons" : button_dict,
"nfc_active": nfc_active ,
"left_stick" : l_stick_dict,
"right_stick" : r_stick_dict
}
async def press_controller_button(self, button: str):
if not self.is_connected():
return
self.controller_state.button_state.set_button(button, pushed=True)
await self.controller_state.send()
async def release_controller_button(self, button: str):
if not self.is_connected():
return
self.controller_state.button_state.set_button(button, pushed=False)
await self.controller_state.send()
async def set_stick_axis(self, stick: ControllerStick, axis: ControllerAxis, value: int):
if not self.is_connected():
return
if stick == ControllerStick.l_stick:
stick_to_change = self.controller_state.l_stick_state
if stick == ControllerStick.r_stick:
stick_to_change = self.controller_state.r_stick_state
if axis == ControllerAxis.x_axis:
stick_to_change.set_h(value)
if axis == ControllerAxis.y_axis:
stick_to_change.set_v(value)
await self.controller_state.send()
async def center_stick(self, stick: ControllerStick):
if not self.is_connected():
return
if stick == ControllerStick.l_stick:
stick_to_change = self.controller_state.l_stick_state
if stick == ControllerStick.r_stick:
stick_to_change = self.controller_state.r_stick_state
stick_to_change.set_center()
await self.controller_state.send()
async def set_nfc_data(self, nfc_data: bytes):
if not self.is_connected():
return
old_nfc = self.controller_state.get_nfc()
if old_nfc is None and nfc_data is None:
return
self.controller_state.set_nfc(nfc_data)
def is_connected(self):
if self.transport is None:
return False
try:
self.transport._itr_sock.getpeername()[0]
except:
return False
return True
def convertStickState(stick_state: StickState):
if stick_state is None:
return None
else:
return { "x_axis" : stick_state.get_h(),
"y_axis" : stick_state.get_v(),
"is_center" : stick_state.is_center()
}