KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DEOS > PeriodicClock


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package DEOS;
20
21 import gov.nasa.jpf.jvm.Verify;
22
23
24 /**
25  * DOCUMENT ME!
26  */

27 public class PeriodicClock {
28   int microSecsPeriod = 0;
29   int usedTime = 0;
30   int startingPeriodTime = 0;
31   boolean systemInterrupt = false;
32   int special_case_usedTime = 0; // used to get correct value when systemtick can happen, but doesn't
33

34   /**
35    * Main constructor
36    * @param periodIn, the period of the clock - clock resets to zero
37    * after period time elapsed
38    */

39   public PeriodicClock (int periodIn) {
40     microSecsPeriod = periodIn;
41   }
42
43   /**
44    * To get the tick value
45    * @param currentTime the current time on the clock
46    * @return true if tick
47    */

48   public boolean isInterrupted () {
49     //assert (usedTime < microSecsPeriod);
50
return systemInterrupt;
51   }
52
53   /**
54    * To get the period
55    * @return microSecsPeriod
56    */

57   public int getPeriod () {
58     return microSecsPeriod;
59   }
60
61   /**
62    * To get remaining time in period
63    */

64   public int getTimeToEOP () {
65     return microSecsPeriod - usedTime;
66   }
67
68   /**
69    * Clock event - abstraction when thread yields without interruption
70    * @param usedTimeIn the current time on the clock
71    */

72   public void setUsedTime (int usedTimeIn) {
73     usedTime = usedTimeIn;
74   }
75
76   /**
77    * To get the time used in period
78    * @param currentTime the current time on the clock
79    */

80   public int getUsedTime () {
81     if (systemInterrupt) {
82       return special_case_usedTime;
83     }
84
85     return usedTime;
86   }
87
88   /**
89    * To clear interrupt
90    */

91   public void clearInterrupt () {
92     systemInterrupt = false;
93     resetUsedTime();
94   }
95
96   /**
97    * Clock ticks - inherited from super class
98    * @param currentTime the current time on the clock
99    * @return void
100    */

101   public void clockTicks (int currentTime) {
102     usedTime = currentTime % microSecsPeriod;
103
104     //System.out.println("Clock - > usedtime = " + usedTime);
105
if (usedTime == 0) {
106       special_case_usedTime = microSecsPeriod;
107       systemInterrupt = true;
108       startingPeriodTime = currentTime;
109     }
110   }
111
112   public void resetUsedTime () {
113     special_case_usedTime = usedTime;
114   }
115 }
Popular Tags