www.pudn.com > ejip.zip > Timer.java
package util;
/**
* A VERY simple timer and WD handling.
*
* A little excures in modulo clac:
*
* get the difference of to values (on wrap over):
*
* thats ok: val-Native.rd(Native.IO_CNT) < 0;
*
* thats WRONG: val < Native.rd(Native.IO_CNT);
*/
import com.jopdesign.sys.*;
public class Timer {
private static boolean blink;
private static int next;
private static int last;
private static int frequ;
private static int ms;
public static int us; // for Amd.progam()
private static int interval;
public static void init(int f, int iv) {
frequ = f;
ms = frequ/1000;
us = frequ/1000000;
interval = ms*iv;
blink = true;
wd(); // make WD happy
wd();
wd();
}
public static int cnt() {
return Native.rd(Native.IO_CNT);
}
public static int getNextCnt(int msOff) {
return Native.rd(Native.IO_CNT) + ms*msOff;
}
public static int getNextCnt(int lastVal, int msOff) {
return lastVal + ms*msOff;
}
public static boolean timeout(int val) {
return val-Native.rd(Native.IO_CNT) < 0;
}
public static void wd() {
if (blink) {
Native.wr(1, Native.IO_WD);
blink = false;
} else {
Native.wr(0, Native.IO_WD);
blink = true;
}
}
public static void start() {
next = Native.rd(Native.IO_CNT);
last = next;
}
public static int usedTime() {
return Native.rd(Native.IO_CNT)-last;
}
public static void waitForNextInterval() {
next += interval;
int i = Native.rd(Native.IO_CNT);
if (next-i < 0) { // missed time!
next = i; // correct next
last = i;
return;
}
while (next-Native.rd(Native.IO_CNT) >= 0)
;
last = Native.rd(Native.IO_CNT);
}
/**
* next Interval in ms (with next increment)
*/
public static void waitForNextMs(int t) {
next += t*ms;
int i = Native.rd(Native.IO_CNT);
if (next-i < 0) { // missed time!
next = i; // correct next
last = i;
return;
}
while (next-Native.rd(Native.IO_CNT) >= 0)
;
last = Native.rd(Native.IO_CNT);
}
/**
* simple wait val t (no wd!, no next increment).
*/
public static void sleep(int t) {
int j = Native.rd(Native.IO_CNT);
for (int i=0; i= 0)
;
}
}
/**
* simple wait val t (no wd!, no next increment).
*/
public static void usleep(int t) {
int j = Native.rd(Native.IO_CNT);
j += t*us;
while (j-Native.rd(Native.IO_CNT) >= 0)
;
}
/**
* simple wait val t with wd.
*/
public static void sleepWd(int t) {
int j = Native.rd(Native.IO_CNT)+ms;
for (int i=0; i= 0)
;
j += ms;
wd();
}
}
}