TRC Library for FTC (2011) 2011
TRC Library Code Project

C:/Users/Michael/Ftc/2011/code/trclib/sm.h

Go to the documentation of this file.
00001 #if 0
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #endif
00014 
00015 #ifndef _SM_H
00016 #define _SM_H
00017 
00018 #pragma systemFile
00019 
00020 #ifdef MOD_ID
00021     #undef MOD_ID
00022 #endif
00023 #define MOD_ID                  MOD_SM
00024 
00025 //
00026 // Constants.
00027 //
00028 #define SMF_READY               0x0100
00029 #define SMF_TIMEOUT             0x0200
00030 #define SMF_MASK                0x00ff
00031 #define SMF_WAIT_ALL            0x0001
00032 #define SMF_CLEAR_EVENTS        0x0002
00033 
00034 #define SMSTATE_DISABLED        0
00035 #define SMSTATE_STARTED         1
00036 
00037 #define EVTTYPE_NONE            0
00038 
00039 #ifndef MAX_WAIT_EVENTS
00040 #define MAX_WAIT_EVENTS         4
00041 #endif
00042 
00043 //
00044 // Macros.
00045 //
00046 #define IsSMDisabled(s)         (s.currState == SMSTATE_DISABLED)
00047 #define IsSMEnabled(s)          (s.currState != SMSTATE_DISABLED)
00048 #define IsSMTimedOut(s)         (s.smFlags & SMF_TIMEOUT)
00049 
00050 //
00051 // Type definitions.
00052 //
00053 typedef struct
00054 {
00055     int  evtType;
00056     int  evtID;
00057     int  evtData;
00058     int  evtParam1;
00059     int  evtParam2;
00060     bool fSignaled;
00061 } WAIT_EVT;
00062 
00063 typedef struct
00064 {
00065     int  currState;
00066     int  nextState;
00067     long timeout;
00068     int  smFlags;
00069     bool fClearEvts;
00070     int  nWaitEvents;
00071     WAIT_EVT WaitEvents[MAX_WAIT_EVENTS];
00072 } SM;
00073 
00079 void
00080 SMClearAllEvents(
00081     __out SM &sm
00082     )
00083 {
00084     TFuncName("SMClearAllEvents");
00085     TLevel(API);
00086     TEnter();
00087 
00088     for (int i = 0; i < MAX_WAIT_EVENTS; ++i)
00089     {
00090         sm.WaitEvents[i].evtType = EVTTYPE_NONE;
00091         sm.WaitEvents[i].evtID = 0;
00092         sm.WaitEvents[i].evtData = 0;
00093         sm.WaitEvents[i].evtParam1 = 0;
00094         sm.WaitEvents[i].evtParam2 = 0;
00095         sm.WaitEvents[i].fSignaled = false;
00096     }
00097     sm.nWaitEvents = 0;
00098     sm.timeout = 0;
00099     sm.smFlags &= ~SMF_TIMEOUT;
00100 
00101     TExit();
00102     return;
00103 }   //SMClearAllEvents
00104 
00110 void
00111 SMInit(
00112     __out SM &sm
00113     )
00114 {
00115     TFuncName("SMInit");
00116     TLevel(INIT);
00117     TEnter();
00118 
00119     sm.currState = SMSTATE_DISABLED;
00120     sm.nextState = SMSTATE_DISABLED;
00121     sm.smFlags = 0;
00122     sm.fClearEvts = false;
00123     SMClearAllEvents(sm);
00124 
00125     TExit();
00126     return;
00127 }   //SMInit
00128 
00134 void
00135 SMStart(
00136     __out SM &sm
00137     )
00138 {
00139     TFuncName("SMStart");
00140     TLevel(API);
00141     TEnter();
00142 
00143     if (sm.currState == SMSTATE_DISABLED)
00144     {
00145         sm.currState = SMSTATE_STARTED;
00146         sm.nextState = SMSTATE_STARTED;
00147         sm.smFlags = SMF_READY;
00148         SMClearAllEvents(sm);
00149     }
00150 
00151     TExit();
00152     return;
00153 }   //SMStart
00154 
00160 void
00161 SMStop(
00162     __out SM &sm
00163     )
00164 {
00165     TFuncName("SMStop");
00166     TLevel(API);
00167     TEnter();
00168 
00169     SMInit(sm);
00170 
00171     TExit();
00172     return;
00173 }   //SMStop
00174 
00180 bool
00181 SMIsReady(
00182     __out SM &sm
00183     )
00184 {
00185     bool fReady;
00186 
00187     TFuncName("SMIsReady");
00188     TLevel(API);
00189     TEnter();
00190 
00191     if (((sm.smFlags & SMF_READY) == 0) &&
00192         (sm.timeout > 0) &&
00193         ((long)nPgmTime >= sm.timeout))
00194     {
00195         sm.smFlags |= (SMF_TIMEOUT | SMF_READY);
00196         if (sm.fClearEvts)
00197         {
00198             sm.fClearEvts = false;
00199             SMClearAllEvents(sm);
00200         }
00201         sm.currState = sm.nextState;
00202     }
00203     fReady = (sm.smFlags & SMF_READY) != 0;
00204 
00205     TExitMsg(("%=d", (byte)fReady));
00206     return fReady;
00207 }   //SMIsReady
00208 
00220 bool
00221 SMAddWaitEvent(
00222     __inout SM &sm,
00223     __in    int evtType,
00224     __in    int evtID,
00225     __in    int evtData
00226     )
00227 {
00228     TFuncName("SMAddWaitEvent");
00229     TLevel(API);
00230     TEnterMsg(("Type=%x,ID=%x", evtType, evtID));
00231 
00232     bool fAdded = false;
00233 
00234     if (sm.nWaitEvents < MAX_WAIT_EVENTS)
00235     {
00236         sm.WaitEvents[sm.nWaitEvents].evtType = evtType;
00237         sm.WaitEvents[sm.nWaitEvents].evtID = evtID;
00238         sm.WaitEvents[sm.nWaitEvents].evtData = evtData;
00239         sm.WaitEvents[sm.nWaitEvents].evtParam1 = 0;
00240         sm.WaitEvents[sm.nWaitEvents].evtParam2 = 0;
00241         sm.WaitEvents[sm.nWaitEvents].fSignaled = false;
00242         sm.nWaitEvents++;
00243         fAdded = true;
00244     }
00245     TInfo(("nEvts=%d", sm.nWaitEvents));
00246 
00247     TExitMsg(("fOK=%d", (byte)fAdded));
00248     return fAdded;
00249 }   //SMAddWaitEvent
00250 
00261 void
00262 SMWaitEvents(
00263     __inout SM &sm,
00264     __in    int nextState,
00265     __in    int timeout,
00266     __in    int flags
00267     )
00268 {
00269     TFuncName("SMWaitEvents");
00270     TLevel(API);
00271     TEnterMsg(("Next=%d,timout=%d", nextState, timeout));
00272 
00273     if (sm.nWaitEvents > 0)
00274     {
00275         sm.nextState = nextState;
00276         sm.timeout = (timeout > 0)? nPgmTime + timeout: 0;
00277         sm.smFlags |= flags & SMF_MASK;
00278         sm.smFlags &= ~(SMF_READY | SMF_TIMEOUT);
00279         if (sm.smFlags & SMF_CLEAR_EVENTS)
00280         {
00281             sm.fClearEvts = true;
00282             sm.smFlags &= ~SMF_CLEAR_EVENTS;
00283         }
00284         else
00285         {
00286             sm.fClearEvts = false;
00287         }
00288     }
00289 
00290     TExit();
00291     return;
00292 }   //SMWaitEvents
00293 
00306 void
00307 SMSetEvent(
00308     __inout SM &sm,
00309     __in    int evtType,
00310     __in    int evtID,
00311     __in    int evtData,
00312     __in    int evtParam1,
00313     __in    int evtParam2
00314     )
00315 {
00316     TFuncName("SMSetEvent");
00317     TLevel(API);
00318     TEnterMsg(("Type=%x,ID=%x", evtType, evtID));
00319 
00320     for (int i = 0; i < sm.nWaitEvents; ++i)
00321     {
00322         if (!sm.WaitEvents[i].fSignaled &&
00323             (sm.WaitEvents[i].evtType == evtType) &&
00324             ((sm.WaitEvents[i].evtID == -1) ||
00325              (sm.WaitEvents[i].evtID == evtID)) &&
00326             ((sm.WaitEvents[i].evtData == -1) ||
00327              (sm.WaitEvents[i].evtData == evtData)))
00328         {
00329             //
00330             // If the all event attributes matched or we don't care some of
00331             // them, we mark the event signaled.
00332             //
00333             TInfo(("Type=%x,ID=%x",
00334                    sm.WaitEvents[i].evtType, sm.WaitEvents[i].evtID));
00335             sm.WaitEvents[i].fSignaled = true;
00336             sm.WaitEvents[i].evtID = evtID;
00337             sm.WaitEvents[i].evtData = evtData;
00338             sm.WaitEvents[i].evtParam1 = evtParam1;
00339             sm.WaitEvents[i].evtParam2 = evtParam2;
00340 
00341             bool fAdvanceState = true;
00342             if (sm.smFlags & SMF_WAIT_ALL)
00343             {
00344                 for (int j = 0; j < sm.nWaitEvents; ++j)
00345                 {
00346                     if (!sm.WaitEvents[j].fSignaled)
00347                     {
00348                         fAdvanceState = false;
00349                         break;
00350                     }
00351                 }
00352             }
00353 
00354             if (fAdvanceState)
00355             {
00356                 TInfo(("AdvanceState"));
00357                 sm.smFlags |= SMF_READY;
00358                 if (sm.fClearEvts)
00359                 {
00360                     sm.fClearEvts = false;
00361                     SMClearAllEvents(sm);
00362                 }
00363                 sm.currState = sm.nextState;
00364             }
00365             break;
00366         }
00367     }
00368 
00369     TExit();
00370   return;
00371 }   //SMSetEvent
00372 
00373 #endif  //ifndef _SM_H
 All Data Structures Files Functions Variables Defines