rt-thread-official/components/drivers/sensors/sensor.cpp

229 lines
4.4 KiB
C++
Raw Normal View History

2014-12-01 12:22:17 +08:00
/*
2018-10-14 19:37:18 +08:00
* Copyright (c) 2006-2018, RT-Thread Development Team
2014-12-01 12:22:17 +08:00
*
2018-10-14 19:37:18 +08:00
* SPDX-License-Identifier: Apache-2.0
2014-12-01 12:22:17 +08:00
*
* Change Logs:
* Date Author Notes
* 2014-08-03 Bernard the first version
*/
2014-11-01 09:09:52 +08:00
#include <stddef.h>
#include <string.h>
2014-11-01 09:09:52 +08:00
#include "sensor.h"
2014-11-01 15:52:25 +08:00
/**
* SensorBase
2014-11-01 09:09:52 +08:00
*/
SensorBase::SensorBase(int type)
2014-11-01 09:09:52 +08:00
{
memset(&(this->config), 0x0, sizeof(SensorConfig));
this->type = type;
2014-11-01 15:52:25 +08:00
this->next = this->prev = NULL;
subscribe(NULL, NULL);
2014-11-01 09:09:52 +08:00
}
SensorBase::~SensorBase()
2014-11-01 09:09:52 +08:00
{
}
int SensorBase::getType(void)
2014-11-01 09:09:52 +08:00
{
return this->type;
}
int SensorBase::setConfig(SensorConfig *config)
{
int result;
/* configure to the low level sensor */
result = this->configure(config);
if (result == 0)
{
this->config = *config;
}
return result;
}
int SensorBase::getConfig(SensorConfig *config)
{
*config = this->config;
return 0;
}
int SensorBase::subscribe(SensorEventHandler_t handler, void *user_data)
2014-11-01 09:09:52 +08:00
{
this->evtHandler = handler;
this->userData = user_data;
return 0;
}
int SensorBase::publish(void)
2014-11-01 09:09:52 +08:00
{
2014-11-01 15:52:25 +08:00
if (this->evtHandler != NULL)
{
/* invoke subscribed handler */
(*evtHandler)(this->userData);
2014-11-01 15:52:25 +08:00
}
2014-11-01 09:09:52 +08:00
return 0;
}
/**
* Sensor Manager
*/
/* sensors list */
static SensorBase *sensor_list = NULL;
2014-11-01 09:09:52 +08:00
SensorManager::SensorManager()
{
}
SensorManager::~SensorManager()
{
}
int SensorManager::registerSensor(SensorBase *sensor)
2014-11-01 09:09:52 +08:00
{
RT_ASSERT(sensor != RT_NULL);
2014-11-01 15:52:25 +08:00
/* add sensor into the list */
if (sensor_list == NULL)
2014-11-01 15:52:25 +08:00
{
sensor->prev = sensor->next = sensor;
}
else
{
sensor_list->prev->next = sensor;
sensor->prev = sensor_list->prev;
2014-11-01 09:09:52 +08:00
sensor_list->prev = sensor;
sensor->next = sensor_list;
2014-11-01 15:52:25 +08:00
}
2014-11-01 09:09:52 +08:00
2014-11-01 15:52:25 +08:00
/* point the sensorList to this sensor */
sensor_list = sensor;
2014-11-01 09:09:52 +08:00
2014-11-01 15:52:25 +08:00
return 0;
2014-11-01 09:09:52 +08:00
}
int SensorManager::unregisterSensor(SensorBase *sensor)
2014-11-01 09:09:52 +08:00
{
2014-11-01 15:52:25 +08:00
/* disconnect sensor list */
sensor->next->prev = sensor->prev;
sensor->prev->next = sensor->next;
2014-11-01 09:09:52 +08:00
2014-11-01 15:52:25 +08:00
/* check the sensorList */
if (sensor == sensor_list)
2014-11-01 15:52:25 +08:00
{
if (sensor->next == sensor) sensor_list = NULL; /* empty list */
else sensor_list = sensor->next;
2014-11-01 15:52:25 +08:00
}
2014-11-01 09:09:52 +08:00
2014-11-01 15:52:25 +08:00
/* re-initialize sensor node */
sensor->next = sensor->prev = sensor;
2014-11-01 09:09:52 +08:00
return 0;
}
SensorBase *SensorManager::getDefaultSensor(int type)
2014-11-01 09:09:52 +08:00
{
SensorBase *sensor = sensor_list;
2014-11-01 09:09:52 +08:00
2014-11-01 15:52:25 +08:00
if (sensor == NULL) return NULL;
2014-11-01 09:09:52 +08:00
2014-11-01 15:52:25 +08:00
do
{
/* find the same type */
if (sensor->getType() == type) return sensor;
2014-11-01 09:09:52 +08:00
2014-11-01 15:52:25 +08:00
sensor = sensor->next;
}while (sensor != sensor_list);
2014-11-01 09:09:52 +08:00
return NULL;
}
int SensorManager::subscribe(int type, SensorEventHandler_t handler, void *user_data)
2014-11-01 09:09:52 +08:00
{
SensorBase *sensor;
2014-11-01 15:52:25 +08:00
sensor = SensorManager::getDefaultSensor(type);
2014-11-01 15:52:25 +08:00
if (sensor != NULL)
{
sensor->subscribe(handler, user_data);
2014-11-01 15:52:25 +08:00
return 0;
}
2014-11-01 09:09:52 +08:00
return -1;
}
int SensorManager::sensorEventReady(SensorBase *sensor)
{
return 0;
}
int SensorManager::pollSensor(SensorBase *sensor, sensors_event_t *events, int number, int duration)
{
rt_tick_t tick;
int result, index;
if (sensor == NULL) return -1;
tick = rt_tick_get();
for (index = 0; index < number; index ++)
{
result = sensor->poll(&events[index]);
if (result < 0) break;
if (rt_tick_get() - tick > duration) break;
}
return index;
}
2015-01-04 20:01:52 +08:00
rt_sensor_t rt_sensor_get_default(int type)
2015-01-04 12:24:46 +08:00
{
2015-01-04 20:01:52 +08:00
return (rt_sensor_t)SensorManager::getDefaultSensor(type);
2015-01-04 12:24:46 +08:00
}
int rt_sensor_subscribe(rt_sensor_t sensor, SensorEventHandler_t handler, void *user_data)
2015-01-04 12:24:46 +08:00
{
2015-01-04 20:01:52 +08:00
SensorBase *sensor_base;
if (sensor == NULL) return -1;
sensor_base = (SensorBase*)sensor;
return sensor_base->subscribe(handler, user_data);
2015-01-04 12:24:46 +08:00
}
int rt_sensor_poll(rt_sensor_t sensor, sensors_event_t *event)
{
SensorBase *sensor_base;
if (sensor == NULL || event == NULL) return -1;
2015-01-04 12:24:46 +08:00
2015-01-04 20:01:52 +08:00
sensor_base = (SensorBase*)sensor;
return sensor_base->poll(event);
2015-01-04 12:24:46 +08:00
}
int rt_sensor_configure(rt_sensor_t sensor, SensorConfig *config)
{
SensorBase *sensor_base;
if (sensor == NULL || config == NULL) return -1;
2015-01-04 12:24:46 +08:00
sensor_base = (SensorBase*)sensor;
return sensor_base->setConfig(config);
2015-01-04 12:24:46 +08:00
}
int rt_sensor_activate(rt_sensor_t sensor, int enable)
{
SensorBase *sensor_base;
if (sensor == NULL) return -1;
2015-01-04 12:24:46 +08:00
sensor_base = (SensorBase*)sensor;
return sensor_base->activate(enable);
2015-01-04 12:24:46 +08:00
}