Repository navigation
Expand file tree
/
Copy pathsibctrl.py
More file actions
executable file
·212 lines (177 loc) · 7.26 KB
/
Copy pathsibctrl.py
File metadata and controls
executable file
·212 lines (177 loc) · 7.26 KB
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
#!/usr/bin/env python3
# -*- mode: python; coding: utf-8 -*-
# Copyright © 2018 Göran Weinholt <goran@weinholt.se>
# SPDX-License-Identifier: LGPL-3.0-or-later
# SteelSeries Siberia 350 control software.
import argparse
import contextlib
import re
import usb1 # python-libusb1
# Equalizer
CMD_REG_EQL_80 = 0x40
CMD_REG_EQL_280 = 0x41
CMD_REG_EQL_1K = 0x42
CMD_REG_EQL_3_5K = 0x43
CMD_REG_EQL_13K = 0x44
CMD_EQL_SELECT = 0x46
CMD_EQL_DATA = 0x47
CMD_UNKNOWN_49 = 0x49
CMD_UNKNOWN_4A = 0x4A
EQL_BANDS = (
'80 Hz', '280 Hz', '1 kHz', '3.5 kHz', '13 kHz'
)
EQL_REGISTERS = (
CMD_REG_EQL_80,
CMD_REG_EQL_280,
CMD_REG_EQL_1K,
CMD_REG_EQL_3_5K,
CMD_REG_EQL_13K,
)
EQL_DATA = ( # Unknown magic. Some are repeated.
( # 80 Hz.
(0x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69, 0xb4, 0xe, 0x1b, 0x34, 0xda),
)
)
# Color.
CMD_UNKNOWN_80 = 0x80
CMD_SET_COLOR = 0x83
CMD_UNKNOWN_93 = 0x93
CMD_UNKNOWN_95 = 0x95
# Microphone.
CMD_MICROPHONE = 0xf0
# MIC_BLOB_0 = '0100fe01005b00000000000000000000'
# MIC_BLOB_1 = '010000005b0027000000000000000000'
# CMD_MIC_0 = 0x5e
# CMD_MIC_1 = 0x5d
# MIC_MIN = 0x02
# MIC_MUTE = 0x40
# MIC_MAX = 0x1c
def headset_command(command, payload):
"""Format a payload for a USB control write to the headset."""
cmd = [0x01, 0x00, command, len(payload)] + payload
cmd += [0] * (16 - len(cmd))
return bytearray(cmd)
class Headset(object):
"""Class for controlling the headset."""
VENDOR_ID = 0x1038
PRODUCT_ID = 0x1229
INTERFACE_NUM = 3
def __init__(self):
self.context = usb1.USBContext()
# self.context.setDebug(usb1.LOG_LEVEL_DEBUG)
self.handle = self.context.openByVendorIDAndProductID(self.VENDOR_ID, self.PRODUCT_ID)
self.handle.setAutoDetachKernelDriver(True)
self.handle.claimInterface(self.INTERFACE_NUM)
def close(self):
"""Release USB resources."""
self.handle.releaseInterface(self.INTERFACE_NUM)
self.context.close()
def _send(self, cmd):
"""Send a control command to the headset.
The command should be generated by :py:func:`headset_command`.
"""
self.handle.controlWrite(usb1.TYPE_CLASS | usb1.RECIPIENT_INTERFACE,
request=9, value=0x0201, index=self.INTERFACE_NUM,
data=cmd)
def set_color(self, red, green, blue):
"""Set the color of the headset to the given RGB value (0-255 values)."""
commands = (
headset_command(CMD_UNKNOWN_95, [0x80, 0xbf]),
headset_command(CMD_UNKNOWN_80, [0x52, 0x20]),
headset_command(CMD_SET_COLOR, [red, green, blue]),
headset_command(CMD_UNKNOWN_93, [0x03, 0x80])
)
for cmd in commands:
self._send(cmd)
def set_microphone_optimization(self, enable):
"""Enable or disable microphone automatic optimization."""
cmd = headset_command(CMD_MICROPHONE, [0xf1 if enable else 0x00])
self._send(cmd)
def set_equalizer(self, band, value):
"""Set the value for an equalizer band.
Bands: {0: '80 Hz', 1: '280 Hz', 2: '1 kHz', 3: '3.5 kHz', 4: '13 kHz'}.
Value is between 0x00 and 0x30 (-12 dB to +12 dB, 0x18 = 0 dB).
"""
assert 0 <= band <= 5
assert 0x00 <= value <= 0x30
zero_db = 0x18
prologue = [
headset_command(EQL_REGISTERS[band], [zero_db]),
headset_command(CMD_EQL_SELECT, [0x0d]),
headset_command(CMD_EQL_SELECT, [band + 1])
]
if value < 0x18:
values = EQL_DATA[band][0]
elif value > 0x18:
values = EQL_DATA[band][2]
else:
values = EQL_DATA[band][1]
value_cmds = [headset_command(CMD_EQL_DATA, [val]) for val in values]
epilogue = [
headset_command(CMD_UNKNOWN_4A, [0x00]),
headset_command(CMD_UNKNOWN_49, [0x04]),
headset_command(EQL_REGISTERS[band], [value]),
headset_command(CMD_EQL_SELECT, [0x0d]),
]
for cmd in prologue + value_cmds + epilogue:
self._send(cmd)
def main():
parser = argparse.ArgumentParser(description='Siberia 350 control')
parser.add_argument('--color',
help='set the color of the headset LEDs (rrggbb)')
parser.add_argument('--mic-auto', dest='mic_auto', action='store_true', default=None,
help='enable mic auto optimization')
parser.add_argument('--no-mic-auto', dest='mic_auto', action='store_false', default=None,
help='disable mic auto optimization')
parser.add_argument('--equalizer',
help='set the equalizer (e.g. 0,0,0,0,0)')
args = parser.parse_args()
if args.color is None and args.mic_auto is None and args.equalizer is None:
parser.print_help()
with contextlib.closing(Headset()) as headset:
if args.color is not None:
color = re.findall('[0-9A-Fa-f]{2}', args.color)
if len(color) != 3:
parser.error('invalid color: %r' % args.color)
red, green, blue = (int(c, 16) for c in color)
print("Setting color to %02x%02x%02x" % (red, green, blue))
headset.set_color(red, green, blue)
if args.mic_auto is not None:
if args.mic_auto:
print('Enabling mic auto optimization')
else:
print('Disabling mic auto optimization')
headset.set_microphone_optimization(args.mic_auto)
if args.equalizer is not None:
values = [float(x) if x != '' else '' for x in args.equalizer.split(',')]
if len(values) != 5 or not all(v == '' or -12 <= v <= 12 for v in values):
parser.error('invalid equalizer setting: %r' % values)
print('Setting equalizer:')
for band, value in enumerate(values):
if value != '':
print(" {:>7} - {:>5} dB".format(EQL_BANDS[band], value))
headset.set_equalizer(band, int(2 * (value + 12)))
if __name__ == '__main__':
main()