C/C++ Guides

Home

Sensor Control in C/C++

Modern smartphones have a variety of in-built sensors to detect, for example, movement, orientation, rotation, proximity, and magnetic fields. This tutorial explains how to start and stop sensors, and to receive sensor data in your application through the MoSync Sensor API.

Only the MoSync runtimes for Android and iPhone have sensor support at the moment, and sensor simulation is not yet available in the MoSync Emulator.

Not all devices have hardware sensors. Before you run the application on the device, check which sensors it has using our SensorTest application.

Accelerometer

An accelerometer is a sensor that measures the acceleration forces. On most modern mobile devices there is a 3-way axis device that determines the phone’s physical position. By measuring the static acceleration due to gravity you can find the angle at which device is tilted and by measuring the dynamic acceleration due to movement you can analyze the direction in which the device is moving.

This sensor is available for both the Android and iPhone runtimes.

Starting the Accelerometer

To start the accelerometer, call the MoSync Sensor API function maSensorStart(sensor, interval). The first parameter should be  SENSOR_TYPE_ACCELEROMETER. The second parameter should be the desired update interval in milliseconds. Note that each device has a minimum value for this interval and setting a smaller value could stop the sensor.

The maSensorStart function returns SENSOR_ERROR_NONE if the sensor is started successfully, or a SENSOR_ERROR_* in case of error.

Receiving Event Data

After the sensor starts, your application will receive EVENT_TYPE_SENSOR events. The event contains a struct with the following values:

  • type - SENSOR_TYPE_ACCELEROMETER.
  • values[0] - the acceleration value (in Gs) for the x-axis of the device.
  • values[1] - the acceleration value (in Gs) for the y-axis of the device.
  • values[2] - the acceleration value (in Gs) for the z-axis of the device.

Stopping the Accelerometer

To stop the accelerometer,  call maSensorStop(sensor) with SENSOR_TYPE_ACCELEROMETER as the parameter.

The function returns SENSOR_ERROR_NONE, if the accelerometer stopped successfully, or SENSOR_ERROR_* in case of error.

Example

#include <MAUtil/Moblet.h>
#include <MAUtil/util.h>
#include <maapi.h>
#include <conprint.h>

extern "C" int MAMain()
{
    maSetColor(0xFF0000);
    maUpdateScreen();

    // start the accelerometer
    maSensorStart(SENSOR_TYPE_ACCELEROMETER, 400);

    while(true)
    {
        MAEvent event;

        // listen for events
        while(maGetEvent(&event))
        {
            // check if it’s a sensor type event
            if(EVENT_TYPE_SENSOR == event.type)
            {
                // check if it’s a accelerometer sensor type event
                if (SENSOR_TYPE_ACCELEROMETER == event.sensor.type)
                {
                    char buffer[50];
                    maSetColor(0xFF0000);
                    sprintf(buffer, "x=%f, y=%f, z=%f",
                            event.sensor.values[0],
                            event.sensor.values[1], 
                            event.sensor.values[2]);

                    maSetColor(0);
                    maFillRect(0, 0, 1000, 100);
                    maSetColor(0xFF0000);
                    maDrawText(10, 10, buffer);
                    maUpdateScreen();
                }
            }
        }
    }
};

Gyroscope

A gyroscope is a device for measuring or maintaining orientation. It can measure the rate of rotation around a particular axis. Unlike the accelerometer, the gyroscope is not affected by gravity.

This sensor is available for Android and iPhone runtimes.

Starting the Gyroscope

To start the gyroscope, call maSensorStart(sensor, interval). The first parameter should be  SENSOR_TYPE_GYROSCOPE. The second parameter should be the desired update interval in milliseconds. Note that each device has a minimum value for this interval and setting a smaller value could stop the sensor.

The maSensorStart function returns SENSOR_ERROR_NONE if the sensor has started successfully, or a SENSOR_ERROR_* in case of error.

Receiving Event Data

After the sensor starts, your application will receive EVENT_TYPE_SENSOR events. The event contains a struct with the following values:

  • type - SENSOR_TYPE_GYROSCOPE.
  • values[0] - the x-axis rotation rate in radians per second.
  • values[1] - the y-axis rotation rate in radians per second.
  • values[2] - the z-axis rotation rate in radians per second.

Stopping the Gyroscope

To stop the gyroscope, call maSensorStop(sensor) with SENSOR_TYPE_GYROSCOPE as the parameter.

The function returns SENSOR_ERROR_NONE if the sensor stopped successfully, or SENSOR_ERROR_* in case of error.

Example

#include <MAUtil/Moblet.h>
#include <MAUtil/util.h>
#include <maapi.h>
#include <conprint.h>

extern "C" int MAMain()
{
    maSetColor(0xFF0000);
    maUpdateScreen();

    // start the gyroscope sensor
    maSensorStart(SENSOR_TYPE_GYROSCOPE, 400);

    while (true)
    {
        MAEvent event;
        // listen for events
        while(maGetEvent(&event))
        {
            // check if it’s a sensor type event
            if(EVENT_TYPE_SENSOR == event.type)
            {
                // check if it’s a gyroscope sensor type event
                if (SENSOR_TYPE_GYROSCOPE == event.sensor.type)
                {
                    char buffer[50];
                    maSetColor(0xFF0000);
                    sprintf(buffer, "x=%f, y=%f, z=%f", 
                            event.sensor.values[0],
                            event.sensor.values[1],
                            event.sensor.values[2]);

                    maSetColor(0);
                    maFillRect(0, 0, 1000, 100);
                    maSetColor(0xFF0000);
                    maDrawText(10, 10, buffer);
                    maUpdateScreen();
                 }
            }
        }
    }
};

Magnetometer

A magnetometer is a instrument used to measure the strength or direction of magnetic fields.

This sensor is available for Android and iPhone runtimes.

Starting the Magnetometer

To start the magnetometer, call maSensorStart(sensor, interval). The first parameter should be SENSOR_TYPE_MAGNETIC_FIELD. As the magnetometer does not require an update interval, the second parameter can be any random number.

The function returns SENSOR_ERROR_NONE if the sensor has started successfully, or a SENSOR_ERROR_* in case of error.

Receiving Event Data

After the sensor starts, your application will receive EVENT_TYPE_SENSOR events. The event contains a struct with the following values:

  • type - SENSOR_TYPE_MAGNETIC_FIELD .
  • values[0] - the geomagnetic data for the x-axis.
  • values[1] - the geomagnetic data for the y-axis.
  • values[2] - the geomagnetic data for the z-axis.

The values are in microteslas. You can calculate the magnetic north using those values.

Stopping the Magnetometer

To stop the magnetometer, call maSensorStop(sensor) with SENSOR_TYPE_MAGNETIC_FIELD as the parameter.

The function returns SENSOR_ERROR_NONE if the sensor stopped successfully, or SENSOR_ERROR_* in case of error.

Example

#include <MAUtil/Moblet.h>
#include <MAUtil/util.h>
#include <maapi.h>
#include <conprint.h>
 
extern "C" int MAMain()
{
    maSetColor(0xFF0000);
    maUpdateScreen();
 
    // start the magnetometer sensor
    maSensorStart(SENSOR_TYPE_MAGNETIC_FIELD, 1234);
 
    while (true)
    {
        MAEvent event;
        // listen for events
        while(maGetEvent(&event))
        {
            // check if it's a sensor type event
            if(EVENT_TYPE_SENSOR == event.type)
             {
                // check if it's a magnetic field sensor type event
                if (SENSOR_TYPE_MAGNETIC_FIELD == event.sensor.type)
                {
                    char buffer[50];
                    maSetColor(0xFF0000);
                    sprintf(buffer, "x = %f, y = %f, z = %f",
                            event.sensor.values[0],
                            event.sensor.values[1],
                            event.sensor.values[2]);
                    
                    maSetColor(0);
                    maFillRect(0, 0, 1000, 100);
                    maSetColor(0xFF0000);
                    maDrawText(10, 10, buffer);
                    maUpdateScreen();
                }
            }
        }
    }
};

Proximity Sensor

A proximity sensor detects the presence of nearby objects.

This sensor is available for Android and iPhone runtimes.

Starting the Proximity Sensor

To start the proximity sensor, call maSensorStart(sensor, interval). The first parameter should be SENSOR_TYPE_PROXIMITY. As the proximity sensor does not require an update interval, the second parameter can be any random number.

The function returns SENSOR_ERROR_NONE if the sensor started successfully, or a SENSOR_ERROR_* in case of error.

Receiving Event Data

After the sensor starts, your application will receive EVENT_TYPE_SENSOR events. The event contains a struct with the following values:

  • type - SENSOR_TYPE_PROXIMITY.
  • values[0] - SENSOR_PROXIMITY_VALUE_NEAR or SENSOR_PROXIMITY_VALUE_FAR

