Skip to main content

Sensors

The Sensors API provides a unified interface for discovering, configuring, and retrieving data from various hardware sensors in Ocre containers. It supports multiple sensor types and channels, allowing applications to interact with environmental, motion, and position sensing capabilities.


Header File

To use the Sensors API in your application, include the ocre_api.h header from the Atym Toolchain, and link the ocre-sdk library when building your application.

#include "ocre_api.h"
Container Permissions

When using the sensors API in your application, make sure to include the ocre_sensors permission in your build.yaml configuration file. This enables build-time validation to ensure your application only uses the sensor functions you've declared.

config:
permissions:
- ocre_sensors

Execution Environment

Container applications shall use ocre_process_events() function to process their sensor events. This is usually done in a loop, similar to following:

    // Main event loop - process events continuously
while (1) {
ocre_process_events();
}

Types

Sensor Handle

A type that represents a sensor device identifier.

typedef int32_t ocre_sensor_handle_t;

Sensor Channels

An enumeration of the different types of sensor channels available.

typedef enum {
SENSOR_CHANNEL_ACCELERATION,
SENSOR_CHANNEL_GYRO,
SENSOR_CHANNEL_MAGNETIC_FIELD,
SENSOR_CHANNEL_LIGHT,
SENSOR_CHANNEL_PRESSURE,
SENSOR_CHANNEL_PROXIMITY,
SENSOR_CHANNEL_HUMIDITY,
SENSOR_CHANNEL_TEMPERATURE,
// Add more channels as needed
} sensor_channel_t;

Sensor Instance

A structure representing a sensor instance with its channels.

typedef struct ocre_sensor_t {
ocre_sensor_handle_t handle; ///< Sensor handle
char *sensor_name; ///< Name of the sensor
int num_channels; ///< Number of supported channels
sensor_channel_t channels[]; ///< Supported channels
} ocre_sensor_t;

Methods

Initialize Sensors

Initializes the sensor subsystem.

int ocre_sensors_init(void);

Returns:

ValueDescription
0Initialization successful
negative valueError code

Discover Sensors

Discovers available sensors on the device.

int ocre_sensors_discover(void);

Returns:

ValueDescription
positive valueNumber of sensors discovered
negative valueError code

Open Sensor

Opens a sensor by handle for reading.

int ocre_sensors_open(ocre_sensor_handle_t handle);

Parameters:

NameTypeDescription
handleocre_sensor_handle_tSensor handle

Returns:

ValueDescription
0Sensor opened successfully
negative valueError code

Open Sensor by Name

Opens a sensor by name for reading.

int ocre_sensors_open_by_name(const char *name);

Parameters:

NameTypeDescription
nameconst char*Name of the sensor

Returns:

ValueDescription
OCRE_SUCCESSSensor opened successfully
negative valueError code

Get Sensor Handle

Gets the handle for a sensor by ID.

int ocre_sensors_get_handle(int sensor_id);

Parameters:

NameTypeDescription
sensor_idintSensor ID

Returns:

ValueDescription
positive valueSensor handle
negative valueError code

Get Sensor Handle by Name

Gets the handle for a sensor by name.

int ocre_sensors_get_handle_by_name(const char *name);

Parameters:

NameTypeDescription
nameconst char*Name of the sensor

Returns:

ValueDescription
positive valueSensor handle
negative valueError code

Get Channel Count

Gets the number of channels for a sensor by ID.

int ocre_sensors_get_channel_count(int sensor_id);

Parameters:

NameTypeDescription
sensor_idintSensor ID

Returns:

ValueDescription
positive valueNumber of channels
negative valueError code

Get Channel Count by Name

Gets the number of channels for a sensor by name.

int ocre_sensors_get_channel_count_by_name(const char *sensor_name);

Parameters:

NameTypeDescription
sensor_nameconst char*Name of the sensor

Returns:

ValueDescription
positive valueNumber of channels
negative valueError code

Get Channel Type

Gets the channel type for a specific channel index.

int ocre_sensors_get_channel_type(int sensor_id, int channel_index);

Parameters:

NameTypeDescription
sensor_idintSensor ID
channel_indexintChannel index

Returns:

ValueDescription
positive valueChannel type from sensor_channel_t enum
negative valueError code

Get Channel Type by Name

Gets the channel type for a specific channel index by sensor name.

int ocre_sensors_get_channel_type_by_name(const char *sensor_name, int channel_index);

Parameters:

NameTypeDescription
sensor_nameconst char*Name of the sensor
channel_indexintChannel index

Returns:

ValueDescription
positive valueChannel type from sensor_channel_t enum
negative valueError code

Read Sensor

Reads data from a sensor channel.

double ocre_sensors_read(int sensor_id, int channel_type);

Parameters:

NameTypeDescription
sensor_idintSensor ID
channel_typeintChannel type to read (from sensor_channel_t enum)

Returns:

ValueDescription
sensor valueSensor reading as double
negative valueError code

Read Sensor by Name

Reads data from a sensor channel by sensor name.

double ocre_sensors_read_by_name(const char *sensor_name, int channel_type);

Parameters:

NameTypeDescription
sensor_nameconst char*Name of the sensor
channel_typeintChannel type to read (from sensor_channel_t enum)

Returns:

ValueDescription
sensor valueSensor reading as double
negative valueError code

Reference

FunctionDescriptionParametersReturn ValueError Conditions
ocre_sensors_initInitializes the sensor subsystemNone0: Success
Negative: Error
System initialization failure
ocre_sensors_discoverDiscovers available sensorsNonePositive: Number found
Negative: Error
No sensors available
Discovery failure
ocre_sensors_openOpens a sensor by handlehandle: Sensor handle0: Success
Negative: Error
Invalid handle
Sensor already open
ocre_sensors_open_by_nameOpens a sensor by namename: Sensor nameOCRE_SUCCESS: Success
Negative: Error
Invalid name
Sensor not found
ocre_sensors_get_handleGets handle by sensor IDsensor_id: Sensor IDPositive: Handle
Negative: Error
Invalid sensor ID
ocre_sensors_get_handle_by_nameGets handle by sensor namename: Sensor namePositive: Handle
Negative: Error
Sensor name not found
ocre_sensors_get_channel_countGets number of channels by IDsensor_id: Sensor IDPositive: Channel count
Negative: Error
Invalid sensor ID
ocre_sensors_get_channel_count_by_nameGets number of channels by namesensor_name: Sensor namePositive: Channel count
Negative: Error
Sensor name not found
ocre_sensors_get_channel_typeGets channel type by IDsensor_id: Sensor ID
channel_index: Channel index
Positive: Channel type
Negative: Error
Invalid sensor ID
Invalid channel index
ocre_sensors_get_channel_type_by_nameGets channel type by namesensor_name: Sensor name
channel_index: Channel index
Positive: Channel type
Negative: Error
Sensor name not found
Invalid channel index
ocre_sensors_readReads sensor data by IDsensor_id: Sensor ID
channel_type: Channel type
Sensor value (double)
Negative: Error
Invalid sensor ID
Invalid channel type
Read failure
ocre_sensors_read_by_nameReads sensor data by namesensor_name: Sensor name
channel_type: Channel type
Sensor value (double)
Negative: Error
Sensor name not found
Invalid channel type
Read failure