/*
 * (c) Your Company, 2011
 *
 * All rights reserved. Reproduction, modification,
 * use or disclosure to third parties without express
 * authority is forbidden.
 */

/* Command line options: -p CADIFRA -l cppx -o oven first_example_step3.cdd   */
/* This file is generated from first_example_step3.cdd - do not edit manually  */
/* Generated on: Sat Oct 29 16:49:01 CEST 2011 / version 2.5 */


#include "mydefs.h"
#include "oven.h"
#include "oven_ext.h"
#include "oven_hlp.h"
#include <stdio.h>

extern unsigned char msg;
extern T_PWR pwr;
oven::oven(void)
{
	m_initialized=0U;
}



/* State names */
const char states[]=s
	"Super\0Completed\0CookingPause\0Idle\0Cooking\0";

const unsigned short state_idx[]={
	0,6,16,29,34,42};

/* Event names */
const char events[]=
	"evDoorOpen\0evDec\0evTimeout\0evPwr\0evDoorClosed\0evInc\0NO_MSG\0";

const unsigned short evt_idx[]={
	0,11,17,27,33,46,52};

const char* oven::getNameByState(unsigned short state) const {
	return states+state_idx[state];
}

const char* oven::getNameByEvent(OVEN_EVENT_T evt) const {
	return events+evt_idx[evt];
}


// Helper(s) to reset history
void oven::resetHistorySuper(void){
	stateVarSuper=Idle;
}


// Helper(s) to find out if the machine is in a certain state

int oven::isInSuper(void) const {return(((stateVar== Super)) ? (true) : (false));}
int oven::isInCompleted(void) const {return(((stateVarSuper== Completed)&&(stateVar== Super)) ? (true) : (false));}
int oven::isInCookingPause(void) const {return(((stateVarSuper== CookingPause)&&(stateVar== Super)) ? (true) : (false));}
int oven::isInIdle(void) const {return(((stateVarSuper== Idle)&&(stateVar== Super)) ? (true) : (false));}
int oven::isInCooking(void) const {return(((stateVarSuper== Cooking)&&(stateVar== Super)) ? (true) : (false));}

// Initialize method. Must be called once to init the machine

void oven::initialize(){	//call on entry code of default states
	if(m_initialized==0U){

		m_initialized=1U;
		//call on entry code of default states
		ovenOff();

		// Set state vars to default states
		stateVar = Super; /* set init state of top state */
		stateVarSuper = Idle; /* set init state of Super */
	}

}

// State machine event handler
int oven::processEvent(OVEN_EVENT_T msg){

	int evConsumed = 0;

	

	if(m_initialized==false) return 0;

	/* action code */
	/* just a comment */


	switch (stateVar) {

		case Super:

			switch (stateVarSuper) {

				case Idle:
					if(msg==evDoorClosed){
						if(timer_preset()>0){
							/* Transition from Idle to Cooking */
							evConsumed=1;

							/* Action code for transition  */
							timer_start();

							/* OnEntry code of state Cooking */
							ovenOn();

							/* adjust state variables  */
							stateVarSuper = Cooking;
						}else{
							/* Intentionally left blank */
						} /*end of event selection */
					}else{
						/* Intentionally left blank */
					} /*end of event selection */
				break; /* end of case Idle  */

				case Cooking:
					if(msg==evDoorOpen){
						/* Transition from Cooking to CookingPause */
						evConsumed=1;

						/* Action code for transition  */
						ovenOff();
						timer_pause();


						/* adjust state variables  */
						stateVarSuper = CookingPause;
					}else if(msg==evTimeout){
						/* Transition from Cooking to Completed */
						evConsumed=1;

						/* Action code for transition  */
						ovenOff();
						timer_clear();


						/* adjust state variables  */
						stateVarSuper = Completed;
					}else{
						/* Intentionally left blank */
					} /*end of event selection */
				break; /* end of case Cooking  */

				case Completed:
					if(msg==evDoorOpen){
						/* Transition from Completed to Idle */
						evConsumed=1;

						/* OnEntry code of state Idle */
						ovenOff();

						/* adjust state variables  */
						stateVarSuper = Idle;
					}else{
						/* Intentionally left blank */
					} /*end of event selection */
				break; /* end of case Completed  */

				case CookingPause:
					if(msg==evDoorClosed){
						/* Transition from CookingPause to Cooking */
						evConsumed=1;

						/* Action code for transition  */
						timer_cont();

						/* OnEntry code of state Cooking */
						ovenOn();

						/* adjust state variables  */
						stateVarSuper = Cooking;
					}else{
						/* Intentionally left blank */
					} /*end of event selection */
				break; /* end of case CookingPause  */

				default:
					/* Intentionally left blank */
				break;
			} /* end switch Super */

		/* Check if event was already processed  */
		if(evConsumed==0){

			if(msg==evDec){
				/* Transition from Super to Super */
				evConsumed=1;
				
				if(stateVarSuper== Idle){
					

				}else if(stateVarSuper== Cooking){
					

				}else if(stateVarSuper== Completed){
					

				}else if(stateVarSuper== CookingPause){
					
				}

				/* Action code for transition  */
				timer_dec();

				stateVar = Super;/* entry chain  */
				if(stateVarSuper== Idle){
					ovenOff();

				}else if(stateVarSuper== Cooking){
					ovenOn();

				}else if(stateVarSuper== Completed){
					
				}else if(stateVarSuper== CookingPause){
					
				}

			}else if(msg==evInc){
				/* Transition from Super to Super */
				evConsumed=1;
				
				if(stateVarSuper== Idle){
					

				}else if(stateVarSuper== Cooking){
					

				}else if(stateVarSuper== Completed){
					

				}else if(stateVarSuper== CookingPause){
					
				}

				/* Action code for transition  */
				timer_inc();

				stateVar = Super;/* entry chain  */
				if(stateVarSuper== Idle){
					ovenOff();

				}else if(stateVarSuper== Cooking){
					ovenOn();

				}else if(stateVarSuper== Completed){
					
				}else if(stateVarSuper== CookingPause){
					
				}

			}else if(msg==evPwr){
				/* Transition from Super to Super */
				evConsumed=1;
				
				if(stateVarSuper== Idle){
					

				}else if(stateVarSuper== Cooking){
					

				}else if(stateVarSuper== Completed){
					

				}else if(stateVarSuper== CookingPause){
					
				}

				/* Action code for transition  */
				ovenSetPwr(pwr);

				stateVar = Super;/* entry chain  */
				if(stateVarSuper== Idle){
					ovenOff();

				}else if(stateVarSuper== Cooking){
					ovenOn();

				}else if(stateVarSuper== Completed){
					
				}else if(stateVarSuper== CookingPause){
					
				}

			}else{
				/* Intentionally left blank */
			} /*end of event selection */
		}
		break; /* end of case Super  */

		default:
			/* Intentionally left blank */
		break;
	} /* end switch stateVar_root */
	return evConsumed;
} // end processEvent


