Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Trigger in Wavegen #5

Open
edudro opened this issue Feb 19, 2025 · 1 comment
Open

Support Trigger in Wavegen #5

edudro opened this issue Feb 19, 2025 · 1 comment

Comments

@edudro
Copy link

edudro commented Feb 19, 2025

Hi, could you add support to this triggered option using the scope detector.

Image

@edudro
Copy link
Author

edudro commented Feb 19, 2025

#!/usr/bin/env python3
"""
single_pulse_photodiode.py
Generates ONE 1ms pulse on WaveGen channel 1 each time the scope
detects a rising edge above 1.0 V on channel 1 (index=0).
"""

import ctypes
import time
from sys import platform, path
from os import sep

# Load the WaveForms library and figure out where dwfconstants.py lives
if platform.startswith("win"):
    dwf = ctypes.cdll.dwf
    constants_path = "C:" + sep + "Program Files (x86)" + sep + "Digilent" + sep + "WaveFormsSDK" + sep + "samples" + sep + "py"
elif platform.startswith("darwin"):
    lib_path = "/Library/Frameworks/dwf.framework/dwf"
    dwf = ctypes.cdll.LoadLibrary(lib_path)
    constants_path = sep + "Applications" + sep + "WaveForms.app" + sep + "Contents" + sep + "Resources" + sep + "SDK" + sep + "samples" + sep + "py"
else:
    dwf = ctypes.cdll.LoadLibrary("libdwf.so")
    constants_path = sep + "usr" + sep + "share" + sep + "digilent" + sep + "waveforms" + sep + "samples" + sep + "py"

path.append(constants_path)
import dwfconstants as constants
from WF_SDK.device import open as device_open, close as device_close, check_error, error
from WF_SDK import scope

def main():
    # 1) Open the device
    dev = device_open()
    print("Device opened successfully.")

    handle = dev.handle
    c_channel = ctypes.c_int(0)  # WaveGen channel 1 => index=0

    # 2) Basic scope init so the “DetectorAnalogIn” can function
    scope.open(dev)  # sets default sample freq, enables analogIn

    # Configure the scope trigger parameters (DetectorAnalogIn)
    check(dwf.FDwfAnalogInTriggerAutoTimeoutSet(handle, ctypes.c_double(0.0)))
    check(dwf.FDwfAnalogInTriggerSourceSet(handle, ctypes.c_ubyte(2)))
    check(dwf.FDwfAnalogInTriggerTypeSet(handle, constants.trigtypeEdge))
    check(dwf.FDwfAnalogInTriggerChannelSet(handle, ctypes.c_int(0)))  # Scope CH1 => index=0
    check(dwf.FDwfAnalogInTriggerLevelSet(handle, ctypes.c_double(1.0)))  # threshold set to 1.0 V
    check(dwf.FDwfAnalogInTriggerConditionSet(handle, constants.trigcondRisingPositive))

    # Arm the scope (so trigger detection is active)
    check(dwf.FDwfAnalogInConfigure(handle, ctypes.c_bool(False), ctypes.c_bool(True)))

    # 3) WaveGen: single 1ms pulse upon each rising edge
    # 3a) Enable wavegen node
    check(dwf.FDwfAnalogOutNodeEnableSet(handle, c_channel, constants.AnalogOutNodeCarrier, ctypes.c_bool(True)))

    # 3c) Configure a 1 ms pulse
    check(dwf.FDwfAnalogOutNodeFunctionSet(handle, c_channel, constants.AnalogOutNodeCarrier, constants.funcPulse))
    check(dwf.FDwfAnalogOutNodeFrequencySet(handle, c_channel, constants.AnalogOutNodeCarrier, ctypes.c_double(1000.0)))  # 1 kHz base
    check(dwf.FDwfAnalogOutNodeAmplitudeSet(handle, c_channel, constants.AnalogOutNodeCarrier, ctypes.c_double(2.0)))
    check(dwf.FDwfAnalogOutNodeOffsetSet(handle, c_channel, constants.AnalogOutNodeCarrier, ctypes.c_double(0.0)))
    check(dwf.FDwfAnalogOutNodeSymmetrySet(handle, c_channel, constants.AnalogOutNodeCarrier, ctypes.c_double(50.0)))
    check(dwf.FDwfAnalogOutRunSet(handle, c_channel, ctypes.c_double(0.001)))   # 1 ms
    check(dwf.FDwfAnalogOutWaitSet(handle, c_channel, ctypes.c_double(0.0)))
    check(dwf.FDwfAnalogOutRepeatSet(handle, c_channel, ctypes.c_int(0)))             # 1 cycle per trigger
    check(dwf.FDwfAnalogOutRepeatTriggerSet(handle, c_channel, ctypes.c_bool(True)))  # Trigger each time

    # “Independent” mode
    check(dwf.FDwfAnalogOutMasterSet(handle, c_channel, ctypes.c_int(0)))

    # 3d) Configure WaveGen to be triggered by DetectorAnalogIn
    check(dwf.FDwfAnalogOutTriggerSourceSet(handle, c_channel, ctypes.c_ubyte(2)))

    # 4) Start WaveGen
    check(dwf.FDwfAnalogOutConfigure(handle, c_channel, ctypes.c_int(1)))

    print("One 1ms pulse each time scope CH1 sees >0.5V rising edge. Press Ctrl+C to stop.")
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        pass

    # Cleanup
    dwf.FDwfAnalogOutReset(handle, c_channel)
    scope.close(dev)
    device_close(dev)
    print("All done.")

def check(status):
    """Helper function to raise if DWF returned an error."""
    if status == 0:
        check_error()

if __name__ == "__main__":
    main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant