Contiki-Inga 3.x
Main Page
Related Pages
Modules
Data Structures
Files
Examples
File List
Globals
core
sys
stimer.c
Go to the documentation of this file.
1
/**
2
* \addtogroup stimer
3
* @{
4
*/
5
6
/**
7
* \file
8
* Timer of seconds library implementation.
9
* \author
10
* Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
11
*/
12
13
/*
14
* Copyright (c) 2004, 2008, Swedish Institute of Computer Science.
15
* All rights reserved.
16
*
17
* Redistribution and use in source and binary forms, with or without
18
* modification, are permitted provided that the following conditions
19
* are met:
20
* 1. Redistributions of source code must retain the above copyright
21
* notice, this list of conditions and the following disclaimer.
22
* 2. Redistributions in binary form must reproduce the above copyright
23
* notice, this list of conditions and the following disclaimer in the
24
* documentation and/or other materials provided with the distribution.
25
* 3. Neither the name of the Institute nor the names of its contributors
26
* may be used to endorse or promote products derived from this software
27
* without specific prior written permission.
28
*
29
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
30
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
33
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39
* SUCH DAMAGE.
40
*
41
* This file is part of the Contiki operating system.
42
*
43
* Author: Adam Dunkels <adam@sics.se>, Nicolas Tsiftes <nvt@sics.se>
44
*
45
*/
46
47
#include "contiki-conf.h"
48
#include "sys/clock.h"
49
#include "
sys/stimer.h
"
50
51
#define SCLOCK_GEQ(a, b) ((unsigned long)((a) - (b)) < \
52
((unsigned long)(~((unsigned long)0)) >> 1))
53
54
/*---------------------------------------------------------------------------*/
55
/**
56
* Set a timer.
57
*
58
* This function is used to set a timer for a time sometime in the
59
* future. The function stimer_expired() will evaluate to true after
60
* the timer has expired.
61
*
62
* \param t A pointer to the timer
63
* \param interval The interval before the timer expires.
64
*
65
*/
66
void
67
stimer_set
(
struct
stimer
*t,
unsigned
long
interval)
68
{
69
t->interval = interval;
70
t->start =
clock_seconds
();
71
}
72
/*---------------------------------------------------------------------------*/
73
/**
74
* Reset the timer with the same interval.
75
*
76
* This function resets the timer with the same interval that was
77
* given to the stimer_set() function. The start point of the interval
78
* is the exact time that the timer last expired. Therefore, this
79
* function will cause the timer to be stable over time, unlike the
80
* stimer_restart() function.
81
*
82
* \param t A pointer to the timer.
83
*
84
* \sa stimer_restart()
85
*/
86
void
87
stimer_reset
(
struct
stimer
*t)
88
{
89
t->start += t->interval;
90
}
91
/*---------------------------------------------------------------------------*/
92
/**
93
* Restart the timer from the current point in time
94
*
95
* This function restarts a timer with the same interval that was
96
* given to the stimer_set() function. The timer will start at the
97
* current time.
98
*
99
* \note A periodic timer will drift if this function is used to reset
100
* it. For preioric timers, use the stimer_reset() function instead.
101
*
102
* \param t A pointer to the timer.
103
*
104
* \sa stimer_reset()
105
*/
106
void
107
stimer_restart
(
struct
stimer
*t)
108
{
109
t->start =
clock_seconds
();
110
}
111
/*---------------------------------------------------------------------------*/
112
/**
113
* Check if a timer has expired.
114
*
115
* This function tests if a timer has expired and returns true or
116
* false depending on its status.
117
*
118
* \param t A pointer to the timer
119
*
120
* \return Non-zero if the timer has expired, zero otherwise.
121
*
122
*/
123
int
124
stimer_expired
(
struct
stimer
*t)
125
{
126
return
SCLOCK_GEQ(
clock_seconds
(), t->start + t->interval);
127
}
128
/*---------------------------------------------------------------------------*/
129
/**
130
* The time until the timer expires
131
*
132
* This function returns the time until the timer expires.
133
*
134
* \param t A pointer to the timer
135
*
136
* \return The time until the timer expires
137
*
138
*/
139
unsigned
long
140
stimer_remaining
(
struct
stimer
*t)
141
{
142
return
t->start + t->interval -
clock_seconds
();
143
}
144
/*---------------------------------------------------------------------------*/
145
/**
146
* The time elapsed since the timer started
147
*
148
* This function returns the time elapsed.
149
*
150
* \param t A pointer to the timer
151
*
152
* \return The time elapsed since the last start of the timer
153
*
154
*/
155
unsigned
long
156
stimer_elapsed
(
struct
stimer
*t)
157
{
158
return
clock_seconds
() - t->start;
159
}
160
161
/*---------------------------------------------------------------------------*/
162
163
/** @} */
Generated on Thu Apr 24 2014 16:26:13 for Contiki-Inga 3.x by
1.8.3.1