incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [15/26] qt commit: Add support for Accelerometer. Submitted on behalf of Alejandro Narancio.
Date Fri, 24 Feb 2012 20:52:11 GMT
Add support for Accelerometer. Submitted on behalf of Alejandro Narancio.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/commit/c2ba52c2
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/tree/c2ba52c2
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/diff/c2ba52c2

Branch: refs/heads/master
Commit: c2ba52c27282a160d9eb4eb253ada2a66cc2b26a
Parents: ff477a0
Author: Jeff Tranter <jtranter@ics.com>
Authored: Fri Feb 17 11:26:17 2012 -0500
Committer: Jeff Tranter <jtranter@ics.com>
Committed: Fri Feb 17 11:26:17 2012 -0500

----------------------------------------------------------------------
 cordovaqt.pro                 |    2 +
 src/plugins/accelerometer.cpp |   58 ++++++++++++++++++++++++++++++++++++
 src/plugins/accelerometer.h   |   56 ++++++++++++++++++++++++++++++++++
 www/basic.js                  |   14 ++++++++-
 www/index.html                |   11 ++++---
 xml/plugins.xml               |    1 +
 6 files changed, 136 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/blob/c2ba52c2/cordovaqt.pro
----------------------------------------------------------------------
diff --git a/cordovaqt.pro b/cordovaqt.pro
index 3d11740..6cce338 100644
--- a/cordovaqt.pro
+++ b/cordovaqt.pro
@@ -44,6 +44,7 @@ SOURCES += main.cpp \
     src/plugins/console.cpp \
     src/plugins/connection.cpp \
     src/plugins/compass.cpp \
+    src/plugins/accelerometer.cpp \
     src/cordova.cpp \
     src/cplugin.cpp \
     src/cwebpage.cpp
@@ -56,6 +57,7 @@ HEADERS += \
     src/plugins/console.h \
     src/plugins/connection.h \
     src/plugins/compass.h \
+    src/plugins/accelerometer.h \
     src/cordova.h \
     src/cplugin.h \
     src/cwebpage.h

http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/blob/c2ba52c2/src/plugins/accelerometer.cpp
----------------------------------------------------------------------
diff --git a/src/plugins/accelerometer.cpp b/src/plugins/accelerometer.cpp
new file mode 100644
index 0000000..8f7c744
--- /dev/null
+++ b/src/plugins/accelerometer.cpp
@@ -0,0 +1,58 @@
+/*
+ *  Copyright 2012  Integrated Computer Solutions - http://www.ics.com
+ *
+ *  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.
+ */
+
+#include "accelerometer.h"
+
+Accelerometer* Accelerometer::m_accelerometer = new Accelerometer();
+
+Accelerometer::Accelerometer() : CPlugin()
+{   
+    PluginRegistry::getRegistry()->registerPlugin( "com.cordova.Accelerometer", this );
+}
+
+Accelerometer::~Accelerometer(){
+   delete m_accelerometerSource;
+}
+
+void Accelerometer::init(){
+    m_x_accel = 0;
+    m_y_accel = 0;
+    m_z_accel = 0;
+
+    m_timestamp = 0;
+
+    m_accelerometerSource = new QAccelerometer();
+    m_accelerometerSource->start();
+    connect(m_accelerometerSource,SIGNAL(readingChanged()), SLOT(updateSensor()));
+}
+
+void Accelerometer::getCurrentAcceleration( int scId, int ecId, QVariantMap p_options){
+
+    QString callbackArguments = "Acceleration.cast({ x: " + QString::number(m_x_accel) +
"," +
+                                "                    y: " + QString::number(m_y_accel) +
"," +
+                                "                    z: " + QString::number(m_z_accel) +
"," +
+                                "                    timestamp: " + QString::number(m_timestamp)
+ "})";
+
+    this->callback( scId, callbackArguments );
+}
+
+void Accelerometer::updateSensor(){
+    QAccelerometerReading *accelerometer = m_accelerometerSource->reading();
+    m_x_accel = accelerometer->x();
+    m_y_accel = accelerometer->y();
+    m_z_accel = accelerometer->z();
+    m_timestamp = accelerometer->timestamp();
+}

