You are on page 1of 4

/****************************************************************************

Module
PopUpMotorService.c
Revision
1.0.1
Description
This is a service that controls the movement of three popup motors.
Notes
History
When Who
--------------------
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for this state machine as well as any machines at the
next lower level in the hierarchy that are sub-machines to this machine
*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_Port.h"
#include "ES_Events.h"
#include "ES_Timers.h"
#include "termio.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h" // Define PART_TM4C123GH6PM in project
#include "driverlib/gpio.h"
#include "PWM16Tiva.h"
#include "PopUpMotorService.h"
#include "PopUpButton.h"
#include "Motor.h"
/*----------------------------- Module Defines ----------------------------*/
/* prototypes for private functions for this service.They should be functions
relevant to the behavior of this service
*/
#define ONE_SEC 976
#define THIRTY_SEC (30*ONE_SEC)
#define FIFTEEN_SEC (15*ONE_SEC)
/*---------------------------- Module Variables ---------------------------*/
// with the introduction of Gen2, we need a module level Priority variable
static uint8_t MyPriority;
static PopUpMotorState_t CurrentState = PopUpInitPState;

/*------------------------------ Module Code ------------------------------*/


/****************************************************************************
Function
InitPopUpMotor
Parameters
uint8_t : the priorty of this service
****************************************************************************/
bool InitPopUpMotor ( uint8_t Priority ){
ES_Event ThisEvent;
//Initialize MyPriority with passed in parameter
MyPriority = Priority;
//Init output pins for all the motors
InitMotor();

//post the inital transition event


ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true){
return true;
}else{
return false;
}
}

/****************************************************************************
Function
PostPopUpMotor
Parameters
ES_Event ThisEvent ,the event to post to the queue
Returns
bool false if the Enqueue operation failed, true otherwise
Description
Posts an event to this state machine's queue
****************************************************************************/
bool PostPopUpMotor( ES_Event ThisEvent ){
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunMorseDecode
Parameters
ES_Event : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here
****************************************************************************/
ES_Event RunPopUpMotor( ES_Event ThisEvent ){
ES_Event ReturnEvent;
// assume no errors
ReturnEvent.EventType = ES_NO_EVENT;
//set NextState to CurrrentState
PopUpMotorState_t NextState = CurrentState;

//CurrentState can be one of: PopUpInitPState, PopUpWaitingState,


PopUp1WorkingState,
//PopUp2WorkingState, PopUp3WorkingState, PopUpResetState
switch (CurrentState) {
//if CurrentState is PopUpInitPState
case PopUpInitPState:
// If ThisEvent is ES_Init, set NextState to PopUpWaitingState
if (ThisEvent.EventType == ES_INIT) {
NextState = PopUpWaitingState;
}
break;
//if CurrentState is PopUpWaitingState
case PopUpWaitingState:
//if this event is ES_KNOB_TURNED
if(ThisEvent.EventType == ES_KNOB_TURNED){
//set NextState to PopUp1WorkingState
NextState = PopUp1WorkingState;
//init POPUP_TIMER to be 30s
ES_Timer_InitTimer(POPUP_TIMER, THIRTY_SEC);
}
break;
//if CurrentState is PopUp1WorkingState
case PopUp1WorkingState:
//if this is a timer timeout event, which means the user doesn't interact
with the PopUp
if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
POPUP_TIMER)){
//stay in PopUpWorkignState
NextState = PopUpWaitingState;
}
//if this event is ES_BUTTON1_PRESSED
if(ThisEvent.EventType == ES_BUTTON1_PRESSED){
//set popup motor1 to rotate to desired position
SetPopUpMotor1();
//set NextState to PopUp2WorkingState
NextState = PopUp2WorkingState;
//init POPUP_TIMER to be 30s
ES_Timer_InitTimer(POPUP_TIMER, THIRTY_SEC);
}
break;
//if CurrentState is PopUp2WorkingState
case PopUp2WorkingState:
//if this event is ES_TIMEOUT
if(ThisEvent.EventType == ES_TIMEOUT){
//reset popup1 to go down
ResetPopUpMotor1();
//set NextState to PopUpWaitingState
NextState = PopUpWaitingState;
}
//if this event is ES_BUTTON2_PRESSED
if(ThisEvent.EventType == ES_BUTTON2_PRESSED){
//set popup motor2 to rotate to desired position
SetPopUpMotor2();
//set NextState to PopUp3WorkingState
NextState = PopUp3WorkingState;
//init POPUP_TIMER to be 30s
ES_Timer_InitTimer(POPUP_TIMER, THIRTY_SEC);
}
break;
//if CurrentState is PopUp3WorkingState
case PopUp3WorkingState:
//if this event is ES_TIMEOUT from POPUP_TIMER
if(ThisEvent.EventType == ES_TIMEOUT && ThisEvent.EventParam ==
POPUP_TIMER){
//reset popup motor1 and popup motor2
ResetPopUpMotor1();
ResetPopUpMotor2();
//set NextState to PopUpWaitingState
NextState = PopUpWaitingState;
}
//if this event is ES_BUTTON3_PRESSED
if(ThisEvent.EventType == ES_BUTTON3_PRESSED){
//set popup motor 3 to rotate to desired position
SetPopUpMotor3();
//set NextState to PopupResetState
NextState = PopUpResetState;
//init POPUP_TIMER to be 30s
ES_Timer_InitTimer(POPUP_TIMER, THIRTY_SEC);
}
break;
//if CurrentState is PopUpResetState
case PopUpResetState:
//if this event is ES_TIMEOUT from POPUP_TIMER
if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
POPUP_TIMER)){
//reset popup motor1, popup motor2, and popup motor3
ResetPopUpMotor1();
ResetPopUpMotor2();
ResetPopUpMotor3();
//set NextState to PopUpWaitingState
NextState = PopUpWaitingState;
}
break;
}
//set CurrentState to NextState
CurrentState = NextState;
//return ReturnEvent
return ReturnEvent;
}

/*------------------------------- Footnotes -------------------------------*/


/*------------------------------ End of file ------------------------------*/

You might also like