Skip to content

Commit 318376f

Browse files
authored
Merge pull request #27 from CerebusOSS/22-chaninfo-for-other-devices
Ignore chaninfo packets for other devices
2 parents a6d0664 + 592d51a commit 318376f

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/pycbsdk/cbhw/device/base.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ def __init__(self, params: Params):
2929
lambda: CBChannelType.Any
3030
), # Filled in upon receiving device config.
3131
"proc_chans": 0,
32+
"instrument": -1,
3233
"channel_infos": {},
3334
"group_infos": {},
35+
"group_nchans": {},
3436
"sysfreq": None, # Should be 30_000 for legacy or 1e9 for Gemini PTP
3537
}
3638
# Init group_callbacks dict with an empty list for each supported smp grp (1-5:SMP; 6:RAW)

src/pycbsdk/cbhw/device/nsp.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ def _handle_sysrep(self, pkt):
386386
if b_general or pkt.header.type == CBPacketType.SYSREPRUNLEV:
387387
self._config["runlevel"] = CBRunLevel(pkt.runlevel)
388388
self._config["sysfreq"] = pkt.sysfreq
389+
self._config["instrument"] = pkt.header.instrument
389390
self._config_events["sysrep"].set()
390391
if b_general or pkt.header.type == CBPacketType.SYSREPRUNLEV:
391392
if self._config["runlevel"] == CBRunLevel.STANDBY:
@@ -396,7 +397,10 @@ def _handle_sysrep(self, pkt):
396397
def _handle_chaninfo(self, pkt):
397398
# If this config packet is limited in scope then it might have some garbage data in its out-of-scope payload.
398399
# We should update our config, but only the parts that this REP packet is scoped to.
399-
if pkt.header.type in [CBPacketType.CHANREP]:
400+
if pkt.header.instrument != self._config["instrument"]:
401+
# Gemini system returns channel info for all instruments.
402+
pass
403+
elif pkt.header.type in [CBPacketType.CHANREP]:
400404
# Full scope; overwrite our config.
401405
self._config["channel_infos"][pkt.chan] = copy.copy(pkt)
402406
self._config["channel_types"][pkt.chan] = get_chantype_from_chaninfo(pkt)
@@ -457,6 +461,7 @@ def _handle_groupinfo(self, pkt):
457461
else:
458462
chan_list = set()
459463
self._config["group_infos"][pkt.group] = chan_list
464+
self._config["group_nchans"][pkt.group] = len(chan_list)
460465

461466
def _handle_configall(self, pkt):
462467
if pkt.header.dlen > 0:
@@ -1017,6 +1022,7 @@ def get_config(
10171022
self._config["channel_infos"] = {}
10181023
# Do not clear sysfreq if we already have it as this cannot change.
10191024
self._config["sysfreq"] = self._config.get("sysfreq", None)
1025+
# self._config["instrument"] = -1
10201026
time.sleep(0.1)
10211027
pkt = self.packet_factory.make_packet(
10221028
None,

src/pycbsdk/cbhw/handler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ def run(self) -> None:
8282
b_debug_unknown = True # If there are no callbacks and it's not a group or event packet, then debug.
8383

8484
# See if we have any callbacks registered for this type of packet.
85+
b_grp = False
8586
if chid & CBSpecialChan.CONFIGURATION:
8687
callbacks = self._device.config_callbacks[pkt_type]
8788
elif chid == CBSpecialChan.GROUP:
8889
# This is a sample group packet. The pkt_type is actually the sample group id (1-6)
8990
if pkt_type in self._device.group_callbacks:
91+
b_grp = True
9092
callbacks = self._device.group_callbacks[pkt_type]
9193
else:
9294
# Known bug https://blackrockengineering.atlassian.net/browse/CSCI-95
@@ -106,6 +108,12 @@ def run(self) -> None:
106108
pkt = self._packet_factory.make_packet(
107109
data, chid=chid, pkt_type=pkt_type, chantype=chantype
108110
)
111+
if b_grp:
112+
# Note: pkt.data length is always a multiple of 4 bytes = 32 bits = 2 channels * 16 bits.
113+
n_chans = self._device.config["group_nchans"][pkt_type]
114+
if n_chans % 2 != 0:
115+
# Odd number of channels enabled then we have an extra 16 bits of data.
116+
pkt.data = pkt.data[:n_chans]
109117
# Between the time the callbacks are grabbed above and the time we actually call them,
110118
# it's possible for the client to unregister the callback.
111119
# Thus it's very important that a callback is unregistered and some time is allowed to pass

src/pycbsdk/examples/group_sample_intervals.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212

1313

1414
class DummyApp:
15-
def __init__(self, duration=21.0, t_step=1 / 30_000):
15+
def __init__(self, nchans: int, duration=21.0, t_step=1 / 30_000):
1616
n_samples = int(np.ceil(duration * 30_000))
17+
self._nchans = nchans
1718
self._t_step = t_step
18-
self._buffer = np.zeros((n_samples, 2), dtype=np.int16)
19+
self._buffer = np.zeros((n_samples, nchans), dtype=np.int16)
1920
self._ts = np.zeros((n_samples,), dtype=np.int64)
2021
self._write_index = 0
2122
self._last_time = 0
2223

2324
def handle_frame(self, pkt):
2425
if self._write_index < self._buffer.shape[0]:
25-
self._buffer[self._write_index, :] = memoryview(pkt.data[:4])
26+
self._buffer[self._write_index, :] = memoryview(pkt.data[:self._nchans])
2627
self._ts[self._write_index] = pkt.header.time
2728
self._write_index += 1
2829

@@ -41,6 +42,7 @@ def finish(self):
4142
def main(
4243
duration: float = 11.0,
4344
smpgroup: int = 6,
45+
nchans: int = 2,
4446
inst_addr: str = "",
4547
inst_port: int = 51002,
4648
client_addr: str = "",
@@ -81,11 +83,12 @@ def main(
8183
for chtype in [CBChannelType.FrontEnd, CBChannelType.AnalogIn]:
8284
cbsdk.set_all_channels_disable(nsp_obj, chtype)
8385

84-
# Enable channel 1 at smpgroup. For smpgroup < 5, this also updates the smpfilter.
85-
_ = cbsdk.set_channel_config(nsp_obj, 1, "smpgroup", smpgroup)
86+
# Enable channels 1 & 2 at smpgroup. For smpgroup < 5, this also updates the smpfilter.
87+
for ch in range(1, nchans + 1):
88+
_ = cbsdk.set_channel_config(nsp_obj, ch, "smpgroup", smpgroup)
8689

8790
# Create a dummy app.
88-
app = DummyApp(duration=duration, t_step=1 / config["sysfreq"])
91+
app = DummyApp(nchans, duration=duration, t_step=1 / config["sysfreq"])
8992

9093
time.sleep(2.0)
9194

0 commit comments

Comments
 (0)