提交 18cd9568 编写于 作者: B Bernard Xiong

[Drivers] Add sensor framework

上级 b84123b0
# SConscript for sensor framework
from building import *
cwd = GetCurrentDir()
src = Glob('*.c') + Glob('*.cpp')
CPPPATH = [cwd, cwd + '/../include']
group = DefineGroup('Sensors', src, depend = ['RT_USING_SENSOR', 'RT_USING_DEVICE'], CPPPATH = CPPPATH)
Return('group')
#include <stddef.h>
#include "sensor.h"
/**
* Sensor
*/
Sensor::Sensor()
{
this->next = this->prev = NULL;
Subscribe(NULL, NULL);
}
Sensor::~Sensor()
{
}
int Sensor::GetType(void)
{
return this->type;
}
int Sensor::Subscribe(SensorEventHandler_t *handler, void* user_data)
{
this->evtHandler = handler;
this->userData = user_data;
return 0;
}
int Sensor::Publish(sensors_event_t* event)
{
if (this->evtHandler != NULL)
{
/* invoke subscribed handler */
(*evtHandler)(this, event, this->userData);
}
return 0;
}
/**
* Sensor Manager
*/
/* sensor manager instance */
static SensorManager _sensor_manager;
SensorManager::SensorManager()
{
sensorList = NULL;
}
SensorManager::~SensorManager()
{
}
int SensorManager::RegisterSensor(Sensor* sensor)
{
SensorManager* self = &_sensor_manager;
RT_ASSERT(sensor != RT_NULL);
/* add sensor into the list */
if (self->sensorList = NULL)
{
sensor->prev = sensor->next = sensor;
}
else
{
sensor->prev = self->sensorList;
sensor->next = self->sensorList->next;
self->sensorList->next->prev = sensor;
self->sensorList->next = sensor;
}
/* point the sensorList to this sensor */
self->sensorList = sensor;
return 0;
}
int SensorManager::DeregisterSensor(Sensor* sensor)
{
SensorManager* self = &_sensor_manager;
/* disconnect sensor list */
sensor->next->prev = sensor->prev;
sensor->prev->next = sensor->next;
/* check the sensorList */
if (sensor == self->sensorList)
{
if (sensor->next == sensor) self->sensorList = NULL; /* empty list */
else self->sensorList = sensor->next;
}
/* re-initialize sensor node */
sensor->next = sensor->prev = sensor;
return 0;
}
Sensor *SensorManager::GetDefaultSensor(int type)
{
SensorManager* self = &_sensor_manager;
Sensor *sensor = self->sensorList;
if (sensor == NULL) return NULL;
do
{
/* find the same type */
if (sensor->GetType() == type) return sensor;
sensor = sensor->next;
}
while (sensor != self->sensorList);
return NULL;
}
int SensorManager::Subscribe(int type, SensorEventHandler_t *handler, void* user_data)
{
Sensor *sensor;
sensor = SensorManager::GetDefaultSensor(type);
if (sensor != NULL)
{
sensor->Subscribe(handler, user_data);
return 0;
}
return -1;
}
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册