DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
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 "ui_InputPortReader.h"
6#include "InputPortReader.h"
7
8namespace DynExpModule
9{
11 : QModuleWidget(Owner, parent),
12 ui(std::make_unique<Ui::InputPortReader>())
13 {
14 ui->setupUi(this);
15
16 ui->AnalogInWidget->setVisible(false);
17 ui->DigitalInWidget->setVisible(false);
18 }
19
24
26 {
27 UIInitialized = false;
28 IsDigitalPort = false;
29 Value = 0;
30 }
31
33 {
34 try
35 {
36 auto ModuleData = DynExp::dynamic_ModuleData_cast<InputPortReader>(Instance.ModuleDataGetter());
37
38 {
39 auto InstrData = DynExp::dynamic_InstrumentData_cast<DynExpInstr::InputPort>(ModuleData->InputPort->GetInstrumentData());
40
41 if (InstrData->GetSampleStream()->CanRead())
42 ModuleData->Value = InstrData->GetSampleStream()->ReadBasicSample().Value;
43 } // InstrData unlocked here.
44 ModuleData->InputPort->ReadData();
45
47 } // ModuleData unlocked here.
48 catch (const Util::TimeoutException& e)
49 {
50 if (NumFailedUpdateAttempts++ >= 3)
51 Instance.GetOwner().SetWarning(e);
52 }
53
55 }
56
61
62 std::unique_ptr<DynExp::QModuleWidget> InputPortReader::MakeUIWidget()
63 {
64 auto Widget = std::make_unique<InputPortReaderWidget>(*this);
65
66 return Widget;
67 }
68
69 void InputPortReader::UpdateUIChild(const ModuleBase::ModuleDataGetterType& ModuleDataGetter)
70 {
71 auto Widget = GetWidget<InputPortReaderWidget>();
72 auto ModuleData = DynExp::dynamic_ModuleData_cast<InputPortReader>(ModuleDataGetter());
73
74 if (!ModuleData->UIInitialized)
75 {
76 Widget->ui->AnalogInWidget->setVisible(!ModuleData->IsDigitalPort);
77 Widget->ui->DigitalInWidget->setVisible(ModuleData->IsDigitalPort);
78 Widget->adjustSize();
79
80 ModuleData->UIInitialized = true;
81 }
82
83 if (!ModuleData->IsDigitalPort)
84 {
85 Widget->ui->ValueLabel->setText(QString::number(ModuleData->Value) + " " + DynExpInstr::DataStreamInstrumentData::UnitTypeToStr(ModuleData->InputPort->GetValueUnit()));
86 Widget->ui->ValueProgressBar->setMinimum(ModuleData->InputPort->GetHardwareMinValue());
87 Widget->ui->ValueProgressBar->setMaximum(ModuleData->InputPort->GetHardwareMaxValue());
88 Widget->ui->ValueProgressBar->setValue(ModuleData->Value);
89 }
90 else
91 {
92 Widget->ui->StateLabel->setText(ModuleData->Value ? "High" : "Low");
93 Widget->ui->StateFrame->setStyleSheet(ModuleData->Value ? "background-color: lime;" : "background-color: red;");
94 }
95 }
96
98 {
99 auto ModuleParams = DynExp::dynamic_Params_cast<InputPortReader>(Instance->ParamsGetter());
100 auto ModuleData = DynExp::dynamic_ModuleData_cast<InputPortReader>(Instance->ModuleDataGetter());
101
102 Instance->LockObject(ModuleParams->InputPort, ModuleData->InputPort);
103 ModuleData->InputPort->ReadData();
104
105 ModuleData->IsDigitalPort = ModuleData->InputPort->GetValueUnit() == DynExpInstr::DataStreamInstrumentData::UnitType::LogicLevel;
106 }
107
109 {
110 auto ModuleData = DynExp::dynamic_ModuleData_cast<InputPortReader>(Instance->ModuleDataGetter());
111
112 Instance->UnlockObject(ModuleData->InputPort);
113 }
114}
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)
std::unique_ptr< Ui::InputPortReader > ui
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:788
Refer to ParamsBase::dispatch_tag.
Definition Module.h:191
Defines data for a thread belonging to a ModuleBase instance. Refer to RunnableInstance.
Definition Module.h:840
const ModuleBase::ModuleDataGetterType ModuleDataGetter
Getter for module's data. Refer to ModuleBase::ModuleDataGetterType.
Definition Module.h:872
Refer to ParamsBase::dispatch_tag.
Definition Object.h:2018
QModuleWidget * Widget
User interface widget belonging to the module.
Definition Module.h:1807
const Object::ParamsGetterType ParamsGetter
Invoke to obtain the parameters (derived from ParamsBase) of Owner.
Definition Object.h:3710
void UnlockObject(LinkedObjectWrapperContainer< ObjectT > &ObjectWrapperContainer)
Unlocks an Object instance stored in the LinkedObjectWrapperContainer ObjectWrapperContainer....
Definition Object.h:3609
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:3593
const auto & GetOwner() const noexcept
Returns Owner.
Definition Object.h:3556
Thrown when an operation timed out before it could be completed, especially used for locking shared d...
Definition Exception.h:262
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.