|
| 1 | +"""Shared utilities for digital component tests.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Callable, TypeVar |
| 6 | + |
| 7 | +import numpy |
| 8 | +from nitypes.waveform import DigitalWaveform |
| 9 | + |
| 10 | +import nidaqmx |
| 11 | + |
| 12 | +_D = TypeVar("_D", bound=numpy.generic) |
| 13 | + |
| 14 | + |
| 15 | +def _start_di_task(task: nidaqmx.Task) -> None: |
| 16 | + # Don't reserve the lines, so we can read what DO is writing. |
| 17 | + task.di_channels.all.di_tristate = False |
| 18 | + task.start() |
| 19 | + |
| 20 | + |
| 21 | +def _start_do_task(task: nidaqmx.Task, is_port: bool = False, num_chans: int = 1) -> None: |
| 22 | + # We'll be doing on-demand, so start the task and drive all lines low |
| 23 | + task.start() |
| 24 | + if is_port: |
| 25 | + if num_chans == 8: |
| 26 | + task.write(0) |
| 27 | + else: |
| 28 | + task.write([0] * num_chans) |
| 29 | + else: |
| 30 | + if num_chans == 1: |
| 31 | + task.write(False) |
| 32 | + else: |
| 33 | + task.write([False] * num_chans) |
| 34 | + |
| 35 | + |
| 36 | +def _get_num_di_lines_in_task(task: nidaqmx.Task) -> int: |
| 37 | + return sum([chan.di_num_lines for chan in task.channels]) |
| 38 | + |
| 39 | + |
| 40 | +def _get_num_do_lines_in_task(task: nidaqmx.Task) -> int: |
| 41 | + return sum([chan.do_num_lines for chan in task.channels]) |
| 42 | + |
| 43 | + |
| 44 | +def _get_digital_data_for_sample(num_lines: int, sample_number: int) -> int: |
| 45 | + result = 0 |
| 46 | + # Simulated digital signals "count" from 0 in binary within each group of 8 lines. |
| 47 | + for _ in range((num_lines + 7) // 8): |
| 48 | + result = (result << 8) | sample_number |
| 49 | + |
| 50 | + line_mask = (2**num_lines) - 1 |
| 51 | + return result & line_mask |
| 52 | + |
| 53 | + |
| 54 | +def _get_expected_data_for_line(num_samples: int, line_number: int) -> list[int]: |
| 55 | + data = [] |
| 56 | + # Simulated digital signals "count" from 0 in binary within each group of 8 lines. |
| 57 | + # Each line represents a bit in the binary representation of the sample number. |
| 58 | + # - line 0 represents bit 0 (LSB) - alternates every sample: 0,1,0,1,0,1,0,1... |
| 59 | + # - line 1 represents bit 1 - alternates every 2 samples: 0,0,1,1,0,0,1,1... |
| 60 | + # - line 2 represents bit 2 - alternates every 4 samples: 0,0,0,0,1,1,1,1... |
| 61 | + line_number %= 8 |
| 62 | + for sample_num in range(num_samples): |
| 63 | + bit_value = (sample_num >> line_number) & 1 |
| 64 | + data.append(bit_value) |
| 65 | + return data |
| 66 | + |
| 67 | + |
| 68 | +def _get_digital_data(num_lines: int, num_samples: int) -> list[int]: |
| 69 | + return [ |
| 70 | + _get_digital_data_for_sample(num_lines, sample_number) |
| 71 | + for sample_number in range(num_samples) |
| 72 | + ] |
| 73 | + |
| 74 | + |
| 75 | +def _get_expected_digital_port_data_port_major( |
| 76 | + task: nidaqmx.Task, num_samples: int |
| 77 | +) -> list[list[int]]: |
| 78 | + return [_get_digital_data(chan.di_num_lines, num_samples) for chan in task.channels] |
| 79 | + |
| 80 | + |
| 81 | +def _get_expected_digital_port_data_sample_major( |
| 82 | + task: nidaqmx.Task, num_samples: int |
| 83 | +) -> list[list[int]]: |
| 84 | + result = _get_expected_digital_port_data_port_major(task, num_samples) |
| 85 | + return numpy.transpose(result).tolist() |
| 86 | + |
| 87 | + |
| 88 | +def _get_digital_port_data_for_sample(task: nidaqmx.Task, sample_number: int) -> list[int]: |
| 89 | + return [ |
| 90 | + _get_digital_data_for_sample(chan.do_num_lines, sample_number) for chan in task.channels |
| 91 | + ] |
| 92 | + |
| 93 | + |
| 94 | +def _get_digital_port_data_port_major(task: nidaqmx.Task, num_samples: int) -> list[list[int]]: |
| 95 | + return [_get_digital_data(chan.do_num_lines, num_samples) for chan in task.channels] |
| 96 | + |
| 97 | + |
| 98 | +def _get_digital_port_data_sample_major(task: nidaqmx.Task, num_samples: int) -> list[list[int]]: |
| 99 | + result = _get_digital_port_data_port_major(task, num_samples) |
| 100 | + return numpy.transpose(result).tolist() |
| 101 | + |
| 102 | + |
| 103 | +def _bool_array_to_int(bool_array: numpy.typing.NDArray[numpy.bool_]) -> int: |
| 104 | + result = 0 |
| 105 | + # Simulated data is little-endian |
| 106 | + for bit in bool_array[::-1]: |
| 107 | + result = (result << 1) | int(bit) |
| 108 | + return result |
| 109 | + |
| 110 | + |
| 111 | +def _int_to_bool_array(num_lines: int, input: int) -> numpy.typing.NDArray[numpy.bool_]: |
| 112 | + result = numpy.full(num_lines, True, dtype=numpy.bool_) |
| 113 | + for bit in range(num_lines): |
| 114 | + result[bit] = (input & (1 << bit)) != 0 |
| 115 | + return result |
| 116 | + |
| 117 | + |
| 118 | +def _get_waveform_data(waveform: DigitalWaveform) -> list[int]: |
| 119 | + assert isinstance(waveform, DigitalWaveform) |
| 120 | + return [_bool_array_to_int(sample) for sample in waveform.data] |
| 121 | + |
| 122 | + |
| 123 | +def _read_and_copy( |
| 124 | + read_func: Callable[[numpy.typing.NDArray[_D]], None], array: numpy.typing.NDArray[_D] |
| 125 | +) -> numpy.typing.NDArray[_D]: |
| 126 | + read_func(array) |
| 127 | + return array.copy() |
0 commit comments