7272_Clarity__SETSVCMODE1 = 0xe0 #1 byte for service mode. SLEEP activates service mode. RUN returns to normal mode.
7373
7474
75-
7675class Clarity (microscope .devices .FilterWheelBase ):
7776 _slide_to_sectioning = {__SLDPOS0 : 'bypass' ,
7877 __SLDPOS1 : 'low' ,
7978 __SLDPOS2 : 'mid' ,
8079 __SLDPOS3 : 'high' ,}
8180 _positions = 4
81+ _resultlen = {__GETONOFF : 1 ,
82+ __GETDOOR : 1 ,
83+ __GETSLIDE : 1 ,
84+ __GETFILT : 1 ,
85+ __GETCAL : 1 ,
86+ __GETSERIAL : 4 ,
87+ __FULLSTAT : 10 }
8288
8389 def __init__ (self , * args , ** kwargs ):
8490 super ().__init__ (self , * args , ** kwargs )
@@ -101,8 +107,13 @@ def _send_command(self, command, param=0, max_length=16, timeout_ms=100):
101107 buffer [2 ] = param # The 2nd element is any command argument.
102108 result = self ._hid .write (buffer )
103109 if result == - 1 :
104- # Nothing to read back
105- return None
110+ # Nothing to read back. Check hid error state.
111+ err = self ._hid .error ()
112+ if err != '' :
113+ self .close ()
114+ raise Exception (err )
115+ else :
116+ return None
106117 while True :
107118 # Read responses until we see the response to our command.
108119 # (We should get the correct response on the first read.)
@@ -112,7 +123,13 @@ def _send_command(self, command, param=0, max_length=16, timeout_ms=100):
112123 return None
113124 elif response [0 ] == command :
114125 break
115- return response [1 :]
126+ bytes = self ._resultlen .get (command , None )
127+ if bytes is None :
128+ return response [1 :]
129+ elif bytes == 1 :
130+ return response [1 ]
131+ else :
132+ return response [1 :]
116133
117134 @property
118135 def is_connected (self ):
@@ -139,7 +156,7 @@ def _on_enable(self):
139156 if not self .is_connected :
140157 self .open ()
141158 self ._send_command (__SETONOFF , __RUN )
142- return self ._send_command (__GETONOFF )[ 0 ] == __RUN
159+ return self ._send_command (__GETONOFF ) == __RUN
143160
144161 def _on_disable (self ):
145162 self ._send_command (__SETONOFF , __SLEEP )
@@ -156,7 +173,7 @@ def get_slide_position(self):
156173 result = self ._send_command (__GETSLIDE )
157174 if result is None :
158175 raise Exception ("Slide position error." )
159- return result [ 0 ]
176+ return result
160177
161178 def set_slide_position (self , position , blocking = True ):
162179 """Set the slide position"""
@@ -173,11 +190,21 @@ def get_slides(self):
173190 def get_status (self ):
174191 # Fetch 10 bytes VERSION[3],ONOFF,SHUTTER,SLIDE,FILT,CAL,??,??
175192 result = self ._send_command (__FULLSTAT )
193+ if result is None :
194+ return
176195 status = {}
177196 status ['on' ] = result [3 ] == __RUN
178- slide = result [4 ]
179- status ['slide' ] = (slide , self ._slide_to_sectioning .get (slide , None ))
180- status ['filter' ] = (result [6 ], self ._filters .get (result [6 ], None ))
197+ status ['shutter' ] = result [4 ]
198+ slide = result [5 ]
199+ if slide == __SLDMID :
200+ status ['slide' ] = (None , 'moving' )
201+ else :
202+ status ['slide' ] = (slide , self ._slide_to_sectioning .get (slide , None ))
203+ filter = result [6 ]
204+ if filter == __FLTMID :
205+ status ['filter' ] = (None , 'moving' )
206+ else :
207+ status ['filter' ] = (result [6 ], self ._filters .get (result [6 ], None ))
181208 status ['calibration' ] = result [7 ] == __CALON
182209 return status
183210
@@ -192,15 +219,20 @@ def moving(self):
192219 # immediately after initiating a move. Trial and error
193220 # indicates a delay of 50ms is required.
194221 time .sleep (0.05 )
195- return any ( (self .get_slide_position () == __SLDMID ,
196- self .get_position () == __FLTMID ) )
222+ # Can return false negatives on long moves, so OR 5 readings.
223+ moving = False
224+ for i in range (5 ):
225+ moving = moving or any ( (self .get_slide_position () == __SLDMID ,
226+ self .get_position () == __FLTMID ) )
227+ time .sleep (0.01 )
228+ return moving
197229
198230 def get_position (self ):
199231 """Return the current filter position"""
200232 result = self ._send_command (__GETFILT )
201233 if result == __FLTERR :
202234 raise Exception ("Filter position error." )
203- return result [ 0 ]
235+ return result
204236
205237 def set_position (self , pos , blocking = True ):
206238 """Set the filter position"""
0 commit comments