sensors/custom_driver/main.c

See examples/sensors/custom_driver

/*
* 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 thermometer sensor driver to the 'sensor' library.
*
* The sensor library provides a standard interface to sensors.
*
* Note the main.c file is the EXACT same as the apps/sensors/thermometer app.
* The underlying hardware drivers may change but the sensor library API remains the same.
* This allows for easily adding new hardware without restructuring the software.
*
* The custom driver functionality is implemented in:
* - drivers/thermo123/thermo123.c : the dummy hardware driver
* - drivers/thermo123/sensor_api.c : the hardware driver sensor library interface
* - drivers/thermo123/thermo123.mk : the driver library specification
*
* The custom driver is included to the app's build process by adding:
*
* $(NAME)_COMPONENTS := drivers/thermo123
*
* To the project makefile: custom_driver.mk
*
*/
#include "zos.h"
#include "sensor.h"
#include "thermometer.h"
#define UPDATE_PERIOD_MS 1000
static void read_data(void *args);
/*************************************************************************************************/
void zn_app_init(void)
{
ZOS_LOG("Starting Custom Thermometer Driver App");
if (sensor_init(SENSOR_THERMOMETER, NULL) != ZOS_SUCCESS)
{
ZOS_LOG("ERROR - Failed to initialise sensor!");
}
else
{
ZOS_LOG("Initialisation successful!");
zn_event_register_periodic(read_data, NULL, UPDATE_PERIOD_MS, EVENT_FLAGS1(RUN_NOW));
}
}
/*************************************************************************************************/
void zn_app_deinit(void)
{
ZOS_LOG("De-initializing app");
// Be sure to cleanup when the app is done!!
}
/*************************************************************************************************/
zos_bool_t zn_app_idle(void)
{
// return true so the event loop sleeps
return ZOS_TRUE;
}
/*************************************************************************************************/
void read_data(void *args)
{
zos_bool_t has_data = ZOS_FALSE;
ZOS_LOG("Getting data....");
if (sensor_has_new_data(SENSOR_THERMOMETER, &has_data) == ZOS_SUCCESS)
{
if (has_data == ZOS_TRUE)
{
thermometer_data_t data;
if (sensor_get_data(SENSOR_THERMOMETER, &data) == ZOS_SUCCESS)
{
ZOS_LOG("Temperature: %d.%d deg C", data.value.whole, data.value.fraction);
}
else
{
ZOS_LOG("Failed to get data!");
}
}
else
{
ZOS_LOG("No new data!");
}
}
else
{
ZOS_LOG("Failed to check for new data!");
}
}