Professional Documents
Culture Documents
Module
StartButton.c
Description
This is the service that handles both of the digital buttons
Notes
History
When
Who
What/Why
-------------- ---------11/15/16
12:55 mwm
Created this service for the ME218A project
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for the framework and this service
*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
"inc/hw_memmap.h"
"inc/hw_types.h"
"inc/hw_gpio.h"
"inc/hw_sysctl.h"
"driverlib/sysctl.h"
"driverlib/pin_map.h"
"driverlib/gpio.h"
"StartButton.h"
"TimingMotor.h"
"TreeSM.h"
#include "BITDEFS.H"
#include "DEFINITIONS.h"
/*----------------------------- Module Defines ----------------------------*/
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this service.They should be functions
relevant to the behavior of this service
*/
/*---------------------------- Module Variables ---------------------------*/
// with the introduction of Gen2, we need a module level Priority variable
static uint8_t MyPriority, LastButtonStateStart, LastButtonStateWeather;
static SBState_t CurrentState;
/*------------------------------ Module Code ------------------------------*/
/****************************************************************************
Function
InitializeStartButton
Parameters
uint8_t : the priority of this service
Returns
bool, false if error in initialization, true otherwise
Description
Saves away the priority, and does any
other required initialization for this service
Notes
Authors
Matthew Miller, 10/26/16
****************************************************************************/
bool InitializeStartButton ( uint8_t Priority )
{
ES_Event ThisEvent;
MyPriority = Priority;
//Initialize the port line for the buttons
// Enable Port B
HWREG(SYSCTL_RCGCGPIO) |= GPIO_PIN_1;
// Make sure the peripheral clock has been set up
while((HWREG(SYSCTL_PRGPIO) & BIT1HI) != BIT1HI)
;
// Make PB5 and PB7 digital and input
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= (GPIO_PIN_5 | GPIO_PIN_7);
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) |= !(GPIO_PIN_5 | GPIO_PIN_7);
printf("\rHardware Initialized\r\n");
//Sample port line and use it to initialize the LastButtonState variables
LastButtonStateStart = HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA +
ALL_BITS))&(BIT5HI);
LastButtonStateWeather = HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA +
ALL_BITS))&(BIT5HI);
//Set CurrentState to be ButtonInit
CurrentState = ButtonInit;
//Start timers
ES_Timer_Init(ES_Timer_RATE_1mS);
ES_Timer_InitTimer(START_BUTTON_TIMER, DEBOUNCE_TIME);
// post the initial transition event
ThisEvent.EventType = ES_INIT;
if (ES_PostToService( MyPriority, ThisEvent) == true)
{
return true;
}else
{
return false;
}
}
/****************************************************************************
Function
PostStartButton
Parameters
EF_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
Notes
Authors
ButtonUpStart
is ButtonUpWeather
}
break;
}
return ReturnEvent;
/****************************************************************************
Function
CheckButtonEvents
Parameters
None
Returns
bool: true if a new event was detected
Description
Check to see if the signal from the the buttons was turned on or turned off
and react accordingly
Notes
Author
Matthew Miller 11/5/2016
****************************************************************************/
bool CheckButtonEvents(void)
{
uint8_t CurrentButtonStateStart, CurrentButtonStateWeather;
bool ReturnVal = false; // Set the return value to false
//Get the CurrentButtonStates from the input lines
CurrentButtonStateStart = HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA +
ALL_BITS))&(BIT5HI);
CurrentButtonStateWeather = HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA +
ALL_BITS))&(BIT7HI);
//Check if the state of the start button input line has changed and
is ready for the start button press
if((CurrentButtonStateStart != LastButtonStateStart) && CurrentState
== WaitingForStart){
//set the return value to true
ReturnVal = true;
//Check if the current start button state is down
if(!CurrentButtonStateStart){
//Post Event ButtonDownStart to this service
ES_Event FunEvent;
FunEvent.EventType = ButtonDownStart;
PostStartButton(FunEvent);
}else{
//If current start button state is up, post
ButtonUpStart to this service
ES_Event NotFunEvent;
NotFunEvent.EventType = ButtonUpStart;
PostStartButton(NotFunEvent);
}
//set ReturnVal equal to true
ReturnVal = true;
}
//Check if the state of the weather button input line has changed
and is ready for the weather button press
if((CurrentButtonStateWeather != LastButtonStateWeather) &&
CurrentState == WaitingForWeather){
//set the return value to true
ReturnVal = true;
//Check if the current weather button state is down
if(!CurrentButtonStateWeather){
//Post Event ButtonDownWeather to this service
ES_Event FunEvent;
FunEvent.EventType = ButtonDownWeather;
PostStartButton(FunEvent);
}else{