summaryrefslogtreecommitdiff
path: root/sensors-service/src/wheeltick.c
blob: 99422ea8dcf4a696cdb68e856f81f81d8a8ed775 (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
/**************************************************************************
* @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 "wheel.h"

WheeltickCallback cbWheelticks = 0;
TWheelticks gWheelticks;

bool snsWheeltickInit()
{
    int i;

    pthread_mutex_lock(&mutexCb);
    cbWheelticks = 0;
    pthread_mutex_unlock(&mutexCb);

    pthread_mutex_lock(&mutexData);
    for(i = 0; i < WHEEL_NUM_ELEMENTS; i++)
    {
        gWheelticks.elements[i].wheeltickCounter = 0;
        gWheelticks.elements[i].wheeltickIdentifier = WHEEL_INVALID;
    }
    pthread_mutex_unlock(&mutexData);

    return true;
}

bool snsWheeltickDestroy()
{
    pthread_mutex_lock(&mutexCb);
    cbWheelticks = 0;
    pthread_mutex_unlock(&mutexCb);

    return true;
}

bool snsWheeltickGetWheelticks(TWheelticks * ticks)
{
    if(!ticks)
    {
        return false;
    }

    pthread_mutex_lock(&mutexData);
    *ticks = gWheelticks;
    pthread_mutex_unlock(&mutexData);

    return true;
}

bool snsWheeltickRegisterCallback(WheeltickCallback callback)
{
    if(!callback)
    {
        return false;
    }

    //printf("snsWheeltickRegisterCallback\n");
    pthread_mutex_lock(&mutexCb);
    if(cbWheelticks != 0) 
    {
        //already registered
        pthread_mutex_unlock(&mutexCb);
        return false; 
    }
    cbWheelticks = callback;
    pthread_mutex_unlock(&mutexCb);

    return true;
}

bool snsWheeltickDeregisterCallback(WheeltickCallback callback)
{
    if(!callback)
    {
        return false;
    }

    //printf("snsWheeltickDeregisterCallback\n");
    pthread_mutex_lock(&mutexCb);
    if(cbWheelticks == callback)
    { 
        cbWheelticks = 0;      
    }
    pthread_mutex_unlock(&mutexCb);  

    return true;
}