/****************************************************************************
Module
FanService.c
Revision
1.0.1
Description
This is the service that controls the fan, which is on in the finale
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 "FanService.h"
/*----------------------------- Module Defines ----------------------------*/
#define ONE_SEC 976
#define HALF_SEC (0.5*ONE_SEC)
#define TWO_SEC (2*ONE_SEC)
#define FIVE_SEC (5*ONE_SEC)
#define TEN_SEC (10*ONE_SEC)
#define THIRTY_SEC (30*ONE_SEC)
#define FIFTEEN_SEC (15*ONE_SEC)
#define TWENTY_SEC (20*ONE_SEC)
#define FAN_TIMER 3
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this service.They should be functions
relevant to the behavior of this service
*/
void InitProx(void);
void InitFanHW(void);
bool GetProxState(void);
void SetFan(void);
void ResetFan(void);
/*---------------------------- Module Variables ---------------------------*/
// with the introduction of Gen2, we need a module level Priority variable
static uint8_t MyPriority;
static bool LastProxState;
static FanState_t CurrentState = FanInitPState;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitFan
Parameter
uint8_t : the priorty of this service
return: bool true or false
****************************************************************************/
bool InitFan ( uint8_t Priority ){
ES_Event ThisEvent;
//Initialize MyPriority with passed in parameter
MyPriority = Priority;
//Init Proximity sensor input pin and fan control pin
InitProx();
InitFanHW();
//get LastProxState from input pin
LastProxState = GetProxState();
//post the inital transition event
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true){
return true;
}else{
return false;
}
}
/****************************************************************************
Function
PostFan
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 PostFan( ES_Event ThisEvent ){
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
Check4Prox
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 Check4Prox(void){
//assume no event
bool ReturnVal = false;
bool CurrentProxState = GetProxState();
//get CurrentProxState from input pin
CurrentProxState = GetProxState();
//if there is an event
if(CurrentProxState != LastProxState){
ES_Event ThisEvent;
ThisEvent.EventType = ES_PROX_DETECTED;
//post the event to FanService, LEDService and VibratingMotor
ES_PostList03(ThisEvent);
//set ReturnVal to true
ReturnVal = true;
}
//set LastProxState to CurrentProxState
LastProxState = CurrentProxState;
//return ReturnVal
return ReturnVal;
}
/****************************************************************************
Function
RunFan
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 RunFan( ES_Event ThisEvent ){
ES_Event ReturnEvent;
//assume no errors
ReturnEvent.EventType = ES_NO_EVENT;
//set NextState to CurrentState
FanState_t NextState = CurrentState;
//CurrentState can be one of: FanIntPState, FanWorkingState, and FanResetState
switch (CurrentState) {
//if CurrentState is FanInitPState
case FanInitPState:
// If ThisEvent is ES_Init
if (ThisEvent.EventType == ES_INIT) {
//set NextState to FanWorkingState
NextState = FanWorkingState;
}
break;
//if CurrentState is FanWorkingState
case FanWorkingState:
//if this event is ES_FAN_TRIGGERED
if(ThisEvent.EventType == ES_FAN_TRIGGER){
//turn on the fan
SetFan();
//init FAN_TIMER to ve 20s
ES_Timer_InitTimer(FAN_TIMER, TWENTY_SEC);
//set NextState to FanResetState
NextState = FanResetState;
//init TERA_TIMER to be 30s for resetting
ES_Timer_InitTimer(TERA_TIMER, THIRTY_SEC);
//init POPUP_TIMER to be 30s for resetting
ES_Timer_InitTimer(POPUP_TIMER, THIRTY_SEC);
}
break;
//if CurrentState is FanResetState
case FanResetState:
//if this is ES_TIMEOUT from FAN_TIMER
if(ThisEvent.EventType == ES_TIMEOUT && ThisEvent.EventParam == FAN_TIMER){
//turn off the fan
ResetFan();
//set NextState to FanWorkingState
NextState = FanWorkingState;
}
break;
}
//set CurrentState to NextState
CurrentState = NextState;
//return ReturnEvent
return ReturnEvent;
}
/****************************************************************************
Private functions
****************************************************************************/
void InitProx(void){
//Proximity sensor input is PA4
//set up port A by enabling the peripheral clock
HWREG(SYSCTL_RCGCGPIO) |= BIT0HI;
//wait for the peripheral to be ready
while ((HWREG(SYSCTL_PRGPIO) & BIT0HI) != BIT0HI);
//set the direction of PA4 to be digital input
HWREG(GPIO_PORTA_BASE+GPIO_O_DEN) |= BIT4HI;
HWREG(GPIO_PORTA_BASE+GPIO_O_DIR) &= BIT4LO;
}
void InitFanHW(void){
//Proximity sensor input is PC5
// set up port C by enabling the peripheral clock
HWREG(SYSCTL_RCGCGPIO) |= BIT2HI;
//wait for the peripheral to be ready
while ((HWREG(SYSCTL_PRGPIO) & BIT2HI) != BIT2HI);
//set the direction of PC5 to be digital input
HWREG(GPIO_PORTC_BASE+GPIO_O_DEN) |= BIT5HI;
HWREG(GPIO_PORTC_BASE+GPIO_O_DIR) |= BIT5HI;
//Init Fan control pin to be low
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA + ALL_BITS)) &= BIT5LO;
}
bool GetProxState(void){
//init ProxState to be 0
bool ProxState = 0;
//if input pin PA4 is HI
if(HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA + ALL_BITS))& BIT4HI){
//set ProxState to 1
ProxState = 1;
}
//return ProxState
return ProxState;
}
void SetFan(void){
//set fan control pin PC5 HI
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA + ALL_BITS)) |= BIT5HI;
}
void ResetFan(void){
//set fan control pin PC5 LO
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA + ALL_BITS)) &= BIT5LO;
}
/*------------------------------- Footnotes -------------------------------*/
/*------------------------------ End of file ------------------------------*/