Mindstorms 3rd Party ROBOTC Drivers RobotC
[Home] [Download] [Submit a bug/suggestion] [ROBOTC Forums] [Blog] [Support this project]

TMR-driver.h

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