1 |
606 |
jeremybenn |
/**
|
2 |
|
|
* \addtogroup timer
|
3 |
|
|
* @{
|
4 |
|
|
*/
|
5 |
|
|
|
6 |
|
|
/**
|
7 |
|
|
* \file
|
8 |
|
|
* Timer library implementation.
|
9 |
|
|
* \author
|
10 |
|
|
* Adam Dunkels <adam@sics.se>
|
11 |
|
|
*/
|
12 |
|
|
|
13 |
|
|
/*
|
14 |
|
|
* Copyright (c) 2004, 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>
|
44 |
|
|
*
|
45 |
|
|
* $Id: timer.c 2 2011-07-17 20:13:17Z filepang@gmail.com $
|
46 |
|
|
*/
|
47 |
|
|
|
48 |
|
|
//_RB_#include "contiki-conf.h"
|
49 |
|
|
#include "uip-conf.h"
|
50 |
|
|
#include "net/clock-arch.h"
|
51 |
|
|
#include "sys/clock.h"
|
52 |
|
|
#include "sys/timer.h"
|
53 |
|
|
|
54 |
|
|
/*---------------------------------------------------------------------------*/
|
55 |
|
|
|
56 |
|
|
/**
|
57 |
|
|
* Set a timer.
|
58 |
|
|
*
|
59 |
|
|
* This function is used to set a timer for a time sometime in the
|
60 |
|
|
* future. The function timer_expired() will evaluate to true after
|
61 |
|
|
* the timer has expired.
|
62 |
|
|
*
|
63 |
|
|
* \param t A pointer to the timer
|
64 |
|
|
* \param interval The interval before the timer expires.
|
65 |
|
|
*
|
66 |
|
|
*/
|
67 |
|
|
void timer_set( struct timer *t, clock_time_t interval )
|
68 |
|
|
{
|
69 |
|
|
t->interval = interval;
|
70 |
|
|
t->start = clock_time();
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
/*---------------------------------------------------------------------------*/
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* Reset the timer with the same interval.
|
77 |
|
|
*
|
78 |
|
|
* This function resets the timer with the same interval that was
|
79 |
|
|
* given to the timer_set() function. The start point of the interval
|
80 |
|
|
* is the exact time that the timer last expired. Therefore, this
|
81 |
|
|
* function will cause the timer to be stable over time, unlike the
|
82 |
|
|
* timer_restart() function.
|
83 |
|
|
*
|
84 |
|
|
* \param t A pointer to the timer.
|
85 |
|
|
*
|
86 |
|
|
* \sa timer_restart()
|
87 |
|
|
*/
|
88 |
|
|
void timer_reset( struct timer *t )
|
89 |
|
|
{
|
90 |
|
|
t->start += t->interval;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
/*---------------------------------------------------------------------------*/
|
94 |
|
|
|
95 |
|
|
/**
|
96 |
|
|
* Restart the timer from the current point in time
|
97 |
|
|
*
|
98 |
|
|
* This function restarts a timer with the same interval that was
|
99 |
|
|
* given to the timer_set() function. The timer will start at the
|
100 |
|
|
* current time.
|
101 |
|
|
*
|
102 |
|
|
* \note A periodic timer will drift if this function is used to reset
|
103 |
|
|
* it. For preioric timers, use the timer_reset() function instead.
|
104 |
|
|
*
|
105 |
|
|
* \param t A pointer to the timer.
|
106 |
|
|
*
|
107 |
|
|
* \sa timer_reset()
|
108 |
|
|
*/
|
109 |
|
|
void timer_restart( struct timer *t )
|
110 |
|
|
{
|
111 |
|
|
t->start = clock_time();
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
/*---------------------------------------------------------------------------*/
|
115 |
|
|
|
116 |
|
|
/**
|
117 |
|
|
* Check if a timer has expired.
|
118 |
|
|
*
|
119 |
|
|
* This function tests if a timer has expired and returns true or
|
120 |
|
|
* false depending on its status.
|
121 |
|
|
*
|
122 |
|
|
* \param t A pointer to the timer
|
123 |
|
|
*
|
124 |
|
|
* \return Non-zero if the timer has expired, zero otherwise.
|
125 |
|
|
*
|
126 |
|
|
*/
|
127 |
|
|
int timer_expired( struct timer *t )
|
128 |
|
|
{
|
129 |
|
|
return( clock_time_t ) ( clock_time() - t->start ) >= ( clock_time_t ) t->interval;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
/*---------------------------------------------------------------------------*/
|
133 |
|
|
|
134 |
|
|
/**
|
135 |
|
|
* The time until the timer expires
|
136 |
|
|
*
|
137 |
|
|
* This function returns the time until the timer expires.
|
138 |
|
|
*
|
139 |
|
|
* \param t A pointer to the timer
|
140 |
|
|
*
|
141 |
|
|
* \return The time until the timer expires
|
142 |
|
|
*
|
143 |
|
|
*/
|
144 |
|
|
clock_time_t timer_remaining( struct timer *t )
|
145 |
|
|
{
|
146 |
|
|
return t->start + t->interval - clock_time();
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
/*---------------------------------------------------------------------------*/
|
150 |
|
|
|
151 |
|
|
/** @} */
|