flashlight.qml Example File
SensorDemo/assets/flashlight.qml
import bb.cascades 1.0
import QtMobility.sensors 1.2
import Flashlight 1.0
Container {
topPadding: 20
leftPadding: 20
rightPadding: 20
attachedObjects: [
Flashlight {
id: flashlight
},
Gyroscope {
id: accel
property int count: 0
active: true
axesOrientationMode: Gyroscope.FixedOrientation
alwaysOn: true
skipDuplicates: true
onReadingChanged: {
if (reading.z > gyroSlider.value.toFixed() * 360 / (2 * 3.14)) {
if (count > 5 && ambLight.val < lightDropDown.selectedIndex) {
count = 0
flashlight.enabled = ! flashlight.enabled;
}
}
count ++;
}
},
AmbientLightSensor {
id: ambLight
property variant val: AmbientLightSensor.Undefined
active: true
alwaysOn: true
skipDuplicates: true
onReadingChanged: {
val = reading.lightLevel;
if (ambLight.val >= lightDropDown.selectedIndex && flashlight.enabled) {
flashlight.enabled = false;
}
}
},
LightSensor {
id: light
property real val: 0
active: true
alwaysOn: true
skipDuplicates: true
onReadingChanged: {
val = reading.lux;
}
}
]
TextArea {
text: qsTr("To enable the flash light give the device a clockwise flick with an angular velocity greater then 'Gyro Threshold'. The flashlight is only enabled in environments darker then 'Light Threshold'. The flashlight will automatically turn off in enviroments brighter then 'Light Threshold'.")
}
Label {
text: {
if (light.val == AmbientLightReading.Dark) {
return qsTr("Light: Dark");
} else if (light.val == AmbientLightReading.Twilight) {
return qsTr("Light: Twilight");
} else if (light.val == AmbientLightReading.Light) {
return qsTr("Light: Light");
} else if (light.val == AmbientLightReading.Bright) {
return qsTr("Light: Bright");
} else if (light.val == AmbientLightReading.Sunny) {
return qsTr("Light: Sunny");
} else {
return qsTr("Light: Undefined");
}
}
}
DropDown {
id: lightDropDown
title: qsTr("Light Threshold")
Option {
text: qsTr("Undefined")
}
Option {
text: qsTr("Dark")
}
Option {
text: qsTr("Twilight")
selected: true
}
Option {
text: qsTr("Light")
}
Option {
text: qsTr("Bright")
}
Option {
text: qsTr("Sunny")
}
}
Label {
topMargin: 50
text: qsTr("Gyro Threshold: %1").arg(gyroSlider.value.toFixed())
}
Slider {
id: gyroSlider
value: 10
fromValue: 0
toValue: 50
}
Label {
horizontalAlignment: HorizontalAlignment.Center
text: qsTr("Luminosity: %1 lux").arg(Math.round(light.val))
textStyle {
base: SystemDefaults.TextStyles.TitleText
color: Color.White
lineHeight: 2
}
}
}