Permalink
Browse files

Fix demo for devices without gyroscopes.

Deciding which event to listen was a bit trickier. DeviceOrientationEvent is defined even if device does not have gyros. We know it has gyro only if DeviceOrientationEvent fires or we have RotationRate in DeviceMotionEvent.
  • Loading branch information...
1 parent a5aced9 commit 6948dece743fad61c59e083158d7125317c9679c @tuupola committed Feb 7, 2011
Showing with 44 additions and 41 deletions.
  1. +44 −41 websocket/connect.html
View
@@ -52,6 +52,7 @@
var interval = 40;
var payload = {};
var packets = 0;
+var have_gyroscope = false;
var query = $.deparam.querystring();
var socket = new WebSocket("ws://ws.appelsiini.net:8080/iphone/" + query.pin);
@@ -63,49 +64,51 @@
};
-/* Check if we have device with Gyroscope (iPhone 4 or iPod Touch 4G). */
-if ("ondeviceorientation" in window) {
-
- window.ondeviceorientation = function(event) {
-
- /* beta: -180..180 (rotation around x axis) */
- /* gamma: -90..90 (rotation around y axis) */
- /* alpha: 0..360 (rotation around z axis) (-180..180) */
-
- data.x = Math.round(event.beta);
- data.y = Math.round(event.gamma);
- data.z = Math.round(event.alpha);
-
- /* jQuery mobile cannot handle negative minimum value. */
- /* so I have to use zero based value to display the slider. */
- $("#beta").val(data.x * 1 + 180).trigger("keyup");
- $("#gamma").val(data.y * 1 + 90).trigger("keyup");
- $("#alpha").val(data.z * 1 + 180).trigger("keyup");
-
- payload = {type: "orientation", data: data};
- };
-
-/* Check if we have device with accelerometer (all older Apple devices) */
-} else if ("ondevicemotion" in window) {
-
- window.ondevicemotion = function(event) {
-
- threshold = 5;
- /* Accelerometers give smaller number thus we multiply */
- /* to have similar rates as with gyroscope. */
- data.x = Math.round(event.accelerationIncludingGravity.x * 10);
- data.y = Math.round(event.accelerationIncludingGravity.y * 10);
- /*data.z = Math.round(event.accelerationIncludingGravity.z * 10);*/
-
- /* jQuery mobile cannot handle negative minimum value. */
- /* so I have to use zero based value to display the slider. */
- $("#beta").val(data.x * 1 + 180).trigger("keyup");
- $("#gamma").val(data.y * 1 + 90).trigger("keyup");
-
- payload = {type: "acceleration", data: data};
- };
+function read_gyroscope() {
+ /* beta: -180..180 (rotation around x axis) */
+ /* gamma: -90..90 (rotation around y axis) */
+ /* alpha: 0..360 (rotation around z axis) (-180..180) */
+
+ data.x = Math.round(event.beta);
+ data.y = Math.round(event.gamma);
+ data.z = Math.round(event.alpha);
+
+ /* jQuery mobile cannot handle negative minimum value. */
+ /* so I have to use zero based value to display the slider. */
+ $("#beta").val(data.x * 1 + 180).trigger("keyup");
+ $("#gamma").val(data.y * 1 + 90).trigger("keyup");
+ $("#alpha").val(data.z * 1 + 180).trigger("keyup");
+
+ payload = {type: "orientation", data: data};
};
+function read_accelerometer() {
+
+ /* If we have rotation rate device has gyroscopes and start */
+ /* reading gyroscopes instead... */
+ if(event.rotationRate) {
+ window.removeEventListener("devicemotion", read_accelerometer, false);
+ window.addEventListener("deviceorientation", read_gyroscope, false);
+ }
+
+ threshold = 5;
+ /* Accelerometers give smaller number thus we multiply */
+ /* to have similar rates as with gyroscope. */
+ data.x = Math.round(event.accelerationIncludingGravity.x * 10);
+ data.y = Math.round(event.accelerationIncludingGravity.y * 10);
+ /*data.z = Math.round(event.accelerationIncludingGravity.z * 10);*/
+
+ /* jQuery mobile cannot handle negative minimum value. */
+ /* so I have to use zero based value to display the slider. */
+ $("#beta").val(data.x * 1 + 180).trigger("keyup");
+ $("#gamma").val(data.y * 1 + 90).trigger("keyup");
+
+ payload = {type: "acceleration", data: data};
+};
+
+/* By default we read accelerometers since they are available in all iGadgets. */
+window.addEventListener("devicemotion", read_accelerometer, false);
+
/* Reduce the amount of data to be sent in two ways. 1) Only send every */
/* interval milliseconds and 2) Only send if user has tilted the phone */
/* more than threshold. */

0 comments on commit 6948dec

Please sign in to comment.