-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterminal.py
More file actions
93 lines (71 loc) · 2.5 KB
/
terminal.py
File metadata and controls
93 lines (71 loc) · 2.5 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
#!/usr/bin/env python3
#requires pyusb
import usb.core
import usb.util
import argparse
import os
import sys
import tty
import termios
import threading
class Terminal(object):
def __init__(self, devicepath):
self.devicepath = devicepath
#self.rawhid = open(self.devicepath, "r+b")
self.setup_hid()
self.fd = sys.stdin.fileno()
self.run = 1
self.receiver_thread = threading.Thread(target=self.reader, name='reader')
self.transmitter_thread = threading.Thread(target=self.writer, name='transmitter')
self.transmitter_thread.daemon = True
self.receiver_thread.daemon = True
def setup_hid(self):
dev = usb.core.find(idVendor=0x16c0, idProduct=0x0486)
if dev is None:
raise ValueError('Device not found')
# get an endpoint instance
for interface in dev.get_active_configuration():
#if interface.bInterfaceNumber in [1,2]:
if dev.is_kernel_driver_active(interface.bInterfaceNumber):
# Detach kernel drivers and claim through libusb
dev.detach_kernel_driver(interface.bInterfaceNumber)
usb.util.claim_interface(dev, interface.bInterfaceNumber)
dev.set_configuration()
self.dev = dev
def runterminal(self):
self.setup_terminal()
self.receiver_thread.start()
self.transmitter_thread.start()
self.receiver_thread.join()
self.transmitter_thread.join()
self.restore_terminal()
print("\r\n")
def setup_terminal(self):
self.old_settings = termios.tcgetattr(self.fd)
tty.setraw(self.fd)
def restore_terminal(self):
tty.tcsetattr(self.fd, termios.TCSANOW, self.old_settings)
def reader(self):
while self.run:
data = sys.stdin.buffer.read(1)
#print ("{:02x}\r\n".format(data[0]))
if data == b'~':
self.run = 0
output = bytearray(64)
output[0] = data[0]
self.dev.write(0x02,data)
def writer(self):
while self.run:
data = []
sret = ""
try:
data = self.dev.read(0x81, 64, 1000)
sret = ''.join([chr(x) for x in data]).strip('\0')
except:
pass
sys.stdout.buffer.write(sret.encode('utf-8', errors='backslashreplace'))
sys.stdout.buffer.flush()
if __name__ == '__main__':
t = Terminal(None)
t.runterminal()
sys.exit()