summaryrefslogtreecommitdiff
path: root/Main.qml
blob: d2ca11d47339f16b0534065bc2ad51a84e7591e2 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Material
import QtQuick.Layouts
import QmlCustomPlot 1.0

import "request.js" as XHR

ApplicationWindow {
    id: mainWindowRoot

    width: 640
    height: 480
    visible: true
    // visibility: Window.Maximized
    visibility: ApplicationWindow.Maximized

    title: qsTr("Hello World")

    // // pistache
    // // readonly property string apiRoot: "http://rpi5:8080/v1"
    // // qhttpserver
    readonly property string apiRoot: "http://rpi5:8081/v1"

    Shortcut {
        sequence: "ctrl+q"
        onActivated: mainWindowRoot.close()
    }

    Material.theme: Material.Dark
    Material.accent: Material.Indigo

    function requestExposureTime(type, uri) {
        console.log("request url: ", apiRoot + uri);

        XHR.sendRequest("GET", apiRoot + uri, function(response) {

            if (response.status != "200") {
                console.log("response status: ", response.status);
                return;
            }

            let isPlainText = response.contentType.length === 0

            if (!isPlainText) {
                console.log("invalid response: ", response);
                return;
            }

            exposureTimeSpinBox.value = parseInt(response.content)
        });
    }

    function readParams() {
        var url = apiRoot + "/sensor/params"
        console.log("readParams:", url);

        XHR.sendRequest("GET", url, function(response) {

            if (response.status != "200") {
                console.log("response status: ", response.status);
                return;
            }

            let isPlainText = response.contentType.length === 0

            if (!isPlainText) {
                console.log("invalid response: ", response);
                return;
            }

            var json = JSON.parse(response.content);

            console.log("readParams result:", json)

            if (exposureTimeSpinBox.value != json.exposureTime) {
                console.log("update exposureTimeSpinBox to", json.exposureTime)
                exposureTimeSpinBox.value = json.exposureTime;
            }

            if (laserLevelSpinBox.value != json.laserLevel) {
                console.log("update laserLevelSpinBox to", json.laserLevel)
                laserLevelSpinBox.value = json.laserLevel;
            }

            if (enableAutoExposureCheckbox.checked != (json.aeEnable == "true")) {
                console.log("update enableAutoExposureCheckbox to", json.aeEnable)
                enableAutoExposureCheckbox.checked = json.aeEnable == "true";
            }
        });
    }

    Component.onCompleted: readParams()

    function writeParams() {
        var url = apiRoot + "/sensor/params";
        console.log("writeParams:", url);

        var json = new Object();
        json["aeEnable"] = enableAutoExposureCheckbox.checked;
        json["exposureTime"] = exposureTimeSpinBox.value;
        json["laserLevel"] = laserLevelSpinBox.value;
        console.log(JSON.stringify(json));

        XHR.sendRequest("POST", url, function(response) {

            if (response.status != "200") {
                console.log("response status: ", response.status);
                return;
            }

            let isPlainText = response.contentType.length === 0;

            if (!isPlainText) {
                console.log("invalid response: ", response);
                return;
            }

            console.log("writeParams result:", response.content);

            // var json = JSON.parse(response.content);
        }, JSON.stringify(json));
    }

    RowLayout {
        id: horizontalLayout

        anchors.fill: parent

        spacing: 0

        Pane {
            id: pane

            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.minimumWidth: 150

            ColumnLayout {
                id: columnLayout

                anchors.fill: parent

                function color_by_value(value) {
                    const limit = 0.02;

                    if (value >= limit) {
                        return "red";
                    }

                    return "green";
                }

                Label {
                    id: fpsLabel

                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignTop

                    color: "green"
                }

                Label {
                    id: maxDiffLabel

                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignTop

                    color: columnLayout.color_by_value(max_diff)
                    text: "max diff: " + max_diff.toFixed(3)
                }

                Label {
                    id: maxDiffIdxLabel

                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignTop

                    text: "max diff: " + max_diff_idx
                }

                Label {
                    id: minDiffLabel

                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignTop

                    color: columnLayout.color_by_value(-min_diff)
                    text: "min diff: " + min_diff.toFixed(3)
                }

                Label {
                    id: minDiffIdxLabel

                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignTop

                    text: "min diff: " + min_diff_idx
                }

                Label {
                    id: avgDiffLabel

                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignTop

                    color: columnLayout.color_by_value(avg_diff)
                    text: "avg diff: " + avg_diff.toFixed(3)
                }

                Label {
                    id: medianhDiffLabel

                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignTop

                    color: columnLayout.color_by_value(median_diff)
                    text: "med. diff: " + median_diff.toFixed(3)
                }

                Label {
                    text: qsTr("Exposure time (us):")
                }

                SpinBox {
                    id: exposureTimeSpinBox

                    Layout.fillWidth: true

                    from: 10
                    to: 30000
                    stepSize: 100
                    editable: true

                    value: 200
                    onValueChanged: writeParams()
                }

                SpinBox {
                    id: laserLevelSpinBox

                    Layout.fillWidth: true

                    from: 500
                    to: 50000
                    stepSize: 250
                    editable: true

                    value: 1500
                    onValueChanged: writeParams()
                }

                CheckBox {
                    id: enableAutoExposureCheckbox

                    text: qsTr("Auto exposure")

                    onCheckedChanged: writeParams()
                }

                Item {
                    Layout.fillHeight: true
                }
            }
        }

        ImageViewer {
            id: image

            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.minimumWidth: 200
        }

        Item {

            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.minimumWidth: 900
            Layout.minimumHeight: 600

            Rectangle {
                color: "red"
                opacity: 0.1
                z: 101
            }

            QmlCustomPlot {
                id: qmlPlot

                anchors.fill: parent

                Label {
                    anchors {
                        top: parent.top
                        topMargin: 8 * 2
                        horizontalCenter: parent.horizontalCenter
                    }
                    text: qmlPlot.fps
                    color: Material.color(Material.accent)
                }

                plot: myPlot
                // Component.onCompleted: initCustomPlot()
            }
        }
    }
}