Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

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

Module
PopUpButton.c
Revision
1.0.1
Description
This is the service for controlling popup buttons and post popup button
events to other services.
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_DeferRecall.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 "PopUpButton.h"
#include "PopUpMotorService.h"
/*----------------------------- Module Defines ----------------------------*/
#ifndef ALL_BITS
#define ALL_BITS (0xff<<2)
#endif
// these times assume a 1.000mS/tick timing
#define ONE_SEC 976
#define TWENTY_SEC (20*ONE_SEC)
#define THIRTY_SEC (30*ONE_SEC)
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this service.They should be functions
relevant to the behavior of this service
*/
bool GetButton1State(void);
bool GetButton2State(void);
bool GetButton3State(void);
/*---------------------------- Module Variables ---------------------------*/
/*Button1: PA5;
* Button2: PA6;
*Button3: PA7;
*/
static bool LastButton1State;
static bool LastButton2State;
static bool LastButton3State;
static uint8_t MyPriority;
static ButtonState_t CurrentState = Debouncing;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitPopUpButton
Parameters: None
Returns: bool true
Description: this function init hardware for popup button
****************************************************************************/
bool InitPopUpButton(uint8_t Priority){
//init MyPriority with passed in parameter
MyPriority = Priority;
//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 PA5, PA6, PA7 to be diginal inputs
HWREG(GPIO_PORTA_BASE+GPIO_O_DEN) |= (BIT5HI | BIT6HI | BIT7HI);
HWREG(GPIO_PORTA_BASE+GPIO_O_DIR) &= (BIT5LO | BIT6LO | BIT7LO);
//init BUTTON_TIMER (debouncing timer) to be 50ms
ES_Timer_InitTimer(BUTTON_TIMER, 50);
//get last button1, button2, and button3 states
LastButton1State = GetButton1State();
LastButton2State = GetButton2State();
LastButton3State = GetButton3State();

return true;
}

