-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmos_switcher.py
145 lines (108 loc) · 4.13 KB
/
mos_switcher.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
#!/usr/bin/python
import sys,os,os.path,glob,argparse,platform,termios,collections,tty
##########################################################################
##########################################################################
def find_device(specified_device):
if specified_device is not None: return specified_device
devices=glob.glob('/dev/cu.usbmodem*')
if len(devices)==0: raise RuntimeError('no usbmodem devices found')
elif len(devices)>1:
raise RuntimeError('multiple usbmodem devices found: %s'%(', '.join(devices)))
return devices[0]
##########################################################################
##########################################################################
TCAttr=collections.namedtuple('TCAttr',
'iflag oflag cflag lflag ispeed ospeed cc')
class Connection:
def __init__(self):
self.verbose=False
self._fd=-1
def __enter__(self): return self
def __exit__(self,exc_type,exc_value,traceback):
if self._fd>=0:
os.close(self._fd)
self._fd=-1
def _v(self,x):
if self.verbose:
sys.stdout.write(x)
sys.stdout.flush()
def _init(self):
if self._fd>=0: return
device=find_device(self.device)
self._fd=os.open(device,os.O_RDWR|os.O_APPEND)
tty.setraw(self._fd)
attr=TCAttr(*termios.tcgetattr(self._fd))
attr=attr._replace(ospeed=termios.B230400)
attr=attr._replace(ispeed=termios.B230400)
termios.tcsetattr(self._fd,
termios.TCSAFLUSH,
list(attr))
self._v('fd=%d; tcgetattr=%s\n'%(self._fd,
TCAttr(*termios.tcgetattr(self._fd))))
def send(self,data,verbose_ok=True):
self._init()
if verbose_ok: self._v('Sending: ``%s\'\'\n'%data)
else: self._v('Sending %d byte(s)\n'%len(data))
i=0
while i<len(data):
self._v('Sending from +%d...\n'%i)
os.write(self._fd,data[i])
i+=1
self._v('Waiting for drain...\n')
termios.tcdrain(self._fd)
self._v('All sent.\n')
def reset(self):
self._v('Resetting...\n')
self.wait_for_ready()
for i in range(3):
self.send('q')
self.wait_for_ok()
def wait_for_ok(self):
self._v('Waiting for OK...\n')
if self._wait_for(['OK','\\','|','/','-'])!='OK':
self._wait_for(['OK'])
self._wait_for(['\\','|','/','-'])
def wait_for_ready(self):
self._v('Waiting for ready...\n')
self._wait_for(['\\','|','/','-'])
def _wait_for(self,options):
self._v('Waiting for one of: %s\n'%(', '.join(options)))
for i in xrange(10000):
line=self.readline()
self._v('Got: ``%s\'\' (%s)\n'%(line,[ord(x) for x in line]))
if line in options: return line
raise RuntimeError('MOS switcher wasn\'t ready in time')
def readline(self):
self._init()
line=''
while True:
c=os.read(self._fd,1)
if c=='\r' or c=='\n':
# just consume leading line breaks.
if line=='': continue
else: break
elif c=='': raise Error('got EOF')
else: line+=c
return line
##########################################################################
##########################################################################
# def _recv_thread(fd,cv,buffer):
# while True:
# try:
# c=os.read(fd,1)
# if c=='': return
# except IOError: return
# cv.acquire()
# buffer.append(ord(c[0]))
# cv.notifyAll()
# cv.release()
# def terminal(specified_device):
# device=find_device(specified_device)
# fd=os.open(device,os.O_RDWR|os.O_APPEND)
# cv=threading.Condition()
# recv_buffer=[]
# recv_th=threading.Thread(target=_recv_thread,
# args=(fd,cv,recv_buffer))
# while True:
# os.close(fd)
# fd=-1