-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMultipleDeviceThreadingExample.py
More file actions
194 lines (145 loc) · 5.61 KB
/
Copy pathMultipleDeviceThreadingExample.py
File metadata and controls
194 lines (145 loc) · 5.61 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
from pathlib import Path
import platform
import sys
import threading
API_BASE_DIR = "PLUX-API-Python3"
API_BASE_PATH = Path(__file__).resolve().parent / API_BASE_DIR
def _python_version():
"""Return the Python major and minor version as integers."""
major, minor = platform.python_version_tuple()[:2]
return int(major), int(minor)
def _is_arm_64(machine, architecture):
"""Return whether the current machine should be treated as ARM 64-bit."""
return machine.startswith("aarch64") or (
machine.startswith("arm") and architecture == "64"
)
def _available_api_dirs(api_base_path):
"""Return the available API directories for diagnostic purposes."""
if not api_base_path.is_dir():
return []
return sorted(path.name for path in api_base_path.iterdir() if path.is_dir())
def _first_existing_api_dir(candidates):
"""Return the first candidate API directory that exists."""
for candidate in candidates:
if (API_BASE_PATH / candidate).is_dir():
return candidate
raise ValueError(
"Unsupported platform/Python combination: "
f"system={platform.system()}, machine={platform.machine().lower()}, "
f"architecture={platform.architecture()[0][:2]}, "
f"python={platform.python_version()}, "
f"attempted_api_dirs={', '.join(candidates)}, "
f"available_api_dirs={', '.join(_available_api_dirs(API_BASE_PATH)) or 'none'}"
)
def _resolve_api_dir():
"""Resolve the PLUX API directory for the current platform."""
system = platform.system()
machine = platform.machine().lower()
architecture = platform.architecture()[0][:2]
major, minor = _python_version()
if not API_BASE_PATH.is_dir():
raise ValueError(f"API base directory does not exist: {API_BASE_PATH}")
if major != 3:
raise ValueError(
f"Unsupported Python major version: {platform.python_version()}. "
"Python 3 is required."
)
version_suffix = f"{major}{minor}"
if system == "Windows":
return _first_existing_api_dir([f"Win{architecture}_{version_suffix}"])
if system == "Darwin":
if machine == "x86_64":
return _first_existing_api_dir(
[f"MacOS/Intel{version_suffix}", "MacOS"]
)
if machine == "arm64":
return _first_existing_api_dir([f"M1_{version_suffix}"])
raise ValueError(f"Unsupported macOS architecture: {machine}")
if system == "Linux":
if machine.startswith("arm") and architecture == "32":
candidates = [f"LinuxARM32_{version_suffix}"]
if minor < 11:
candidates.append("LinuxARM32")
return _first_existing_api_dir(candidates)
if _is_arm_64(machine, architecture):
return _first_existing_api_dir([f"LinuxARM64_{version_suffix}"])
if architecture == "64":
return _first_existing_api_dir([f"Linux64_{version_suffix}", "Linux64"])
raise ValueError(
f"Unsupported Linux architecture: machine={machine}, "
f"architecture={architecture}"
)
raise ValueError(f"Unsupported operating system: {system}")
try:
api_dir = _resolve_api_dir()
api_path = API_BASE_PATH / api_dir
sys.path.append(str(api_path))
print(f"Loaded API: {api_path}")
except ValueError as error:
print(error)
sys.exit(1)
import plux
class NewDevice(plux.SignalsDev):
def __init__(self, address):
plux.MemoryDev.__init__(address)
self.time = 0
self.frequency = 0
def onRawFrame(self, nSeq, data): # onRawFrame takes three arguments
if nSeq % 2000 == 0:
print(nSeq)
if nSeq / self.frequency > self.time:
return True
return False
# example routines
def exampleAcquisition(address, time, freq, code): # time acquisition for each frequency
"""
Example acquisition.
Supported channel number codes:
{1 channel - 0x01, 2 channels - 0x03, 3 channels - 0x07
4 channels - 0x0F, 5 channels - 0x1F, 6 channels - 0x3F
7 channels - 0x7F, 8 channels - 0xFF}
Maximum acquisition frequencies for number of channels:
1 channel - 8000, 2 channels - 5000, 3 channels - 4000
4 channels - 3000, 5 channels - 3000, 6 channels - 2000
7 channels - 2000, 8 channels - 2000
"""
device = NewDevice(address)
device.time = time # interval of acquisition
device.frequency = freq
device.start(device.frequency, code, 16)
device.loop() # calls device.onRawFrame until it returns True
device.stop()
device.close()
def createThreads(address_list, time, freq_list, code_list):
thread_list = []
for index in range(len(address_list)):
thread_list.append(
threading.Thread(
target=exampleAcquisition,
args=(
address_list[index],
time,
freq_list[index],
code_list[index],
),
)
)
thread_list[index].start()
for index in range(len(address_list)):
thread_list[index].join()
if platform.system() == "Darwin":
plux.MacOS.stopMainLoop()
def createMainThread(address_list, time, freq_list, code_list):
main_thread = threading.Thread(
target=createThreads, args=(address_list, time, freq_list, code_list)
)
main_thread.start()
if platform.system() == "Darwin":
plux.MacOS.runMainLoop()
main_thread.join()
createMainThread(
["BTH00:07:80:D8:AB:46", "BTH00:07:80:3B:46:58", "BTH00:07:80:4D:2E:76"],
20,
[1000, 1000, 1000],
[0xFF, 0xFF, 0x01],
)