/****************************************************************************
Function
PostPopUpButton
Parameters: None
Returns: None
Description: this function init hardware for popup button
****************************************************************************/
bool PostPopUpButton(ES_Event ThisEvent){
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
Check4Button
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 Check4Button(void){
//Assume no event
bool ReturnVal = false;
ES_Event ThisEvent;
//get CurrentButton1State, CurrentButton2State and CurrentButton3State from
input pins
bool CurrentButton1State = GetButton1State();
bool CurrentButton2State = GetButton2State();
bool CurrentButton3State = GetButton3State();

//check if Button1 state changed


if(CurrentButton1State != LastButton1State){
//if Button1 is pressed
if(CurrentButton1State == 1){
//set ThisEvent to ES_BUTTON1_PRESSED
ThisEvent.EventType = ES_BUTTON1_PRESSED;
}else{//else
//set ThisEvent to ES_BUTTON1_RELEASED
ThisEvent.EventType = ES_BUTTON1_RELEASED;
}
//post ThisEvent to PopUpButton
PostPopUpButton(ThisEvent);
//set ReturnVal to true
ReturnVal = true;
}
//check if Button2 state changed
if(CurrentButton2State != LastButton2State){
//if Button2 is pressed
if(CurrentButton2State == 1){
//set ThisEvent to ES_BUTTON2_PRESSED
ThisEvent.EventType = ES_BUTTON2_PRESSED;
}else{//else
//set ThisEvent to ES_BUTTON2_RELEASED
ThisEvent.EventType = ES_BUTTON2_RELEASED;
}
//post ThisEvent to PopUpButton
PostPopUpButton(ThisEvent);
//set ReturnVal to true
ReturnVal = true;
}
//check if Button3 state changed
if(CurrentButton3State != LastButton3State){
//if Button3 is pressed
if(CurrentButton3State == 1){
//set ThisEvent to ES_BUTTON3_PRESSED
ThisEvent.EventType = ES_BUTTON3_PRESSED;
}else{//else
//set ThisEvent to ES_BUTTON3_RELEASED
ThisEvent.EventType = ES_BUTTON3_RELEASED;
}
//post ThisEvent to PopUpButton
PostPopUpButton(ThisEvent);
//set ReturnVal to true
ReturnVal = true;
}

//update LastButtonState with CurrrentButtonState


LastButton1State = CurrentButton1State;
LastButton2State = CurrentButton2State;
LastButton3State = CurrentButton3State;
//return ReturnVal
return ReturnVal;
}

/****************************************************************************
Function
RunPopUpButton
Parameters
ES_Event ThisEvent
Returns
Description
Posts an event to this state machine's queue
****************************************************************************/
ES_Event RunPopUpButton(ES_Event ThisEvent){
ES_Event ReturnEvent;
//assume no errors
ReturnEvent.EventType = ES_NO_EVENT;
//CurrentState of PopUpButton can be one of: Debouncing, and Ready2Sample
switch(CurrentState){
//if CurrentState is Debouncing
case Debouncing:
//if this event is ES_TIMEOUT from BUTTON_TIMER
if((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
BUTTON_TIMER)){
//set CurrentState to Ready2Sample
CurrentState = Ready2Sample;
}
break;
//if CurrentState is Ready2Sample
case Ready2Sample:
//if this event is ES_BUTTON1_PRESSED
if(ThisEvent.EventType == ES_BUTTON1_PRESSED){
//post ThisEvent to PopUpMotorService, LEDService, VibratingMotor, and
TeraMotorService
ES_PostList02(ThisEvent);
//set CurrentState to Debouncing
CurrentState = Debouncing;
//init BUTTON_TIMER to be 50ms
ES_Timer_InitTimer(BUTTON_TIMER, 50);
//init TERA_TIMER to be 30s
ES_Timer_InitTimer(TERA_TIMER, THIRTY_SEC);
}
//if this event is ES_BUTTON1_RELEASED
if(ThisEvent.EventType == ES_BUTTON1_RELEASED){
//set CurrentState to Debouncing
CurrentState = Debouncing;
//init BUTTON_TIMER to be 50ms
ES_Timer_InitTimer(BUTTON_TIMER, 50);
}
//if this event is ES_BUTTON2_PRESSED
if(ThisEvent.EventType == ES_BUTTON2_PRESSED){
//post ThisEvent to PopUpMotorService, LEDService, VibratingMotor, and
TeraMotorService
ES_PostList02(ThisEvent);
//set CurrentState to Debouncing
CurrentState = Debouncing;
//init BUTTON_TIMER to be 50ms
ES_Timer_InitTimer(BUTTON_TIMER, 50);
//init TERA_TIMER to be 30s
ES_Timer_InitTimer(TERA_TIMER, THIRTY_SEC);
}
//if this event is ES_BUTTON2_RELEASED
if(ThisEvent.EventType == ES_BUTTON2_RELEASED){
//set CurrentState to Debouncing
CurrentState = Debouncing;
//init BUTTON_TIMER to be 50ms
ES_Timer_InitTimer(BUTTON_TIMER, 50);
}
//if this event is ES_BUTTON3_PRESSED
if(ThisEvent.EventType == ES_BUTTON3_PRESSED){
//post ThisEvent to PopUpMotorService, LEDService, VibratingMotor, and
TeraMotorService
ES_PostList02(ThisEvent);
//set CurrentState to Debouncing
CurrentState = Debouncing;
//init BUTTON_TIMER to be 50ms
ES_Timer_InitTimer(BUTTON_TIMER, 50);
//init TERA_TIMER to be 30s
ES_Timer_InitTimer(TERA_TIMER, THIRTY_SEC);
}
//if this event is ES_BUTTON3_RELEASED
if(ThisEvent.EventType == ES_BUTTON3_RELEASED){
//set CurrentState to Debouncing
CurrentState = Debouncing;
//init BUTTON_TIMER to be 50ms
ES_Timer_InitTimer(BUTTON_TIMER, 50);
}
}
return ReturnEvent;
}

/****************************************************************************
Private function
****************************************************************************/
/****************************************************************************
Function
GetButtonXState
Parameters: None
Returns: bool ButtonX state
Description: this function init hardware for popup button
****************************************************************************/
bool GetButton1State(void){
//initialize Button1 to be 0
bool Button1 = 0;
//if input PA5 is HI
if(HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA + ALL_BITS))& BIT5HI){
//set Button1 to 1
Button1 = 1;
}
//return Button1
return Button1;
}

bool GetButton2State(void){
//initialize Button2 to be 0
bool Button2 = 0;
//if input PA6 is HI
if(HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA + ALL_BITS))& BIT6HI){
//set Button2 to 1
Button2 = 1;
}
//return Button2
return Button2;
}

bool GetButton3State(void){
//initialize Button3 to be 0
bool Button3 = 0;
//if input PA7 is HI
if(HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA + ALL_BITS))& BIT7HI){
//set Button3 to 1
Button3 = 1;
}
//return Button3
return Button3;
}

You might also like