User Tools

Site Tools


Sidebar

Privacy Policy

News

Version 2.50 is out since Jan 1st 2022


Frequently Asked Questions


Namespaces

Note for contributors

If you wish to create a new page in the DroidScript wiki, please click on the most appropriate namespace above and follow the notes for contributors there.

Because of spam, it has been necessary to add a CAPTCHA to the registration form and the save option for editing pages. You will not need to prove you are human if you are logged in, so please register.

Please feel free to improve any existing page, as well as adding new pages to increase the sum of public knowledge about DroidScript.

Formatting Syntax

built_in:sensors

Sensors

(Information and examples taken from the DroidScript documentation). Information provided by Nick Weber

Description

Create Sensor objects using the CreateSensor function of the app object:

sns = app.CreateSensor( type, options );

Types available are:

  • Accelerometer
  • MagneticField
  • Orientation
  • Light
  • Proximity
  • Temperature
  • GameRotation
  • GeomagneticRotation
  • Gravity
  • Gyroscope
  • HeartRate
  • Acceleration
  • Pressure
  • Humidity
  • Rotation
  • Motion
  • StepCounter
  • StepDetector

Try sns.GetNames() which lists all the sensor information. You can probably scan for certain keywords.

You can also use a sensor id number instead of the name (just in case a future sensor is added).

You can use the SetOnChange function of the Sensor to set the name of a function you want to be called when a the changes occur.

Change the rate that a sensor checks for changes by adding one the options “Fastest”, “Fast”, “Medium” or “Slow”. “Slow” is the default.

Methods

Some controls use the same methods.
For examples of the same methods look here.

Method Description
Sensor.GetAzimuth()
Sensor.GetHeight()
Sensor.GetNames() returns a list of all available sensors
Sensor.GetPitch()
Sensor.GetRoll()
Sensor.GetType()
Sensor.GetValues()
Sensor.SetMaxRate( rate ) default 20ms
Sensor.SetMinChange( value ) a value between 0 and 1
Sensor.SetOnChange( callback )
Sensor.Start()
Sensor.Stop()

Examples

Sensor Type Code Example
Accelerometer
function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
 
  txt = app.CreateText( "", 0.8, 0.3, "Multiline" );
  lay.AddChild( txt );
  app.AddLayout( lay );
 
  sns = app.CreateSensor( "Accelerometer" );
  sns.SetOnChange( sns_OnChange );
  sns.Start();
 
}
 
function sns_OnChange( x, y, z, time )
{
  txt.SetText( "x="+x + "\n y="+y + "\n z="+z );
}
Orientation
function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
 
  txt = app.CreateText( "", 0.8, 0.3, "Multiline" );
  lay.AddChild( txt );
  app.AddLayout( lay );
 
  sns = app.CreateSensor( "Orientation" );
  sns.SetOnChange( sns_OnChange );
  sns.Start();
 
}
 
function sns_OnChange( azimuth, pitch, roll, time )
{
  var msg = " azimuth = " + azimuth.toFixed(1);
  msg += "\n pitch = " + pitch.toFixed(1);
  msg += "\n roll = " + roll.toFixed(1);
  txt.SetText( msg );
}
Light
function OnStart()
{
  lay = app.CreateLayout( "Linear", "VCenter,FillXY" );
 
  txt = app.CreateText( "", 0.8, 0.3 );
  lay.AddChild( txt );
  app.AddLayout( lay );
 
  sns = app.CreateSensor( "Light" );
  sns.SetOnChange( sns_OnChange );
  sns.Start();
 
}
 
function sns_OnChange( lux )
{
  txt.SetText( "level = " + lux + " lux" );
}
built_in/sensors.txt · Last modified: 2018/01/03 10:23 (external edit)