home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff239.lzh / JGoodies / JustBeeps / SimpleTimer.f < prev   
Text File  |  1989-08-21  |  3KB  |  95 lines

  1. \ Use the Amiga Timer Device for accurate timing
  2. \ of music and sound.
  3. \ This is more accurate then using DOS_LIB DELAY
  4. \ or software timing loops.
  5. \
  6. \ Author: Phil Burk
  7. \ Hereby placed in the Public Domain. May be freely redistributed.
  8. \
  9. decimal
  10. getmodule includes
  11. include? CreatePort() ju:Exec_Support
  12. include? BeginIO() ju:device-calls
  13. include? TimeRequest ji:devices/timer.j
  14.  
  15. ANEW TASK-TIMER.F
  16.  
  17. : TIMER.DELETE ( timereq --  , delete timer request message)
  18.     dup CloseDevice()
  19. \ Delete Port if one attached.
  20.     dup .. io_message ..@ mn_replyport if>rel ?dup
  21.     IF  DeletePort()
  22.     THEN
  23.     sizeof() TimeRequest DeleteExtIO()
  24. ;
  25.  
  26. : TIMER.CREATE  ( units -- timereq , open and initialize timer )
  27. \ Create a reply port
  28.     0 0 createPort() ?dup
  29.     IF  ( -- units port )
  30. \
  31. \ Create an extended IO Request message
  32.         dup sizeof() TimeRequest CreateExtIO() ?dup
  33.         IF  ( -- units port iob )
  34. \
  35. \ Open the device using this message.
  36.             >r TIMERNAME rot r@ 0 OpenDevice()
  37.             IF ( -- port )
  38.                ." Couldn't Open Timer Device!" cr
  39.                r> sizeof() TimeRequest DeleteExtIO()
  40.                DeletePort() NULL
  41.             ELSE drop r>
  42.             THEN
  43.         ELSE ." TIMER.CREATE - Couldn't CreateExtIO" cr
  44.             DeletePort() drop NULL
  45.         THEN
  46.     ELSE ." TIMER.CREATE - Couldn't create port." cr
  47.          drop NULL
  48.     THEN
  49. ;
  50.  
  51. : TIMER.SEND  ( seconds micros timereq -- , send off request)
  52. \ Set time values and command in message and send.
  53.     dup>r .. tr_time ..! tv_micro
  54.     r@ .. tr_time ..! tv_secs
  55.     TR_ADDREQUEST r@ ..! io_command
  56.     r> SendIO() drop ( result is meaningless )
  57. ;
  58.  
  59.  
  60. \ Example of using Timer
  61. variable TIMER-MSG
  62.  
  63. : IOB>SIGNAL ( iob -- signal_mask , extract signal from IO request )
  64.     .. io_message ..@ mn_replyport >rel
  65.     ..@ mp_sigbit
  66.     1 swap shift
  67. ;
  68.  
  69. : Wait() ( signalmask -- signals , wait for some signals )
  70.     call exec_lib wait
  71. ;
  72.  
  73. : TEST.TIMER ( -- )
  74.     UNIT_MICROHZ timer.create ?dup
  75.     IF  timer-msg !
  76.         cr ." Delay 2 seconds" cr
  77.         2 0 timer-msg @ timer.send
  78.         timer-msg @  WaitIO() ?dup
  79.         IF ." Error on WaitIO() = " . cr
  80.         THEN
  81. \
  82.         ." Delay 3/4 seconds" cr
  83.         0 750,000 timer-msg @ timer.send
  84. \ Instead of using WaitIO, you could get the signal bits
  85. \ from this message, OR them with other signal bits,
  86. \ then wait on and several signals.
  87.         timer-msg @ iob>signal
  88. \ optionally OR with other bits  ( mouse_signal OR ) etc.
  89.         Wait() ." Signal = " .hex cr
  90. \
  91.         ." Done!" cr
  92.         timer-msg @ timer.delete
  93.     THEN
  94. ;
  95.