sensor.cpp 4.6 KB
Newer Older
B
bernard 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * File      : sensors.cpp
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2014, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2014-08-03     Bernard      the first version
 */

B
Bernard Xiong 已提交
15
#include <stddef.h>
16 17
#include <string.h>

B
Bernard Xiong 已提交
18 19
#include "sensor.h"

B
Bernard Xiong 已提交
20
/**
21
 * SensorBase
B
Bernard Xiong 已提交
22
 */
23
SensorBase::SensorBase(int type)
B
Bernard Xiong 已提交
24
{
25
    memset(&(this->config), 0x0, sizeof(SensorConfig));
26

27
    this->type = type;
B
Bernard Xiong 已提交
28
    this->next = this->prev = NULL;
29
    subscribe(NULL, NULL);
B
Bernard Xiong 已提交
30 31
}

32
SensorBase::~SensorBase()
B
Bernard Xiong 已提交
33 34 35
{
}

36
int SensorBase::getType(void)
B
Bernard Xiong 已提交
37 38 39 40
{
    return this->type;
}

41 42
int SensorBase::setConfig(SensorConfig *config)
{
43
    int result;
44

45 46 47 48 49 50
    /* configure to the low level sensor */
    result = this->configure(config);
    if (result == 0)
    {
        this->config = *config;
    }
51

52
    return result;
53 54 55 56
}

int SensorBase::getConfig(SensorConfig *config)
{
57 58
    *config = this->config;
    return 0;
59 60 61
}

int SensorBase::subscribe(SensorEventHandler_t *handler, void *user_data)
B
Bernard Xiong 已提交
62 63 64 65 66 67 68
{
    this->evtHandler = handler;
    this->userData = user_data;

    return 0;
}

69
int SensorBase::publish(sensors_event_t *event)
B
Bernard Xiong 已提交
70
{
B
Bernard Xiong 已提交
71 72 73
    if (this->evtHandler != NULL)
    {
        /* invoke subscribed handler */
B
bernard 已提交
74
        (*evtHandler)(event, this->userData);
B
Bernard Xiong 已提交
75
    }
B
Bernard Xiong 已提交
76 77 78 79 80 81 82

    return 0;
}

/**
 * Sensor Manager
 */
83 84
/* sensors list */
static SensorBase *sensor_list = NULL;
B
Bernard Xiong 已提交
85 86 87 88 89 90 91 92 93

SensorManager::SensorManager()
{
}

SensorManager::~SensorManager()
{
}

94
int SensorManager::registerSensor(SensorBase *sensor)
B
Bernard Xiong 已提交
95 96 97
{
    RT_ASSERT(sensor != RT_NULL);

B
Bernard Xiong 已提交
98
    /* add sensor into the list */
99
    if (sensor_list == NULL)
B
Bernard Xiong 已提交
100 101 102 103 104
    {
        sensor->prev = sensor->next = sensor;
    }
    else
    {
105 106
        sensor_list->prev->next = sensor;
        sensor->prev = sensor_list->prev;
B
Bernard Xiong 已提交
107

108 109
        sensor_list->prev = sensor;
        sensor->next = sensor_list;
B
Bernard Xiong 已提交
110
    }
B
Bernard Xiong 已提交
111

B
Bernard Xiong 已提交
112
    /* point the sensorList to this sensor */
113
    sensor_list = sensor;
B
Bernard Xiong 已提交
114

B
Bernard Xiong 已提交
115
    return 0;
B
Bernard Xiong 已提交
116 117
}

118
int SensorManager::unregisterSensor(SensorBase *sensor)
B
Bernard Xiong 已提交
119
{
B
Bernard Xiong 已提交
120 121 122
    /* disconnect sensor list */
    sensor->next->prev = sensor->prev;
    sensor->prev->next = sensor->next;
B
Bernard Xiong 已提交
123

B
Bernard Xiong 已提交
124
    /* check the sensorList */
125
    if (sensor == sensor_list)
B
Bernard Xiong 已提交
126
    {
127 128
        if (sensor->next == sensor) sensor_list = NULL; /* empty list */
        else sensor_list = sensor->next;
B
Bernard Xiong 已提交
129
    }
B
Bernard Xiong 已提交
130

B
Bernard Xiong 已提交
131 132
    /* re-initialize sensor node */
    sensor->next = sensor->prev = sensor;
B
Bernard Xiong 已提交
133 134 135 136

    return 0;
}

