Merge pull request #363 from BernardXiong/master

[Sensor] code cleanup
This commit is contained in:
Bernard Xiong 2014-11-01 15:54:00 +08:00
commit ecbde6bb9d
2 changed files with 112 additions and 98 deletions

View File

@ -1,13 +1,13 @@
#include <stddef.h>
#include "sensor.h"
/**
/**
* Sensor
*/
Sensor::Sensor()
{
this->next = this->prev = NULL;
Subscribe(NULL, NULL);
this->next = this->prev = NULL;
Subscribe(NULL, NULL);
}
Sensor::~Sensor()
@ -19,7 +19,7 @@ int Sensor::GetType(void)
return this->type;
}
int Sensor::Subscribe(SensorEventHandler_t *handler, void* user_data)
int Sensor::Subscribe(SensorEventHandler_t *handler, void *user_data)
{
this->evtHandler = handler;
this->userData = user_data;
@ -27,13 +27,13 @@ int Sensor::Subscribe(SensorEventHandler_t *handler, void* user_data)
return 0;
}
int Sensor::Publish(sensors_event_t* event)
int Sensor::Publish(sensors_event_t *event)
{
if (this->evtHandler != NULL)
{
/* invoke subscribed handler */
(*evtHandler)(this, event, this->userData);
}
if (this->evtHandler != NULL)
{
/* invoke subscribed handler */
(*evtHandler)(this, event, this->userData);
}
return 0;
}
@ -46,89 +46,89 @@ static SensorManager _sensor_manager;
SensorManager::SensorManager()
{
sensorList = NULL;
sensorList = NULL;
}
SensorManager::~SensorManager()
{
}
int SensorManager::RegisterSensor(Sensor* sensor)
int SensorManager::RegisterSensor(Sensor *sensor)
{
SensorManager* self = &_sensor_manager;
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;
/* 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;
}
self->sensorList->next->prev = sensor;
self->sensorList->next = sensor;
}
/* point the sensorList to this sensor */
self->sensorList = sensor;
/* point the sensorList to this sensor */
self->sensorList = sensor;
return 0;
return 0;
}
int SensorManager::DeregisterSensor(Sensor* sensor)
int SensorManager::DeregisterSensor(Sensor *sensor)
{
SensorManager* self = &_sensor_manager;
SensorManager *self = &_sensor_manager;
/* disconnect sensor list */
sensor->next->prev = sensor->prev;
sensor->prev->next = sensor->next;
/* 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;
}
/* 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;
/* re-initialize sensor node */
sensor->next = sensor->prev = sensor;
return 0;
}
Sensor *SensorManager::GetDefaultSensor(int type)
{
SensorManager* self = &_sensor_manager;
Sensor *sensor = self->sensorList;
SensorManager *self = &_sensor_manager;
Sensor *sensor = self->sensorList;
if (sensor == NULL) return NULL;
if (sensor == NULL) return NULL;
do
{
/* find the same type */
if (sensor->GetType() == type) return sensor;
do
{
/* find the same type */
if (sensor->GetType() == type) return sensor;
sensor = sensor->next;
}
while (sensor != self->sensorList);
sensor = sensor->next;
}
while (sensor != self->sensorList);
return NULL;
}
int SensorManager::Subscribe(int type, SensorEventHandler_t *handler, void* user_data)
int SensorManager::Subscribe(int type, SensorEventHandler_t *handler, void *user_data)
{
Sensor *sensor;
Sensor *sensor;
sensor = SensorManager::GetDefaultSensor(type);
if (sensor != NULL)
{
sensor->Subscribe(handler, user_data);
return 0;
}
sensor = SensorManager::GetDefaultSensor(type);
if (sensor != NULL)
{
sensor->Subscribe(handler, user_data);
return 0;
}
return -1;
}

View File

