motionalarm.qml Example File
SensorDemo/assets/motionalarm.qml
import bb.cascades 1.0
import bb.multimedia 1.0
import QtMobility.sensors 1.2
Container {
leftPadding: 20
rightPadding: 20
bottomPadding: 20
attachedObjects: [
SystemSound {
id: sound
sound: SystemSound.InputKeypress
},
Accelerometer {
id: alarm
property double x: 0
property double y: 0
property double z: 0
property bool movement: false
active: true
axesOrientationMode: Accelerometer.FixedOrientation
accelerationMode: Accelerometer.User
alwaysOn: true
skipDuplicates: true
onReadingChanged: {
x = reading.x;
y = reading.y;
z = reading.z;
movement = Math.sqrt(reading.x * reading.x + reading.y * reading.y + reading.z * reading.z) > .2;
if (movement) {
sound.play();
}
}
}
]
layout: DockLayout {}
Label {
horizontalAlignment: HorizontalAlignment.Center
verticalAlignment: VerticalAlignment.Center
text: alarm.movement ? qsTr("ALARM: Movement Detected!") : qsTr("No Movement Detected")
textStyle {
base: SystemDefaults.TextStyles.TitleText
color: alarm.movement ? Color.Red : Color.White
}
}
Container {
horizontalAlignment: HorizontalAlignment.Fill
verticalAlignment: VerticalAlignment.Bottom
layout: StackLayout {
orientation: LayoutOrientation.LeftToRight
}
Label {
layoutProperties: StackLayoutProperties {
spaceQuota: 1
}
text: qsTr("X: %1").arg((alarm.x).toPrecision(5))
textStyle {
base: SystemDefaults.TextStyles.BodyText
color: Color.White
}
}
Label {
layoutProperties: StackLayoutProperties {
spaceQuota: 1
}
text: qsTr("Y: %1").arg((alarm.y).toPrecision(5))
textStyle {
base: SystemDefaults.TextStyles.BodyText
color: Color.White
}
}
Label {
layoutProperties: StackLayoutProperties {
spaceQuota: 1
}
text: qsTr("Z: %1").arg((alarm.z).toPrecision(5))
textStyle {
base: SystemDefaults.TextStyles.BodyText
color: Color.White
}
}
}
}