00001 /** 00002 * \addtogroup timer 00003 * @{ 00004 */ 00005 00006 /** 00007 * \file 00008 * Timer library implementation. 00009 * \author 00010 * Adam Dunkels <adam@sics.se> 00011 */ 00012 00013 /* 00014 * Copyright (c) 2004, Swedish Institute of Computer Science. 00015 * All rights reserved. 00016 * 00017 * Redistribution and use in source and binary forms, with or without 00018 * modification, are permitted provided that the following conditions 00019 * are met: 00020 * 1. Redistributions of source code must retain the above copyright 00021 * notice, this list of conditions and the following disclaimer. 00022 * 2. Redistributions in binary form must reproduce the above copyright 00023 * notice, this list of conditions and the following disclaimer in the 00024 * documentation and/or other materials provided with the distribution. 00025 * 3. Neither the name of the Institute nor the names of its contributors 00026 * may be used to endorse or promote products derived from this software 00027 * without specific prior written permission. 00028 * 00029 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 00030 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00031 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00032 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 00033 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00034 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00035 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00036 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00037 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00038 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00039 * SUCH DAMAGE. 00040 * 00041 * This file is part of the Contiki operating system. 00042 * 00043 * Author: Adam Dunkels <adam@sics.se> 00044 * 00045 * $Id: timer.c,v 1.8 2010/06/21 19:07:24 joxe Exp $ 00046 */ 00047 00048 #include "contiki-conf.h" 00049 #include "sys/clock.h" 00050 #include "sys/timer.h" 00051 00052 /*---------------------------------------------------------------------------*/ 00053 /** 00054 * Set a timer. 00055 * 00056 * This function is used to set a timer for a time sometime in the 00057 * future. The function timer_expired() will evaluate to true after 00058 * the timer has expired. 00059 * 00060 * \param t A pointer to the timer 00061 * \param interval The interval before the timer expires. 00062 * 00063 */ 00064 void 00065 timer_set(struct timer *t, clock_time_t interval) 00066 { 00067 t->interval = interval; 00068 t->start = clock_time(); 00069 } 00070 /*---------------------------------------------------------------------------*/ 00071 /** 00072 * Reset the timer with the same interval. 00073 * 00074 * This function resets the timer with the same interval that was 00075 * given to the timer_set() function. The start point of the interval 00076 * is the exact time that the timer last expired. Therefore, this 00077 * function will cause the timer to be stable over time, unlike the 00078 * timer_restart() function. 00079 * 00080 * \param t A pointer to the timer. 00081 * 00082 * \sa timer_restart() 00083 */ 00084 void 00085 timer_reset(struct timer *t) 00086 { 00087 t->start += t->interval; 00088 } 00089 /*---------------------------------------------------------------------------*/ 00090 /** 00091 * Restart the timer from the current point in time 00092 * 00093 * This function restarts a timer with the same interval that was 00094 * given to the timer_set() function. The timer will start at the 00095 * current time. 00096 * 00097 * \note A periodic timer will drift if this function is used to reset 00098 * it. For preioric timers, use the timer_reset() function instead. 00099 * 00100 * \param t A pointer to the timer. 00101 * 00102 * \sa timer_reset() 00103 */ 00104 void 00105 timer_restart(struct timer *t) 00106 { 00107 t->start = clock_time(); 00108 } 00109 /*---------------------------------------------------------------------------*/ 00110 /** 00111 * Check if a timer has expired. 00112 * 00113 * This function tests if a timer has expired and returns true or 00114 * false depending on its status. 00115 * 00116 * \param t A pointer to the timer 00117 * 00118 * \return Non-zero if the timer has expired, zero otherwise. 00119 * 00120 */ 00121 int 00122 timer_expired(struct timer *t) 00123 { 00124 /* Note: Can not return diff >= t->interval so we add 1 to diff and return 00125 t->interval < diff - required to avoid an internal error in mspgcc. */ 00126 clock_time_t diff = (clock_time() - t->start) + 1; 00127 return t->interval < diff; 00128 00129 } 00130 /*---------------------------------------------------------------------------*/ 00131 /** 00132 * The time until the timer expires 00133 * 00134 * This function returns the time until the timer expires. 00135 * 00136 * \param t A pointer to the timer 00137 * 00138 * \return The time until the timer expires 00139 * 00140 */ 00141 clock_time_t 00142 timer_remaining(struct timer *t) 00143 { 00144 return t->start + t->interval - clock_time(); 00145 } 00146 /*---------------------------------------------------------------------------*/ 00147 00148 /** @} */