|
00001 /*!@addtogroup other 00002 * @{ 00003 * @defgroup tmr Timer Library 00004 * Timer Library 00005 * @{ 00006 */ 00007 00008 /* 00009 * $Id: timer.h 133 2013-03-10 15:15:38Z xander $ 00010 */ 00011 00012 #ifndef __TMR_H__ 00013 #define __TMR_H__ 00014 /** \file timer.h 00015 * \brief Additional _timers for ROBOTC. 00016 * 00017 * timer.h provides additional timers for ROBOTC. Please note that there is no 00018 * roll-over checking done at all. That means that if the program runs for more than 00019 * around 596 hours, it will roll over and weird stuff will happen. 00020 * 00021 * The default number of timers is 10, but this can be changed by defining MAX_TIMERS 00022 * before this driver is included. 00023 * 00024 * License: You may use this code as you wish, provided you give credit where its due. 00025 * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. 00026 00027 * 00028 * Changelog: 00029 * - 0.1: Initial release 00030 * - 0.2: Removed task and cleaned up the code some more 00031 * - 0.3: Added hogCPU and releaseCPU calls at start and end of each critical call 00032 * 00033 * \author Xander Soldaat (xander_at_botbench.com) 00034 * \date 31 October 2010 00035 * \version 0.3 00036 * \example timer-test1.c 00037 */ 00038 00039 #pragma systemFile 00040 00041 #ifndef MAX_TIMERS 00042 #define MAX_TIMERS 10 /*!< Maximum number of _timers */ 00043 #endif 00044 00045 00046 /*!< Struct for timer data */ 00047 typedef struct { 00048 long startTime; 00049 long duration; 00050 } typeTMR; 00051 00052 typeTMR _timers[MAX_TIMERS]; /*!< Array to hold timer data */ 00053 00054 // Prototypes 00055 int TMRnewTimer(); 00056 bool TMRisExpired(int timerIdx); 00057 void TMRreset(int timerIdx); 00058 void TMRreset(int timerIdx, long duration); 00059 void TMRsetup(int timerIdx, long duration); 00060 00061 /** 00062 * Create a new timer. It's an index to the next available timer in the array of 00063 * timer structs. 00064 * @return the first available slot in the timer object array or -1 if all slots 00065 * have been used. Increase the MAX_TIMERS variable in this case. 00066 */ 00067 int TMRnewTimer() { 00068 static int _tmrIdx = -1; 00069 if (_tmrIdx < (MAX_TIMERS - 2)) 00070 return ++_tmrIdx; 00071 else 00072 return -1; 00073 } 00074 00075 00076 /** 00077 * Check if the timer has expired. 00078 * @param timerIdx the timer to be checked. 00079 * @return true if the timer has expired, false if it hasn't. 00080 */ 00081 bool TMRisExpired(int timerIdx) { 00082 hogCPU(); 00083 if (_timers[timerIdx].startTime < 0) { 00084 return true; 00085 } else { 00086 return (bool)((nPgmTime - _timers[timerIdx].startTime) > _timers[timerIdx].duration); 00087 } 00088 releaseCPU(); 00089 } 00090 00091 00092 /** 00093 * Reset the timer, will also mark "expired" flag as false.\n 00094 * This function will also check if the TMRtask is running and 00095 * start it up if this isn't the case. 00096 * @param timerIdx the timer to be checked. 00097 */ 00098 void TMRreset(int timerIdx) { 00099 hogCPU(); 00100 _timers[timerIdx].startTime = nPgmTime; 00101 releaseCPU(); 00102 } 00103 00104 00105 /** 00106 * Reset the timer, will also mark "expired" flag as false. 00107 * @param timerIdx the timer to be checked. 00108 * @param duration the amount of time the timer should run for before expiring. 00109 */ 00110 void TMRreset(int timerIdx, long duration) { 00111 hogCPU(); 00112 _timers[timerIdx].duration = duration; 00113 _timers[timerIdx].startTime = nPgmTime; 00114 releaseCPU(); 00115 } 00116 00117 00118 /** 00119 * Cause the timer to expire. 00120 * @param timerIdx the timer to be expired. 00121 */ 00122 void TMRexpire(int timerIdx) { 00123 hogCPU(); 00124 _timers[timerIdx].startTime = -1; 00125 releaseCPU(); 00126 } 00127 00128 00129 /** 00130 * Configure the duration of the timer. 00131 * @param timerIdx the timer to be checked. 00132 * @param duration the amount of time the timer should run for before expiring. 00133 */ 00134 void TMRsetup(int timerIdx, long duration) { 00135 hogCPU(); 00136 _timers[timerIdx].duration = duration; 00137 releaseCPU(); 00138 } 00139 00140 #endif // __TMR_H__ 00141 00142 /* 00143 * $Id: timer.h 133 2013-03-10 15:15:38Z xander $ 00144 */ 00145 /* @} */ 00146 /* @} */