adding complete compass support
diff --git a/Res/index.html b/Res/index.html
index 683c390..2fb76bf 100755
--- a/Res/index.html
+++ b/Res/index.html
@@ -24,11 +24,20 @@
<div>
<h3>Accelerometer</h3>
<input onclick="getCurrentAcceleration()" type="submit" value="Accelerometer.getCurrentAcceleration">
- <input onclick="toggleStartSensor(this)" type="submit" value="Accelerometer.watchAcceleration">
+ <input onclick="toggleStartAcceleration(this)" type="submit" value="Accelerometer.watchAcceleration">
<div id="accelerometer" style="display:none;">
x: 0, y: 0, z: 0, timestamp: 0
</div>
</div>
+ <!-- Compass -->
+ <h3>Compass</h3>
+ <div>
+ <input onclick="getCurrentHeading()" type="submit" value="Compass.getCurrentHeading">
+ <input onclick="toggleStartCompass(this)" type="submit" value="Compass.watchHeading">
+ <div id="compass" style="display:none;">
+ x: 0, y: 0, z: 0, timestamp: 0
+ </div>
+ </div>
<!-- Network -->
<div>
<h3>Network</h3>
diff --git a/Res/main.js b/Res/main.js
index b0460de..c94090a 100755
--- a/Res/main.js
+++ b/Res/main.js
@@ -90,7 +90,7 @@
}
-function toggleStartSensor(em) {
+function toggleStartAcceleration(em) {
try {
var accelerometer = document.getElementById('accelerometer');
if(em.value == "Accelerometer.watchAcceleration") {
@@ -190,3 +190,71 @@
contact.name = name;
contact.save(onSuccess, onError);
}
+
+// Compass
+var watchCompassId = null;
+
+function startWatchCompass() {
+ var options = { frequency: 3000 };
+ var onSuccess = function(compass) {
+ var element = document.getElementById('compass');
+ element.innerHTML = 'Compass X: ' + compass.x + '<br />' +
+ 'Compass Y: ' + compass.y + '<br />' +
+ 'Compass Z: ' + compass.z + '<br />' +
+ 'Timestamp: ' + compass.timestamp + '<br />';
+ };
+
+ var onFail = function() {
+ debugPrint('Compass Error!');
+ };
+ watchCompassId = navigator.compass.watchHeading(onSuccess, onFail, options);
+}
+
+function stopWatchCompass() {
+ try {
+ navigator.compass.clearWatch(watchCompassId);
+ watchCompassId= null;
+ } catch(e) {
+ debugPrint("stopWatchCompass: "+e.message);
+ }
+}
+
+function getCurrentHeading() {
+ try {
+ var compass = document.getElementById('compass');
+ var onSuccess = function(compass) {
+ var element = document.getElementById('compass');
+ element.innerHTML = 'Compass X: ' + compass.x + '<br />' +
+ 'Compass Y: ' + compass.y + '<br />' +
+ 'Compass Z: ' + compass.z + '<br />' +
+ 'Timestamp: ' + compass.timestamp + '<br />';
+ }
+
+ var onFail = function() {
+ debugPrint('Compass Error!');
+ }
+ compass.style.display = 'block';
+ navigator.compass.getCurrentHeading(onSuccess, onFail, { frequency: 5000 });
+ } catch(e) {
+ alert(e.message);
+ }
+}
+
+
+function toggleStartCompass(em) {
+ try {
+ var compass = document.getElementById('compass');
+ if(em.value == "Compass.watchHeading") {
+ em.value = "Compass.clearWatch";
+ compass.style.display = 'block';
+ startWatchCompass();
+ } else {
+ em.value = "Compass.watchHeading";
+ compass.style.display = 'none';
+ stopWatchCompass();
+ }
+ }
+ catch(e) {
+ alert(e.message);
+ }
+}
diff --git a/Res/phonegap/compass.js b/Res/phonegap/compass.js
index bbeb391..d8c9653 100644
--- a/Res/phonegap/compass.js
+++ b/Res/phonegap/compass.js
@@ -6,8 +6,7 @@
/**
* The last known Compass position.
*/
- this.lastHeading = null;
- this.lastError = null;
+ this.uuid = null;
};
/**
@@ -20,12 +19,7 @@
* such as timeout.
*/
Compass.prototype.getCurrentHeading = function(successCallback, errorCallback, options) {
- if (this.lastHeading == null) {
- PhoneGap.exec(successCallback, errorCallback, "com.phonegap.Compass", "getCurrentHeading", options);
- }
- else {
- successCallback(this.lastHeading);
- }
+ PhoneGap.exec(successCallback, errorCallback, "com.phonegap.Compass", "getCurrentHeading", options);
};
/**
@@ -38,7 +32,9 @@
* such as timeout and the frequency of the watch.
*/
Compass.prototype.watchHeading= function(successCallback, errorCallback, options) {
- PhoneGap.exec(successCallback, errorCallback, "com.phonegap.Compass", "startHeading", args);
+ this.uuid = PhoneGap.createUUID();
+ PhoneGap.exec(successCallback, errorCallback, "com.phonegap.Compass", "watchHeading", [this.uuid, options.frequency || 3000]);
+ return this.uuid;
};
@@ -47,7 +43,12 @@
* @param {String} watchId The ID of the watch returned from #watchHeading.
*/
Compass.prototype.clearWatch = function(watchId) {
- PhoneGap.exec(null, null, "com.phonegap.Compass", "stopHeading");
+ if(this.uuid == watchId) {
+ PhoneGap.exec(null, null, "com.phonegap.Compass", "clearWatch", [this.uuid]);
+ this.uuid = null;
+ } else {
+ debugPrint('no clear watch');
+ }
};
PhoneGap.addConstructor(function() {
diff --git a/Res/phonegap/phonegap.base.js b/Res/phonegap/phonegap.base.js
index fd2b19a..4411438 100644
--- a/Res/phonegap/phonegap.base.js
+++ b/Res/phonegap/phonegap.base.js
@@ -489,7 +489,6 @@
PhoneGap.run_command = function() {
if (!PhoneGap.available() || !PhoneGap.queue.ready)
return;
-
PhoneGap.queue.ready = false;
var args = PhoneGap.queue.commands.shift();
diff --git a/Res/phonegap/phonegap.bat b/Res/phonegap/phonegap.bat
index 70fdbf4..348234c 100755
--- a/Res/phonegap/phonegap.bat
+++ b/Res/phonegap/phonegap.bat
@@ -1 +1 @@
-copy /B phonegap.base.js+geolocation.js+position.js+accelerometer.js+network.js+debugconsole.js+contact.js+device.js phonegap.js
+copy /B phonegap.base.js+geolocation.js+position.js+accelerometer.js+network.js+debugconsole.js+contact.js+device.js+compass.js phonegap.js
diff --git a/inc/WebForm.h b/inc/WebForm.h
index 93e5134..722ff06 100755
--- a/inc/WebForm.h
+++ b/inc/WebForm.h
@@ -12,6 +12,7 @@
#include "Accelerometer.h"
#include "Network.h"
#include "DebugConsole.h"
+#include "Compass.h"
using namespace Osp::Base;
using namespace Osp::Base::Collection;
@@ -44,6 +45,7 @@
Accelerometer* accel;
Network* network;
DebugConsole* console;
+ Compass* compass;
String* __phonegapCommand;
public:
diff --git a/src/Compass.cpp b/src/Compass.cpp
index fa2c604..f00c008 100755
--- a/src/Compass.cpp
+++ b/src/Compass.cpp
@@ -8,15 +8,15 @@
#include "../inc/Compass.h"
Compass::Compass(Web* pWeb) : PhoneGapCommand(pWeb) {
-}
-
-Compass::~Compass() {
__sensorMgr.Construct();
started = false;
x = y = z = 0.0;
timestamp = 0;
}
+Compass::~Compass() {
+}
+
void
Compass::Run(const String& command) {
if (!command.IsEmpty()) {
@@ -25,10 +25,14 @@
String delim(L"/");
command.SubString(String(L"gap://").GetLength(), args);
StringTokenizer strTok(args, delim);
+ if(strTok.GetTokenCount() < 2) {
+ AppLogDebug("Not Enough Params");
+ return;
+ }
String method;
strTok.GetNextToken(method);
// Getting callbackId
- for(int i = 0 ; i < 2 && strTok.HasMoreTokens() ; i++, strTok.GetNextToken(callbackId));
+ strTok.GetNextToken(callbackId);
AppLogDebug("Method %S, callbackId: %S", method.GetPointer(), callbackId.GetPointer());
// used to determine callback ID
if(method == L"com.phonegap.Compass.watchHeading" && !callbackId.IsEmpty() && !IsStarted()) {
@@ -61,7 +65,7 @@
} else {
AppLogException("Compass sensor is not available");
String res;
- res.Format(256, L"PhoneGap.callbacks['%S'].fail({message:'Acceleration sensor is not available',code:'001'});", callbackId.GetPointer());
+ res.Format(256, L"PhoneGap.callbacks['%S'].fail({message:'Magnetic sensor is not available',code:'001'});", callbackId.GetPointer());
pWeb->EvaluateJavascriptN(res);
return false;
}
@@ -91,7 +95,7 @@
void
Compass::GetLastHeading() {
String res;
- res.Format(256, L"PhoneGap.callbacks['%S']({x:%f,y:%f,z:%f,timestamp:%d});", callbackId.GetPointer(), x, y, z, timestamp);
+ res.Format(256, L"PhoneGap.callbacks['%S'].success({x:%f,y:%f,z:%f,timestamp:%d});", callbackId.GetPointer(), x, y, z, timestamp);
pWeb->EvaluateJavascriptN(res);
}
@@ -106,7 +110,7 @@
AppLogDebug("x: %f, y: %f, z: %f timestamp: %d", x, y, z, timestamp);
String res;
- res.Format(256, L"PhoneGap.callbacks['%S']({x:%f,y:%f,z:%f,timestamp:%d});", callbackId.GetPointer(), x, y, z, timestamp);
+ res.Format(256, L"PhoneGap.callbacks['%S'].success({x:%f,y:%f,z:%f,timestamp:%d});", callbackId.GetPointer(), x, y, z, timestamp);
AppLogDebug("%S", res.GetPointer());
pWeb->EvaluateJavascriptN(res);
}
diff --git a/src/WebForm.cpp b/src/WebForm.cpp
index 1b8ff84..6c528a0 100755
--- a/src/WebForm.cpp
+++ b/src/WebForm.cpp
@@ -7,6 +7,8 @@
device = null;
accel = null;
network = null;
+ console = null;
+ compass = null;
}
WebForm::~WebForm(void)
@@ -143,6 +145,9 @@
else if(__phonegapCommand->StartsWith(L"gap://com.phonegap.DebugConsole", 0)) {
console->Run(*__phonegapCommand);
}
+ else if(__phonegapCommand->StartsWith(L"gap://com.phonegap.Compass", 0)) {
+ compass->Run(*__phonegapCommand);
+ }
delete __phonegapCommand;
__phonegapCommand = null;
}
@@ -183,6 +188,7 @@
accel = new Accelerometer(__pWeb);
network = new Network(__pWeb);
console = new DebugConsole(__pWeb);
+ compass = new Compass(__pWeb);
}
return r;