-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
197 lines (147 loc) · 5.04 KB
/
Copy pathmain.py
File metadata and controls
197 lines (147 loc) · 5.04 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
import base64
import subprocess
import tempfile
import time
import hid
from pynput.keyboard import Key, Controller, Listener
keyboard = Controller()
shift_pressed = False
ctrl_pressed = False
def on_key_press(key):
global shift_pressed, ctrl_pressed
if key == Key.shift_l:
shift_pressed = True
elif key == Key.ctrl_l:
ctrl_pressed = True
def on_key_release(key):
global shift_pressed, ctrl_pressed
if key == Key.shift_l:
shift_pressed = False
elif key == Key.ctrl_l:
ctrl_pressed = False
def toggle_play_pause():
keyboard.press(Key.media_play_pause)
keyboard.release(Key.media_play_pause)
def mute():
keyboard.press(Key.media_volume_mute)
keyboard.release(Key.media_volume_mute)
def next_track():
keyboard.press(Key.media_next)
keyboard.release(Key.media_next)
def previous_track():
keyboard.press(Key.media_previous)
keyboard.release(Key.media_previous)
def volume_up():
keyboard.press(Key.media_volume_up)
keyboard.release(Key.media_volume_up)
def volume_down():
keyboard.press(Key.media_volume_down)
keyboard.release(Key.media_volume_down)
# TODO: make use of this artwork
def decode_artwork(artwork_data):
"""Decode base64 artwork and return temp file path"""
if not artwork_data:
return None
try:
# Handle data URL format
if artwork_data.startswith("data:"):
artwork_data = artwork_data.split("base64,")[1]
# Decode base64
image_bytes = base64.b64decode(artwork_data)
# Create temp file
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png", dir="/tmp")
temp_file.write(image_bytes)
temp_file.close()
return temp_file.name
except Exception as e:
print(f"Artwork decode error: {e}")
return None
def get_device(device_name="Creative Pebble"):
keywords = device_name.split(" ")
for device in hid.enumerate():
product = device["product_string"]
if any(keyword.lower() in product.lower() for keyword in keywords):
print(f"Found device: {product}")
print("-" * 50)
print(f"Vendor ID: 0x{device['vendor_id']:04x}")
print(f"Product ID: 0x{device['product_id']:04x}")
print(f"Product: {device['product_string']}")
print(f"Manufacturer: {device['manufacturer_string']}")
print("-" * 50)
return {
"vendor_id": int(f"0x{device['vendor_id']:04x}", 16),
"product_id": int(f"0x{device['product_id']:04x}", 16),
}
return None
def get_volume():
cmd = "output volume of (get volume settings)"
result = subprocess.run(["osascript", "-e", cmd], capture_output=True, text=True)
return result.stdout
def set_volume(volume):
cmd = f"set volume output volume {volume}"
result = subprocess.run(["osascript", "-e", cmd], capture_output=True, text=True)
return result.stdout
def listen_for_knob(device):
try:
while True:
data = device.read(64)
if data:
if data[1] == 0:
continue
direction = data[1]
if shift_pressed:
volume = get_volume()
if direction == 1:
next_track()
else:
previous_track()
set_volume(volume)
elif ctrl_pressed:
if direction == 1:
volume = get_volume()
toggle_play_pause()
set_volume(volume)
else:
mute()
else:
if direction == 1:
volume_up()
else:
volume_down()
finally:
device.close()
def check_device_and_listen():
device = None
try:
while not device:
device = get_device()
if not device:
raise Exception("Device not found")
VENDOR_ID = device["vendor_id"]
PRODUCT_ID = device["product_id"]
device = hid.device()
device.open(VENDOR_ID, PRODUCT_ID)
device.set_nonblocking(True)
print("Listening for knob rotation... Press Ctrl+C to exit")
print("Left Shift + clockwise → Next track")
print("Left Shift + counterclockwise → Previous track")
print("Ctrl + clockwise → Play/Pause")
print("Ctrl + counterclockwise → Mute")
print("Normal rotation → Volume (unchanged)")
listen_for_knob(device)
except Exception as e:
print(e)
time.sleep(1)
check_device_and_listen()
except KeyboardInterrupt:
print("\nStopped listening")
def main():
global shift_pressed, ctrl_pressed
check_device_and_listen()
if __name__ == "__main__":
listener = Listener(on_press=on_key_press, on_release=on_key_release)
listener.start()
try:
main()
finally:
listener.stop()