-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensor.cpp
149 lines (127 loc) · 3.82 KB
/
sensor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <QDebug>
#include "sensor.h"
#include "ui_sensor.h"
#include "ui_poti.h"
using namespace stubserver;
Sensor::Sensor(QWidget *parent, const char *title, bool rotary)
: SensorInterface(parent)
, uiS(rotary ? nullptr : new Ui::Sensor())
, uiP(rotary ? new Ui::Potentiometer() : nullptr)
{
QGroupBox *grp;
if (rotary) {
uiP->setupUi(this);
setCheckBox(uiP->check);
grp = uiP->groupBox;
sensor = uiP->sensor;
labelMin = uiP->labelMin;
labelMax = uiP->labelMax;
label = uiP->label;
}
else {
uiS->setupUi(this);
setCheckBox(uiS->check);
connectTooltipTo(uiS->groupBox);
grp = uiS->groupBox;
sensor = uiS->sensor;
labelMin = uiS->labelMin;
labelMax = uiS->labelMax;
label = uiS->label;
statusLED= uiS->statusLED;
}
grp->setTitle(title);
connect(this, &Sensor::valueChanged, this, &Sensor::updateUi);
connect(this, &Sensor::ledChanged, this, &Sensor::updateLed);
connect(sensor, &QAbstractSlider::sliderPressed, this, &SensorInterface::activateManualControl);
connect(sensor, &QAbstractSlider::sliderMoved, this, &Sensor::sliderMoved);
connect(sensor, &QAbstractSlider::actionTriggered, this, &Sensor::actionTriggered);
connect(checkBox, &QCheckBox::stateChanged, this, &SensorInterface::checkBoxClicked);
}
Sensor::~Sensor()
{
setCheckBox(nullptr);
delete uiP;
delete uiS;
}
/**
* Sets the additional label for the value type.
* @param label - label string
*/
void Sensor::setValueLabel(const std::string &label) const
{
if (label.length() > 0 && uiS)
uiS->valueTypeLabel->setText(label.c_str());
}
/**
* Slider changes via keyboard.
*/
void Sensor::actionTriggered(int action)
{
int newValue = currentValue;
switch (action) {
case QAbstractSlider::SliderPageStepAdd:
newValue += 10;
break;
case QAbstractSlider::SliderPageStepSub:
newValue -= 10;
break;
case QAbstractSlider::SliderSingleStepAdd:
newValue += 1;
break;
case QAbstractSlider::SliderSingleStepSub:
newValue -= 1;
break;
case QAbstractSlider::SliderToMinimum:
newValue = min;
break;
case QAbstractSlider::SliderToMaximum:
newValue = max;
break;
case QAbstractSlider::SliderNoAction:
case QAbstractSlider::SliderMove:
return;
}
if (newValue + zeroPoint > max)
newValue = max;
if (newValue + zeroPoint < min)
newValue = min;
// printf("actionTriggered %d %d, current %d\n", newValue, zeroPoint, currentValue);
currentValue = newValue;
sensor->setValue(newValue + zeroPoint);
label->setText(QString::number(newValue));
}
// new sensor value manually (other zero point handling)
void Sensor::sliderMoved(int v)
{
// printf("sliderMoved %d %d, current %d\n", v, zeroPoint, currentValue);
updateUi(v - zeroPoint);
}
/**
* @brief Sensor::updateUi signal receiver method when a value changes
* @param newValue
*/
void Sensor::updateUi(int newValue)
{
// printf("updateUi %d %d, current %d\n", newValue, zeroPoint, currentValue);
currentValue = newValue;
if (!minMaxSet)
{
labelMin->setText(QString::number(min));
labelMax->setText(QString::number(max));
sensor->setRange(min, max);
sensor->setPageStep((max - min) / 80);
minMaxSet = true;
}
label->setText(QString::number(newValue));
// this method was called due to manual change?
if (!manualControl)
sensor->setValue(newValue + zeroPoint);
}
/**
* @brief Sensor::updateLed signal receiver method when the led state changes
* @param ledValue
*/
void Sensor::updateLed(bool ledValue)
{
setLedOn(ledValue);
}