adamdunkels | 0278435 | 2005-02-07 22:45:14 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \defgroup timer Timer library |
| 3 | * @{ |
| 4 | * |
| 5 | * The Contiki kernel does not provide support for timed |
| 6 | * events. Rather, an application that wants to use timers needs to |
| 7 | * explicitly use the timer library. |
| 8 | * |
| 9 | * The timer library provides functions for setting, resetting and |
| 10 | * restarting timers, and for checking if a timer has expired. An |
| 11 | * application must "manually" check if its timers have expired; this |
| 12 | * is not done automatically. |
| 13 | * |
| 14 | * A timer is declared as a \c struct \c timer and all access to the |
| 15 | * timer is made by a pointer to the declared timer. |
| 16 | * |
| 17 | * Example: |
| 18 | \code |
| 19 | |
| 20 | static struct timer periodic; |
| 21 | |
| 22 | LOADER_INIT_FUNC(example_init, arg) { |
| 23 | timer_set(&periodic, CLOCK_SECOND); |
| 24 | } |
| 25 | |
| 26 | EK_POLLHANDLER(pollhandler) { |
| 27 | if(timer_expired(&periodic)) { |
| 28 | timer_restart(&periodic); |
| 29 | do_periodic_stuff(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | \endcode |
| 34 | * |
| 35 | * \note The timer library uses the \ref clock "Clock library" to |
| 36 | * measure time. Intervals should be specified in the format used by |
| 37 | * the clock library. |
| 38 | * |
| 39 | * \sa \ref clock "Clock library" |
| 40 | */ |
| 41 | |
| 42 | /** |
| 43 | * \file |
| 44 | * Timer library implementation. |
| 45 | * \author |
| 46 | * Adam Dunkels <adam@sics.se> |
| 47 | */ |
| 48 | |
adamdunkels | a2f3c42 | 2004-09-12 20:24:53 +0000 | [diff] [blame] | 49 | /* |
| 50 | * Copyright (c) 2004, Swedish Institute of Computer Science. |
| 51 | * All rights reserved. |
| 52 | * |
| 53 | * Redistribution and use in source and binary forms, with or without |
| 54 | * modification, are permitted provided that the following conditions |
| 55 | * are met: |
| 56 | * 1. Redistributions of source code must retain the above copyright |
| 57 | * notice, this list of conditions and the following disclaimer. |
| 58 | * 2. Redistributions in binary form must reproduce the above copyright |
| 59 | * notice, this list of conditions and the following disclaimer in the |
| 60 | * documentation and/or other materials provided with the distribution. |
| 61 | * 3. Neither the name of the Institute nor the names of its contributors |
| 62 | * may be used to endorse or promote products derived from this software |
| 63 | * without specific prior written permission. |
| 64 | * |
| 65 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
| 66 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 67 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 68 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
| 69 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 70 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 71 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 72 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 73 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 74 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 75 | * SUCH DAMAGE. |
| 76 | * |
| 77 | * This file is part of the Contiki operating system. |
| 78 | * |
| 79 | * Author: Adam Dunkels <adam@sics.se> |
| 80 | * |
adamdunkels | 0278435 | 2005-02-07 22:45:14 +0000 | [diff] [blame] | 81 | * $Id: timer.c,v 1.5 2005/02/07 22:45:14 adamdunkels Exp $ |
adamdunkels | a2f3c42 | 2004-09-12 20:24:53 +0000 | [diff] [blame] | 82 | */ |
adamdunkels | d106ecc | 2004-06-06 06:09:31 +0000 | [diff] [blame] | 83 | #include "timer.h" |
| 84 | |
| 85 | /*---------------------------------------------------------------------------*/ |
adamdunkels | 0278435 | 2005-02-07 22:45:14 +0000 | [diff] [blame] | 86 | /** |
| 87 | * Set a timer. |
| 88 | * |
| 89 | * This function is used to set a timer for a time sometime in the |
| 90 | * future. The function timer_expired() will evaluate to true after |
| 91 | * the timer has expired. |
| 92 | * |
| 93 | * \param t A pointer to the timer |
| 94 | * \param interval The interval before the timer expires. |
| 95 | * |
| 96 | */ |
adamdunkels | d106ecc | 2004-06-06 06:09:31 +0000 | [diff] [blame] | 97 | void |
| 98 | timer_set(struct timer *t, clock_time_t interval) |
| 99 | { |
| 100 | t->interval = interval; |
adamdunkels | 123ab1c | 2004-09-01 19:05:21 +0000 | [diff] [blame] | 101 | t->start = clock_time(); |
adamdunkels | d106ecc | 2004-06-06 06:09:31 +0000 | [diff] [blame] | 102 | } |
| 103 | /*---------------------------------------------------------------------------*/ |
adamdunkels | 0278435 | 2005-02-07 22:45:14 +0000 | [diff] [blame] | 104 | /** |
| 105 | * Reset the timer with the same interval. |
| 106 | * |
| 107 | * This function resets the timer with the same interval that was |
| 108 | * given to the timer_set() function. The start point of the interval |
| 109 | * is the exact time that the timer last expired. Therefore, this |
| 110 | * function will cause the timer to be stable over time, unlike the |
| 111 | * timer_rester() function. |
| 112 | * |
| 113 | * \param t A pointer to the timer. |
| 114 | * |
| 115 | * \sa timer_restart() |
| 116 | */ |
adamdunkels | d106ecc | 2004-06-06 06:09:31 +0000 | [diff] [blame] | 117 | void |
| 118 | timer_reset(struct timer *t) |
| 119 | { |
adamdunkels | 0278435 | 2005-02-07 22:45:14 +0000 | [diff] [blame] | 120 | t->start += t->interval; |
| 121 | } |
| 122 | /*---------------------------------------------------------------------------*/ |
| 123 | /** |
| 124 | * Restart the timer from the current point in time |
| 125 | * |
| 126 | * This function restarts a timer with the same interval that was |
| 127 | * given to the timer_set() function. The timer will start at the |
| 128 | * current time. |
| 129 | * |
| 130 | * \note A periodic timer will drift if this function is used to reset |
| 131 | * it. For preioric timers, use the timer_reset() function instead. |
| 132 | * |
| 133 | * \param t A pointer to the timer. |
| 134 | * |
| 135 | * \sa timer_reset() |
| 136 | */ |
| 137 | void |
| 138 | timer_restart(struct timer *t) |
| 139 | { |
adamdunkels | 4c09222 | 2004-09-12 07:24:41 +0000 | [diff] [blame] | 140 | t->start = clock_time(); |
adamdunkels | d106ecc | 2004-06-06 06:09:31 +0000 | [diff] [blame] | 141 | } |
| 142 | /*---------------------------------------------------------------------------*/ |
adamdunkels | 0278435 | 2005-02-07 22:45:14 +0000 | [diff] [blame] | 143 | /** |
| 144 | * Check if a timer has expired. |
| 145 | * |
| 146 | * This function tests if a timer has expired and returns true or |
| 147 | * false depending on its status. |
| 148 | * |
| 149 | * \param t A pointer to the timer |
| 150 | * |
| 151 | * \return Non-zero if the timer has expired, zero otherwise. |
| 152 | * |
| 153 | */ |
adamdunkels | d106ecc | 2004-06-06 06:09:31 +0000 | [diff] [blame] | 154 | int |
| 155 | timer_expired(struct timer *t) |
| 156 | { |
adamdunkels | 123ab1c | 2004-09-01 19:05:21 +0000 | [diff] [blame] | 157 | return (clock_time_t)(clock_time() - t->start) >= (clock_time_t)t->interval; |
adamdunkels | d106ecc | 2004-06-06 06:09:31 +0000 | [diff] [blame] | 158 | } |
| 159 | /*---------------------------------------------------------------------------*/ |
| 160 | |