-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaqhats_utils.py
128 lines (98 loc) · 3.86 KB
/
daqhats_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""
This file contains helper functions for the MCC DAQ HAT Python examples.
"""
from __future__ import print_function
from daqhats import hat_list, HatError
def select_hat_device(filter_by_id):
# type: (HatIDs) -> int
"""
This function performs a query of available DAQ HAT devices and determines
the address of a single DAQ HAT device to be used in an example. If a
single HAT device is present, the address for that device is automatically
selected, otherwise the user is prompted to select an address from a list
of displayed devices.
Args:
filter_by_id (int): If this is :py:const:`HatIDs.ANY` return all DAQ
HATs found. Otherwise, return only DAQ HATs with ID matching this
value.
Returns:
int: The address of the selected device.
Raises:
Exception: No HAT devices are found or an invalid address was selected.
"""
selected_hat_address = None
# Get descriptors for all of the available HAT devices.
hats = hat_list(filter_by_id=filter_by_id)
number_of_hats = len(hats)
# Verify at least one HAT device is detected.
if number_of_hats < 1:
raise HatError(0, 'Error: No HAT devices found')
elif number_of_hats == 1:
selected_hat_address = hats[0].address
else:
# Display available HAT devices for selection.
for hat in hats:
print('Address ', hat.address, ': ', hat.product_name, sep='')
print('')
address = int(input('Select the address of the HAT device to use: '))
# Verify the selected address if valid.
for hat in hats:
if address == hat.address:
selected_hat_address = address
break
if selected_hat_address is None:
raise ValueError('Error: Invalid HAT selection')
return selected_hat_address
def enum_mask_to_string(enum_type, bit_mask):
# type: (Enum, int) -> str
"""
This function converts a mask of values defined by an IntEnum class to a
comma separated string of names corresponding to the IntEnum names of the
values included in a bit mask.
Args:
enum_type (Enum): The IntEnum class from which the mask was created.
bit_mask (int): A bit mask of values defined by the enum_type class.
Returns:
str: A comma separated string of names corresponding to the IntEnum
names of the values included in the mask
"""
item_names = []
if bit_mask == 0:
item_names.append('DEFAULT')
for item in enum_type:
if item & bit_mask:
item_names.append(item.name)
return ', '.join(item_names)
def chan_list_to_mask(chan_list):
# type: (list[int]) -> int
"""
This function returns an integer representing a channel mask to be used
with the MCC daqhats library with all bit positions defined in the
provided list of channels to a logic 1 and all other bit positions set
to a logic 0.
Args:
chan_list (int): A list of channel numbers.
Returns:
int: A channel mask of all channels defined in chan_list.
"""
chan_mask = 0
for chan in chan_list:
chan_mask |= 0x01 << chan
return chan_mask
def validate_channels(channel_set, number_of_channels):
# type: (set, int) -> None
"""
Raises a ValueError exception if a channel number in the set of
channels is not in the range of available channels.
Args:
channel_set (set): A set of channel numbers.
number_of_channels (int): The number of available channels.
Returns:
None
Raises:
ValueError: If there is an invalid channel specified.
"""
valid_chans = range(number_of_channels)
if not channel_set.issubset(valid_chans):
raise ValueError('Error: Invalid channel selected - must be '
'{} - {}'.format(min(valid_chans), max(valid_chans)))