home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / sys / mac / programm / 18789 < prev    next >
Encoding:
Text File  |  1992-11-22  |  5.8 KB  |  185 lines

  1. Path: sparky!uunet!cs.utexas.edu!zaphod.mps.ohio-state.edu!rpi!bu.edu!dartvax!coos.dartmouth.edu!mac
  2. From: mac@coos.dartmouth.edu (Milton A. Colvin)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Re: Time Manager Sample Code...
  5. Message-ID: <1992Nov22.222518.5497@dartvax.dartmouth.edu>
  6. Date: 22 Nov 92 22:25:18 GMT
  7. References: <1992Nov20.172111.11068@mcs.drexel.edu>
  8. Sender: news@dartvax.dartmouth.edu (The News Manager)
  9. Organization: Dartmouth College, Hanover, NH
  10. Lines: 173
  11.  
  12. urritche@mcs.drexel.edu (Ralph Paul Ritchey) writes:
  13.  
  14.  
  15. >    Does anyone know of any code floating around that I can get my
  16. >hands on that uses the Time Manager?  I've poked around but haven't turned
  17. >anything up yet....   Thanks in advance!
  18.  
  19.  
  20.  
  21. Is MPW C++ OK?
  22.  
  23. FILE Ticker.h
  24. //----------------------------------------------------------------------------------------
  25. // Included files:
  26. //----------------------------------------------------------------------------------------
  27. #include    <Types.h>
  28. #include    <Timer.h>            // toolbox timer manager
  29.  
  30. //========================================================================================
  31. // class CTicker
  32. //========================================================================================
  33. // implement a cyclic subroutine called every (usec) 5sec.
  34. // this typically triggers some event or resets something
  35. // the subroutine is called as a timer task
  36. //    it must be memory resident
  37. //    it cannot reference globals
  38. //    it cannot call the OS
  39. //
  40. class CTicker : public TMTask
  41. {
  42. public:
  43.     CTicker(long usec) : fInterval(-usec),fCount(0) { }    // create inactive ticker
  44.     ~CTicker()                        { Off(); }            // inactivate & destroy
  45.     
  46.     void            On();            // start ticking
  47.     void            Off();            // stop
  48.     Boolean            Busy();            // determine whether running
  49.     
  50.     virtual    void    Tick();            // tick went off
  51.     long            Elapsed();        // elapsed time in usec
  52.  
  53.     long            fCount;            // incremented
  54.  
  55. protected:
  56.     QElemPtr        Task();            // timer queue entry
  57.     static pascal void    Ticked();    // handle & forward call
  58.     
  59.     long            fInterval;        // -usec intervals
  60. };
  61.  
  62. //========================================================================================
  63. // class CTicker
  64. //========================================================================================
  65. // run a precise timer
  66. // this timer measures the time from OnIOff
  67. // Once Off the elapsed time is available.
  68. // the timer is reset by reuse
  69.  
  70. class CTimer : public TMTask
  71. {
  72. public:
  73.     CTimer()                        { }        // create inactive timer
  74.     ~CTimer()                        { Off(); }    // inactivate & destroy timer
  75.     
  76.     void            On();            // start running
  77.     void            Off();            // stop
  78.     Boolean            Busy();            // determine whether running
  79.     long            Elapsed();        // elapsed time in usec
  80.  
  81. protected:
  82.     QElemPtr        Task();            // timer queue entry
  83. };
  84.  
  85.  
  86. FILE Ticker.cp
  87.  
  88. // implement various kinds of clock
  89. #include    "Ticker.h"
  90. #include    <Types.h>
  91. #include    <Timer.h>            // toolbox timer manager
  92. #include    <Limits.h>            // big numbers for times
  93.  
  94. #pragma segment Main            // some of this must be memory-resident
  95.  
  96. //========================================================================================
  97. // class CTicker
  98. //========================================================================================
  99.  
  100. // a timer task handler is called with the task pointer in A1
  101.  
  102. static void    *GetA1() = 0x2009;                    // MOVE.L A1,D0
  103.  
  104. //----------------------------------------------------------------------------------------
  105. // pass to OS as timer queue entry
  106. inline QElemPtr    CTicker::Task()    { return (QElemPtr)this; }
  107.  
  108. //----------------------------------------------------------------------------------------
  109. // to enable the ticker fill in the timer task and prime it
  110. void
  111. CTicker::On()
  112. {    qLink = NULL;
  113.     qType = 0;
  114.     tmCount = tmWakeUp = tmReserved = 0;
  115.     tmAddr = Ticked;                // our handler
  116.     InsXTime(Task());
  117.     PrimeTime(Task(),fInterval);
  118. }
  119.  
  120. //----------------------------------------------------------------------------------------
  121. // remove ticker from active timer queue
  122. //
  123. void    CTicker::Off()    { RmvTime(Task()); }
  124.  
  125. // i think this is true
  126. Boolean    CTicker::Busy()    { return qLink!=NULL; }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // when called we set up the this pointer
  130. // incrment the counter and forward the call
  131. //
  132. pascal void
  133. CTicker::Ticked()
  134. {    CTicker    &me = *(CTicker*)GetA1();    // this in A1
  135.     PrimeTime(me.Task(),me.fInterval);        // reschedule
  136.     me.fCount++;                        // just for good measure
  137.     me.Tick();                            // pass control
  138. }
  139.  
  140. //----------------------------------------------------------------------------------------
  141. //
  142. long    CTicker::Elapsed()    { return fCount*-fInterval; }
  143.  
  144. //----------------------------------------------------------------------------------------
  145. // the default handler does nothing
  146. //
  147. void    CTicker::Tick()    { }
  148.  
  149.  
  150. //========================================================================================
  151. // class CTimer
  152. //========================================================================================
  153.  
  154. // this limits the maximum duration we can measure
  155. // if we really wanted to be good,
  156. // we could handle the expiration and restart the timer
  157.  
  158. const long kMaxTime = INT_MAX;        // set a timer for as long as possible
  159.  
  160. //----------------------------------------------------------------------------------------
  161. // pass to OS as timer queue entry
  162. //
  163. inline QElemPtr    CTimer::Task()    { return (QElemPtr)this; }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // fill in the timer task and prime it
  167. //
  168. void
  169. CTimer::On()
  170. {    qLink = NULL;
  171.     qType = 0;
  172.     tmCount = tmWakeUp = tmReserved = 0;
  173.     tmAddr = NULL;                // no handler
  174.     InsXTime(Task());
  175.     PrimeTime(Task(),-kMaxTime);
  176. }
  177.  
  178. //----------------------------------------------------------------------------------------
  179. //
  180. void    CTimer::Off()    { RmvTime(Task()); }
  181.  
  182. //----------------------------------------------------------------------------------------
  183. //
  184. long    CTimer::Elapsed()    { return tmCount-kMaxTime; }
  185.