Dzisiaj o sensorach i geolokalizacji.
Jak w dwóch poprzednich odcinkach nie widać znaczących różnic w Consumer Preview. Moją uwagę jedynie zwróciło jawne ustawianie częstotliwości zdarzeń i obsługa innego zdarzenia w JS.
JS
Responding to motion and orientation sensors
Accelerometer
(function () {
"use strict";
var accelerometer;
var app = WinJS.Application;
// This function responds to all app activations.
app.onactivated = function (eventObject) {
if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
if (accelerometer == null) {
accelerometer = Windows.Devices.Sensors.Accelerometer.getDefault();
// Establish the report interval
var minimumReportInterval = accelerometer.minimumReportInterval;
var reportInterval = minimumReportInterval > 16 ? minimumReportInterval : 16;
accelerometer.reportInterval = reportInterval;
// Establish the event handler
accelerometer.addEventListener("readingchanged", onDataChanged);
}
WinJS.UI.processAll();
}
};
// This function is called each time an accelerometer event
// is fired by the driver.
function onDataChanged(e) {
var reading = e.reading;
var accelX = reading.accelerationX;
var accelY = reading.accelerationY;
var accelZ = reading.accelerationZ;
id('eventOutputX').innerHTML = accelX.toFixed(2);
id('eventOutputY').innerHTML = accelY.toFixed(2);
id('eventOutputZ').innerHTML = accelZ.toFixed(2);
}
// This function is invoked within onDataChanged to
// retrieve the given identifier from the HTML document.
function id(elementId) {
return document.getElementById(elementId);
}
app.start();
})();
This code retrieves the minimum interval supported by the device and compares it to a requested interval of 16 milliseconds (which approximates a 60-Hz refresh rate).
Gyrometer
(function () {
"use strict";
var gyrometer;
function id(elementId) {
return document.getElementById(elementId);
}
function onDataChanged(e) {
var reading = e.reading;
id('txtXVelocity').innerHTML = reading.angularVelocityX.toFixed(2);
id('txtYVelocity').innerHTML = reading.angularVelocityY.toFixed(2);
id('txtZVelocity').innerHTML = reading.angularVelocityZ.toFixed(2);
}
var app = WinJS.Application;
// This function responds to all app activations.
app.onactivated = function (eventObject) {
if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
gyrometer = Windows.Devices.Sensors.Gyrometer.getDefault();
// Choose a report interval supported by the sensor
var minimumReportInterval = gyrometer.minimumReportInterval;
var reportInterval = minimumReportInterval > 16 ? minimumReportInterval : 16;
gyrometer.reportInterval = reportInterval;
// Set the event handler
gyrometer.addEventListener("readingchanged", onDataChanged);
WinJS.UI.processAll();
}
};
app.start();
})();
Compass
(function () {
"use strict";
var compass;
var app = WinJS.Application;
// This function is invoked within onDataChanged to
// retrieve the given identifier from the HTML document.
function id(elementId) {
return document.getElementById(elementId);
}
// This function is called each time a compass event
// is fired by the driver.
function onDataChanged(e) {
var reading = e.reading;
id('txtMagNorth').innerHTML = reading.headingMagneticNorth.toFixed(2);
if (reading.headingTrueNorth != null) {
id('txtTrueNorth').innerHTML = reading.headingTrueNorth.toFixed(2);
}
}
// This function responds to all app activations.
app.onactivated = function (eventObject) {
if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
// Retrieve the default compass and
compass = Windows.Devices.Sensors.Compass.getDefault();
// Choose a report interval supported by the sensor
var minimumReportInterval = compass.minimumReportInterval;
var reportInterval = minimumReportInterval > 16 ? minimumReportInterval : 16;
compass.reportInterval = reportInterval;
// Establish the event handler
compass.addEventListener("readingchanged", onDataChanged);
WinJS.UI.processAll();
}
};
app.start();
})();
Inclinometer
(function () {
"use strict";
var inclinometer;
function id(elementId) {
return document.getElementById(elementId);
}
function onDataChanged(e) {
var reading = e.reading;
var pitch = reading.pitchDegrees;
var roll = reading.rollDegrees;
var yaw = reading.yawDegrees;
id('txtXAngle').innerHTML = pitch.toFixed(2);
id('txtYAngle').innerHTML = roll.toFixed(2);
id('txtZAngle').innerHTML = yaw.toFixed(2);
}
var app = WinJS.Application;
// This function responds to all app activations.
app.onactivated = function (eventObject) {
if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
inclinometer = Windows.Devices.Sensors.Inclinometer.getDefault();
// Choose a report interval supported by the sensor
var minimumReportInterval = inclinometer.minimumReportInterval;
var reportInterval = minimumReportInterval > 16 ? minimumReportInterval : 16;
inclinometer.reportInterval = reportInterval;
// Set the event handler
inclinometer.addEventListener("readingchanged", onDataChanged);
WinJS.UI.processAll();
}
};
app.start();
})();
Orientation sensor (quaternion and rotation matrix)
You can use the Orientation sensor to retrieve a Quaternion and rotation matrix with an app written in JavaScript. Developers typically use this data to control complex games.
A Quaternion can be most easily understood as a rotation of a point [x,y,z] about a single arbitrary axis. This is different from a rotation matrix, which represents rotations around three axes.
(function () {
"use strict";
var orientationSensor;
function id(elementId) {
return document.getElementById(elementId);
}
function onDataChanged(e) {
var reading = e.reading;
id('txtQuaternion').innerHTML = "W: " + reading.quaternion.w.toFixed(6)
+ "X: " + reading.quaternion.x.toFixed(6)
+ "Y: " + reading.quaternion.y.toFixed(6)
+ "Z: " + reading.quaternion.z.toFixed(6);
id('txtRotationMatrix').innerHTML = "M11: " + reading.rotationMatrix.m11.toFixed(6)
+ "M12: " + reading.rotationMatrix.m12.toFixed(6)
+ "M13: " + reading.rotationMatrix.m13.toFixed(6)
+ "M21: " + reading.rotationMatrix.m21.toFixed(6)
+ "M22: " + reading.rotationMatrix.m22.toFixed(6)
+ "M23: " + reading.rotationMatrix.m23.toFixed(6)
+ "M31: " + reading.rotationMatrix.m31.toFixed(6)
+ "M32: " + reading.rotationMatrix.m32.toFixed(6)
+ "M33: " + reading.rotationMatrix.m33.toFixed(6);
}
var app = WinJS.Application;
// This function responds to all app activations.
app.onactivated = function (eventObject) {
if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
// Retrieve the default orientation sensor
orientationSensor = Windows.Devices.Sensors.OrientationSensor.getDefault();
// Choose a report interval supported by the sensor
var minimumReportInterval = orientationSensor.minimumReportInterval;
var reportInterval = minimumReportInterval > 16 ? minimumReportInterval : 16;
orientationSensor.reportInterval = reportInterval;
// Set the event handler
orientationSensor.addEventListener("readingchanged", onDataChanged);
WinJS.UI.processAll();
}
};
app.start();
})();
Responding to changes in lighting
(function () {
"use strict";
var lightSensor;
// This function is invoked within onDataChanged to
// retrieve the given identifier from the HTML document.
function id(elementId) {
return document.getElementById(elementId);
}
// This function is called each time an accelerometer event
// is fired by the driver.
function onDataChanged(e) {
id('eventOutputIlluminance').innerHTML = e.reading.illuminanceInLux.toFixed(2);
}
var app = WinJS.Application;
// This function responds to all app activations.
app.onactivated = function (eventObject) {
if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
if (lightSensor == null) {
lightSensor = Windows.Devices.Sensors.LightSensor.getDefault();
// Set the report interval
var minimumReportInterval = lightSensor.minimumReportInterval;
var reportInterval = minimumReportInterval > 16 ? minimumReportInterval : 16;
lightSensor.reportInterval = reportInterval;
// Establish the event handler
lightSensor.addEventListener("readingchanged", onDataChanged);
WinJS.UI.processAll();
}
};
app.start();
})();
Detecting geolocation
To help conserve power, your app should set the DesiredAccuracy property, to indicate to the location platform whether your app needs high accuracy data or not. If no apps require high-accuracy data, the system can save power by not turning on GPS providers.
Apps that have specific accuracy requirements, such as navigation apps, should use the Geocoordinate.Accuracy property to determine whether the available location data meets the app's requirements.
C#
Accelerometer
using Windows.UI.Core;
using Windows.Devices.Sensors;
namespace AccelerometerCS
{
partial class BlankPage
{
// Sensor and dispatcher variables
private Accelerometer _accelerometer;
// This event handler writes the current accelerometer reading to
// the three acceleration text blocks on the app's main page.
private void ReadingChanged(object sender, AccelerometerReadingChangedEventArgs e)
{
Dispatcher.InvokeAsync(CoreDispatcherPriority.Normal, (s, a) =>
{
AccelerometerReading reading = (a.Context as AccelerometerReadingChangedEventArgs).Reading;
txtXAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationX);
txtYAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationY);
txtZAxis.Text = String.Format("{0,5:0.00}", reading.AccelerationZ);
}, this, e);
}
public BlankPage()
{
InitializeComponent();
_accelerometer = Accelerometer.GetDefault();
if (_accelerometer != null)
{
// Establish the report interval
uint minReportInterval = _accelerometer.MinimumReportInterval;
uint reportInterval = minReportInterval > 16 ? minReportInterval : 16;
_accelerometer.ReportInterval = reportInterval;
// Assign an event handler for the reading-changed event
_accelerometer.ReadingChanged += new TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs>(ReadingChanged);
}
}
}
}
Brak komentarzy:
Prześlij komentarz