ScanImage 5 Galvo/Galvo allows live waveform updates for the galvo/galvo control signal. This is useful to change the zoomFactor,scanShift,etc. while focusing.

Discussion

DAQmx allows the live update of an analog output buffer, for both finite and continuous tasks. However, there is one limitation: The waveform last written to the buffer needs to be generated at least once, before the buffer can be overwritten. Otherwise, DAQmx returns an error.

The straight forward way to update the waveform is to just write new data to the buffer with the call hAO.writeAnalogData(waveform,timeout) and set the timeout to a high value. The call writeAnalogData will then wait till the buffer is ready to accept new data.

Problem: writeAnalogData is a blocking call. If the task is not ready to accept new data, the call will first wait for the duration specified by 'timeout' and then throw an error. This blocks the Matlab code unnecessarily and should be avoided

To work around this limitation, we need to make sure that the last waveform written to hAO is generated before writing new data to the task.

Implementation

Use the DAQmx callback 'everyNSamples' to determine the right time when a waveform can be updated. If the buffer length is 100 samples, set 'everyNSamples' to 100. The everyNSamples callback is then called at the end of each completed waveform generation. When the callback is executed, we know that the data in the buffer has been generated at least once and can be safely overwritten.

If the waveform needs to be updated (e.g. the zoomFactor is changed), instead of directly write data to the task, set a flag. This flag is queried by the AO tasks's everyNSamples callback. If the flag is false, the callback returns. If the flag is true, new data is written to the task.

Disadvantage: the waveform can only be updated at the beginning of a frame. If the user changes the zoomFactor in the middle of the frame, the waveform update is delayed.