1 /****************************************************************************
  2  Module
  3    JSRHandlerHSM.c
  4 
  5  Revision
  6    2.0.1
  7 
  8  Description
  9    This is SM that handles all of the JSR SPI interpreting
 10 
 11  Notes
 12 
 13  History
 14  When           Who     What/Why
 15  -------------- ---     --------
 16  02/07/13 21:00 jec      corrections to return variable (should have been
 17                          ReturnEvent, not CurrentEvent) and several EV_xxx
 18                          event names that were left over from the old version
 19  02/08/12 09:56 jec      revisions for the Events and Services Framework Gen2
 20  02/13/10 14:29 jec      revised Start and run to add new kind of entry function
 21                          to make implementing history entry cleaner
 22  02/13/10 12:29 jec      added NewEvent local variable to During function and
 23                          comments about using either it or Event as the return
 24  02/11/10 15:54 jec      more revised comments, removing last comment in during
 25                          function that belongs in the run function
 26  02/09/10 17:21 jec      updated comments about internal transitions on During funtion
 27  02/18/09 10:14 jec      removed redundant call to RunLowerlevelSM in EV_Entry
 28                          processing in During function
 29  02/20/07 21:37 jec      converted to use enumerated type for events & states
 30  02/13/05 19:38 jec      added support for self-transitions, reworked
 31                          to eliminate repeated transition code
 32  02/11/05 16:54 jec      converted to implment hierarchy explicitly
 33  02/25/03 10:32 jec      converted to take a passed event parameter
 34  02/18/99 10:19 jec      built template from MasterMachine.c
 35  02/14/99 10:34 jec      Began Coding
 36 ****************************************************************************/
 37 /*----------------------------- Include Files -----------------------------*/
 38 // Basic includes for a program using the Events and Services Framework
 39 #include "ES_Configure.h"
 40 #include "ES_Framework.h"
 41 
 42 /* include header files for this state machine as well as any machines at the
 43    next lower level in the hierarchy that are sub-machines to this machine
 44 */
 45 #include "JSRHandlerHSM.h"
 46 #include "JSRGamePlayHSM.h"
 47 #include "GameMasterHSM.h"
 48 
 49 #include "MotorControl.h"	// for stop
 50 
 51 /*----------------------------- Module Defines ----------------------------*/
 52 // define constants for the states for this machine
 53 // and any other local defines
 54 
 55 #define INITIAL_STATE JSRWaitingToStart
 56 
 57 /*---------------------------- Module Functions ---------------------------*/
 58 /* prototypes for private functions for this machine, things like during
 59    functions, entry & exit functions.They should be functions relevant to the
 60    behavior of this state machine
 61 */
 62 static ES_Event DuringJSRWaitingToStart( ES_Event Event);
 63 static ES_Event DuringJSRGamePlay( ES_Event Event);
 64 static ES_Event DuringGameOver( ES_Event Event);
 65 
 66 /*---------------------------- Module Variables ---------------------------*/
 67 // everybody needs a state variable, you may need others as well
 68 static JSRHandlerHSMS_t CurrentState;
 69 
 70 /*------------------------------ Module Code ------------------------------*/
 71 /****************************************************************************
 72  Function
 73      StartJSRHandlerHSMS
 74 
 75  Parameters
 76      None
 77 
 78  Returns
 79      None
 80 
 81  Description
 82      Does any required initialization for this state machine
 83  Notes
 84 
 85  Author
 86      J. Edward Carryer, 2/18/99, 10:38AM
 87 ****************************************************************************/
 88 void StartJSRHandlerHSMS ( ES_Event CurrentEvent )
 89 {
 90    // local variable to get debugger to display the value of CurrentEvent
 91    ES_Event LocalEvent = CurrentEvent;
 92    
 93    CurrentState = INITIAL_STATE;
 94    if ( EV_CONDITIONAL_ENTRY == CurrentEvent.EventType )
 95    {
 96         // CurrentState = Other State;
 97    }
 98    // call the entry function (if any) for the INITIAL_STATE
 99    RunJSRHandlerHSMS(CurrentEvent);
100 }
101 
102 #pragma MESSAGE DISABLE C1855
103 /****************************************************************************
104  Function
105     RunJSRHandlerHSMS
106 
107  Parameters
108    ES_Event: the event to process
109 
110  Returns
111    ES_Event: an event to return
112 
113  Description
114    add your description here
115  Notes
116    uses nested switch/case to implement the machine.
117  Author
118    J. Edward Carryer, 2/11/05, 10:45AM
119 ****************************************************************************/
120 ES_Event RunJSRHandlerHSMS( ES_Event CurrentEvent )
121 {
122    unsigned char MakeTransition = false;/* are we making a state transition? */
123    JSRHandlerHSMS_t NextState = CurrentState;
124    ES_Event EntryEventKind = { EV_ENTRY, 0 };// default to normal entry to new state
125    ES_Event ReturnEvent = CurrentEvent; // assume we are not consuming event
126 
127    /* React differently based on the state you’re in */
128 	switch (CurrentState)
129 	{
130 		case JSRWaitingToStart:
131 			/* Execute During to respond to event */
132 			CurrentEvent = DuringJSRWaitingToStart(CurrentEvent);
133 
134 			/* Handle all events relevant to WaitingToStart */
135 			if (ES_NO_EVENT != CurrentEvent.EventType)
136 			{
137 				switch (CurrentEvent.EventType)
138 				{
139 					case JSR_NEW_ROUND:
140 						// Switch to JSRGamePlay
141 						NextState = JSRGamePlay; 
142 						MakeTransition = True; 
143 						EntryEventKind.EventType = EV_ENTRY; 
144 						ReturnEvent = CurrentEvent;
145 						break;
146 				}
147 			}
148 			break;
149 
150 		case JSRGamePlay:
151 			/* Execute During to respond to event */
152 			CurrentEvent = DuringJSRGamePlay(CurrentEvent);
153 
154 			/* Handle all events relevant to JSRGamePlay */
155 			if (ES_NO_EVENT != CurrentEvent.EventType)
156 			{
157 				switch (CurrentEvent.EventType)
158 				{
159 					case JSR_GAME_OVER:
160 						// Switch to GameOver
161 						NextState = GameOver; 
162 						MakeTransition = True; 
163 						EntryEventKind.EventType = EV_ENTRY; 
164 						ReturnEvent = CurrentEvent;
165 						break;
166 				}
167 			}
168 			break;
169 
170 
171 	case GameOver:
172 			/* Execute During to respond to event */
173 			CurrentEvent = DuringGameOver(CurrentEvent);
174 			// Final state – no transitions
175 			break;
176 
177 	}
178 
179     //   If we are making a state transition
180     if (MakeTransition == True)
181     {
182 		/* Exit this state */
183 		CurrentEvent.EventType = EV_EXIT;
184 		RunJSRHandlerHSMS(CurrentEvent);
185 
186 		/* Enter next state */
187 		CurrentState = NextState; //Modify state variable
188 		CurrentEvent = EntryEventKind; // this defaults to EV_ENTRY
189 		RunJSRHandlerHSMS(CurrentEvent);
190      }
191      return(ReturnEvent);
192 }
193 
194 /****************************************************************************
195  Function
196      QueryJSRHandlerHSMS
197 
198  Parameters
199      None
200 
201  Returns
202      JSRHandlerHSMS_t The current state of the Template state machine
203 
204  Description
205      returns the current state of the Template state machine
206  Notes
207 
208  Author
209      J. Edward Carryer, 2/11/05, 10:38AM
210 ****************************************************************************/
211 JSRHandlerHSMS_t QueryJSRHandlerHSMS ( void )
212 {
213    return(CurrentState);
214 }
215 
216 /***************************************************************************
217  private functions
218  ***************************************************************************/
219 
220 static ES_Event DuringJSRWaitingToStart( ES_Event Event)
221 {
222     ES_Event ReturnEvent = Event; // assmes no re-mapping or comsumption
223 	
224 	if ((EV_ENTRY == Event.EventType) || 
225 	(EV_CONDITIONAL_ENTRY == Event.EventType) ) {
226 		// Execute Entry actions here
227 		ES_Event JSREvent = { EV_JSR_READY, 0 };
228 		PostGameMasterHSMS(JSREvent);
229 		/* No lower level SM(s) */
230 
231 	} 
232 	else if (EV_EXIT == Event.EventType)
233 	{
234 		/* No lower level SM(s) */
235 		// Execute Exit actions here
236 
237 	}
238 	else
239 	{	
240 		/* No lower level SM(s) */
241 		// Execute any internal event response here
242 	}
243 	return ReturnEvent;
244 }
245 
246 static ES_Event DuringJSRGamePlay( ES_Event Event)
247 {
248     ES_Event ReturnEvent = Event; // assmes no re-mapping or comsumption
249 	
250 	if ((EV_ENTRY == Event.EventType) || 
251 	(EV_CONDITIONAL_ENTRY == Event.EventType) ) {
252 		// Execute Entry actions here
253 		/* Start lower level SM(s) */
254 		StartJSRGamePlayHSMS(Event);
255 
256 	} 
257 	else if (EV_EXIT == Event.EventType)
258 	{
259 		/* Exit lower level SM(s) */
260 		RunJSRGamePlayHSMS(Event);
261 		// Execute Exit actions here
262 
263 	}
264 	else
265 	{	
266 		/* Pass event to lower level SM(s) */
267 		ReturnEvent = RunJSRGamePlayHSMS(Event);
268 		// Execute any internal event response here
269 	}
270 	return ReturnEvent;
271 }
272 
273 static ES_Event DuringGameOver( ES_Event Event)
274 {
275     ES_Event ReturnEvent = Event; // assmes no re-mapping or comsumption
276 	
277 	if ((EV_ENTRY == Event.EventType) || 
278 	(EV_CONDITIONAL_ENTRY == Event.EventType) ) {
279 		// Execute Entry actions here
280 		StopMotors();
281 		/* No lower level SM(s) */
282 
283 	} 
284 	else if (EV_EXIT == Event.EventType)
285 	{
286 		/* No lower level SM(s) */
287 		// Execute Exit actions here
288 
289 	}
290 	else
291 	{	
292 		/* No lower level SM(s) */
293 		// Execute any internal event response here
294 	}
295 	return ReturnEvent;
296 }