sensor.cpp 2.8 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 16 17
#include <stddef.h>
#include "sensor.h"

B
Bernard Xiong 已提交
18
/**
B
Bernard Xiong 已提交
19 20 21 22
 * Sensor
 */
Sensor::Sensor()
{
B
Bernard Xiong 已提交
23 24
    this->next = this->prev = NULL;
    Subscribe(NULL, NULL);
B
Bernard Xiong 已提交
25 26 27 28 29 30 31 32 33 34 35
}

Sensor::~Sensor()
{
}

int Sensor::GetType(void)
{
    return this->type;
}

B
Bernard Xiong 已提交
36
int Sensor::Subscribe(SensorEventHandler_t *handler, void *user_data)
B
Bernard Xiong 已提交
37 38 39 40 41 42 43
{
    this->evtHandler = handler;
    this->userData = user_data;

    return 0;
}

B
Bernard Xiong 已提交
44
int Sensor::Publish(sensors_event_t *event)
B
Bernard Xiong 已提交
45
{
B
Bernard Xiong 已提交
46 47 48 49 50
    if (this->evtHandler != NULL)
    {
        /* invoke subscribed handler */
        (*evtHandler)(this, event, this->userData);
    }
B
Bernard Xiong 已提交
51 52 53 54 55 56 57 58 59 60 61 62

    return 0;
}

/**
 * Sensor Manager
 */
/* sensor manager instance */
static SensorManager _sensor_manager;

SensorManager::SensorManager()
{
B
Bernard Xiong 已提交
63
    sensorList = NULL;
B
Bernard Xiong 已提交
64 65 66 67 68 69
}

SensorManager::~SensorManager()
{
}

B
Bernard Xiong 已提交
70
int SensorManager::RegisterSensor(Sensor *sensor)
B
Bernard Xiong 已提交
71
{
B
Bernard Xiong 已提交
72 73
    SensorManager *self = &_sensor_manager;

B
Bernard Xiong 已提交
74 75
    RT_ASSERT(sensor != RT_NULL);

B
Bernard Xiong 已提交
76
    /* add sensor into the list */
B
bernard 已提交
77
    if (self->sensorList == NULL)
B
Bernard Xiong 已提交
78 79 80 81 82 83 84
    {
        sensor->prev = sensor->next = sensor;
    }
    else
    {
        sensor->prev = self->sensorList;
        sensor->next = self->sensorList->next;
B
Bernard Xiong 已提交
85

B
Bernard Xiong 已提交
86 87 88
        self->sensorList->next->prev = sensor;
        self->sensorList->next = sensor;
    }
B
Bernard Xiong 已提交
89

B
Bernard Xiong 已提交
90 91
    /* point the sensorList to this sensor */
    self->sensorList = sensor;
B
Bernard Xiong 已提交
92

B
Bernard Xiong 已提交
93
    return 0;
B
Bernard Xiong 已提交
94 95
}

B
Bernard Xiong 已提交
96
int SensorManager::DeregisterSensor(Sensor *sensor)
B
Bernard Xiong 已提交
97
{
B
Bernard Xiong 已提交
98
    SensorManager *self = &_sensor_manager;
B
Bernard Xiong 已提交
99

B
Bernard Xiong 已提交
100 101 102
    /* disconnect sensor list */
    sensor->next->prev = sensor->prev;
    sensor->prev->next = sensor->next;
B
Bernard Xiong 已提交
103

B
Bernard Xiong 已提交
104 105 106 107 108 109
    /* check the sensorList */
    if (sensor == self->sensorList)
    {
        if (sensor->next == sensor) self->sensorList = NULL; /* empty list */
        else self->sensorList = sensor->next;
    }
B
Bernard Xiong 已提交
110

B
Bernard Xiong 已提交
111 112
    /* re-initialize sensor node */
    sensor->next = sensor->prev = sensor;
B
Bernard Xiong 已提交
113 114 115 116 117 118

    return 0;
}

Sensor *SensorManager::GetDefaultSensor(int type)
{
B
Bernard Xiong 已提交
119 120
    SensorManager *self = &_sensor_manager;
    Sensor *sensor = self->sensorList;
B
Bernard Xiong 已提交
121

B
Bernard Xiong 已提交
122
    if (sensor == NULL) return NULL;
B
Bernard Xiong 已提交
123

B
Bernard Xiong 已提交
124 125 126 127
    do
    {
        /* find the same type */
        if (sensor->GetType() == type) return sensor;
B
Bernard Xiong 已提交
128

B
Bernard Xiong 已提交
129
        sensor = sensor->next;
B
bernard 已提交
130
    }while (sensor != self->sensorList);
B
Bernard Xiong 已提交
131 132 133 134

    return NULL;
}

B
Bernard Xiong 已提交
135
int SensorManager::Subscribe(int type, SensorEventHandler_t *handler, void *user_data)
B
Bernard Xiong 已提交
136
{
B
Bernard Xiong 已提交
137 138 139 140 141 142 143 144
    Sensor *sensor;

    sensor = SensorManager::GetDefaultSensor(type);
    if (sensor != NULL)
    {
        sensor->Subscribe(handler, user_data);
        return 0;
    }
B
Bernard Xiong 已提交
145 146 147 148

    return -1;
}