QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
APMLightsComponent.qml
Go to the documentation of this file.
1import QtQuick
2import QtQuick.Controls
3
4import QGroundControl
5import QGroundControl.FactControls
6import QGroundControl.Controls
7
8SetupPage {
9 id: lightsPage
10 pageComponent: lightsPageComponent
11
12 Component {
13 id: lightsPageComponent
14
15 Column {
16 spacing: _margins
17 width: availableWidth
18
19 FactPanelController { id: controller; }
20
21 property var _activeVehicle: QGroundControl.multiVehicleManager.activeVehicle
22 property bool _oldFW: _activeVehicle.versionCompare(3, 5, 2) < 0
23 property Fact _rc5Function: controller.getParameterFact(-1, "SERVO5_FUNCTION")
24 property Fact _rc6Function: controller.getParameterFact(-1, "SERVO6_FUNCTION")
25 property Fact _rc7Function: controller.getParameterFact(-1, "SERVO7_FUNCTION")
26 property Fact _rc8Function: controller.getParameterFact(-1, "SERVO8_FUNCTION")
27 property Fact _rc9Function: controller.getParameterFact(-1, "SERVO9_FUNCTION")
28 property Fact _rc10Function: controller.getParameterFact(-1, "SERVO10_FUNCTION")
29 property Fact _rc11Function: controller.getParameterFact(-1, "SERVO11_FUNCTION")
30 property Fact _rc12Function: controller.getParameterFact(-1, "SERVO12_FUNCTION")
31 property Fact _rc13Function: controller.getParameterFact(-1, "SERVO13_FUNCTION")
32 property Fact _rc14Function: controller.getParameterFact(-1, "SERVO14_FUNCTION")
33 property Fact _rc15Function: controller.getParameterFact(-1, "SERVO15_FUNCTION")
34 property Fact _rc16Function: controller.getParameterFact(-1, "SERVO16_FUNCTION")
35 property Fact _stepSize: _oldFW ? controller.getParameterFact(-1, "JS_LIGHTS_STEP") : null // v3.5.1 and prior
36 property Fact _numSteps: _oldFW ? null : controller.getParameterFact(-1, "JS_LIGHTS_STEPS") // v3.5.2 and up
37
38 readonly property real _margins: ScreenTools.defaultFontPixelHeight
39 readonly property int _rcFunctionDisabled: 0
40 readonly property int _rcFunctionRCIN9: 59
41 readonly property int _rcFunctionRCIN10: 60
42 readonly property int _firstLightsOutChannel: 5
43 readonly property int _lastLightsOutChannel: 16
44
45 Component.onCompleted: {
46 calcLightOutValues()
47 calcCurrentStep()
48 }
49
50 /// Light output channels are stored in SERVO#_FUNCTION parameters. We need to loop through those
51 /// to find them and setup the ui accordindly.
52 function calcLightOutValues() {
53 lightsLoader.lights1OutIndex = 0
54 lightsLoader.lights2OutIndex = 0
55 for (var channel=_firstLightsOutChannel; channel<=_lastLightsOutChannel; channel++) {
56 var functionFact = controller.getParameterFact(-1, "SERVO" + channel + "_FUNCTION")
57 if (functionFact.value == _rcFunctionRCIN9) {
58 lightsLoader.lights1OutIndex = channel - 4
59 } else if (functionFact.value == _rcFunctionRCIN10) {
60 lightsLoader.lights2OutIndex = channel - 4
61 }
62 }
63 }
64
65 function setRCFunction(channel, rcFunction) {
66 // First clear any previous settings for this function
67 for (var index=_firstLightsOutChannel; index<=_lastLightsOutChannel; index++) {
68 var functionFact = controller.getParameterFact(-1, "SERVO" + index + "_FUNCTION")
69 if (functionFact.value != _rcFunctionDisabled && functionFact.value == rcFunction) {
70 functionFact.value = _rcFunctionDisabled
71 }
72 }
73
74 // Now set the function into the new channel
75 if (channel != 0) {
76 var functionFact = controller.getParameterFact(-1, "SERVO" + channel + "_FUNCTION")
77 functionFact.value = rcFunction
78 }
79 }
80
81 function calcCurrentStep() {
82 if (_oldFW) {
83 var i = 1
84 for(i; i <= 10; i++) {
85 var stepSize = (1900-1100)/i
86 if(_stepSize.value >= stepSize) {
87 _stepSize.value = stepSize;
88 break;
89 }
90 }
91 if (_stepSize.value < 80) {
92 _stepSize.value = 80;
93 }
94 lightsLoader.lightsSteps = i
95 } else {
96 lightsLoader.lightsSteps = _numSteps.value
97 }
98 }
99
100 function calcStepSize(steps) {
101 if (_oldFW) {
102 _stepSize.value = (1900-1100)/steps
103 } else {
104 _numSteps.value = steps
105 }
106 }
107
108 // Whenever any SERVO#_FUNCTION parameters chagnes we need to go looking for light output channels again
109 Connections { target: _rc5Function; onValueChanged: calcLightOutValues() }
110 Connections { target: _rc6Function; onValueChanged: calcLightOutValues() }
111 Connections { target: _rc7Function; onValueChanged: calcLightOutValues() }
112 Connections { target: _rc8Function; onValueChanged: calcLightOutValues() }
113 Connections { target: _rc9Function; onValueChanged: calcLightOutValues() }
114 Connections { target: _rc10Function; onValueChanged: calcLightOutValues() }
115 Connections { target: _rc11Function; onValueChanged: calcLightOutValues() }
116 Connections { target: _rc12Function; onValueChanged: calcLightOutValues() }
117 Connections { target: _rc13Function; onValueChanged: calcLightOutValues() }
118 Connections { target: _rc14Function; onValueChanged: calcLightOutValues() }
119
120 ListModel {
121 id: lightsOutModel
122 // It appears that QGCComboBox can't handle models that don't have a initial item
123 // after onModelChanged
124 ListElement { text: qsTr("Disabled"); value: 0 }
125
126 function update(number) {
127 // Not enough channels
128 if(number < 6) {
129 return
130 }
131 for(var i = 5; i <= number; i++) {
132 var text = qsTr("Channel ") + i
133 append({"text": text, "value": i})
134 }
135 }
136
137 Component.onCompleted: {
138 // Number of main outputs
139 var baseValue = 8
140 // Extra outputs
141 // http://ardupilot.org/copter/docs/parameters.html#brd-pwm-count-auxiliary-pin-config
142 var brd_pwm_count_value = controller.getParameterFact(-1, "BRD_PWM_COUNT").value
143 update(8 + (brd_pwm_count_value == 7 ? 3 : brd_pwm_count_value))
144 }
145 }
146
147 Component {
148 id: lightSettings
149
150 Item {
151 width: rectangle.x + rectangle.width
152 height: rectangle.y + rectangle.height
153
154 QGCLabel {
155 id: settingsLabel
156 text: qsTr("Light Output Channels")
157 font.bold: true
158 }
159
160 Rectangle {
161 id: rectangle
162 anchors.topMargin: _margins / 2
163 anchors.top: settingsLabel.bottom
164 width: lights1Combo.x + lights1Combo.width + lightsStepCombo.width + _margins
165 height: lights2Combo.y + lights2Combo.height + lightsStepCombo.height + 2*_margins
166 color: qgcPal.windowShade
167
168 QGCLabel {
169 id: lights1Label
170 anchors.margins: _margins
171 anchors.right: lights1Combo.left
172 anchors.baseline: lights1Combo.baseline
173 text: qsTr("Lights 1:")
174 }
175
176 QGCComboBox {
177 id: lights1Combo
178 anchors.margins: _margins
179 anchors.top: parent.top
180 anchors.left: lightsStepLabel.right
181 width: ScreenTools.defaultFontPixelWidth * 15
182 model: lightsOutModel
183 textRole: "text"
184 currentIndex: lights1OutIndex
185
186 onActivated: (index) => { setRCFunction(lightsOutModel.get(index).value, lights1Function) }
187 }
188
189 QGCLabel {
190 id: lights2Label
191 anchors.margins: _margins
192 anchors.right: lights2Combo.left
193 anchors.baseline: lights2Combo.baseline
194 text: qsTr("Lights 2:")
195 }
196
197 QGCComboBox {
198 id: lights2Combo
199 anchors.margins: _margins
200 anchors.top: lights1Combo.bottom
201 anchors.left: lightsStepLabel.right
202 width: lights1Combo.width
203 model: lightsOutModel
204 textRole: "text"
205 currentIndex: lights2OutIndex
206
207 onActivated: (index) => { setRCFunction(lightsOutModel.get(index).value, lights2Function) }
208 }
209
210 QGCLabel {
211 id: lightsStepLabel
212 anchors.margins: _margins
213 anchors.left: parent.left
214 anchors.baseline: lightsStepCombo.baseline
215 text: qsTr("Brightness Steps:")
216 }
217
218 QGCComboBox {
219 id: lightsStepCombo
220 anchors.margins: _margins
221 anchors.top: lights2Combo.bottom
222 anchors.left: lightsStepLabel.right
223 width: lights2Combo.width
224 model: [1,2,3,4,5,6,7,8,9,10]
225 currentIndex: lightsSteps-1
226
227 onActivated: (index) => { calcStepSize(index+1) }
228 }
229 } // Rectangle
230 } // Item
231 } // Component - lightSettings
232
233 Loader {
234 id: lightsLoader
235 sourceComponent: lightSettings
236
237 property int lights1OutIndex: 0
238 property int lights2OutIndex: 0
239 property int lights1Function: _rcFunctionRCIN9
240 property int lights2Function: _rcFunctionRCIN10
241 property int lightsSteps: 1
242 }
243 } // Column
244 } // Component
245} // SetupPage