def send_halt(self): """Send a halt packet without stopping the transmitter.""" heap = {_spead.HEAP_CNT_ID: (_spead.IMMEDIATEADDR, '\xff\xff\xff\xff\xff\xff'), _spead.STREAM_CTRL_ID: (_spead.IMMEDIATEADDR, _spead.pack(DEFAULT_FMT, ((_spead.STREAM_CTRL_TERM_VAL,),)))} logger.info('TX.end: Sending stream terminator') self.send_heap(heap)
def to_descriptor_string(self): """Create a string representation that encodes the attributes of this descriptor.""" if self.size == -1: shape = _spead.pack(SHAPE_FMT, ((2, 0),)) else: shape = _spead.pack(SHAPE_FMT, [(0, s) for s in self.shape]) heap = { ID_ID: (_spead.IMMEDIATEADDR, _spead.pack(ID_FMT, ((0, self.id),))), SHAPE_ID: (_spead.DIRECTADDR, shape), FORMAT_ID: (_spead.DIRECTADDR, self.format), NAME_ID: (_spead.DIRECTADDR, self.name), DESCRIPTION_ID: (_spead.DIRECTADDR, self.description), _spead.HEAP_CNT_ID: (_spead.IMMEDIATEADDR, ADDRNULL), } if self.dtype_str is not None: heap[DTYPE_ID] = (_spead.DIRECTADDR, self.dtype_str) return ''.join([p for p in iter_genpackets(heap)])
def to_descriptor_string(self): """Create a string representation that encodes the attributes of this descriptor.""" if self.size == -1: shape = _spead.pack(SHAPE_FMT, ((2, 0), )) else: shape = _spead.pack(SHAPE_FMT, [(0, s) for s in self.shape]) heap = { ID_ID: (_spead.IMMEDIATEADDR, _spead.pack(ID_FMT, ((0, self.id), ))), SHAPE_ID: (_spead.DIRECTADDR, shape), FORMAT_ID: (_spead.DIRECTADDR, self.format), NAME_ID: (_spead.DIRECTADDR, self.name), DESCRIPTION_ID: (_spead.DIRECTADDR, self.description), _spead.HEAP_CNT_ID: (_spead.IMMEDIATEADDR, ADDRNULL), } if self.dtype_str is not None: heap[DTYPE_ID] = (_spead.DIRECTADDR, self.dtype_str) return ''.join([p for p in iter_genpackets(heap)])
def send_halt(self): """Send a halt packet without stopping the transmitter.""" heap = { _spead.HEAP_CNT_ID: (_spead.IMMEDIATEADDR, '\xff\xff\xff\xff\xff\xff'), _spead.STREAM_CTRL_ID: (_spead.IMMEDIATEADDR, _spead.pack(DEFAULT_FMT, ((_spead.STREAM_CTRL_TERM_VAL, ), ))) } logger.info('TX.end: Sending stream terminator') self.send_heap(heap)
def pack(self, val): """Convert a series of values into a binary string according to the format of this Descriptor. Multi-dimensonal arrays are serialized in C-like order (as opposed to Fortran-like).""" if self.shape != -1 and len(self.shape) != 0: val = numpy.array(val) dim = calcdim(self.format) if self.shape == -1: val = numpy.reshape(val, (val.size/dim, dim)) else: val = numpy.reshape(val, (self.size, dim)) st = time.time() ret = _spead.pack(self.format, val) return ret
def pack(self, val): """Convert a series of values into a binary string according to the format of this Descriptor. Multi-dimensonal arrays are serialized in C-like order (as opposed to Fortran-like).""" if self.shape != -1 and len(self.shape) != 0: val = numpy.array(val) dim = calcdim(self.format) if self.shape == -1: val = numpy.reshape(val, (val.size / dim, dim)) else: val = numpy.reshape(val, (self.size, dim)) st = time.time() ret = _spead.pack(self.format, val) return ret
def get_heap(self, heap=None): """Return the heap that must be transmitted to propagate the change in the state of this ItemGroup since the last time this function was called. An existing heap (a dictionary) can be provided as a starting point, if desired.""" # Inject an automatically generated heap count logger.info('ITEMGROUP.get_heap: Building heap with HEAP_CNT=%d' % self.heap_cnt) if heap is None: heap = {} heap[_spead.HEAP_CNT_ID] = (_spead.IMMEDIATEADDR, _spead.pack(DEFAULT_FMT, ((self.heap_cnt, ), ))) self.heap_cnt += 1 # Switching back to dict based _new_names to avoid memory leaks from long running multiple updates. # NOTE: The behaviour of the previous list approach is broken anyway as the new_name all resolve to a single ID # that is provided by self._names. So essentially this was doing nothing. heap[_spead.DESCRIPTOR_ID] = [] sent_names = [] for item in self._new_names.itervalues(): if DEBUG: logger.debug( 'ITEMGROUP.get_heap: Adding descriptor for id=%d (name=%s)' % (item.id, item.name)) heap[_spead.DESCRIPTOR_ID].append(item.to_descriptor_string()) sent_names.append(item.name) for name in sent_names: self._new_names.pop(name) # we explicitly remove the names we have sent, rather than dumping the whole dict, just in case # things get modified as we iterate. # Add entries for any items that have changed for item in self._items.itervalues(): if not item.has_changed(): continue val = item.to_value_string() if len(val) > _spead.ADDRLEN or item.size < 0: mode = _spead.DIRECTADDR else: mode = _spead.IMMEDIATEADDR if DEBUG: logger.debug( 'ITEMGROUP.get_heap: Adding entry for id=%d (name=%s)' % (item.id, item.name)) heap[item.id] = (mode, val) # Once data is gathered from changed item, mark it as unchanged item.unset_changed() logger.info('ITEMGROUP.get_heap: Done building heap with HEAP_CNT=%d' % (self.heap_cnt - 1)) return heap
def get_heap(self, heap=None): """Return the heap that must be transmitted to propagate the change in the state of this ItemGroup since the last time this function was called. An existing heap (a dictionary) can be provided as a starting point, if desired.""" # Inject an automatically generated heap count logger.info('ITEMGROUP.get_heap: Building heap with HEAP_CNT=%d' % self.heap_cnt) if heap is None: heap = {} heap[_spead.HEAP_CNT_ID] = (_spead.IMMEDIATEADDR, _spead.pack(DEFAULT_FMT, ((self.heap_cnt,),))) self.heap_cnt += 1 # Switching back to dict based _new_names to avoid memory leaks from long running multiple updates. # NOTE: The behaviour of the previous list approach is broken anyway as the new_name all resolve to a single ID # that is provided by self._names. So essentially this was doing nothing. heap[_spead.DESCRIPTOR_ID] = [] sent_names = [] for item in self._new_names.itervalues(): if DEBUG: logger.debug('ITEMGROUP.get_heap: Adding descriptor for id=%d (name=%s)' % (item.id, item.name)) heap[_spead.DESCRIPTOR_ID].append(item.to_descriptor_string()) sent_names.append(item.name) for name in sent_names: self._new_names.pop(name) # we explicitly remove the names we have sent, rather than dumping the whole dict, just in case # things get modified as we iterate. # Add entries for any items that have changed for item in self._items.itervalues(): if not item.has_changed(): continue val = item.to_value_string() if len(val) > _spead.ADDRLEN or item.size < 0: mode = _spead.DIRECTADDR else: mode = _spead.IMMEDIATEADDR if DEBUG: logger.debug('ITEMGROUP.get_heap: Adding entry for id=%d (name=%s)' % (item.id, item.name)) heap[item.id] = (mode, val) # Once data is gathered from changed item, mark it as unchanged item.unset_changed() logger.info('ITEMGROUP.get_heap: Done building heap with HEAP_CNT=%d' % (self.heap_cnt - 1)) return heap
def mkfmt(*args): return _spead.pack(FORMAT_FMT, args)