# Timer
[Core]

The Timer subsystem provides a timer in a millisecond timeframe. More...

## Functions
int | Timer_SetActive (bool active)
| Controls the active state of the Timer subsystem.
int | Timer_GetActive ()
| Returns whether the timer subsystem is active or not.
void | Timer_InitializeEntry (TimerEntry *timerEntry, void(*timerCallback)(int id), int id, int timeMs, bool repeat)
| Initializes a new timer structure.
int | Timer_Set (TimerEntry *timerEntry)
| Sets a given TimerEntry to run.
int | Timer_Cancel (TimerEntry *timerEntry)
| Cancel a timer event.

## Detailed Description

The Timer subsystem provides a timer in a millisecond timeframe.

For higher resolution timing, check the Fast Timer

The Timer subsystem is based on a collection of TimerEntries. To start a new timer, create a new TimerEntry structure, initialize it with Timer_InitializeEntry( ), and start it with Timer_Set( ).

There are currently a couple of limitations to the Timer system:

- In your callback function, you must not sleep or make any FreeRTOS-related calls.
- To modify an existing TimerEntry, you must cancel the timer with Timer_Cancel( ), and reinitialize the TimerEntry.

Todo:
  Allow the timer callbacks to cooperate with the RTOS

  Allow existing timer entries to be modified (repeat or not, modify the period, etc.)

## Function Documentation

int Timer_Cancel | ( | TimerEntry * | timerEntry | ) |

Cancel a timer event.

Parameters:

  | timerEntry | The entry to be removed.

Returns:
  0 on success.

See also:
  Timer_Set

Definition at line 218 of file timer.c.

int Timer_GetActive | ( | void |  | ) |

Returns whether the timer subsystem is active or not.

Returns:
  active.

See also:
  Timer_Set, Timer_Cancel

Definition at line 97 of file timer.c.

void Timer_InitializeEntry | ( | TimerEntry * | timerEntry,
|  | void(*)(int id) | timerCallback,
|  | int | id,
|  | int | timeMs,
|  | bool | repeat |
| ) |  |  |

Initializes a new timer structure.

The event is signified by a callback to the function provided, after the interval specified. The specified ID is passed back to the function to permit one function to work for many events. Pass repeat = true to make the event continue to create callbacks until it is canceled.

Parameters:

  | timerEntry | pointer to the TimerEntry to be intialized.
  | timerCallback | pointer to the callback function. The function must be of the form
      void timerCallback( int id )
  | id | An integer specifying the ID the callback function is to be provided with.
  | timeMs | The time in milliseconds desired for the callback.
  | repeat | Set whether the timer repeats or is a one-time event.

See also:
  Timer_Cancel

Example

        TimerEntry myTimer; // our TimerEntry
        Timer_InitializeEntry( &myTimer, myCallback, 0, 250, true );
        Timer_Set( &myTimer ); // start our timer

        void myCallback( int id ) // our code that will get called by the timer every 250 ms.
        {
          // do something here
        }

Definition at line 127 of file timer.c.

int Timer_Set | ( | TimerEntry * | timerEntry | ) |

Sets a given TimerEntry to run.

This routine adds the entry to the running queue and then decides if it needs to start the timer (if it's not running) or alter the timer's clock for a shorter period.

Parameters:

  | timerEntry | pointer to the FastTimerEntry to be run.

Definition at line 147 of file timer.c.

int Timer_SetActive | ( | bool | active | ) |

Controls the active state of the Timer subsystem.

Parameters:

  | active | whether this subsystem is active or not

Returns:
  Zero on success.

See also:
  Timer_Set, Timer_Cancel

Definition at line 68 of file timer.c.