Soft timers in object-oriented C
Given Listing 2 (the interface) and Listing 1 (system tick service interrupt), we can show an example of how we implement the clock retrieve function in Listing 3.unsigned long sysRetrieveClock (void)
{
return (my_system_tick);
}
Listing 3. Access to the system tick hard timer variable
Yes! That is all there is to accessing this variable. All we need is the value of the updated variable. (Of course, we assume the fetch of
my_system_tick is atomic with respect to the hardware).
Soft timer operations
It is tempting to say operator. However, the term "operator" has a well-formed meaning in C++. So instead of operator, I can say operation. After all, in our code we shall instance a soft timer and do "operations" on it. The 1st operation is the initialization. The general usage for object oriented C is a two or three step process. The steps are as follows:
- Instantiate the object, (global, stack etc)
- Initialize the object, (initialize near point of use)
- Setup, (this step is optional and may be needed to sync the object with the hardware)
Soft timer init
This is where the soft timer is initialized. In general, we do this initialization to allow the 1st capture of the system tick variable, and the setup of the timeOut value. The code for the soft timer initialization is shown in Listing 4.
#define __GPTIMER_INIT_C
/****************************************************************************/
/* FILE: GPTIMER_init.c */
/* */
/* A general purpose C/C++ timer object */
/* */
/* BY: Ken Wada */
/* 12-January-2003 */
/* */
/****************************************************************************/
#include "h\gpTimer.h"
/****************************************************************************/
/* CODE STARTS HERE */
/****************************************************************************/
void GPTIMER_init (GPTIMER *_this, const unsigned long tics)
{
_this->timeOut = tics;
_this->lastCount = sysRetrieveClock();
_this->timedOut = 0;
}
Listing 4. How to initialize a soft timer object
Here we see the
sysRetrieveClock() interface that we defined earlier. We also show the timeOut member variable being initialized to the desired timeout in tics. We also show the timedOut control variable being set to zero. The timedOut variable is used to indicate whether or not the soft timer has indeed timed out.


Loading comments... Write a comment