137
SensorBase *SensorManager::getDefaultSensor(int type)
B
Bernard Xiong 已提交
138
{
139
    SensorBase *sensor = sensor_list;
B
Bernard Xiong 已提交
140

B
Bernard Xiong 已提交
141
    if (sensor == NULL) return NULL;
B
Bernard Xiong 已提交
142

B
Bernard Xiong 已提交
143 144 145
    do
    {
        /* find the same type */
146
        if (sensor->getType() == type) return sensor;
B
Bernard Xiong 已提交
147

B
Bernard Xiong 已提交
148
        sensor = sensor->next;
149
    }while (sensor != sensor_list);
B
Bernard Xiong 已提交
150 151 152 153

    return NULL;
}

154
int SensorManager::subscribe(int type, SensorEventHandler_t *handler, void *user_data)
B
Bernard Xiong 已提交
155
{
156
    SensorBase *sensor;
B
Bernard Xiong 已提交
157

158
    sensor = SensorManager::getDefaultSensor(type);
B
Bernard Xiong 已提交
159 160
    if (sensor != NULL)
    {
161
        sensor->subscribe(handler, user_data);
B
Bernard Xiong 已提交
162 163
        return 0;
    }
B
Bernard Xiong 已提交
164 165 166 167

    return -1;
}

168 169
int SensorManager::sensorEventReady(SensorBase *sensor)
{
170
    return 0;
171 172 173 174
}

int SensorManager::pollSensor(SensorBase *sensor, sensors_event_t *events, int number, int duration)
{
175 176
    rt_tick_t tick;
    int result, index;
177

178
    if (sensor == NULL) return -1;
179

180 181 182 183 184
    tick = rt_tick_get();
    for (index = 0; index < number; index ++)
    {
        result = sensor->poll(&events[index]);
        if (result < 0) break;
185

186 187
        if (rt_tick_get() - tick > duration) break;
    }
188

189
    return index;
190 191
}

B
bernard 已提交
192
rt_sensor_t rt_sensor_get_default(int type)
B
bernard 已提交
193
{
B
bernard 已提交
194
    return (rt_sensor_t)SensorManager::getDefaultSensor(type);
B
bernard 已提交
195 196
}

B
bernard 已提交
197
int rt_sensor_subscribe(rt_sensor_t sensor, SensorEventHandler_t *handler, void *user_data)
B
bernard 已提交
198
{
B
bernard 已提交
199 200 201 202 203 204
    SensorBase *sensor_base;
    if (sensor == NULL) return -1;

    sensor_base = (SensorBase*)sensor;

    return sensor_base->subscribe(handler, user_data);
B
bernard 已提交
205 206 207 208 209
}

int rt_sensor_poll(rt_sensor_t sensor, sensors_event_t *event)
{
    SensorBase *sensor_base;
210
    if (sensor == NULL || event == NULL) return -1;
B
bernard 已提交
211

B
bernard 已提交
212
    sensor_base = (SensorBase*)sensor;
213
    return sensor_base->poll(event);
B
bernard 已提交
214 215 216 217 218
}

int rt_sensor_configure(rt_sensor_t sensor, SensorConfig *config)
{
    SensorBase *sensor_base;
219
    if (sensor == NULL || config == NULL) return -1;
B
bernard 已提交
220 221

    sensor_base = (SensorBase*)sensor;
222
    return sensor_base->setConfig(config);
B
bernard 已提交
223 224 225 226 227
}

int rt_sensor_activate(rt_sensor_t sensor, int enable)
{
    SensorBase *sensor_base;
228 229
    if (sensor == NULL) return -1;
    
B
bernard 已提交
230
    sensor_base = (SensorBase*)sensor;
231
    return sensor_base->activate(enable);
B
bernard 已提交
232
}