-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrepl.py
executable file
·62 lines (44 loc) · 1.33 KB
/
repl.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
#!/usr/bin/env python3
import sys
import readline
import threading
from mv7config.text_hid import TextHID
from mv7config.microphone import Microphone
prompt = "> "
class ReaderThread(threading.Thread):
def __init__(self, device):
super().__init__()
self._device = device
self._stop_event = threading.Event()
def run(self):
while not self._stop_event.is_set():
message = self._device.read_message(timeout_ms=200)
if message:
print(
"\r", message,
prompt, readline.get_line_buffer(),
sep="", end="", flush=True
)
def stop(self):
self._stop_event.set()
self.join()
def main():
available_devices = Microphone.enumerate()
if not available_devices:
print("No MV7 microphone found")
sys.exit(1)
with TextHID(next(iter(available_devices))) as device:
thread = ReaderThread(device)
thread.start()
while True:
print(prompt, end="", flush=True)
try:
command = input()
except (KeyboardInterrupt, EOFError):
break
if command:
device.send_command(command)
print()
thread.stop()
if __name__ == "__main__":
main()