在Ubuntu上的传感器

185 篇文章 7 订阅
159 篇文章 2 订阅

我们知道传感器在现代手机中非常重要,我们需要使用它做一些有创新的应用。这里我们来显示怎么在Ubuntu上使用它所提供的传感器。


1)显示所有的传感器


为了能够使用sensor的API,我们必须包含如下的库:

import QtSensors 5.0


在这里我们来做一个例子来显示所有传感器的列表。我们知道QML提供了一个便利的方法可以很方便地列车所有已有的传感器。

        Component.onCompleted: {
            var types = QmlSensors.sensorTypes();
            console.log(types.join(", "));
        }

为了很方便地显示,我们用一个列表来显示所有被支持的sensor。

            ListView {
                width: parent.width
                height: parent.height/2

                delegate: Text {
                    text: modelData
                }

                model:QmlSensors.sensorTypes()
            }

这里是个非常简单的列表,直接使用QmlSensors的方法sensorTypes()来作为一个model数据。我们以使用一个最简单的delegate来显示数据。

2)使用传感器数据


Accelerometer

我们可以使用如下的方法来得到传感器的数据:

        Accelerometer {
            id: accel
            active: true
            dataRate: 20

            onReadingChanged: {
                accelLabel.text = "Accel " + "x: " + reading.x.toFixed(1) +" y: " + reading.y.toFixed(1) + " z: " + reading.z.toFixed(1)
            }
        }

TiltSensor

我们可以使用如下的方法来得到传感器的数据:

        TiltSensor {
            id: tilt
            active: false

            onReadingChanged: {
                tiltLabel.text = "Tilt " + "x " + tilt.reading.xRotation.toFixed(1) + " y " + tilt.reading.yRotation.toFixed(1);
            }
        }

AmbientLigthSensor

我们可以使用如下的方法来得到传感器的数据:

        AmbientLightSensor {
            active: true
            onReadingChanged: {
                if (reading.lightLevel === AmbientLightReading.Dark) {
                    lightLabel.text = "It is dark"
                }  else if ( reading.lightLevel === AmbientLightReading.Twilight) {
                    lightLabel.text = "It is moderately dark";
                } else if ( reading.lightLevel === AmbientLightReading.Light) {
                    lightLabel.text = "It is light (eg. internal lights)";
                } else if ( reading.lightLevel === AmbientLightReading.Bright) {
                    lightLabel.text = "It is bright (eg. shade)";
                } else if ( reading.lightLevel === AmbientLightReading.Sunny) {
                    lightLabel.text = "It is very bright (eg. direct sunlight)";
                }else if ( reading.lightLevel === AmbientLightReading.Undefined) {
                    lightLabel.text = "It is unknown";
                }
            }
        }

OrientationSensor


我们可以使用如下的方法来得到传感器的数据:

        OrientationSensor {
            active: true
            onReadingChanged: {
                orientationLabel.text = "something happened"
                if ( reading.orientation === OrientationReading.TopUp) {
                    orientationLabel.text = "TopUp";
                } else if ( reading.orientation === OrientationReading.TopDown) {
                    orientationLabel.text = "TopDown";
                } else if ( reading.orientation === OrientationReading.LeftUp) {
                    orientationLabel.text = "LeftUp";
                } else if ( reading.orientation === OrientationReading.RightUp) {
                    orientationLabel.text= "RightUp";
                } else if ( reading.orientation === OrientationReading.FaceDown) {
                    orientationLabel.text = "FaceDown";
                }  else if ( reading.orientation === OrientationReading.FaceUp) {
                    orientationLabel.text = "FaceUp";
                }
            }
        }

RotationSensor

我们可以使用如下的方法来得到传感器的数据:

        RotationSensor {
            id: rotation
            onReadingChanged: {
                rotationLabel.text = "Rotation x: " + rotation.reading.x.toFixed(1) + " y: "
                        + rotation.reading.y.toFixed(1) + " z: " + rotation.reading.z.toFixed(1);
            }
        }


SensorGesture


        SensorGesture {
            id: sensorGesture

            Component.onCompleted: {
                console.log("The invalid gestures: ");
                for ( var gesture in invalidGestures ) {
                    console.log(" invalid[" + gesture + "]: " + invalidGestures[gesture])
                }

                console.log("The available gestures: ");
                for ( var gesture in availableGestures ) {
                    console.log(" available[" + gesture + "]: " + availableGestures[gesture])
                }
            }

            gestures: ["QtSensors.SecondCounter",
                       "QtSensors.cover",
                       "QtSensors.doubletap",
                       "QtSensors.hover",
                       "QtSensors.freefall",
                       "QtSensors.pickup",
                       "QtSensors.shake2",
                       "QtSensors.slam",
                       "QtSensors.turnover",
                       "QtSensors.twist",
                       "QtSensors.whip",
                       "QtSensors.shake"
                      ]

            enabled: true

            onDetected: {
                console.log("detected gesture: " + gesture);
            }
        }


3) 源码


整个测试程序的源码可以在如下的网址找到:


在手机上的运行结果如下:






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值