home *** CD-ROM | disk | FTP | other *** search
- -- Listing 5. A timer body for slow, non-critical
- -- applications.
-
- with CALENDAR;
- package body POLLED_TIMER is
-
- CALENDAR_TICK : float := 65_536.0 / 1_193_180.0;
- -- CALENDAR_TICK is system dependent. The value
- -- given is for the IBM PC AT.
- CALENDAR_MAX : float := 86_400.0;
-
- TIMER_PERIOD : Duration;
- TIMER_MODE : Modes;
- START_TIME, DONE : CALENDAR.Time;
- TIME_ALREADY_USED : Duration;
- STOPPED : boolean;
-
- procedure Set(PERIOD : Seconds;
- MODE : Modes) is
- begin
- if Dimensionless(PERIOD)
- > CALENDAR_MAX
- then raise
- INVALID_PERIOD;
- end if;
- if Dimensionless(PERIOD)
- < CALENDAR_TICK
- then raise
- INVALID_PERIOD;
- end if;
- TIMER_PERIOD := Duration(
- Dimensionless(PERIOD));
- TIME_ALREADY_USED := 0.0;
- STOPPED := TRUE;
- TIMER_MODE := MODE;
- end Set;
-
- procedure Start is
- use CALENDAR; -- for "+" and "-"
- begin
- START_TIME := CALENDAR.Clock;
- DONE := START_TIME + TIMER_PERIOD
- - TIME_ALREADY_USED;
- STOPPED := FALSE;
- end Start;
-
- procedure Restart is
- begin
- TIME_ALREADY_USED := 0.0;
- Start;
- end Restart;
-
- function Has_Expired return boolean is
- use CALENDAR; -- for ">=" and "+"
- begin
- if Dimensionless(Time_Left) > 0.0 then
- return FALSE;
- else
- case TIMER_MODE is
- when SINGLE =>
- Stop;
- return TRUE;
- when REPEATED =>
- TIME_ALREADY_USED := CALENDAR.Clock - DONE;
- Start;
- return TRUE;
- end case;
- end if;
- end Has_Expired;
-
- procedure Stop is
- use CALENDAR; -- for "-"
- begin
- TIME_ALREADY_USED := CALENDAR.Clock
- - START_TIME;
- STOPPED := TRUE;
- end Stop;
-
- function Time_Used return Seconds is
- TIME : float;
- use CALENDAR; -- for "-"
- begin
- if STOPPED then
- TIME := float(TIME_ALREADY_USED);
- else
- TIME := float(TIMER_PERIOD
- - (DONE - CALENDAR.Clock));
- end if;
- return Type_Convert(TIME);
- end Time_Used;
-
- function Time_Left return Seconds is
- DIFFERENCE : float;
- use CALENDAR; -- for "-"
- begin
- if STOPPED then
- DIFFERENCE := float(TIMER_PERIOD - TIME_ALREADY_USED);
- else
- DIFFERENCE := float(DONE - CALENDAR.Clock);
- end if;
- return Type_Convert(DIFFERENCE);
- end Time_Left;
-
- function Max_Period return Seconds is
- begin
- return Type_Convert(CALENDAR_MAX);
- end Max_Period;
-
- function Single_Tick return Seconds is
- begin
- return Type_Convert(CALENDAR_TICK);
- end Single_Tick;
-
- end POLLED_TIMER;
-