def __init__(self, stream, subdevice, channels, physical=False, plot=False): self.stream = stream self.subdevice = subdevice self.channels = channels self.physical = physical self.plot = plot if plot: if _pyplot is None: raise _matplotlib_import_error self.figure = _pyplot.figure() self.axes = self.figure.add_subplot(1, 1, 1) self.n = 100 self.index = 0 self.tdata = _numpy.arange(self.n) self.ydata = _numpy.zeros( (2*self.n, len(channels)), dtype=_numpy.float32) self.lines = [] for i,channel in enumerate(channels): self.lines.append(_Line2D( self.tdata, self.ydata[:self.n,i], linestyle='', color='red', marker='.')) self.axes.add_line(self.lines[-1]) self.axes.set_xlim(0, self.n-1) self.axes.set_ylim(0, max(c.get_maxdata() for c in channels)) _pyplot.draw() _LOG.debug('data writer initialized') _LOG.debug('poll: {}'.format(self.subdevice.poll())) _LOG.debug('get_buffer contents: {}'.format( self.subdevice.get_buffer_contents()))
def __call__(self, data): _LOG.debug('new data: {}'.format(data)) d = _numpy.ndarray(shape=(1,data.size), dtype=data.dtype, buffer=data) if self.plot: self.ydata[self.index,:] = d self.ydata[self.index + self.n,:] = d for i,line in enumerate(self.lines): line.set_data( self.tdata, self.ydata[self.index+1:self.index + self.n+1,i]) _pyplot.draw() self.index = (self.index + 1) % len(self.tdata) write_data( stream=self.stream, channels=self.channels, data=d, physical=self.physical) _LOG.debug('poll: {}'.format(self.subdevice.poll())) _LOG.debug('get_buffer contents: {}'.format( self.subdevice.get_buffer_contents()))
def read(device, subdevice=None, channels=[0], range=0, aref=0, period=0, num_scans=2, reader=_utility.Reader, physical=False, plot=False, stream=_sys.stdout): """Read ``num_scans`` samples from each specified channel. """ subdevice,channels = open_channels( device=device, subdevice=subdevice, channels=channels, range=range, aref=aref) subdevice.cmd = prepare_command( subdevice=subdevice, channels=channels, period=period, num_scans=num_scans) rc = test_command(subdevice=subdevice) kwargs = {} if reader == _utility.CallbackReader: read_buffer_shape = (len(channels),) kwargs = { 'callback': DataWriter( stream=stream, subdevice=subdevice, channels=channels, physical=physical, plot=plot), 'count': num_scans, } else: read_buffer_shape = (num_scans, len(channels)) read_buffer = _numpy.zeros(read_buffer_shape, dtype=subdevice.get_dtype()) reader = reader( subdevice=subdevice, buffer=read_buffer, name='Reader', **kwargs) start = _time.time() _LOG.info('start time: {}'.format(start)) subdevice.command() reader.start() while subdevice.get_flags().running: _LOG.debug('running...') _LOG.debug('poll: {}'.format(subdevice.poll())) _LOG.debug('get_buffer offset: {}'.format( subdevice.get_buffer_offset())) _LOG.debug('get_buffer contents: {}'.format( subdevice.get_buffer_contents())) _time.sleep(0.5) _LOG.debug('stopped running. joining reader...') stop = _time.time() _LOG.info('stop time: {}'.format(stop)) reader.join() join = _time.time() _LOG.info('join time: {}'.format(join)) _LOG.debug('poll: {}'.format(subdevice.poll())) _LOG.debug('get_buffer offset: {}'.format( subdevice.get_buffer_offset())) _LOG.debug('get_buffer contents: {}'.format( subdevice.get_buffer_contents())) _LOG.info('stop delay: {}'.format(stop - start)) _LOG.info('join delay: {}'.format(join - stop)) if not isinstance(reader, _utility.CallbackReader): write_data( stream=stream, channels=channels, data=read_buffer, physical=physical)