DynExp
Highly flexible laboratory automation for dynamically changing experiments.
InputPortReader.cpp
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
3 #include "stdafx.h"
4 #include "moc_InputPortReader.cpp"
5 #include "InputPortReader.h"
6 
7 namespace DynExpModule
8 {
9  InputPortReaderWidget::InputPortReaderWidget(InputPortReader& Owner, QModuleWidget* parent) : QModuleWidget(Owner, parent)
10  {
11  ui.setupUi(this);
12 
13  ui.AnalogInWidget->setVisible(false);
14  ui.DigitalInWidget->setVisible(false);
15  }
16 
18  {
19  Init();
20  }
21 
23  {
24  UIInitialized = false;
25  IsDigitalPort = false;
26  Value = 0;
27  }
28 
30  {
31  try
32  {
33  auto ModuleData = DynExp::dynamic_ModuleData_cast<InputPortReader>(Instance.ModuleDataGetter());
34 
35  {
36  auto InstrData = DynExp::dynamic_InstrumentData_cast<DynExpInstr::InputPort>(ModuleData->InputPort->GetInstrumentData());
37 
38  if (InstrData->GetSampleStream()->CanRead())
39  ModuleData->Value = InstrData->GetSampleStream()->ReadBasicSample().Value;
40  } // InstrData unlocked here.
41  ModuleData->InputPort->ReadData();
42 
44  } // ModuleData unlocked here.
45  catch (const Util::TimeoutException& e)
46  {
47  if (NumFailedUpdateAttempts++ >= 3)
48  Instance.GetOwner().SetWarning(e);
49  }
50 
52  }
53 
55  {
57  }
58 
59  std::unique_ptr<DynExp::QModuleWidget> InputPortReader::MakeUIWidget()
60  {
61  auto Widget = std::make_unique<InputPortReaderWidget>(*this);
62 
63  return Widget;
64  }
65 
66  void InputPortReader::UpdateUIChild(const ModuleBase::ModuleDataGetterType& ModuleDataGetter)
67  {
68  auto Widget = GetWidget<InputPortReaderWidget>();
69  auto ModuleData = DynExp::dynamic_ModuleData_cast<InputPortReader>(ModuleDataGetter());
70 
71  if (!ModuleData->UIInitialized)
72  {
73  Widget->ui.AnalogInWidget->setVisible(!ModuleData->IsDigitalPort);
74  Widget->ui.DigitalInWidget->setVisible(ModuleData->IsDigitalPort);
75  Widget->adjustSize();
76 
77  ModuleData->UIInitialized = true;
78  }
79 
80  if (!ModuleData->IsDigitalPort)
81  {
82  Widget->ui.ValueLabel->setText(QString::number(ModuleData->Value) + " " + DynExpInstr::DataStreamInstrumentData::UnitTypeToStr(ModuleData->InputPort->GetValueUnit()));
83  Widget->ui.ValueProgressBar->setMinimum(ModuleData->InputPort->GetHardwareMinValue());
84  Widget->ui.ValueProgressBar->setMaximum(ModuleData->InputPort->GetHardwareMaxValue());
85  Widget->ui.ValueProgressBar->setValue(ModuleData->Value);
86  }
87  else
88  {
89  Widget->ui.StateLabel->setText(ModuleData->Value ? "High" : "Low");
90  Widget->ui.StateFrame->setStyleSheet(ModuleData->Value ? "background-color: lime;" : "background-color: red;");
91  }
92  }
93 
95  {
96  auto ModuleParams = DynExp::dynamic_Params_cast<InputPortReader>(Instance->ParamsGetter());
97  auto ModuleData = DynExp::dynamic_ModuleData_cast<InputPortReader>(Instance->ModuleDataGetter());
98 
99  Instance->LockObject(ModuleParams->InputPort, ModuleData->InputPort);
100  ModuleData->InputPort->ReadData();
101 
102  ModuleData->IsDigitalPort = ModuleData->InputPort->GetValueUnit() == DynExpInstr::DataStreamInstrumentData::UnitType::LogicLevel;
103  }
104 
106  {
107  auto ModuleData = DynExp::dynamic_ModuleData_cast<InputPortReader>(Instance->ModuleDataGetter());
108 
109  Instance->UnlockObject(ModuleData->InputPort);
110  }
111 }
Implementation of a module to read single values from input port instruments.
@ LogicLevel
Logic level (TTL) units (1 or 0)
static const char * UnitTypeToStr(const UnitType &Unit)
Returns a descriptive string of a respective unit to be e.g. used in plots.
void ResetImpl(dispatch_tag< QModuleDataBase >) override final
InputPortReaderWidget(InputPortReader &Owner, QModuleWidget *parent=nullptr)
void ResetImpl(dispatch_tag< QModuleBase >) override final
std::unique_ptr< DynExp::QModuleWidget > MakeUIWidget() override final
Used by InitUI() as a factory function for the module's user interface widget. Create the widget here...
void OnInit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread starts. Override it to lock instruments this m...
void UpdateUIChild(const ModuleBase::ModuleDataGetterType &ModuleDataGetter) override final
Util::DynExpErrorCodes::DynExpErrorCodes ModuleMainLoop(DynExp::ModuleInstance &Instance) override final
Module main loop. The function is executed periodically by the module thread. Also refer to GetMainLo...
void OnExit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread terminates (not due to an exception,...
const std::unique_ptr< ModuleDataType > ModuleData
Module data belonging to this ModuleBase instance.
Definition: Module.h:743
Refer to ParamsBase::dispatch_tag.
Definition: Module.h:189
Defines data for a thread belonging to a ModuleBase instance. Refer to RunnableInstance.
Definition: Module.h:793
const ModuleBase::ModuleDataGetterType ModuleDataGetter
Getter for module's data. Refer to ModuleBase::ModuleDataGetterType.
Definition: Module.h:825
Refer to ParamsBase::dispatch_tag.
Definition: Object.h:2018
QModuleWidget * Widget
User interface widget belonging to the module.
Definition: Module.h:1491
const Object::ParamsGetterType ParamsGetter
Invoke to obtain the parameters (derived from ParamsBase) of Owner.
Definition: Object.h:3671
void UnlockObject(LinkedObjectWrapperContainer< ObjectT > &ObjectWrapperContainer)
Unlocks an Object instance stored in the LinkedObjectWrapperContainer ObjectWrapperContainer....
Definition: Object.h:3570
void LockObject(const ParamsBase::Param< ObjectLink< ObjectT >> &LinkParam, LinkedObjectWrapperContainer< ObjectT > &ObjectWrapperContainer, std::chrono::milliseconds Timeout=ObjectLinkBase::LockObjectTimeoutDefault)
Locks an Object instance referenced by a parameter LinkParam of type ParamsBase::Param< ObjectLink< O...
Definition: Object.h:3554
const auto & GetOwner() const noexcept
Returns Owner.
Definition: Object.h:3524
Thrown when an operation timed out before it could be completed, especially used for locking shared d...
Definition: Exception.h:261
DynExp's module namespace contains the implementation of DynExp modules which extend DynExp's core fu...
DynExpErrorCodes
DynExp's error codes
Definition: Exception.h:22
Accumulates include statements to provide a precompiled header.