@ -55,7 +55,8 @@
* Availability: SENSORS_DEVICE_API_VERSION_1_0
* see (*batch)() documentation for details
*/
enum {
enum
{
SENSORS_BATCH_DRY_RUN = 0x00000001,
SENSORS_BATCH_WAKE_UPON_FIFO_FULL = 0x00000002
};
@ -63,7 +64,8 @@ enum {
/*
* what field for meta_data_event_t
*/
enum {
enum
{
/* a previous flush operation has completed */
META_DATA_FLUSH_COMPLETE = 1,
META_DATA_VERSION /* always last, leave auto-assigned */
@ -749,14 +751,17 @@ enum {
*/
typedef struct
{
union {
union
{
float v[3];
struct {
struct
{
float x;
float y;
float z;
};
struct {
struct
{
float azimuth;
float pitch;
float roll;
@ -771,22 +776,26 @@ typedef struct
*/
typedef struct
{
union {
float uncalib[3];
struct {
float x_uncalib;
float y_uncalib;
float z_uncalib;
union
{
float uncalib[3];
struct
{
float x_uncalib;
float y_uncalib;
float z_uncalib;
};
};
};
union {
float bias[3];
struct {
float x_bias;
float y_bias;
float z_bias;
union
{
float bias[3];
struct
{
float x_bias;
float y_bias;
float z_bias;
};
};
};
} uncalibrated_event_t;
typedef struct meta_data_event
@ -799,7 +808,8 @@ typedef struct meta_data_event
* Union of the various types of sensor data
* that can be returned.
*/
typedef struct sensors_event_t {
typedef struct sensors_event_t
{
/* must be sizeof(struct sensors_event_t) */
int32_t version;
@ -815,8 +825,10 @@ typedef struct sensors_event_t {
/* time is in nanosecond */
int64_t timestamp;
union {
union {
union
{
union
{
float data[16];
/* acceleration values are in meter per second per second (m/s^2) */
@ -859,7 +871,8 @@ typedef struct sensors_event_t {
meta_data_event_t meta_data;
};
union {
union
{
uint64_t data[8];
/* step-counter */
@ -872,15 +885,16 @@ typedef struct sensors_event_t {
/* see SENSOR_TYPE_META_DATA */
typedef sensors_event_t sensors_meta_data_event_t;
typedef struct sensor_t {
typedef struct sensor_t
{
/* Name of this sensor.
* All sensors of the same "type" must have a different "name".
*/
const char* name;
const char *name;
/* vendor of the hardware part */
const char* vendor;
const char *vendor;
/* version of the hardware part + driver. The value of this field
* must increase when the driver is updated in a way that changes the
@ -929,7 +943,7 @@ typedef struct sensor_t {
uint32_t fifoMaxEventCount;
/* reserved fields, must be zero */
void* reserved[6];
void *reserved[6];
} sensor_t;
class SensorConfigure
@ -939,7 +953,7 @@ class SensorConfigure
class Sensor;
class SensorManager;
typedef void (*SensorEventHandler_t)(Sensor* sensor, sensors_event_t* event, void* user_data);
typedef void (*SensorEventHandler_t)(Sensor *sensor, sensors_event_t *event, void *user_data);
/**
* Sensor Base Class
@ -961,17 +975,17 @@ public:
int GetType(void);
int Subscribe(SensorEventHandler_t *handler, void* user_data);
int Publish(sensors_event_t* event);
int Subscribe(SensorEventHandler_t *handler, void *user_data);
int Publish(sensors_event_t *event);
protected:
protected:
Sensor *next;
Sensor *prev;
Sensor *prev;
SensorEventHandler_t *evtHandler;
void *userData;
friend class SensorManager;
friend class SensorManager;
};
/**
@ -982,15 +996,15 @@ class SensorManager
public:
SensorManager();
~SensorManager();
static int RegisterSensor(Sensor* sensor);
static int DeregisterSensor(Sensor* sensor);
static int RegisterSensor(Sensor *sensor);
static int DeregisterSensor(Sensor *sensor);
static Sensor *GetDefaultSensor(int type);
static int Subscribe(int type, SensorEventHandler_t *handler, void* user_data);
static int Subscribe(int type, SensorEventHandler_t *handler, void *user_data);
private:
Sensor* sensorList;
Sensor *sensorList;
};
#endif