Note: the device’s OS establishes the meaning of “near” and “far”.

Stopping the Proximity Sensor

To stop the proximity sensor, call maSensorStop(sensor) with SENSOR_TYPE_PROXIMITY as parameter.

The function returns SENSOR_ERROR_NONE if the sensor stopped successfully, or SENSOR_ERROR_* in case of error.

Example

#include <MAUtil/Moblet.h>
#include <MAUtil/util.h>
#include <maapi.h>
#include <conprint.h>

extern "C" int MAMain()
{
    maSetColor(0xFF0000);
    maUpdateScreen();

    // start the proximity sensor
    maSensorStart(SENSOR_TYPE_PROXIMITY, 123);

    while (true)
    {
        MAEvent event;
        // listen for events
        while(maGetEvent(&event))
        {
            // check if it's a sensor type event
            if(EVENT_TYPE_SENSOR == event.type)
            {
                // check if it's a proximity sensor type event
                if (SENSOR_TYPE_PROXIMITY == event.sensor.type)
                {
                    char buffer[50];
                    maSetColor(0xFF0000);
                    sprintf(buffer, "proximity sensor value = %f",
                            event.sensor.values[0]);

                    maSetColor(0);
                    maFillRect(0, 0, 1000, 100);
                    maSetColor(0xFF0000);
                    maDrawText(10, 10, buffer);
                    maUpdateScreen();
                }
            }
        }
    }
};

Orientation Sensor

The device’s operating system uses the accelerometer to determine the current orientation of the device and can therefore behave as an orientation sensor.

This sensor is available for Android and iPhone runtimes.

Starting the Orientation Sensor

To start the orientation sensor, call maSensorStart(sensor, interval). The first parameter should be  SENSOR_TYPE_ORIENTATION. As the orientation sensor does not require an update interval, the second parameter can be any random number.

The function returns SENSOR_ERROR_NONE if the sensor started successfully, or a SENSOR_ERROR_* in case of error.

Receiving Event Data

After the sensor starts, your application will receive EVENT_TYPE_SENSOR events. The event contains a struct with the following values:

type
- SENSOR_TYPE_ORIENTATION
values[0]
- UIDEVICE_ORIENTATION_UNKNOWN              
- UIDEVICE_ORIENTATION_PORTRAIT                           
- UIDEVICE_ORIENTATION_PORTRAIT_UPSIDE_DOWN                           
- UIDEVICE_ORIENTATION_LANDSCAPE_LEFT                           
- UIDEVICE_ORIENTATION_LANDSCAPE_RIGHT                           
- UIDEVICE_ORIENTATION_FACE_UP                           
- UIDEVICE_ORIENTATION_FACE_DOWN                                                   

Stopping the Orientation Sensor

To stop the orientation sensor, call maSensorStop(sensor) with SENSOR_TYPE_ORIENTATION as the parameter.

The function returns SENSOR_ERROR_NONE if the sensor stopped successfully, or SENSOR_ERROR_* in case of error.

Example

#include <MAUtil/Moblet.h>
#include <MAUtil/util.h>
#include <maapi.h>
#include <conprint.h>

extern "C" int MAMain()
{
    maSetColor(0xFF0000);
    maUpdateScreen();

    // start the orientation sensor
    maSensorStart(SENSOR_TYPE_ORIENTATION, 123);

    while (true)
    {
        MAEvent event;
        // listen for events
        while(maGetEvent(&event))
        {
            // check if it's a sensor type event
            if(EVENT_TYPE_SENSOR == event.type)
            {
                // check if it's a orientation sensor type event
                if (SENSOR_TYPE_ORIENTATION == event.sensor.type)
                {
                    char buffer[50];
                    maSetColor(0xFF0000);
                    sprintf(buffer, "orientation sensor value = %f",
                            event.sensor.values[0]);

                    maSetColor(0);
                    maFillRect(0, 0, 1000, 100);
                    maSetColor(0xFF0000);
                    maDrawText(10, 10, buffer);
                    maUpdateScreen();
                }
            }
        }
    }
};

SensorTest Example Application

Our SensorTest example application implements all of the sensor types supported by the MoSync Sensor API. When it is run on a device, it checks for the presence of each type of sensor and shows you the current event data being received from those that it finds.

MoSync SDK 3.3
Copyright © 2013 MoSync AB
www.mosync.com