forked from tknapstad/BluePloverPi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blueraspberry_pi.py
executable file
·308 lines (268 loc) · 9.96 KB
/
blueraspberry_pi.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
# Modified from code at https://github.com/tknapstad/BluePloverPi/
# Code fixed from http://www.linuxuser.co.uk/tutorials/emulate-a-bluetooth-keyboard-with-the-raspberry-pi
# Updated to work with Bluez 5
import os
import sys
import bluetooth
from bluetooth import *
import dbus
import time
import evdev
from evdev import *
import uuid
BT_ADAPTER_PATH = "/org/bluez/hci0"
BT_UUID = str(uuid.uuid4())
class Bluetooth:
P_CTRL = 17
P_INTR = 19
HOST = 0
PORT = 1
def __init__(self):
self.scontrol = BluetoothSocket(L2CAP)
self.sinterrupt = BluetoothSocket(L2CAP)
self.scontrol.bind(("", Bluetooth.P_CTRL))
self.sinterrupt.bind(("", Bluetooth.P_INTR))
self.bus = dbus.SystemBus()
adapter = dbus.Interface(
self.bus.get_object("org.bluez", BT_ADAPTER_PATH),
"org.freedesktop.DBus.Properties",
)
# The Name and Class of a device should be statically configured
# according to the Bluez dbus interface docs. These are set
# in /etc/bluetooth/main.conf
adapter.Set("org.bluez.Adapter1", "Alias", "Bluetooth N64 Controller")
adapter.Set("org.bluez.Adapter1", "Powered", dbus.Boolean(1))
adapter.Set("org.bluez.Adapter1", "PairableTimeout", dbus.UInt32(0))
adapter.Set("org.bluez.Adapter1", "Pairable", dbus.Boolean(1))
adapter.Set("org.bluez.Adapter1", "DiscoverableTimeout", dbus.UInt32(0))
adapter.Set("org.bluez.Adapter1", "Discoverable", dbus.Boolean(1))
# Register the SDP defined in the XML file
self.manager = dbus.Interface(
self.bus.get_object("org.bluez", "/org/bluez"), "org.bluez.ProfileManager1"
)
with open(sys.path[0] + "/sdp_record.xml", "r") as fh:
self.service_record = fh.read()
def listen(self):
profile = {
"ServiceRecord": self.service_record,
}
# Register our device profile
self.manager.RegisterProfile(BT_ADAPTER_PATH, BT_UUID, profile)
print("Service record added")
self.scontrol.listen(1) # Limit of 1 connection
self.sinterrupt.listen(1)
print("Waiting for a connection")
self.ccontrol, self.cinfo = self.scontrol.accept()
print(
"Got a connection on the control channel from " + self.cinfo[Bluetooth.HOST]
)
self.cinterrupt, self.cinfo = self.sinterrupt.accept()
print(
"Got a connection on the interrupt channel fro "
+ self.cinfo[Bluetooth.HOST]
)
def send_input(self, ir):
"""Convert specified bytes and lists of bits into appropriate format and send."""
# Convert the hex array to a string
hex_str = ""
for element in ir:
if type(element) is list:
# This is our bit array - convrt it to a single byte represented
# as a char
bin_str = ""
for bit in element:
bin_str += str(bit)
hex_str += chr(int(bin_str, 2))
else:
# This is a hex value - we can convert it straight to a char
hex_str += chr(element)
# Send an input report
self.cinterrupt.send(hex_str)
class Joystick:
# Only consider devices with at least 8 buttons as joysticks
JOYSTICK_BTN_THRESHOLD = 8
# Event types
# TODO use well-known constants, like this from evdev ecodes where possible
# Buttons like Start, Z, A, B
TYPE_BTN = 1
# Joysticks like the joystick or Dpad
TYPE_JOY = 3
# Button IDs from the USB device
BTN_YELLOW_UP = 288
BTN_YELLOW_RIGHT = 289
BTN_YELLOW_DOWN = 290
BTN_YELLOW_LEFT = 291
BTN_L_BUMP = 292
BTN_R_BUMP = 293
BTN_A = 294
BTN_Z = 295
BTN_B = 296
BTN_START = 297
# Joystick axis IDs from the USB device
JOY_THUMB_HORIZ = 0
JOY_THUMB_VERT = 1
JOY_THUMB_HORIZ_ALT = 2
JOY_DPAD_HORIZ = 16
JOY_DPAD_VERT = 17
# Make signal less noisy, since the joystick reports lots of bogus changes
ANALOG_THRESH_LO = 127
ANALOG_THRESH_HI = 135
# Not sure why these show up, but seems like we can ignore them
# 0 is some empty event and 4 appears somewhat redundant with key down
IGNORED_EVENT_TYPES = {0, 4}
def __init__(self):
# Button-pressed booleans
self.buttons = {
Joystick.BTN_YELLOW_UP: 0,
Joystick.BTN_YELLOW_RIGHT: 0,
Joystick.BTN_YELLOW_DOWN: 0,
Joystick.BTN_YELLOW_LEFT: 0,
Joystick.BTN_L_BUMP: 0,
Joystick.BTN_R_BUMP: 0,
Joystick.BTN_A: 0,
Joystick.BTN_Z: 0,
Joystick.BTN_B: 0,
Joystick.BTN_START: 0,
}
# Joystick/directional axes values
self.axes = {
Joystick.JOY_THUMB_HORIZ: 0x80,
Joystick.JOY_THUMB_VERT: 0x80,
Joystick.JOY_THUMB_HORIZ_ALT: 0x80,
Joystick.JOY_DPAD_HORIZ: 0, # From -1 to 1 (right to left, respectively)
Joystick.JOY_DPAD_VERT: 0, # From -1 to 1 (down to up, respectively)
}
self.wait_for_device()
def wait_for_device(self):
"""Loop until we get a device"""
self.dev = None
while not self.dev:
devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
for device in devices:
if self.btn_count(device) >= Joystick.JOYSTICK_BTN_THRESHOLD:
self.dev = device
break
if self.dev:
break
print(
"Joystick not found in {count} devices, waiting 3 seconds and retrying ({devices})".format(
count=len(devices), devices=devices
)
)
time.sleep(3)
print("Found a joystick ({dev})".format(dev=self.dev))
def btn_count(self, device):
"""Count how many buttons exist for the specified device. Used by heuristic to identify joystick vs keyboard/mouse/etc."""
cap = device.capabilities(verbose=True)
if ("EV_KEY", ecodes.EV_KEY) not in cap:
return 0
# for v in cap.get(('EV_KEY', ecodes.EV_KEY)):
# print(v)
btns = ["BTN" in v[0] for v in cap.get(("EV_KEY", ecodes.EV_KEY))]
return btns.count(True)
def near_origin(self, value):
"""Returns a boolean indicating the specified axis value is near the origin. I.e. if it should be rounded to 0x80."""
return value > Joystick.ANALOG_THRESH_LO and value < Joystick.ANALOG_THRESH_HI
def apply_event(self, event):
"""Apply specified event to current state. Return boolean indicating if state effectively changed.
Some things like minor joystick drift can be ignored here."""
c = event.code
t = event.type
input_value = event.value
changed = True
if t == Joystick.TYPE_BTN:
assert c in self.buttons
self.buttons[c] = input_value
elif t == Joystick.TYPE_JOY:
old_val = self.axes[c]
if self.near_origin(input_value) and self.near_origin(old_val):
changed = False
self.axes[c] = input_value
else:
# TODO warn about unrecognized stuff that we might care about
pass
if changed:
print("applied event: {event}\n".format(event=event))
else:
# Could log debug here
pass
return changed
def event_loop(self, bt):
for event in self.dev.read_loop():
if (
event
and event.type not in Joystick.IGNORED_EVENT_TYPES
and self.apply_event(event)
):
bt.send_input(self.build_report())
def get_dpad_encoding(self):
"""Convert dpad directions into the rotational-encoding array."""
up = self.axes[Joystick.JOY_DPAD_VERT] == -1
down = self.axes[Joystick.JOY_DPAD_VERT] == 1
right = self.axes[Joystick.JOY_DPAD_HORIZ] == 1
left = self.axes[Joystick.JOY_DPAD_HORIZ] == -1
if up and right:
return [0, 0, 0, 1]
if down and right:
return [0, 0, 1, 1]
if down and left:
return [0, 1, 0, 1]
if up and left:
return [0, 1, 1, 1]
if up:
return [0, 0, 0, 0]
if right:
return [0, 0, 1, 0]
if down:
return [0, 1, 0, 0]
if left:
return [0, 1, 1, 0]
return [1, 1, 1, 1]
def build_report(self):
"""Build an input report for the current state."""
return [
0xA1, # This is an input report
0x01, # Report Id
self.axes[Joystick.JOY_THUMB_HORIZ],
self.axes[Joystick.JOY_THUMB_VERT],
self.axes[Joystick.JOY_THUMB_HORIZ_ALT],
0x80, # Not sure what these are
0x80,
# TODO verify the ordering is right
[
self.buttons[Joystick.BTN_YELLOW_LEFT],
self.buttons[Joystick.BTN_YELLOW_DOWN],
self.buttons[Joystick.BTN_YELLOW_RIGHT],
self.buttons[Joystick.BTN_YELLOW_UP],
]
+ self.get_dpad_encoding(),
[
0,
0,
self.buttons[Joystick.BTN_START],
self.buttons[Joystick.BTN_B],
self.buttons[Joystick.BTN_Z],
self.buttons[Joystick.BTN_A],
self.buttons[Joystick.BTN_R_BUMP],
self.buttons[Joystick.BTN_L_BUMP],
],
# Not sure what these are for...
[
0,
1,
0,
0,
0,
0,
0,
0,
],
]
if __name__ == "__main__":
if not os.geteuid() == 0:
sys.exit("Only root can run this script")
bt = Bluetooth()
bt.listen()
dev = Joystick()
dev.event_loop(bt)