forked from SiriusDely/Cascades-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetalfinder.qml
127 lines (106 loc) · 3.52 KB
/
metalfinder.qml
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
/* Copyright (c) 2012, 2013 BlackBerry Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import bb.cascades 1.0
import QtMobility.sensors 1.2
import bb.vibrationController 1.0
Container {
leftPadding: 20
rightPadding: 20
bottomPadding: 20
//! [0]
attachedObjects: [
Magnetometer {
id: metalfinder
// Create various variables to hold values from magnetometer reading
property double baseline: 0
property double magnitude: 0
property double intensity: 0
property double x: 0
property double y: 0
property double z: 0
// Turn on the sensor
active: true
// Keep the sensor active when the app isn't visible or the screen is off (requires app permission in bar-descriptor)
alwaysOn: true
onReadingChanged: { // Called when a magnetometer reading is available
magnitude = Math.sqrt(reading.x * reading.x + reading.y * reading.y + reading.z * reading.z);
if (0 == baseline) {
baseline = magnitude;
}
intensity = ((magnitude - baseline) / magnitude) * 100;
if (intensity > 0) {
vib.start(intensity, 100);
}
x = reading.x;
y = reading.y;
z = reading.z;
}
},
VibrationController {
id: vib
}
]
//! [0]
layout: DockLayout {}
//! [1]
Button {
horizontalAlignment: HorizontalAlignment.Center
verticalAlignment: VerticalAlignment.Center
text: qsTr("Calibrate Metal Finder")
onClicked: {
metalfinder.baseline = metalfinder.magnitude
}
}
//! [1]
Container {
horizontalAlignment: HorizontalAlignment.Fill
verticalAlignment: VerticalAlignment.Bottom
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
//! [2]
Label {
layoutProperties: StackLayoutProperties {
spaceQuota: 1
}
text: qsTr("X: %1").arg((metalfinder.x * 1000000).toPrecision(5))
textStyle {
base: SystemDefaults.TextStyles.BodyText
color: Color.White
}
}
//! [2]
Label {
layoutProperties: StackLayoutProperties {
spaceQuota: 1
}
text: qsTr("Y: %1").arg((metalfinder.y * 1000000).toPrecision(5))
textStyle {
base: SystemDefaults.TextStyles.BodyText
color: Color.White
}
}
Label {
layoutProperties: StackLayoutProperties {
spaceQuota: 1
}
text: qsTr("Z: %1").arg((metalfinder.z * 1000000).toPrecision(5))
textStyle {
base: SystemDefaults.TextStyles.BodyText
color: Color.White
}
}
}
}