-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcap.py
executable file
·262 lines (201 loc) · 8.05 KB
/
cap.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
This script read/write data comming from a fanatec wheel.
Copyright (C) 2015 darknao
https://github.com/darknao/btClubSportWheel
This file is part of btClubSportWheel.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Fanatec Pinout:
1 2 1.MISO 8.CS
O O 2.MOSI 9.USB+
3 O O O O 6 3.GND 10.
7 O 4 5 O 8 4.5v 11.DP1
9 O O 10 5.GND 12.USB-
11 O O 12 6.SCK 13.DP2
13 O 7.3v3
Porsche/BMW/Formula connector pinout:
--
|-- 1 5v 4
|-- 2 3v3 7
|-- 3 GND 3
|-- 4 GND 5
|-- 5 MISO 1
|-- 6 MOSI 2
|-- 7 SCK 6
|-- 8 CS 8
--
UNI HUB pinout:
--
|-- 1 USB- 12
|-- 2 DP2 13
|-- 3 DP1 11
|-- 4 CS 8
|-- 5 SCK 6
|-- 6 MOSI 2
|-- 7 MISO 1
|-- 8 GND 5
|-- 9 GND 3
|-- 10 3v3 7
|-- 11 5v 4
|-- 12 USB+ 9
--
DataPort (for information only):
r---------j 1. USB+
r--l l--j 2. DP1
| | 3. DP2
| 1 2 3 4 5 6 | 4.
| _ _ _ _ _ _ | 5.
----------------- 6. USB-
Fanatec Packet INPUT:
headers
^ wheel ID?
| ^ display
| | ^ revlights
| | | ^ rumbles
| | | | ^ nothing CRC8
|__|__|________|_____|_____|____________________________________________________________________|__|
a5 03 FF FF FF FF 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 XX
0 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
display:
1 byte for 7segments + dot
1
_______
| |
6 | | 2
| 7 |
|-------|
| | 3
5 | |
|_______| o 8
4
revlights:
9 leds = 9 bits
FF 01
1111 1111 0000 0001
2 to 9 1
rumbles:
1 byte per motor (left & right)
Fanatec Packet OUTPUT:
headers
^ wheel ID? encoder
| ^ buttons ^ PS btns
| | array1 | ^
| | ^ Joy | buttons |
| | | X|Y | copy |sqr
| | | ^ | ^ L&R|
| | | | | | |
|__|__|________|_____|__|_____|_____|
a5 04 00 00 00 00 00 00 00 00 00 40
0 1 2 3 4 5 6 7 8 9 10 11
details:
buttons array1:
o o o o o o o o | o o o o o o o o | o o o o o o o o
'buttons copy' report the same buttons as 'buttons array', in another order (UNI HUB only)
Except for BUT_5 which is only reported once (first octet, values are 08 10 20)
PS btns are Playstation buttons (UNI HUB)
Firt octet is the cross/square/triangle/round buttons
Second one is L&R buttons
Few samples (incoming packet):
01:14:00 0xa5 0x3 0x0 0x0 0x0 0x0 0x0 0x0 0x48 0x83 0x87 0x43 0x5b 0x9a 0x39 0xa0 0xc9 0xb5 0xd8 0x19 0x72 0x30 0x28 0xfa 0x62 0xf7 0x93 0xc 0xcb 0x98 0xd0 0x12 0x42
01:14:03 0xa5 0x3 0x0 0x80 0x0 0x0 0x0 0x0 0x48 0x83 0x87 0x43 0x5b 0x9a 0x39 0xa0 0xc9 0xb5 0xd8 0x19 0x72 0x30 0x28 0xfa 0x62 0xf7 0x93 0xc 0xcb 0x98 0xd0 0x12 0x58
01:14:04 0xa5 0x3 0x0 0x0 0x0 0x0 0x0 0x0 0x48 0x83 0x87 0x43 0x5b 0x9a 0x39 0xa0 0xc9 0xb5 0xd8 0x19 0x72 0x30 0x28 0xfa 0x62 0xf7 0x93 0xc 0xcb 0x98 0xd0 0x12 0x42
01:14:04 0xa5 0x3 0x10 0x0 0x0 0x0 0x0 0x0 0x48 0x83 0x87 0x43 0x5b 0x9a 0x39 0xa0 0xc9 0xb5 0xd8 0x19 0x72 0x30 0x28 0xfa 0x62 0xf7 0x93 0xc 0xcb 0x98 0xd0 0x12 0x3b
01:14:04 0xa5 0x3 0x0 0x0 0x0 0x0 0x0 0x0 0x48 0x83 0x87 0x43 0x5b 0x9a 0x39 0xa0 0xc9 0xb5 0xd8 0x19 0x72 0x30 0x28 0xfa 0x62 0xf7 0x93 0xc 0xcb 0x98 0xd0 0x12 0x42
UNI HUB:
20:08:26 0xa5 0x4 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x40 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x13 0x4 True
20:08:26 0xa5 0x4 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x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0x0 0x13 0xfc True
"""
import spidev
import time
import random
from bitstring import BitArray
import crcmod
def hexf(byte):
""" Convert byte to hex string """
return "%4s" % hex(byte)
def printhex(listOfBytes, compare=None):
""" Print a list of bytes,
and optionnaly, show the diff with another list of bytes (compare)"""
newBytes = map(hexf, listOfBytes)
if compare is not None:
if len(compare) == len(listOfBytes):
for idx in range(len(listOfBytes)):
if listOfBytes[idx] != compare[idx]:
newBytes[idx] = green(newBytes[idx])
else:
newBytes = map(green, newBytes)
hexstring = " ".join(newBytes)
print '%s %s %s' % (time.strftime('%8X'), hexstring, checkcrc(listOfBytes))
def printascii(listOfBytes):
""" Print the ascii representation (if possible) of a list of bytes """
print repr("".join(map(chr, listOfBytes)))
def green(string):
""" colorize the string in green """
return "\033[92m%s\033[0m" % string
""" Convert string to hex """
s2h = lambda x: int(x, 16)
def crcs(stri):
""" return the CRC of a string of hex bytes """
return crc(map(s2h, stri.split()))
def crc(bytes):
""" return the CRC of a list of bytes """
f = crcmod.mkCrcFun(0x131)
return f("".join(map(chr, bytes)))
def checkcrc(bytes):
CRC = crc(bytes[:-1])
if CRC == bytes[-1]:
return True
else:
return False
def buildcrc(data):
""" Put the CRC at the end of a list of bytes """
crcbyte = crc(data[:-1])
data[-1] = crcbyte
return data
def send_data(data):
""" Send a fanatec packet to SPI """
data = buildcrc(data)
spi.writebytes(data)
if __name__ == '__main__':
spi = spidev.SpiDev(0, 0)
oldbytes = []
# prebuild a fanatec packet
dataout = [0 for i in range(32)]
dataout[0] = 0xa5
dataout[1] = 0x03
"""
dataout[2] = 0x49
dataout[3] = 0x36
dataout[4] = 0x49
"""
# shutdown everything (leds, display and rumble)
send_data(dataout)
while True:
bytes = spi.readbytes(34)
raw = b"".join(map(chr, bytes))
barray = BitArray(bytes=raw)
barray_s = barray << 1
# unkbit = barray[0] >> 7 & 0x1
data_br = barray_s[:-8]
data = map(ord, data_br.bytes)
# Print only if something has changed
if cmp(oldbytes, data):
printhex(data, oldbytes)
oldbytes = data