def configure(): """Configures LabJack U3 to use FIO ports as Digital Output. Sets signal 0 on card. Returns 0 if everything is OK""" global u3card u3card = u3.U3() u3card.configU3(FIOAnalog=0, FIODirection=255, FIOState=0) u3card.configIO() u3card.getFeedback( u3.PortStateWrite(State=[0, 0x00, 0x00], WriteMask=[0xff, 0x00, 0x00])) return 0
def initializeLabjack(params): # Initialize labjack device object and get calibration data. if params['options_sendSYNC'] == True: try: params['SYNC_deviceObj'] = u3.U3() params['SYNC_deviceObj'].getCalibrationData() if params['SYNC_useDigitalOut'] == True: # we are using digital output # set FI02 direcection to output params['SYNC_deviceObj'].getFeedback(u3.BitDirWrite(2, 1)) print( 'Sync pulses are sent from the FI02 channel. Connect cathode (red wire) to FI02 and annode (black wire) to ground.' ) else: # we are using analog output params['SYNC_pulse_val'] = params[ 'SYNC_deviceObj'].voltageToDACBits(params['SYNC_volt'], dacNumber=0, is16Bits=False) params['SYNC_zero_val'] = params[ 'SYNC_deviceObj'].voltageToDACBits(0, dacNumber=0, is16Bits=False) print( 'Sync pulses are sent from the DAC0 channel. Connect cathode (red wire) to DAC0 and annode (black wire) to ground.' ) except: print( 'Unable to open LABJACK. Check if it is connected. If not using sync pulses, set "options_sendSYNC" to False' ) core.quit() # returns updated params return params
from psychopy import visual, core, event, sound from labjack import u3 import numpy, sys, platform # setup window (can use for visual pulses) win = visual.Window([800, 800], monitor='testMonitor') win.recordFrameIntervals = False stim = visual.GratingStim(win, color=-1, sf=0) sound.init(rate=48000, buffer=48) print('Using %s(with %s) for sounds' % (sound.audioLib, sound.audioDriver)) timeWithLabjack = True maxReps = 100 # setup labjack U3 ports = u3.U3() ports.__del__ = ports.close # try to autoclose the ports if script crashes # get zero value of FIO6 startVal = ports.getFIOState(6) # is FIO6 high or low? print('FIO6 is at', startVal, end='') print('AIN0 is at', ports.getAIN(0)) if timeWithLabjack: print('OS\tOSver\taudioAPI\tPsychoPy\trate\tbuffer\tmean\tsd\tmin\tmax') snd = sound.Sound(1000, secs=0.1) core.wait(2) # give the system time to settle? delays = [] nReps = 0 while True: # run the repeats for this sound server if event.getKeys('q'):
def trigger_parallel(code=1,trigDuration=0.010): """ Here the wait is based on time.sleep(), in which "the substantive part of the sleep operation is wrapped in a Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS block, allowing other threads to continue to execute while the current one sleeps" [http://stackoverflow.com/questions/92928/time-sleep-sleeps-thread-or-process] This leads to variability of the trigger durations if used in threaded mode, but likely give the experiment excellent timing. """ setParallelData(code) # core.wait(trigDuration,trigDuration/2.) # equivalent to time.sleep(0.005) plus a tight while-loop for another 0.005 secs! # core.wait(trigDuration) # equivalent to time.sleep(0.010) # setParallelData(0) u3dev = u3.U3() u3dev.getFeedback(u3.PortStateWrite([0x00, 0x00, 0x00])) def trigger_u3(code=1, trigDuration=0.010): u3dev.getFeedback(u3.PortStateWrite([0x00, code, 0x00])) # core.wait(trigDuration,trigDuration/2.) # equivalent to time.sleep(0.005) plus a tight while-loop for another 0.005 secs! # core.wait(trigDuration) # u3dev.getFeedback(u3.PortStateWrite([0x00, 0x00, 0x00])) # u3dev.toggleLED() ###### Window and stimulus definitions bckColour = '#000000' monitor = 'testMonitor' frameRate = 120.0 stimSize = 8. stimCycPerDeg = 1.
def __init__(self): # Have to ensure u3 isn't imported unless the U3Port is explicitly needed from labjack import u3 # noqa self.u3dev = u3.U3() # noqa self.PortStateWrite = u3.PortStateWrite
params = {} params['numTrials'] = 30 params['SYNC_useDigitalOut'] = True #params['SYNC_digitalOut_chan'] = 'FI02' # These are hard coded #params['SYNC_analogOut_chan'] = 'DAC0' params['SYNC_volt'] = 1.5 # 1.5 V pulse (only matters for analog out) # these are populated after getting calibration data params['SYNC_pulse_val'] = None params['SYNC_zero_val'] = None params['SYNC_deviceObj'] = None # initialize labjack try: params['SYNC_deviceObj'] = u3.U3() params['SYNC_deviceObj'].getCalibrationData() if params['SYNC_useDigitalOut'] == True: # we are using digital output # set FI02 direcection to output params['SYNC_deviceObj'].getFeedback(u3.BitDirWrite(2,1)) print('Sync pulses are sent from the FI02 channel. Connect cathode (red wire) to FI02 and annode (black wire) to ground.') else: # we are using analog output params['SYNC_pulse_val'] = params['SYNC_deviceObj'].voltageToDACBits(params['SYNC_volt'], dacNumber = 0, is16Bits = False) params['SYNC_zero_val'] = params['SYNC_deviceObj'].voltageToDACBits(0, dacNumber = 0, is16Bits = False)