http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/blob/c2ba52c2/src/plugins/accelerometer.h
----------------------------------------------------------------------
diff --git a/src/plugins/accelerometer.h b/src/plugins/accelerometer.h
new file mode 100644
index 0000000..8657e89
--- /dev/null
+++ b/src/plugins/accelerometer.h
@@ -0,0 +1,56 @@
+/*
+ *  Copyright 2012  Integrated Computer Solutions - http://www.ics.com
+ *
+ *  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.
+ */
+
+#ifndef ACCELEROMETER_H
+#define ACCELEROMETER_H
+
+#include "../cplugin.h"
+#include "../pluginregistry.h"
+#include <QtSensors/QAccelerometer>
+#include <QObject>
+#include <QMap>
+#include <QDebug>
+
+#ifdef QTM_NAMESPACE
+QTM_USE_NAMESPACE
+#endif
+
+class Accelerometer : public CPlugin
+{
+    Q_OBJECT
+public:
+    explicit Accelerometer();
+    void init();
+    ~Accelerometer();
+
+signals:
+
+public slots:
+    void getCurrentAcceleration( int scId, int ecId, QVariantMap p_options);
+
+protected slots:
+    void updateSensor();
+
+private:
+    static Accelerometer *m_accelerometer;
+    QAccelerometer* m_accelerometerSource;
+    double m_x_accel;
+    double m_y_accel;
+    double m_z_accel;
+    unsigned long m_timestamp;
+};
+
+#endif // ACCELEROMETER_H

http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/blob/c2ba52c2/www/basic.js
----------------------------------------------------------------------
diff --git a/www/basic.js b/www/basic.js
index 58b0671..22bd59a 100644
--- a/www/basic.js
+++ b/www/basic.js
@@ -13,7 +13,8 @@ function init()
 {
     navigator.accelerometer.watchAcceleration(function (v) {
         get("accel_val").innerHTML = v.x + '   ' + v.y + '    ' + v.z;
-    }, null, {});
+    }, null, {frequency:100});
+
 }
 
 function ping_google() 
@@ -53,6 +54,17 @@ function getCurrentHeading() {
                                              {timestamp: (new Date()).getTime()} );
 }
 
+function getCurrentAcceleration() {
+    navigator.accelerometer.getCurrentAcceleration( function( acceleration ) {
+                                                 get("accel_val").innerHTML = acceleration.x
+ ' ' + acceleration.y + ' ' + acceleration.z;
+                                             },
+                                             function( error ) {
+                                             });
+
+}
+
+
+
 function test_requestFileSystem() {
     window.requestFileSystem( LocalFileSystem.PERSISTENT, 0, function( p_fileSystem ) {
                                  get( "debug_output" ).innerHTML = p_fileSystem.name + "
/ " + p_fileSystem.root.fullPath;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/blob/c2ba52c2/www/index.html
----------------------------------------------------------------------
diff --git a/www/index.html b/www/index.html
index 3910318..0545f93 100644
--- a/www/index.html
+++ b/www/index.html
@@ -13,19 +13,17 @@
         <script language="javascript" type="text/javascript" src="js/geolocation.js"></script>
         <script language="javascript" type="text/javascript" src="js/notification.js"></script>
         <script language="javascript" type="text/javascript" src="js/compass.js"></script>
+        <script language="javascript" type="text/javascript" src="js/accelerometer.js"></script>
 
         <script language="javascript" type="text/javascript" src="basic.js"></script>
     </head>
     <body>
-        <input type="button" value="Vibrate!" onclick="test_vibra();"/>
-        <br />
-        <br />
-        <div id="accel_val"> AAA </div>
+        <input type="button" value="Init Watchers!" onclick="init();"/>
         <br />
+        <input type="button" value="Vibrate!" onclick="test_vibra();"/>
         <br />
         <input type="button" value="Ping Google!" onclick="ping_google();">
         <br />
-        <br />
         <input type="button" value="RequestFileSystem" onclick="test_requestFileSystem();">
         <br />
         <div id="debug_output"> Unknown </div>
@@ -34,6 +32,9 @@
         <br />
         <div id="position_val"> Location </div>
         <br />
+        <input type="button" value="Get Acceleration" onclick="getCurrentAcceleration();">
+        <br />
+        <div id="accel_val"> Acceleration </div>
         <input type="button" value="getCurrentHeading" onclick="getCurrentHeading();">
         <br />
         <div id="heading_val"> Heading </div>

http://git-wip-us.apache.org/repos/asf/incubator-cordova-qt/blob/c2ba52c2/xml/plugins.xml
----------------------------------------------------------------------
diff --git a/xml/plugins.xml b/xml/plugins.xml
index 67464b7..ac1d660 100644
--- a/xml/plugins.xml
+++ b/xml/plugins.xml
@@ -2,6 +2,7 @@
 <plugins>
     <plugin name="File" value="com.cordova.File"/>
     <plugin name="Notification" value="com.cordova.Notification"/>
+    <plugin name="Accelerometer" value="com.cordova.Accelerometer"/>
     <plugin name="Geolocation" value="com.cordova.Geolocation"/>
     <plugin name="Device" value="com.cordova.Device"/>
     <plugin name="Console" value="com.cordova.Console"/>


Mime
View raw message