Skip to content

Commit 1dca5f5

Browse files
committed
Merge pull request #2 from dorukeker/availibility-check
Availibility check
2 parents fbe783a + f506671 commit 1dca5f5

File tree

3 files changed

+237
-39
lines changed

3 files changed

+237
-39
lines changed

README.md

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,55 @@ Add the JS file to your HTML
88

99
<script src="js/gyronorm.js"></script>
1010

11-
Initialiize and start the <em>gn</em> object
11+
Initialize and start the <em>gn</em> object. Good practice is to start the object in the `ready` callback function, which you pass when initializing.
1212

1313
Access the values in the callback function of the `gn.start()`
1414

1515
<script src="/js/gyronorm.js"></script>
1616

17-
var gn = new GyroNorm();
18-
gn.start(function(data){
19-
// Process:
17+
var args = {read:ongnReady};
18+
var gn = new GyroNorm(args);
19+
20+
var ongnReady = function(){
21+
gn.start(function(data){
22+
// Process:
2023
// data.do.alpha ( deviceorientation event alpha value )
2124
// data.do.beta ( deviceorientation event beta value )
2225
// data.do.gamma ( deviceorientation event gamma value )
2326
// data.do.absolute ( deviceorientation event absolute value )
24-
27+
2528
// data.dm.x ( devicemotion event acceleration x value )
2629
// data.dm.y ( devicemotion event acceleration y value )
2730
// data.dm.z ( devicemotion event acceleration z value )
28-
31+
2932
// data.dm.gx ( devicemotion event accelerationIncludingGravity x value )
3033
// data.dm.gy ( devicemotion event accelerationIncludingGravity y value )
3134
// data.dm.gz ( devicemotion event accelerationIncludingGravity z value )
32-
35+
3336
// data.dm.alpha ( devicemotion event rotationRate alpha value )
3437
// data.dm.beta ( devicemotion event rotationRate beta value )
3538
// data.dm.gamma ( devicemotion event rotationRate gamma value )
3639
});
40+
}
41+
42+
###Backward Compatibility
43+
In the previous version you were able to initialize and start the <em>gn</em> object directly, without the `ready` function
44+
45+
var gn = new GyroNorm();
46+
gn.start(function(data){ ... });
47+
48+
This method still works. However the return values from the `gn.isAvailable()` function will not be reliable. I recommend to use the `ready` callback function as described above.
3749

3850
###Options
39-
You can pass arguments as an object to the `gn.init()` method. The values you pass overwrites the default values. Below is the list of available options and their default values.
51+
You can pass arguments as an object to the to the constructor method. The values you pass overwrites the default values. Below is the list of available options and their default values.
4052

4153
var args = {
4254
frequency:50, // ( how often the object sends the values - milliseconds )
4355
gravityNormalized:true, // ( if the garvity related values to be normalized )
4456
directionAbsolute:false, // ( if the do.alpha value is absolute, if false the value is relative to the initial position of the device )
4557
decimalCount:2 // ( how many digits after the decimal point wil there be in the return values )
4658
logger:null // ( function to be called to log messages from GyroNorm )
59+
ready:null // ( callback function to be called when the availability of the events and values are known )
4760
}
4861

4962
var gn = new GyroNorm(args);
@@ -74,7 +87,44 @@ Starts returning values via the callback function. The callback function is call
7487

7588
callback - function(data) - Function that returns values via the <em>data</em> object. The available values via <em>data</em> are listed above.
7689

90+
####isAvailable()
91+
92+
Tells the availability of device orientation or device motion values on the device+browser combination.
93+
94+
#####Syntax
95+
96+
gn.isAvailable(valueType);
97+
98+
// or
99+
100+
gn.isAvailable();
101+
102+
#####Parameters
103+
104+
valueType - string - optional - If passed, the method returns `true` or `false`, depending on the availablity of the specified value. Possible values are `deviceorientation`,`acceleration`,`accelerationinludinggravity`,`rotationrate` or `compassneedscalibration`
105+
106+
When called without a parameter returns availibility for values.
77107

108+
#####Example
109+
110+
var args = {ready:ongnReady};
111+
var gn = new GyroNorm(args);
112+
var ongnReady = function(){
113+
var doAvailable = gn.isAvailable("deviceorientation");
114+
// Parameter can also be "acceleration","accelerationinludinggravity","rotationrate" or "compassneedscalibration"
115+
// This example returns true if deviceorientation is available. Returns false if not.
116+
117+
var gnAvailable = gn.isAvailable();
118+
/* Returns the following object
119+
{
120+
deviceOrientationAvailable : true/false,
121+
accelerationAvailable : true/false,
122+
accelerationIncludingGravityAvailable : true/false,
123+
rotationRateAvailable : true/false,
124+
compassNeedsCalibrationAvailable : true/false
125+
}
126+
*/
127+
}
78128

79129
####normalizeGravity()
80130

@@ -90,10 +140,13 @@ flag - boolean - <em>true</em> sets the option <em>gravityNormalized</em> on, <e
90140

91141
#####Example
92142

93-
var gn = new GyroNorm();
94-
gn.start(function(){
95-
// Process return values here
96-
});
143+
var args = {ready:ongnReady};
144+
var gn = new GyroNorm(args);
145+
var ongnReady = function(){
146+
gn.start(function(){
147+
// Process return values here
148+
});
149+
}
97150

98151
// At this point callback function returns normalized gravity-related values.
99152

@@ -115,10 +168,13 @@ flag - boolean - <em>true</em> sets the option <em>directionAbsolute</em> on, <e
115168

116169
#####Example
117170

118-
var gn = new GyroNorm();
119-
gn.start(function(){
120-
// Process return values here
121-
});
171+
var args = {ready:ongnReady};
172+
var gn = new GyroNorm(args);
173+
var ongnReady = function(){
174+
gn.start(function(){
175+
// Process return values here
176+
});
177+
}
122178

123179
// At this point callback function returns do.alpha values relative to the initial position of the device
124180

@@ -138,16 +194,19 @@ Once this method is called <em>directionAbsolute</em> option is also set to <em>
138194

139195
#####Example
140196

141-
var gn = new GyroNorm();
142-
gn.start(function(){
143-
// Process return values here
144-
});
197+
var args = {ready:ongnReady};
198+
var gn = new GyroNorm(args);
199+
var ongnReady = function(){
200+
gn.start(function(){
201+
// Process return values here
202+
});
203+
}
145204

146205
// At this point callback function returns do.alpha values relative to the initial position of the device
147206

148207
gn.setHeadDirection();
149208

150-
// At this point callback function returns do.alpha values relative to the position of the device when the above line is called
209+
// At this point callback function starts to return do.alpha values relative to the position of the device when the above line is called
151210

152211
####stop()
153212

@@ -217,7 +276,7 @@ The human-readable message.
217276

218277
Below is a list of device/operating system/browser combinations. The demo.html file worked on them out of the box. I will update this list in the future. Please also share your findings so we can have a more complete list.
219278

220-
- iPhone 5 - iOS 8 beta - Safari
279+
- iPhone 5 - iOS 8.0.2 - Safari
221280
- iPhone 4 - iOS 6.x - Safari
222281
- HTC One - Android 4.2.2 - Native Browser
223282
- HTC One - Android 4.2.2 - Chrome 35.0.1916.141
@@ -231,8 +290,8 @@ Below is a list of things I plan to add in the future. Please note that it is no
231290

232291
- Option to track deviceorientation and devicemotion events seperately
233292
- Providing alpha values even in the calibration mode
234-
- Boolean values to check if events are available in the device
235293
- Option to get snap shot of the values (currently it is only tracking)
294+
- Boolean value to check if GyroNorm is tracking or not
236295

237296
##Contact
238297

demo/index.html

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,41 @@
152152
var gn;
153153

154154
function init_gn(){
155-
var args = {logger:function(data){
156-
$('#error-message').val(data.message);
157-
}};
155+
var args = {
156+
logger:logger,
157+
ready:ongnReady
158+
};
158159
gn = new GyroNorm(args);
160+
}
161+
162+
function logger(data){
163+
$('#error-message').append(data.message + "\n");
164+
}
165+
166+
var ongnReady = function(){
167+
var isAvailable = gn.isAvailable();
168+
if(!isAvailable.deviceOrientationAvailable){
169+
logger({message:'Device orientation is not available.'});
170+
}
171+
172+
if(!isAvailable.accelerationAvailable){
173+
logger({message:'Device acceleration is not available.'});
174+
}
175+
176+
if(!isAvailable.accelerationIncludingGravityAvailable){
177+
logger({message:'Device acceleration incl. gravity is not available.'});
178+
}
179+
180+
if(!isAvailable.rotationRateAvailable){
181+
logger({message:'Device rotation rate is not available.'});
182+
}
183+
184+
if(!isAvailable.compassNeedsCalibrationAvailable){
185+
logger({message:'Device compass needs calibration is not available.'});
186+
}
187+
159188
start_gn();
160-
}
189+
}
161190

162191
function stop_gn(){
163192
gn.stop();

0 commit comments

Comments
 (0)