Skip to content

Instantly share code, notes, and snippets.

@npyoung
Last active August 29, 2015 14:24
Show Gist options
  • Save npyoung/1e1e73be04afd24ecb2d to your computer and use it in GitHub Desktop.
Save npyoung/1e1e73be04afd24ecb2d to your computer and use it in GitHub Desktop.
Control a DG-4 filter wheel over a serial port
from serial import Serial
from time import sleep
class DG4(object):
"""Control a Sutter Lambda DG-4 over serial interface"""
SWITCH_TO_SERIAL = 238 # switch the DG4 to serial mode, DG4 hangs until it receives the next serial command
FREEZE_DISPLAY = 218
TURN_DISPLAY_ON = 219
OPEN_SHUTTER = 170
ND_ADJ_MARKER = 171 # DATANUM = 171 MARKER BETWEEN 235 OR 236 IN N.D. ADJUST
CLOSE_SHUTTER = 172
ENABLE_TURBO_BLANKING =186
DISABLE_TURBO_BLANKING = 188
TRIGGERED_BY_STROBE = 202
DISABLE_TRIGGERED_BY_STROBE = 203
SELECT_VIDEO_SYNC = 204
DISABLE_VIDEO_SYNC = 205
SELECT_VIDEO_SYNC_GATED_BY_STROBE_LOW = 206
DISABLE_VIDEO_SYNC_GATED_BY_STROBE_LOW = 207
START_LOADING_RING_BUFFER = 223 #START LOADING RING BUFFER (UP TO 256 VALUES)
START_REMOTE_CHANGE_OF_ND_FILTER_SETTING = 234 #START REMOTE CHANGE OF N.D. FILTER SETTING
INCREASE_THE_DAC_GALVO_VALUE = 235 #-SEND 171 AS SPACE
DECREASE_THE_DAC_GALVO_VALUE = 236 # -SEND 171 AS SPACE
END_CHANGE_OF_ND_FILTER_SETTING_AND_SAVE = 237
FORCE_ONLINE_FOR_CHANNEL_THAT_RECEIVED_IT = 238
END_LOADING_RING_BUFFER_FILTERS = 240
RUN_RING_BUFFER = 241 # (FIRST SELECT TRIGGER SOURCE)
END_RING_BUFFER_RUN = 242
def __init__(self, com=0, latency=0.1):
self.latency = latency
self.ser = Serial(com)
self.sendrecv(DG4.SWITCH_TO_SERIAL)
def __enter__(self):
self.sendrecv(DG4.SWITCH_TO_SERIAL)
return self
def __exit__(self, type, value, traceback):
self.ser.close()
def setup_ring_buffer(self, sequence):
"""Sets up the DG-4's ring buffer with the given integer sequence"""
self.sendrecv(DG4.START_LOADING_RING_BUFFER)
for i in sequence:
self.sendrecv(i)
self.sendrecv(DG4.END_LOADING_RING_BUFFER_FILTERS)
def send(self, msg):
"""Send an integer command, handles the conversion to bytes"""
self.ser.write(chr(msg))
def sendrecv(self, msg):
"""Send an integer command and print any reply"""
self.send(msg)
sleep(self.latency)
while self.ser.inWaiting():
reply = ord(self.ser.read(1))
print "dg-4: %d" % reply
return True
# A table of custom command aliases for our application
command_table = {
'strobe': DG4.TRIGGERED_BY_STROBE,
'white': 1,
'red': 2,
'yfp': 1,
'nearbeads': 2,
'farbeads': 1,
'380': 3,
'340': 4,
'fura': DG4.RUN_RING_BUFFER,
'': DG4.END_RING_BUFFER_RUN
}
if __name__ == '__main__':
with DG4() as dg:
dg.sendrecv(DG4.TRIGGERED_BY_STROBE)
dg.setup_ring_buffer([3, 4])
cmd = 'start'
while True:
cmd = raw_input("Enter command...")
try:
dg.sendrecv(command_table[cmd])
except KeyError as e:
if cmd != 'quit' and cmd != 'q':
print "'%s' not a valid command to DG4" % cmd
print "Valid commands are: " + ", ".join([k if k != '' else '<Enter>' for k in sorted(command_table)])
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment