sensors/custom_sensors/main.c

See examples/sensors/custom_sensors

/*
* ZentriOS SDK LICENSE AGREEMENT | Zentri.com, 2015.
*
* Use of source code and/or libraries contained in the ZentriOS SDK is
* subject to the Zentri Operating System SDK license agreement and
* applicable open source license agreements.
*
*/
/*
* This app demonstrates adding a custom sensors to the 'sensor' library.
*
* The sensor library provides a standard interface to sensors.
*
* The custom sensors are implemented as standalone libraries
* in the speedometer and tachometer directories.
*
* This app uses the custom sensors library as follows:
* 1. In the project .mk file: custom_sensors.mk, add:
*
* GLOBAL_DEFINES := SENSOR_LIB_USER_SENSORS
*
* This tells the sensor library that custom sensors are used.
*
* 2. Reference the custom sensor libraries in the project .mk:
*
* $(NAME)_COMPONENTS := tachometer \
speedometer
*
* This tells the build system to use the libraries.
*
*
* 3. Create a header files with the name: user_sensors.h
* - This includes the sensor library headers
* - Specifies the sensor 'ids'
* - And registers the sensors with the sensor library
*
*
* 4. Allow for custom_sensors.h to be found by the sensor library
* by configuring the global includes path in the project .mk:
*
* GLOBAL_INCLUDES := .
*
* This adds the current application directory to the includes path. In this
* way the sensor library can include user_sensors.h
*
*
* 5. Specify the sensor drivers to use by configuring the project .mk:
*
* GLOBAL_DEFINES := DRIVER_TACHOMETER_TACHO123 \
DRIVER_SPEEDOMETER_SPEEDO123
*
* This tells the build system to use the specified sensor drivers.
*
*
*/
#include "zos.h"
#include "sensor.h"
#include "tachometer.h"
#include "speedometer.h"
#define UPDATE_PERIOD_MS 5000
static void read_tachometer_data(void *args);
static void read_speedometer_data(void *args);
/*************************************************************************************************/
void zn_app_init(void)
{
ZOS_LOG("Starting Custom Sensors App");
const tachometer_config_t tacho_config =
{
.max_rpm = 100000,
.idle_rpm = 600
};
if (sensor_init(SENSOR_TACHOMETER, &tacho_config) != ZOS_SUCCESS)
{
ZOS_LOG("ERROR - Failed to initialise tachometer!");
}
else if (sensor_init(SENSOR_SPEEDOMETER, NULL) != ZOS_SUCCESS)
{
ZOS_LOG("ERROR - Failed to initialise speedometer!");
}
else
{
ZOS_LOG("Initialisation successful!");
zn_event_register_periodic(read_tachometer_data, NULL, UPDATE_PERIOD_MS, EVENT_FLAGS1(RUN_NOW));
zn_event_register_periodic(read_speedometer_data, NULL, UPDATE_PERIOD_MS, EVENT_FLAGS1(RUN_NOW));
}
}
/*************************************************************************************************/
void zn_app_deinit(void)
{
ZOS_LOG("De-initializing app");
}
/*************************************************************************************************/
zos_bool_t zn_app_idle(void)
{
// return true so the event loop sleeps
return ZOS_TRUE;
}
/*************************************************************************************************/
static void read_tachometer_data(void *args)
{
zos_bool_t has_data;
ZOS_LOG("\r\n-----------------------------");
ZOS_LOG("Reading tachometer ...");
if (sensor_has_new_data(SENSOR_TACHOMETER, &has_data) == ZOS_SUCCESS)
{
if (has_data == ZOS_TRUE)
{
tachometer_data_t data;
if (sensor_get_data(SENSOR_TACHOMETER, &data) == ZOS_SUCCESS)
{
ZOS_LOG("Average RPM: %u", data.average_rpm);
ZOS_LOG("Max RPM: %u", data.max_rpm);
ZOS_LOG("Current RPM: %u", data.current_rpm);
}
else
{
ZOS_LOG("Failed to get data!");
}
}
else
{
ZOS_LOG("No new data!");
}
}
else
{
ZOS_LOG("Failed to check for new data!");
}
}
/*************************************************************************************************/
static void read_speedometer_data(void *args)
{
zos_bool_t has_data;
ZOS_LOG("\r\n-----------------------------");
ZOS_LOG("Reading speedometer ...");
if (sensor_has_new_data(SENSOR_SPEEDOMETER, &has_data) == ZOS_SUCCESS)
{
if (has_data == ZOS_TRUE)
{
speedometer_data_t data;
if (sensor_get_data(SENSOR_SPEEDOMETER, &data) == ZOS_SUCCESS)
{
ZOS_LOG("Average speed: %u", data.average_speed);
ZOS_LOG("Max speed: %u", data.max_speed);
ZOS_LOG("Current speed: %u", data.current_speed);
}
else
{
ZOS_LOG("Failed to get data!");
}
}
else
{
ZOS_LOG("No new data!");
}
}
else
{
ZOS_LOG("Failed to check for new data!");
}
}