DynExp
Highly flexible laboratory automation for dynamically changing experiments.
DataStreamInstrument.cpp
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
3 #include "stdafx.h"
4 #include "DataStreamInstrument.h"
5 
6 namespace DynExpInstr
7 {
9  {
10  auto InstrData = DynExp::dynamic_InstrumentData_cast<DataStreamInstrument>(Instance.InstrumentDataGetter());
11 
12  InstrData->GetSampleStream()->SetStreamSize(BufferSizeInSamples);
13 
14  return {};
15  }
16 
18  {
19  for (const auto& Sample : Samples)
20  WriteBasicSampleChild(Sample);
21  }
22 
24  {
25  if (!CanRead())
26  return {};
27 
28  BasicSampleListType Samples;
29 
30  for (decltype(Count) i = 0; i < Count; ++i)
31  Samples.push_back(ReadBasicSampleChild());
32 
33  return Samples;
34  }
35 
37  {
38  throw Util::NotImplementedException("This data stream does not support writing samples of type BasicSample.");
39  }
40 
42  {
43  throw Util::NotImplementedException("This data stream does not support reading samples of type BasicSample.");
44 
45  return {};
46  }
47 
49  {
50  return std::min(GetNumSamplesWritten() - Count, GetStreamSizeRead());
51  }
52 
54  {
55  auto OldReadPos = GetReadPosition();
56  auto ReadLength = Util::NumToT<signed long long>(GetNumRecentBasicSamples(Count));
57  SeekEqual(std::ios_base::in);
58  SeekRel(-ReadLength, std::ios_base::cur, std::ios_base::in);
59  auto Samples = ReadBasicSamples(ReadLength);
60  SeekAbs(OldReadPos, std::ios_base::in);
61 
62  return Samples;
63  }
64 
66  {
68  }
69 
71  {
73  { "Perform reading/writing only once.", SamplingModeType::Single },
74  { "Perform reading/writing continuously.", SamplingModeType::Continuous }
75  };
76 
77  return List;
78  }
79 
81  {
84  }
85 
87  {
88  switch (Unit)
89  {
90  case UnitType::Arbitrary: return "a.u.";
91  case UnitType::LogicLevel: return "TTL";
92  case UnitType::Counts: return "#";
93  case UnitType::Volt: return "V";
94  case UnitType::Ampere: return "A";
95  case UnitType::Power_W: return "W";
96  case UnitType::Power_dBm: return "dBm";
97  default: return "<unknown unit>";
98  }
99  }
100 
102  : SampleStream(std::move(SampleStream)), HardwareMinValue(0), HardwareMaxValue(0), ValueUnit(UnitType::Arbitrary)
103  {
104  if (!this->SampleStream)
105  throw Util::InvalidArgException("SampleStream cannot be nullptr.");
106  }
107 
109  {
110  SampleStream->Clear();
111 
112  HardwareMinValue = 0;
113  HardwareMaxValue = 0;
115 
117  }
118 
120  {
121  }
122 
124  {
125  }
126 
128  {
129  }
130 
132  {
133  // Do not throw a Util::NotImplementedException here, since it is fine to read data
134  // which has been written to the buffer of e.g. a derived function generator instrument.
135  }
136 
138  {
139  throw Util::NotImplementedException("This data stream instrument does not support write operations.");
140  }
141 
143  {
144  Stop();
145  Start(CallbackFunc);
146  }
147 
148  void DataStreamInstrument::SetStreamSize(size_t BufferSizeInSamples, DynExp::TaskBase::CallbackType CallbackFunc) const
149  {
150  MakeAndEnqueueTask<DataStreamInstrumentTasks::SetStreamSizeTask>(BufferSizeInSamples, CallbackFunc);
151  }
152 
153  bool DataStreamInstrument::CanRead(const std::chrono::milliseconds Timeout) const
154  {
155  // Issue a read task, so it is allowed to poll CanRead() to await at least one sample becoming available.
156  ReadData();
157 
158  return dynamic_InstrumentData_cast<DataStreamInstrument>(GetInstrumentData(Timeout))->GetSampleStream()->CanRead();
159  }
160 
161  void DataStreamInstrument::Clear(const std::chrono::milliseconds Timeout) const
162  {
163  DynExp::dynamic_InstrumentData_cast<DataStreamInstrument>(GetInstrumentData(Timeout))->GetSampleStream()->Clear();
164 
165  ClearData();
166  }
167 
169  {
171  }
172 }
Implementation of a data stream meta instrument and of data streams input/output devices might work o...
size_t GetNumRecentBasicSamples(size_t Count) const
Determines the amount of samples which have been written to the stream after the last Count samples....
BasicSampleListType ReadRecentBasicSamples(size_t Count)
Reads the most recent samples from the stream skipping Count samples. Also refer to GetNumRecentBasic...
virtual void WriteBasicSampleChild(const BasicSample &Sample)
Writes a single basic sample to the stream.
BasicSampleListType ReadBasicSamples(size_t Count)
Reads a list of basic sample from the stream.
std::vector< BasicSample > BasicSampleListType
Type of a list containing data stream samples of type BasicSample.
virtual BasicSample ReadBasicSampleChild()
Reads a single basic sample from the stream.
void WriteBasicSamples(const BasicSampleListType &Samples)
Writes a list of basic sample to the stream.
UnitType
Units which can be used for data stream instruments.
ValueType HardwareMaxValue
Maximal possible value to read/write from/to the hardware adapter.
DataStreamInstrumentData(DataStreamBasePtrType &&SampleStream)
Constructs a DataStreamInstrumentData instance.
UnitType ValueUnit
Unit type of the values to be read/written from/to the hardware adapter.
static const char * UnitTypeToStr(const UnitType &Unit)
Returns a descriptive string of a respective unit to be e.g. used in plots.
const DataStreamBasePtrType SampleStream
Refer to DynExp::InstrumentDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl()...
void ResetImpl(dispatch_tag< InstrumentDataBase >) override final
ValueType HardwareMinValue
Minimal possible value to read/write from/to the hardware adapter.
const size_t BufferSizeInSamples
New stream buffer size in samples.
virtual DynExp::TaskResultType RunChild(DynExp::InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
virtual void WriteData(DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const
Enqueues a task to write data from the data stream to the hardware.
virtual void ClearData(DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const
Enqueues a task to clear the underlying hardware adapter's buffer.
virtual void Restart(DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const
Enqueues a task to make the underlying hardware adapter restart data acquisition or writing data....
virtual void ReadData(DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const
Enqueues a task to read data from the hardware to the data stream. The default implementation does no...
virtual void SetStreamSize(size_t BufferSizeInSamples, DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const
Enqueues a task to set the size of the instrument's sample stream. The default implementation just ca...
virtual void Start(DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const
Enqueues a task to make the underlying hardware adapter start data acquisition or writing data.
bool CanRead(const std::chrono::milliseconds Timeout=GetInstrumentDataTimeoutDefault) const
Calls ReadData(), locks the instrument data, and determines whether at least one sample can be read f...
void Clear(const std::chrono::milliseconds Timeout=GetInstrumentDataTimeoutDefault) const
Immediately clears the instrument's data stream and then issues a ClearData task by locking the instr...
virtual void Stop(DynExp::TaskBase::CallbackType CallbackFunc=nullptr) const
Enqueues a task to make the underlying hardware adapter stop data acquisition or writing data.
void ResetImpl(dispatch_tag< InstrumentBase >) override final
static Util::TextValueListType< SamplingModeType > SamplingModeTypeStrList()
Maps description strings to the SamplingModeType enum's items.
void DisableUserEditable()
Calls DynExp::ParamsBase::DisableUserEditable() on all bundled parameters.
void DisableUserEditable()
Calls DynExp::ParamsBase::DisableUserEditable() on all bundled parameters.
InstrumentDataTypeSyncPtrType GetInstrumentData(const std::chrono::milliseconds Timeout=GetInstrumentDataTimeoutDefault)
Locks the mutex of the instrument data class instance InstrumentData assigned to this InstrumentBase ...
Definition: Instrument.cpp:222
Refer to ParamsBase::dispatch_tag.
Definition: Instrument.h:146
Defines data for a thread belonging to a InstrumentBase instance. Refer to RunnableInstance.
Definition: Instrument.h:772
const InstrumentBase::InstrumentDataGetterType InstrumentDataGetter
Getter for instrument's data. Refer to InstrumentBase::InstrumentDataGetterType.
Definition: Instrument.h:791
Refer to ParamsBase::dispatch_tag.
Definition: Object.h:2018
static void DisableUserEditable(ParamBase &Param) noexcept
Sets the UserEditable property of the parameter Param to false. Refer to ParamBase::UserEditable.
Definition: Object.cpp:292
std::function< void(const TaskBase &, ExceptionContainer &)> CallbackType
Type of a callback function which is invoked when a task has finished, failed or has been aborted....
Definition: Instrument.h:939
Defines the return type of task functions.
Definition: Instrument.h:824
An invalid argument like a null pointer has been passed to a function.
Definition: Exception.h:137
Thrown when a requested feature is either under development and thus not implemented yet or when a sp...
Definition: Exception.h:299
@ Continuous
Run continuously disabling the trigger.
DynExp's instrument namespace contains the implementation of DynExp instruments which extend DynExp's...
Definition: Instrument.h:1254
DataStreamPtrType< DataStreamBase > DataStreamBasePtrType
Type of a pointer owning a DataStreamBase instance.
std::vector< std::pair< TextType, ValueType > > TextValueListType
Type of a list containing key-value pairs where key is a text of type Util::TextType.
Definition: QtUtil.h:37
Accumulates include statements to provide a precompiled header.
Defines a trivially-copyable basic sample as time (t)-value (f(t)) pairs (t, f(t)).