DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


sample_code:shake

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:shake [2015/11/15 10:56] (current)
stevegarman created
Line 1: Line 1:
 +====== Shake ======
 +This sample offers an OnShake function using the accelerometer.
 +<code JavaScript OnShake.js>​
 +var lastx,​lasty,​lastz,​lastms
 +function OnStart()
 +{
 +  var lay = app.CreateLayout( "​Linear",​ "​VCenter,​FillXY"​ );​
 +  var txt = app.CreateText( "​Shake test"​ );​
 +  lay.AddChild( txt );​
 +  app.AddLayout( lay );​
  
 +  var sns = app.CreateSensor( "​Accelerometer"​ );​
 +  sns.SetOnChange( sns_OnChange );​
 +  sns.Start();​
 +}
 +
 +function sns_OnChange( x,​ y,​ z,​ time )
 +{
 +  try
 +  {
 +     var diffms,​speed;​
 +     if(lastx!==undefined)
 +     {
 +       diffms=time-lastms;​
 +       speed = Math.abs(x+y+z-lastx-lasty-lastz) /​ diffms * 10000;​ 
 +       if(speed>​800) OnShake(speed/​800);​
 +     }
 +     lastx=x;​
 +     lasty=y;​
 +     lastz=z;​
 +     lastms=time;​
 +  }
 +  catch(e) {}
 +}
 +
 +function OnShake(speed)
 +{
 +   var msg = "​Shake\n"​+speed.toFixed(2);​
 +   app.ShowPopup(msg,"​Short"​);​
 +}
 +</​code>​
sample_code/shake.txt · Last modified: 2015/11/15 10:56 by stevegarman