DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
OutputPortWriter.cpp
Go to the documentation of this file.
1// This file is part of DynExp.
2
3#include "stdafx.h"
4#include "moc_OutputPortWriter.cpp"
5#include "ui_OutputPortWriter.h"
6#include "OutputPortWriter.h"
7
8namespace DynExpModule
9{
11 : QModuleWidget(Owner, parent),
12 ui(std::make_unique<Ui::OutputPortWriter>())
13 {
14 ui->setupUi(this);
15
16 ui->AnalogOutWidget->setVisible(false);
17 ui->DigitalOutWidget->setVisible(false);
18 }
19
24
26 {
27 UIInitialized = false;
28 IsDigitalPort = false;
29 ValueChanged = false;
32 Value = 0;
33 }
34
36 {
37 auto ModuleData = DynExp::dynamic_ModuleData_cast<OutputPortWriter>(Instance.ModuleDataGetter());
38
39 if (ModuleData->ValueChanged)
40 {
41 {
42 auto InstrData = DynExp::dynamic_InstrumentData_cast<DynExpInstr::OutputPort>(ModuleData->OutputPort->GetInstrumentData());
43
44 InstrData->GetSampleStream()->SeekEnd(std::ios_base::out);
45 InstrData->GetSampleStream()->WriteBasicSample({ ModuleData->Value });
46 } // InstrData unlocked here.
47 ModuleData->OutputPort->WriteData();
48
49 ModuleData->ValueChanged = false;
50 }
51
53 }
54
58
59 std::unique_ptr<DynExp::QModuleWidget> OutputPortWriter::MakeUIWidget()
60 {
61 auto Widget = std::make_unique<OutputPortWriterWidget>(*this);
62
63 Connect(Widget->ui->ValueDial, &QAbstractSlider::valueChanged, this, &OutputPortWriter::OnValueChanged);
64 Connect(Widget->ui->StateButton, &QPushButton::clicked, this, &OutputPortWriter::OnStateButtonClicked);
65
66 return Widget;
67 }
68
69 void OutputPortWriter::UpdateUIChild(const ModuleBase::ModuleDataGetterType& ModuleDataGetter)
70 {
71 auto Widget = GetWidget<OutputPortWriterWidget>();
72 auto ModuleData = DynExp::dynamic_ModuleData_cast<OutputPortWriter>(ModuleDataGetter());
73
74 QString ValueUnitStr(" ");
75 ValueUnitStr += DynExpInstr::DataStreamInstrumentData::UnitTypeToStr(ModuleData->OutputPort->GetValueUnit());
76
77 if (!ModuleData->UIInitialized)
78 {
79 Widget->ui->AnalogOutWidget->setVisible(!ModuleData->IsDigitalPort);
80 Widget->ui->DigitalOutWidget->setVisible(ModuleData->IsDigitalPort);
81 Widget->adjustSize();
82
83 Widget->ui->ValueDial->setMinimum(ModuleData->MinAllowedValue * DialControlValueDivider);
84 Widget->ui->ValueDial->setMaximum(ModuleData->MaxAllowedValue * DialControlValueDivider);
85 Widget->ui->ValueDial->setSingleStep(ModuleData->MaxAllowedValue - ModuleData->MinAllowedValue);
86 Widget->ui->ValueDial->setPageStep((ModuleData->MaxAllowedValue - ModuleData->MinAllowedValue) * DialControlValueDivider / 10);
87 Widget->ui->MinValueLabel->setText(QString::number(ModuleData->MinAllowedValue) + ValueUnitStr);
88 Widget->ui->MaxValueLabel->setText(QString::number(ModuleData->MaxAllowedValue) + ValueUnitStr);
89
90 ModuleData->UIInitialized = true;
91 }
92
93 if (!ModuleData->IsDigitalPort)
94 {
95 if (!Widget->ui->ValueDial->hasFocus())
96 Widget->ui->ValueDial->setValue(ModuleData->Value * DialControlValueDivider);
97
98 Widget->ui->ValueLabel->setText(QString::number(ModuleData->Value) + ValueUnitStr);
99
100 }
101 else
102 {
103 Widget->ui->StateButton->setText(ModuleData->Value ? "High" : "Low");
104 Widget->ui->StateButton->setStyleSheet(ModuleData->Value ? "background-color: lime;" : "background-color: red;");
105 }
106 }
107
109 {
110 auto ModuleParams = DynExp::dynamic_Params_cast<OutputPortWriter>(Instance->ParamsGetter());
111 auto ModuleData = DynExp::dynamic_ModuleData_cast<OutputPortWriter>(Instance->ModuleDataGetter());
112
113 Instance->LockObject(ModuleParams->OutputPort, ModuleData->OutputPort);
114
115 ModuleData->IsDigitalPort = ModuleData->OutputPort->GetValueUnit() == DynExpInstr::DataStreamInstrumentData::UnitType::LogicLevel;
116 ModuleData->MinAllowedValue = ModuleData->OutputPort->GetUserMinValue();
117 ModuleData->MaxAllowedValue = ModuleData->OutputPort->GetUserMaxValue();
118 }
119
121 {
122 auto ModuleData = DynExp::dynamic_ModuleData_cast<OutputPortWriter>(Instance->ModuleDataGetter());
123
124 Instance->UnlockObject(ModuleData->OutputPort);
125 }
126
128 {
129 auto ModuleData = DynExp::dynamic_ModuleData_cast<OutputPortWriter>(Instance->ModuleDataGetter());
130 ModuleData->Value = static_cast<double>(Value) / DialControlValueDivider;
131 ModuleData->ValueChanged = true;
132 }
133
135 {
136 auto ModuleData = DynExp::dynamic_ModuleData_cast<OutputPortWriter>(Instance->ModuleDataGetter());
137 ModuleData->Value = Checked;
138 ModuleData->ValueChanged = true;
139 }
140}
Implementation of a module to write single values to output 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
OutputPortWriterWidget(OutputPortWriter &Owner, QModuleWidget *parent=nullptr)
std::unique_ptr< Ui::OutputPortWriter > ui
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 OnStateButtonClicked(DynExp::ModuleInstance *Instance, bool Checked) const
void OnExit(DynExp::ModuleInstance *Instance) const override final
This event is triggered right before the module thread terminates (not due to an exception,...
static constexpr auto DialControlValueDivider
void OnValueChanged(DynExp::ModuleInstance *Instance, int Value) const
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 UpdateUIChild(const ModuleBase::ModuleDataGetterType &ModuleDataGetter) override final
void ResetImpl(dispatch_tag< QModuleBase >) override final
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...
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
void Connect(SenderType *Sender, SignalType Signal, ReceiverType *Receiver, EventType Event)
Uses Qt's connect mechanism to connect a QObject's signal to a DynExp module's event....
Definition Module.h:1816
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
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.