Add C programming language APIs.

This commit is contained in:
bernard 2015-01-04 12:24:46 +08:00
parent 58163f49e0
commit 46bca6f84a
2 changed files with 130 additions and 72 deletions

View File

@ -71,7 +71,7 @@ int SensorBase::publish(sensors_event_t *event)
if (this->evtHandler != NULL)
{
/* invoke subscribed handler */
(*evtHandler)(this, event, this->userData);
(*evtHandler)(event, this->userData);
}
return 0;
@ -189,3 +189,39 @@ int SensorManager::pollSensor(SensorBase *sensor, sensors_event_t *events, int n
return index;
}
int rt_sensor_subscribe(int type, SensorEventHandler_t *handler, void *user_data)
{
return SensorManager::subscribe(type, handler, user_data);
}
rt_sensor_t rt_sensor_get_default(int type)
{
return (rt_sensor_t)SensorManager::getDefaultSensor(type);
}
int rt_sensor_poll(rt_sensor_t sensor, sensors_event_t *event)
{
SensorBase *sensor_base;
if (sensor == NULL || event == NULL) return -1;
return sensor_base->poll(event);
}
int rt_sensor_configure(rt_sensor_t sensor, SensorConfig *config)
{
SensorBase *sensor_base;
if (sensor == NULL || config == NULL) return -1;
sensor_base = (SensorBase*)sensor;
return sensor_base->setConfig(config);
}
int rt_sensor_activate(rt_sensor_t sensor, int enable)
{
SensorBase *sensor_base;
if (sensor == NULL) return -1;
sensor_base = (SensorBase*)sensor;
return sensor_base->activate(enable);
}

View File

@ -36,7 +36,9 @@
#include <rtdevice.h>
#include <stdint.h>
#ifdef __CC_ARM /* skip warning in armcc */
#pragma anon_unions
#endif
/**
* Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
@ -918,7 +920,6 @@ typedef sensors_event_t sensors_meta_data_event_t;
typedef struct sensor_t
{
/* Name of this sensor.
* All sensors of the same "type" must have a different "name".
*/
@ -1028,10 +1029,6 @@ enum SensorDataRate
SENSOR_DATARATE_0_10HZ,
};
class SensorBase;
class SensorManager;
typedef void (*SensorEventHandler_t)(SensorBase *sensor, sensors_event_t *event, void *user_data);
/**
* Sensor Configuration
*/
@ -1048,6 +1045,12 @@ typedef struct SensorConfig
} range;
}SensorConfig;
typedef void (*SensorEventHandler_t)(sensors_event_t *event, void *user_data);
#ifdef __cplusplus
class SensorBase;
class SensorManager;
/**
* Sensor Base Class
*/
@ -1105,6 +1108,25 @@ public:
static int sensorEventReady(SensorBase *sensor);
static int pollSensor(SensorBase *sensor, sensors_event_t *events, int number, int duration);
};
#endif
/* C programming language APIs */
/* rt_sensor_t is a C typedef for SensorBase */
typedef void* rt_sensor_t;
#ifdef __cplusplus
extern "C" {
#endif
rt_sensor_t rt_sensor_get_default(int type);
int rt_sensor_subscribe(rt_sensor_t sensor, SensorEventHandler_t *handler, void *user_data);
int rt_sensor_activate (rt_sensor_t sensor, int enable);
int rt_sensor_configure(rt_sensor_t sensor, SensorConfig *config);
int rt_sensor_poll(rt_sensor_t sensor, sensors_event_t *event);
#ifdef __cplusplus
}
#endif
#endif