From 6823c8be8e70e6e58d12a658db08c9b30b3139a1 Mon Sep 17 00:00:00 2001 From: jlin-NI <152445080+jlin-NI@users.noreply.github.com> Date: Mon, 17 Jun 2024 17:25:59 -0500 Subject: [PATCH] add tests for trigger APIs (#597) * add tests for trigger APIs * address review feedback --- tests/component/task/test_triggers.py | 215 +++++++++++++++++++++++++- tests/conftest.py | 6 + tests/max_config/nidaqmxMaxConfig.ini | 9 ++ 3 files changed, 229 insertions(+), 1 deletion(-) diff --git a/tests/component/task/test_triggers.py b/tests/component/task/test_triggers.py index 7357aabf..e4f3d8b5 100644 --- a/tests/component/task/test_triggers.py +++ b/tests/component/task/test_triggers.py @@ -5,7 +5,16 @@ from hightime import datetime as ht_datetime, timedelta as ht_timedelta import nidaqmx -from nidaqmx.constants import Edge, Slope, Timescale, TimestampEvent, TriggerType +from nidaqmx.constants import ( + DigitalPatternCondition, + Edge, + LineGrouping, + Slope, + Timescale, + TimestampEvent, + TriggerType, + WindowTriggerCondition1, +) from nidaqmx.error_codes import DAQmxErrors from nidaqmx.task import Task from nidaqmx.utils import flatten_channel_string @@ -33,6 +42,15 @@ def sim_6363_ai_voltage_task(task, sim_6363_device) -> Task: return task +@pytest.fixture() +def sim_6535_di_single_line_task(task, sim_6535_device) -> Task: + """Gets DI task.""" + task.di_channels.add_di_chan( + sim_6535_device.di_lines[0].name, line_grouping=LineGrouping.CHAN_FOR_ALL_LINES + ) + return task + + @pytest.fixture() def sim_9775_ai_voltage_multi_edge_task(task, sim_9775_device) -> Task: """Gets AI voltage multi edge task.""" @@ -179,6 +197,100 @@ def test___start_trigger___cfg_anlg_edge_start_trig___no_errors( ) +@pytest.mark.parametrize( + "window_top, window_bottom, trig_src, trig_when", + [ + (8.456, 3.9, "APFI0", WindowTriggerCondition1.ENTERING_WINDOW), + (8.456, 3.9, "APFI1", WindowTriggerCondition1.LEAVING_WINDOW), + ], +) +def test___start_trigger___cfg_anlg_window_start_trig___no_errors( + sim_6363_ai_voltage_task: Task, + window_top: float, + window_bottom: float, + trig_src: str, + trig_when: int, +): + device_name = sim_6363_ai_voltage_task.devices[0].name + + sim_6363_ai_voltage_task.timing.cfg_samp_clk_timing(1000) + sim_6363_ai_voltage_task.triggers.start_trigger.cfg_anlg_window_start_trig( + window_top=window_top, + window_bottom=window_bottom, + trigger_source=trig_src, + trigger_when=trig_when, + ) + + assert ( + sim_6363_ai_voltage_task.triggers.start_trigger.anlg_win_src == f"/{device_name}/{trig_src}" + ) + assert sim_6363_ai_voltage_task.triggers.start_trigger.anlg_win_top == pytest.approx( + window_top, abs=0.001 + ) + assert sim_6363_ai_voltage_task.triggers.start_trigger.anlg_win_btm == pytest.approx( + window_bottom, abs=0.001 + ) + assert sim_6363_ai_voltage_task.triggers.start_trigger.anlg_win_trig_when == trig_when + + +def test___start_trigger___disable___no_errors( + sim_6363_ai_voltage_task: Task, +): + sim_6363_ai_voltage_task.timing.cfg_samp_clk_timing(1000) + sim_6363_ai_voltage_task.triggers.start_trigger.disable_start_trig() + + assert sim_6363_ai_voltage_task.triggers.start_trigger.trig_type == TriggerType.NONE + + +def test___reference_trigger___disable___no_errors( + sim_6363_ai_voltage_task: Task, +): + sim_6363_ai_voltage_task.timing.cfg_samp_clk_timing(1000) + sim_6363_ai_voltage_task.triggers.reference_trigger.disable_ref_trig() + + assert sim_6363_ai_voltage_task.triggers.reference_trigger.trig_type == TriggerType.NONE + + +@pytest.mark.parametrize( + "window_top, window_bottom, pretrigger_samples, trig_src, trig_when", + [ + (8.456, 3.9, 6, "APFI0", WindowTriggerCondition1.ENTERING_WINDOW), + (8.456, 3.9, 6, "APFI1", WindowTriggerCondition1.LEAVING_WINDOW), + ], +) +def test___reference_trigger___cfg_anlg_window_ref_trig___no_errors( + sim_6363_ai_voltage_task: Task, + window_top: float, + window_bottom: float, + pretrigger_samples: int, + trig_src: str, + trig_when: int, +): + device_name = sim_6363_ai_voltage_task.devices[0].name + + sim_6363_ai_voltage_task.timing.cfg_samp_clk_timing(1000) + sim_6363_ai_voltage_task.triggers.reference_trigger.cfg_anlg_window_ref_trig( + window_top=window_top, + window_bottom=window_bottom, + pretrigger_samples=pretrigger_samples, + trigger_source=trig_src, + trigger_when=trig_when, + ) + + assert ( + sim_6363_ai_voltage_task.triggers.reference_trigger.anlg_win_src + == f"/{device_name}/{trig_src}" + ) + assert sim_6363_ai_voltage_task.triggers.reference_trigger.anlg_win_top == pytest.approx( + window_top, abs=0.001 + ) + assert sim_6363_ai_voltage_task.triggers.reference_trigger.anlg_win_btm == pytest.approx( + window_bottom, abs=0.001 + ) + assert sim_6363_ai_voltage_task.triggers.reference_trigger.pretrig_samples == pretrigger_samples + assert sim_6363_ai_voltage_task.triggers.reference_trigger.anlg_win_trig_when == trig_when + + @pytest.mark.parametrize( "trig_slopes, trig_levels", [ @@ -218,6 +330,35 @@ def test___start_trigger___cfg_anlg_multi_edge_start_trig___no_errors( ) +@pytest.mark.parametrize( + "trig_src, trig_pattern, trig_when", + [ + ("port1/line2, port1/line4", "1R", DigitalPatternCondition.PATTERN_MATCHES), + ( + "port1/line2, port1/line3, port1/line4", + "0EF", + DigitalPatternCondition.PATTERN_DOES_NOT_MATCH, + ), + ], +) +def test___start_trigger__cfg_dig_pattern_start_trig___no_errors( + sim_6535_di_single_line_task: Task, + trig_src: str, + trig_pattern: str, + trig_when: int, +): + sim_6535_di_single_line_task.timing.cfg_samp_clk_timing(1000) + sim_6535_di_single_line_task.triggers.start_trigger.cfg_dig_pattern_start_trig( + trigger_source=trig_src, + trigger_pattern=trig_pattern, + trigger_when=trig_when, + ) + + assert sim_6535_di_single_line_task.triggers.start_trigger.dig_pattern_src.name == f"{trig_src}" + assert sim_6535_di_single_line_task.triggers.start_trigger.dig_pattern_pattern == trig_pattern + assert sim_6535_di_single_line_task.triggers.start_trigger.dig_pattern_trig_when == trig_when + + @pytest.mark.parametrize( "trig_src, pretrig_samples, trig_slope, trig_level", [ @@ -298,3 +439,75 @@ def test___reference_trigger___cfg_anlg_multi_edge_ref_trig___no_errors( sim_9775_ai_voltage_multi_edge_task.triggers.reference_trigger.anlg_multi_edge_lvls == pytest.approx(trig_levels, abs=0.001) ) + + +@pytest.mark.parametrize( + "trig_src, pretrig_samples, trig_edge", + [ + ("APFI0", 10, Edge.RISING), + ("APFI1", 20, Edge.FALLING), + ], +) +def test___reference_trigger___cfg_dig_edge_ref_trig___no_errors( + sim_6363_ai_voltage_task: Task, + trig_src: str, + pretrig_samples: int, + trig_edge: Edge, +): + device_name = sim_6363_ai_voltage_task.devices[0].name + + sim_6363_ai_voltage_task.timing.cfg_samp_clk_timing(1000) + sim_6363_ai_voltage_task.triggers.reference_trigger.cfg_dig_edge_ref_trig( + trigger_source=trig_src, + pretrigger_samples=pretrig_samples, + trigger_edge=trig_edge, + ) + + assert ( + sim_6363_ai_voltage_task.triggers.reference_trigger.dig_edge_src + == f"/{device_name}/{trig_src}" + ) + assert sim_6363_ai_voltage_task.triggers.reference_trigger.pretrig_samples == pretrig_samples + assert sim_6363_ai_voltage_task.triggers.reference_trigger.dig_edge_edge == trig_edge + + +@pytest.mark.parametrize( + "trig_src, trig_pattern, pretrig_samples, trig_when", + [ + ("port1/line2, port1/line4", "1E", 10, DigitalPatternCondition.PATTERN_MATCHES), + ( + "port1/line2, port1/line3, port1/line4", + "0RF", + 20, + DigitalPatternCondition.PATTERN_DOES_NOT_MATCH, + ), + ], +) +def test___reference_trigger__cfg_dig_pattern_ref_trig___no_errors( + sim_6535_di_single_line_task: Task, + trig_src: str, + trig_pattern: str, + pretrig_samples: int, + trig_when: int, +): + sim_6535_di_single_line_task.timing.cfg_samp_clk_timing(1000) + sim_6535_di_single_line_task.triggers.reference_trigger.cfg_dig_pattern_ref_trig( + trigger_source=trig_src, + trigger_pattern=trig_pattern, + pretrigger_samples=pretrig_samples, + trigger_when=trig_when, + ) + + assert ( + sim_6535_di_single_line_task.triggers.reference_trigger.dig_pattern_src.name + == f"{trig_src}" + ) + assert ( + sim_6535_di_single_line_task.triggers.reference_trigger.dig_pattern_pattern == trig_pattern + ) + assert ( + sim_6535_di_single_line_task.triggers.reference_trigger.pretrig_samples == pretrig_samples + ) + assert ( + sim_6535_di_single_line_task.triggers.reference_trigger.dig_pattern_trig_when == trig_when + ) diff --git a/tests/conftest.py b/tests/conftest.py index 3c385d90..37633092 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -192,6 +192,12 @@ def sim_6363_device(system: nidaqmx.system.System) -> nidaqmx.system.Device: return _device_by_product_type("PCIe-6363", DeviceType.SIMULATED, system) +@pytest.fixture(scope="function") +def sim_6535_device(system: nidaqmx.system.System) -> nidaqmx.system.Device: + """Gets simulated 6535 device information.""" + return _device_by_product_type("PCIe-6535", DeviceType.SIMULATED, system) + + @pytest.fixture(scope="function") def sim_9189_device(system: nidaqmx.system.System) -> nidaqmx.system.Device: """Gets simulated 9185 device information.""" diff --git a/tests/max_config/nidaqmxMaxConfig.ini b/tests/max_config/nidaqmxMaxConfig.ini index 42bcd0ec..e2eab77c 100644 --- a/tests/max_config/nidaqmxMaxConfig.ini +++ b/tests/max_config/nidaqmxMaxConfig.ini @@ -195,6 +195,15 @@ BusType = PCIe PCI.BusNum = 0x0 PCI.DevNum = 0x0 +[DAQmxDevice hsdioTester] +ProductType = PCIe-6535 +DevSerialNum = 0x0 +DevIsSimulated = 1 +ProductNum = 0x719F +BusType = PCIe +PCI.BusNum = 0x0 +PCI.DevNum = 0x0 + [DAQmxDevice positionTester] ProductType = PXIe-4340 DevSerialNum = 0x0