summaryrefslogtreecommitdiff
path: root/sensors-service/src/gyroscope.c
blob: fa1b2b119b5fbf2c0ddc59a39fce27bd4f90f38c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**************************************************************************
* @licence app begin@
*
* SPDX-License-Identifier: MPL-2.0
*
* \ingroup SensorsService
* \author Marco Residori <marco.residori@xse.de>
*
* \copyright Copyright (C) 2013, XS Embedded GmbH
* 
* \license
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
* this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* @licence end@
**************************************************************************/

#include "globals.h"
#include "gyroscope.h"
#include "sns-meta-data.h"

static pthread_mutex_t mutexCb  = PTHREAD_MUTEX_INITIALIZER;   //protects the callbacks
static pthread_mutex_t mutexData = PTHREAD_MUTEX_INITIALIZER;  //protects the data

static volatile GyroscopeCallback cbGyroscope = 0;
static TGyroscopeData gGyroscopeData;
TGyroscopeConfiguration gGyroscopeConfiguration;

bool iGyroscopeInit()
{
    pthread_mutex_lock(&mutexCb);
    cbGyroscope = 0;
    pthread_mutex_unlock(&mutexCb);

    pthread_mutex_lock(&mutexData);
    //example gyroscope configuration for a 3-axis gyro
    gGyroscopeConfiguration.angleYaw = 0;
    gGyroscopeConfiguration.anglePitch = 0;
    gGyroscopeConfiguration.angleRoll = 0;
    gGyroscopeConfiguration.momentOfYawInertia = 0;
    gGyroscopeConfiguration.sigmaGyroscope = 0;
    gGyroscopeConfiguration.typeBits = 
        GYROSCOPE_YAWRATE_PROVIDED |
        GYROSCOPE_PITCHRATE_PROVIDED |
        GYROSCOPE_ROLLRATE_PROVIDED;
    gGyroscopeConfiguration.validityBits = 
      GYROSCOPE_CONFIG_ANGLEYAW_VALID | 
      GYROSCOPE_CONFIG_ANGLEPITCH_VALID |
      GYROSCOPE_CONFIG_ANGLEROLL_VALID |
      GYROSCOPE_CONFIG_TYPE_VALID;
    pthread_mutex_unlock(&mutexData);

    return true;
}

bool iGyroscopeDestroy()
{
    pthread_mutex_lock(&mutexCb);
    cbGyroscope = 0;
    pthread_mutex_unlock(&mutexCb);

    return true;
}

bool snsGyroscopeGetGyroscopeData(TGyroscopeData * gyroData)
{
    bool retval = false;
    if(gyroData)
    {
        pthread_mutex_lock(&mutexData);
        *gyroData = gGyroscopeData;
        pthread_mutex_unlock(&mutexData);
        retval = true;
    }
    return retval;    
}

bool snsGyroscopeRegisterCallback(GyroscopeCallback callback)
{
    bool retval = false;

    //only if valid callback and not already registered
    if(callback && !cbGyroscope)
    {
        pthread_mutex_lock(&mutexCb);
        cbGyroscope = callback;
        pthread_mutex_unlock(&mutexCb);
        retval = true;
    }
    return retval;
}

bool snsGyroscopeDeregisterCallback(GyroscopeCallback callback)
{
    bool retval = false;

    if((cbGyroscope == callback) && callback)
    {
        pthread_mutex_lock(&mutexCb);
        cbGyroscope = 0;
        pthread_mutex_unlock(&mutexCb);
        retval = true;
    }

    return retval;
}

bool snsGyroscopeGetMetaData(TSensorMetaData *data)
{
    bool retval = false;    
    
    if(data) 
    {
        pthread_mutex_lock(&mutexData);
        *data = gSensorsMetaData[1];
        pthread_mutex_unlock(&mutexData);
        retval = true;
    }

    return retval;
}

bool snsGyroscopeGetConfiguration(TGyroscopeConfiguration* gyroConfig)
{
    bool retval = false; 
    if(gyroConfig) 
    {
        pthread_mutex_lock(&mutexData);
        *gyroConfig = gGyroscopeConfiguration;
        pthread_mutex_unlock(&mutexData);
        retval = true;
    }

    return retval;
}

void updateGyroscopeData(const TGyroscopeData gyroData[], uint16_t numElements)
{
    if (gyroData != NULL && numElements > 0)
    {
        pthread_mutex_lock(&mutexData);
        gGyroscopeData = gyroData[numElements-1];
        pthread_mutex_unlock(&mutexData);
        pthread_mutex_lock(&mutexCb);
        if (cbGyroscope)
        {
            cbGyroscope(gyroData, numElements);
        }
        pthread_mutex_unlock(&mutexCb);
    }
}