|
Author: Anh Vo, Santa Clara, California
Abstract: Ada Gem #15 — Timers are essential software elements of embedded and real-time systems. Thus, making an intuitive and easy way to create timers helps in the software design process.
Timers are essential software elements of embedded and real-time systems. Thus, making an intuitive and easy way to create timers helps in the software design process. Ada provides a predefined package named Ada.Real_Time.Timing_Events for doing just that. With this package, a timer, one-shot or periodic one, can be created with a breeze. Furthermore, these timers can be genericized for different durations. Going one step further these generic timers can be combined in a single generic package as shown below along with the test codes. Note that the test codes are pretty crude. However, the output is printed out for each second approximately. Thus, the test results can be checked using one thousand count rule, one thousand one, one thousand two…
Here is an example for creating a 250 millisecond one-shot timer.
with Generic_Timers;
with Ada.Real_Time;
with Ada.Text_Io;
--...
declare
use Ada;
Span : constant Time_Span := Real_Time.Milliseconds (250);
Timer_Id : constant String := "250 Millisecond One Shot timer";
procedure Timer_Handler is
begin
Put_Line ("Do whatever is necessary when the timer expires");
end Timer_Handler;
package The_Timer is new Generic_Timers (True, Timer_Id, Span, Timer_Hander); --timer object
begin
--...
The_Timer.Start;
--...
end;
--...
Other one-shot timers and periodic timers for different durations are in the test package Timers_Test. Note that at my current project creating a timer takes five to ten times longer and much less intuitive. On this note, enjoy it with a smile.
Ada Gems example files are distributed by AdaCore and may be used or modified for any purpose without restrictions.
|
6.3Kb |
Posted
in Ada / Ada 2005, Development Log, Devt log - Gem of the Week
If you have an idea for a Gem you would like to contribute please feel free to contact us at: gems@adacore.com
Anh Vo said:
After reading my own gem again, I have noticed two typos. The first one is in the procedure Timer_Handler. The string should read Do whatever is necessary when the timer expires (extra is). The second typo is on first with clause where an s was missing. It should read with Generic_Timers.
Thomas Quinot said:
Thanks Anh, we have updated the gem text.
Marc said:
I keep getting compile warning with the code given above (the other example works like a breeze): “non local pointer can not point to local object”.
And then there is a typo in the elaboration of package The_Timer, the third argument should be Time_Handler not Time_Hander.
Otherwise a real gem. I’d never have known. Thanks!