-
Notifications
You must be signed in to change notification settings - Fork 20
/
__init__.py
166 lines (130 loc) · 4.91 KB
/
__init__.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
154
155
156
157
158
159
160
161
162
163
164
165
166
import re
import time
from platypush.backend import Backend
from platypush.message.event.wiimote import (
WiimoteEvent,
WiimoteConnectionEvent,
WiimoteDisconnectionEvent,
)
class WiimoteBackend(Backend):
"""
Backend to communicate with a Nintendo WiiMote controller
Requires:
* **python3-wiimote** (follow instructions at https://github.com/azzra/python3-wiimote)
"""
_wiimote = None
_inactivity_timeout = 300
_connection_attempts = 0
_last_btn_event_time = 0
_bdaddr = None
def __init__(
self, bdaddr=_bdaddr, inactivity_timeout=_inactivity_timeout, *args, **kwargs
):
"""
:param bdaddr: If set, connect to this specific Wiimote physical address (example: 00:11:22:33:44:55)
:type bdaddr: str
:param inactivity_timeout: Number of seconds elapsed from the last Wiimote action before disconnecting the
device (default: 300 seconds)
:type inactivity_timeout: float
"""
super().__init__(*args, **kwargs)
self._bdaddr = bdaddr
self._inactivity_timeout = inactivity_timeout
def get_wiimote(self):
import cwiid
if not self._wiimote:
if self._bdaddr:
self._wiimote = cwiid.Wiimote(bdaddr=self._bdaddr)
else:
self._wiimote = cwiid.Wiimote()
self._wiimote.enable(cwiid.FLAG_MOTIONPLUS)
self._wiimote.rpt_mode = (
cwiid.RPT_ACC | cwiid.RPT_BTN | cwiid.RPT_MOTIONPLUS
)
self.logger.info('WiiMote connected')
self._last_btn_event_time = time.time()
self.bus.post(WiimoteConnectionEvent())
return self._wiimote
def get_state(self):
import cwiid
wm = self.get_wiimote()
state = wm.state
parsed_state = {}
# Get buttons
all_btns = [attr for attr in dir(cwiid) if attr.startswith('BTN_')]
parsed_state['buttons'] = {
btn: True
for btn in all_btns
if state.get('buttons', 0) & getattr(cwiid, btn) != 0
}
# Get LEDs
all_leds = [attr for attr in dir(cwiid) if re.match(r'LED\d_ON', attr)]
parsed_state['led'] = {
led[:4]: True
for led in all_leds
if state.get('leds', 0) & getattr(cwiid, led) != 0
}
# Get errors
all_errs = [attr for attr in dir(cwiid) if attr.startswith('ERROR_')]
parsed_state['error'] = {
err: True
for err in all_errs
if state.get('errs', 0) & getattr(cwiid, err) != 0
}
parsed_state['battery'] = round(state.get('battery', 0) / cwiid.BATTERY_MAX, 3)
parsed_state['rumble'] = bool(state.get('rumble', 0))
if 'acc' in state:
parsed_state['acc'] = tuple(int(acc / 5) * 5 for acc in state['acc'])
if 'motionplus' in state:
parsed_state['motionplus'] = {
'angle_rate': tuple(
int(angle / 100) for angle in state['motionplus']['angle_rate']
),
'low_speed': state['motionplus']['low_speed'],
}
return parsed_state
def close(self):
if not self._wiimote:
return
try:
self._wiimote.close()
except Exception as e:
self.logger.warning('Could not close Wiimote connection: {}'.format(str(e)))
self._wiimote = None
self.bus.post(WiimoteDisconnectionEvent())
def run(self):
super().run()
self._connection_attempts = 0
last_state = {}
self.logger.info('Initialized Wiimote backend')
while not self.should_stop():
try:
state = self.get_state()
changed_state = {
k: state[k] for k in state.keys() if state[k] != last_state.get(k)
}
if changed_state:
self.bus.post(WiimoteEvent(**changed_state))
if 'buttons' in changed_state:
self._last_btn_event_time = time.time()
elif (
last_state
and time.time() - self._last_btn_event_time
>= self._inactivity_timeout
):
self.logger.info('Wiimote disconnected upon timeout')
self.close()
last_state = state
time.sleep(0.1)
except RuntimeError as e:
if (
type(e) == RuntimeError
and str(e) == 'Error opening wiimote connection'
):
if self._connection_attempts == 0:
self.logger.info('Press 1+2 to pair your WiiMote controller')
else:
self.logger.exception(e)
self.close()
self._connection_attempts += 1
# vim:sw=4:ts=4:et: