Listing 4 (reprate.cpp)

  1: /*
  2: REPRATE.CPP
  3:
  4: Source for repetition rate classes
  5: Paul A. Cornelius, August 1991
  6: */
  7:
  8: #include <math.h>
  9: #include <limits.h>
 10: #include <sound.h>
 11: #include <assert.h>
 12:
 13: #include "int70.hpp"
 14: #include "reprate.hpp"
 15:
 16: RepRateList TimerList;
 17: void handle_tick()  // called by int_70 handler
 18: {
 19:     // couple to list of RepRate objects
 20:     TimerList.HandleTick();
 21: }
 22:
 23: RepRate::RepRate(double Hz,CALL_ME pfun)
 24: {
 25:     ChangeRepRate(Hz);
 26:     ChangeFunction(pfun);
 27: }
 28:
 29: double RepRate::ChangeRepRate(double Hz)
 30: {
 31:     // interval is integer number of timer events
 32:     double interval =
 33:      floor(TICKS_PER_SECOND / Hz + 0.5);
 34:
 35:     // check for interval out of bounds
 36:     if (interval < 1.0)
 37:         interval = 1.0;
 38:     if (interval > LONG_MAX)
 39:         interval = LONG_MAX;
 40:     tick_interval = interval;
 41:     count_now = 0;
 42:
 43:     // return the "true" rep rate
 44:     return GetRepRate();
 45: }
 46:
 47: void RepRate::HandleTick()
 48: {
 49:     if (++count_now >= tick_interval)
 50:     // if true, call user function
 51:     {
 52:         count_now = 0;
 53:         func();
 54:     }
 55: }
 56:
 57: RepRateList::RepRateList ()
 58: {
 59:     assert(On == 0);     // 1 RepRateList per program!
 60:     On++;
 61:     TimerOn = 0;
 62:     Error: 0;
 63: }
 64:
 65: RepRateList::~RepRateList()
 66: {
 67:     TurnOffTimer();
 68: }
 69:
 70: void RepRateList::Linkin(RepRate *prep)
 71: {
 72:     // link in the object
 73:     Replist.linkin(prep);
 74:     if (Replist.error())        // linked list error
 75:     {
 76:         TurnOffTimer();
 77:         Error [SYM]= 1;
 78:     }
 79:     // First object on list turns on timer
 80:     if (Replist.size() == 1)
 81:         TurnOnTimer();
 82: }
 83:
 84: void RepRateList::Linkout(RepRate *prep)
 85: {
 86:     // Last object turns off timer
 87:     if (Replist.size() == 1)
 88:         TurnOffTimer();
 89:     // no list objects, do nothing
 90:     if (Replist.size() == 0)
 91:         return;
 92:
 93:     // rewind the iterator
 94:     Replist.start();
 95:     for (int i=0;i<Replist.size();i++,++Replist)
 96:     {
 97:         // look for the object to be linked out
 98:         if (Replist.get() == prep)
 99:         {
100:             Replist.linkout();       // found it
101:             return;
102:         }
103:     }
104:     // prep was not found, thus 1 item still on list
105:     if (Replist.size() == 1)
106:         TurnOnTimer();
107: }
108:
109: void RepRateList::TurnOnTimer()
110: {
111:     if (TimerOn == 0)
112:     {
113:         if (start_timer() == 0)
114:         {
115:             TimerOn = 1;
116:             // let me know the clock is started
117:             sound_beep(400.0);
118:         }
119:     }
120: }
121:
122:
123: void RepRateList::TurnOffTimer()
124: {
125:     if (TimerOn == 1)
126:         stop_timer();
127:     TimerOn = 0;
128: }
129:
130:
131: void RepRateList::HandleTick()
132: {
133:     Replist.start();
134:     for (int i=0;i<Replist.size();i++,++Replist)
135:     {
136:         Replist()->HandleTick();
137:     }
138: }

// End of File