-
Notifications
You must be signed in to change notification settings - Fork 0
/
x7.py
145 lines (120 loc) · 4.19 KB
/
x7.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
import bluetooth
import threading
from enum import IntEnum
from utils import packet_to_str
class X7Commands(IntEnum):
ACK = 2
HARDWARE_BUTTON_STATE = 38
SPEAKER_CONFIGURATION = 41
# Speaker Configuration
class X7SpeakerConfiguration(IntEnum):
# This is actually Interger.MIN_VALUE in Java. This allows changing output
# to speakers without actually knowing the current SpeakerConfiguration
TOGGLE_TO_SPEAKER = -(2 ** 31)
HEADPHONES = 1
STEREO_2_0 = 2
MULTI_CHANNEL_5_1 = 3
# Properties for SpeakerConfiguration
# class X7SpeakerAdvancedConfiguration(IntEnum):
# STEREO_2_0 = 1.0
# MULTI_CHANNEL_2_1 = 2.0
# MULTI_CHANNEL_3_0 = 3.0
# MULTI_CHANNEL_3_1 = 4.0
# MULTI_CHANNEL_4_0 = 5.0
# MULTI_CHANNEL_4_1 = 6.0
# MULTI_CHANNEL_5_0 = 7.0
# MULTI_CHANNEL_5_1 = 8.0
# FIXME: We should ask for hardware capabilities first
class X7HardwareButtons(IntEnum):
SBX = 1
MUTE = 8
CRYSTAL_VOICE = 17
# Not available on X7
VOICE = 4
MICROPHONE = 5
PHONE = 7
NOISE_REDUCTION = 9
# Back Buttons (BP = Bluetooth Player?), Not Available on X7
BP_PLAY = 10
BP_PREV_TRACK = 11
BP_NEXT_TRACK = 12
BP_PREV_FOLDER = 13
BP_NEXT_FOLDER = 14
BP_PLAY_RECORDING = 15
BP_RECORD_RECORDING = 16
# magic methods for argparse compatibility
def __str__(self):
return self.name
def __repr__(self):
return str(self)
@staticmethod
def argparse(s):
try:
return X7HardwareButtons[s.upper()]
except KeyError:
return s
class X7BluetoothController:
STARTBYTEID = 90
COMM_CHANNEL_ID = 1
def __init__(self, mac, nonblocking):
self.mac = mac
self.socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
self.socket.connect((self.mac, X7BluetoothController.COMM_CHANNEL_ID))
if nonblocking:
self.socket.settimeout(0.2)
t = threading.Thread(target=self.read_response)
t.start()
def _read_packet(self):
packet = self.socket.recv(1)
if packet[0] != X7BluetoothController.STARTBYTEID:
print("Invalid start byte:", packet[0])
return
packet = self.socket.recv(2)
packet_id = packet[0]
packet_len = packet[1]
data = self.socket.recv(packet_len)
if packet_id == X7Commands.ACK:
print("Packet ACK:", packet_to_str(packet))
if packet_id == X7Commands.HARDWARE_BUTTON_STATE:
is_get = data[0]
if is_get == 1 and packet_len == 5:
button_states = data[1] | data[2] << 8 | data[3] << 16 | data[4] << 24
print("BUTTON_STATES MASK", bin(button_states))
for button in X7HardwareButtons:
print(button.name, bool(button_states >> (button.value - 1) & 1))
def read_response(self):
try:
while True:
self._read_packet()
except bluetooth.btcommon.BluetoothError:
# Used to exit CLI mode
pass
def send_command(self, command_id, payload):
payload_length = len(payload)
b = [0] * (payload_length + 4)
b[0] = X7BluetoothController.STARTBYTEID
b[1] = command_id
b[2] = payload_length
b[3:] = payload
print("Sending: %s", packet_to_str(b))
self.socket.send(bytes(b))
class X7:
def __init__(self, mac, cli_mode=False):
self.bluetooth = X7BluetoothController(mac, cli_mode)
def _set_hardware_button(self, button, status):
self.bluetooth.send_command(
X7Commands.HARDWARE_BUTTON_STATE, [0, button, status]
)
def mute(self, enabled):
self._set_hardware_button(X7HardwareButtons.MUTE, enabled)
def sbx(self, enabled):
self._set_hardware_button(X7HardwareButtons.SBX, enabled)
def set_audio_output(self, output):
if output == "headphones":
config = X7SpeakerConfiguration.HEADPHONES
else:
config = X7SpeakerConfiguration.TOGGLE_TO_SPEAKER
payload = config.to_bytes(4, byteorder="little", signed=True)
self.bluetooth.send_command(
X7Commands.SPEAKER_CONFIGURATION, [0] + list(payload)
)