00001 /* ============================================================================= 00002 00003 Copyright (c) 2008 Pieter Conradie [www.piconomic.co.za] 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions are met: 00008 00009 * Redistributions of source code must retain the above copyright 00010 notice, this list of conditions and the following disclaimer. 00011 00012 * Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in 00014 the documentation and/or other materials provided with the 00015 distribution. 00016 00017 * Neither the name of the copyright holders nor the names of 00018 contributors may be used to endorse or promote products derived 00019 from this software without specific prior written permission. 00020 00021 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00022 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00024 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00025 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00026 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00027 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00030 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00031 POSSIBILITY OF SUCH DAMAGE. 00032 00033 Title: Polled software timers 00034 Author(s): Pieter Conradie 00035 Creation Date: 2008/02/11 00036 Revision Info: $Id: tmr.c 116 2010-06-20 21:52:14Z pieterconradie $ 00037 00038 ============================================================================= */ 00039 00040 /* _____PROJECT INCLUDES_____________________________________________________ */ 00041 #include "tmr.h" 00042 00043 /* _____DEFINITIONS _________________________________________________________ */ 00044 00045 /* _____MACROS_______________________________________________________________ */ 00046 00047 /* _____GLOBAL FUNCTIONS_____________________________________________________ */ 00048 void tmr_start(tmr_t *tmr, const tmr_ticks_t delay_in_ticks) 00049 { 00050 // Save delay in case timer is restarted 00051 tmr->delay_in_ticks = delay_in_ticks; 00052 00053 // Store start tick 00054 tmr->start_tick = systmr_get_counter(); 00055 00056 // Set state to indicate that timer has started 00057 tmr->state = TMR_STARTED; 00058 } 00059 00060 bool_t tmr_has_expired(tmr_t* tmr) 00061 { 00062 tmr_ticks_t tick; 00063 00064 // See if timer has been stopped 00065 if (tmr->state == TMR_STOPPED) return FALSE; 00066 00067 // See if timer has already expired 00068 if (tmr->state == TMR_EXPIRED) return TRUE; 00069 00070 // Fetch current time 00071 tick = systmr_get_counter(); 00072 00073 // Timer expire test 00074 if( (tick - tmr->start_tick) < tmr->delay_in_ticks ) 00075 { 00076 return FALSE; 00077 } 00078 00079 // Set state to indicate that timer has expired 00080 tmr->state = TMR_EXPIRED; 00081 00082 return TRUE; 00083 } 00084 00085 void tmr_stop(tmr_t *tmr) 00086 { 00087 // Stop timer 00088 tmr->state = TMR_STOPPED; 00089 } 00090 00091 void tmr_restart(tmr_t *tmr) 00092 { 00093 // Store start tick 00094 tmr->start_tick = systmr_get_counter(); 00095 00096 // Set state to indicate that timer has started 00097 tmr->state = TMR_STARTED; 00098 } 00099 00100 void tmr_reset(tmr_t *tmr) 00101 { 00102 // Calculate and store new start tick 00103 tmr->start_tick += tmr->delay_in_ticks; 00104 00105 // Set state to indicate that timer has started 00106 tmr->state = TMR_STARTED; 00107 } 00108 00109 void tmr_wait(const tmr_ticks_t delay_in_ticks) 00110 { 00111 tmr_t tmr; 00112 00113 tmr_start(&tmr, delay_in_ticks); 00114 00115 while (!tmr_has_expired(&tmr)) 00116 { 00117 ; 00118 } 00119 } 00120 tmr_ticks_t tmr_ticks_elapsed(tmr_t *tmr) 00121 { 00122 // Fetch current time 00123 tmr_ticks_t tick = systmr_get_counter(); 00124 00125 return (tick - tmr->start_tick); 00126 } 00127 00128 /* _____LOG__________________________________________________________________ */ 00129 /* 00130 00131 2008-02-11 : Pieter.Conradie 00132 - Creation 00133 00134 2010-04-21 : Pieter.Conradie 00135 - Moved from "tmr_glue.h" to "systmr.h" 00136 - Added tmr_ticks_elapsed(...) 00137 00138 */
1.6.3