You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2012/02/22 09:15:23 UTC
[6/29] wp7 commit: Fix issue with single call to get heading from
Compass API
Fix issue with single call to get heading from Compass API
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/commit/0587c74e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/tree/0587c74e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/diff/0587c74e
Branch: refs/heads/master
Commit: 0587c74ebef67b418e54e658696613a65bd7f6ad
Parents: f95d878
Author: Jesse MacFadyen <pu...@gmail.com>
Authored: Thu Feb 9 17:39:51 2012 -0800
Committer: Jesse MacFadyen <pu...@gmail.com>
Committed: Thu Feb 9 17:39:51 2012 -0800
----------------------------------------------------------------------
framework/Cordova/Commands/Compass.cs | 64 +++++++++++----
.../www/autotest/tests/compass.tests.js | 11 +--
2 files changed, 51 insertions(+), 24 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/0587c74e/framework/Cordova/Commands/Compass.cs
----------------------------------------------------------------------
diff --git a/framework/Cordova/Commands/Compass.cs b/framework/Cordova/Commands/Compass.cs
index f08e017..9bda086 100644
--- a/framework/Cordova/Commands/Compass.cs
+++ b/framework/Cordova/Commands/Compass.cs
@@ -147,30 +147,38 @@ namespace WP7GapClassLib.Cordova.Commands
}
else
{
- if (compass == null)
- {
- // Instantiate the compass.
- compass = new DeviceCompass();
- compass.TimeBetweenUpdates = TimeSpan.FromMilliseconds(40);
- compass.CurrentValueChanged += new EventHandler<Microsoft.Devices.Sensors.SensorReadingEventArgs<Microsoft.Devices.Sensors.CompassReading>>(compass_CurrentValueChanged);
- compass.Calibrate += new EventHandler<Microsoft.Devices.Sensors.CalibrationEventArgs>(compass_Calibrate);
- }
+ //if (compass == null)
+ //{
+ // // Instantiate the compass.
+ // compass = new DeviceCompass();
+ // compass.TimeBetweenUpdates = TimeSpan.FromMilliseconds(40);
+ // compass.CurrentValueChanged += new EventHandler<Microsoft.Devices.Sensors.SensorReadingEventArgs<Microsoft.Devices.Sensors.CompassReading>>(compass_CurrentValueChanged);
+ // compass.Calibrate += new EventHandler<Microsoft.Devices.Sensors.CalibrationEventArgs>(compass_Calibrate);
+ //}
- compass.Start();
+ //compass.Start();
+
}
try
{
if (currentStatus != Running)
{
- int status = this.start();
- if (status == ErrorFailedToStart)
+
+ lock (compass)
{
- DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ErrorFailedToStart));
- return;
+ compass.CurrentValueChanged += compass_SingleHeadingValueChanged;
+ compass.Start();
+ this.SetStatus(Starting);
}
+ //if (status == ErrorFailedToStart)
+ //{
+ // DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ErrorFailedToStart));
+ // return;
+ //}
+
long timeout = 2000;
while ((currentStatus == Starting) && (timeout > 0))
{
@@ -186,12 +194,13 @@ namespace WP7GapClassLib.Cordova.Commands
}
lock (compass)
{
- if (watchers.ContainsKey(getCompassId))
+
+ compass.CurrentValueChanged -= compass_SingleHeadingValueChanged;
+ if (watchers.Count < 1)
{
- compass.CurrentValueChanged -= watchers[getCompassId].compass_CurrentValueChanged;
- watchers.Remove(getCompassId);
+ compass.Stop();
}
- DispatchCommandResult(new PluginResult(PluginResult.Status.OK, GetHeadingFormatted(compass.CurrentValue)));
+ //DispatchCommandResult(new PluginResult(PluginResult.Status.OK, GetHeadingFormatted(compass.CurrentValue)));
}
}
catch (UnauthorizedAccessException)
@@ -204,13 +213,31 @@ namespace WP7GapClassLib.Cordova.Commands
}
}
+ void compass_SingleHeadingValueChanged(object sender, Microsoft.Devices.Sensors.SensorReadingEventArgs<CompassReading> e)
+ {
+ this.SetStatus(Running);
+ if (compass.IsDataValid)
+ {
+ // trueHeading :: The heading in degrees from 0 - 359.99 at a single moment in time.
+ // magneticHeading:: The heading relative to the geographic North Pole in degrees 0 - 359.99 at a single moment in time.
+ // A negative value indicates that the true heading could not be determined.
+ // headingAccuracy :: The deviation in degrees between the reported heading and the true heading.
+ //rawMagnetometerReading = e.SensorReading.MagnetometerReading;
+
+ //Debug.WriteLine("Compass Result :: " + GetHeadingFormatted(e.SensorReading));
+
+ PluginResult result = new PluginResult(PluginResult.Status.OK, GetHeadingFormatted(e.SensorReading));
+
+ DispatchCommandResult(result);
+ }
+ }
+
/// <summary>
/// Starts listening for compass sensor
/// </summary>
/// <returns>status of listener</returns>
private int start()
{
-
if ((currentStatus == Running) || (currentStatus == Starting))
{
return currentStatus;
@@ -313,6 +340,7 @@ namespace WP7GapClassLib.Cordova.Commands
void compass_CurrentValueChanged(object sender, Microsoft.Devices.Sensors.SensorReadingEventArgs<CompassReading> e)
{
+ this.SetStatus(Running);
if (compass.IsDataValid)
{
// trueHeading :: The heading in degrees from 0 - 359.99 at a single moment in time.
http://git-wip-us.apache.org/repos/asf/incubator-cordova-wp7/blob/0587c74e/tests/MobileSpecUnitTests/www/autotest/tests/compass.tests.js
----------------------------------------------------------------------
diff --git a/tests/MobileSpecUnitTests/www/autotest/tests/compass.tests.js b/tests/MobileSpecUnitTests/www/autotest/tests/compass.tests.js
index e9f629a..824325f 100644
--- a/tests/MobileSpecUnitTests/www/autotest/tests/compass.tests.js
+++ b/tests/MobileSpecUnitTests/www/autotest/tests/compass.tests.js
@@ -21,24 +21,23 @@ Tests.prototype.CompassTests = function()
QUnit.stop(Tests.TEST_TIMEOUT);
var win = function(a)
{
- ok(typeof a == 'Object', "Compass heading object returned in getCurrentHeading success callback should be of type 'Object'.");
+ ok(typeof a == 'object', "Compass heading object returned in getCurrentHeading success callback should be of type 'Object'.");
ok(a.magneticHeading != null, "Compass heading object has a magneticHeading property");
- ok(typeof a.magneticHeading == "Number", "Compass heading.magneticHeading is a Number");
+ ok(typeof a.magneticHeading == "number", "Compass heading.magneticHeading is a Number");
ok(a.trueHeading != null, "Compass heading object has a magneticHeading property");
- ok(typeof a.trueHeading == "Number", "Compass heading.trueHeading is a Number");
+ ok(typeof a.trueHeading == "number", "Compass heading.trueHeading is a Number");
ok(a.headingAccuracy != null, "Compass heading object has a magneticHeading property");
- ok(typeof a.headingAccuracy == "Number", "Compass heading.headingAccuracy is a Number");
+ ok(typeof a.headingAccuracy == "number", "Compass heading.headingAccuracy is a Number");
ok(a.timestamp != null, "Compass heading object has a magneticHeading property");
- ok(typeof a.timestamp == "Number", "Compass heading.timestamp is a Number");
+ ok(typeof a.timestamp == "number", "Compass heading.timestamp is a Number");
start();
};
var fail = function(a)
{
- console.log("compass failed to return a result :: " + a);
ok(typeof a == 'object', "Compass heading object returned in getCurrentHeading FAIL callback should be of type 'object'.");
ok(a.code != null, "Compass error result should have a 'code' property");
ok(typeof a.code == 'string', "Compass error result should have a 'code' property of type string");