Implement (MSEC) using usleep() (#125)
[debian/pforth] / fth / misc2.fth
index f5f9f345574fc2e118b0dd46d59718b2818328c9..b1ae8eae96309eb67d84f01b256af6d38c4d12b9 100644 (file)
@@ -49,16 +49,39 @@ variable if-debug
 ;
 
 decimal
-create msec-delay 10000 ,  ( default for SUN )
-: (MSEC) ( #msecs -- )
+create MSEC-DELAY 100000 ,   \ calibrate this for your system
+: (MSEC.SPIN) ( #msecs -- , busy wait, not accurate )
+    0 max   \ avoid endless loop
     0
-    do  msec-delay @ 0
+    ?do  msec-delay @ 0
         do loop
     loop
 ;
 
+: (MSEC) ( millis -- )
+    dup (sleep) \ call system sleep in kernel
+    IF
+        ." (SLEEP) failed or not implemented! Using (MSEC.SPIN)" CR
+        (msec.spin)
+    ELSE
+        drop
+    THEN
+;
+
 defer msec
-' (msec) is msec
+
+\ (SLEEP) uses system sleep functions to actually sleep.
+\ Use (MSEC.SPIN) on embedded systems that do not support Win32 Sleep() posix usleep().
+1 (SLEEP) [IF]
+    ." (SLEEP) failed or not implemented! Use (MSEC.SPIN) for MSEC" CR
+    ' (msec.spin) is msec
+[ELSE]
+    ' (msec) is msec
+[THEN]
+
+: MS ( msec -- , sleep, ANS standard )
+    msec
+;
 
 : SHIFT ( val n -- val<<n )
     dup 0<
@@ -67,7 +90,6 @@ defer msec
     THEN
 ;
 
-
 variable rand-seed here rand-seed !
 : random ( -- random_number )
     rand-seed @