You are on page 1of 6

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

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"

// Define PART_TM4C123GH6PM in project

#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

Matthew Miller, 11/5/16


****************************************************************************/
bool PostStartButton( ES_Event ThisEvent )
{
return ES_PostToService( MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunStartButtonSM
Parameters
ES_Event : the event to process
Returns
ES_Event, ES_NO_EVENT if no error ES_ERROR otherwise
Description
Runs the state machine for the Start Button
Notes
Authors
Matthew Miller, 11/5/16
****************************************************************************/
ES_Event RunStartButtonSM( ES_Event ThisEvent )
{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
switch (CurrentState){
case ButtonInit : // CurrentState is ButtonInit
if ( ThisEvent.EventType == ES_TIMEOUT ) { // If ThisEvent is
ES_TIMEOUT
// set the CurrentState to WaitingForStart
CurrentState = WaitingForStart;
}
break;
case WaitingForStart : // CurrentState is WaitingForStart
if( ThisEvent.EventType == ButtonDownStart ){ // If ThisEvent is
ButtonDownStart
// Start start button debounce timer
ES_Timer_InitTimer(START_BUTTON_TIMER, DEBOUNCE_TIME);
// Set CurrentState to DebouncingStart
CurrentState = DebouncingStart;

//Post BTN_START to Tree service


ES_Event FunEvent;
FunEvent.EventType = BTN_START;
PostTreeSM(FunEvent);

ButtonUpStart

if( ThisEvent.EventType == ButtonUpStart ){ // If ThisEvent is


// Start start button timer for debounce time
ES_Timer_InitTimer(START_BUTTON_TIMER, DEBOUNCE_TIME);
// Set CurrentState to DebouncingStart
CurrentState = DebouncingStart;
}
break;

case DebouncingStart : // CurrentState is DebouncingStart


if ( ThisEvent.EventType == ES_TIMEOUT){ // If ThisEvent is ES_TIMEOUT
// set the CurrentState to WaitingToReset
CurrentState = WaitingToReset;
}
break;
case WaitingToReset : // CurrentState is WaitingToReset
if ( ThisEvent.EventType == START) { // If ThisEvent is START
// set the CurrentState to WaitingForWeather
CurrentState = WaitingForWeather;
}
break;
case WaitingForWeather : // CurrentState is WaitingForWeather
if( ThisEvent.EventType == ButtonDownWeather ){ // If
ThisEvent is ButtonDownWeather
// Start start button timer for debounce time
ES_Timer_InitTimer(START_BUTTON_TIMER, DEBOUNCE_TIME);
// Set CurrentState to DebouncingWeather
CurrentState = DebouncingWeather;

//Post BTN_WEATHER to TreeSM


ES_Event FunEvent;
FunEvent.EventType = BTN_WEATHER;
PostTreeSM(FunEvent);

if( ThisEvent.EventType == ButtonUpWeather ){ // If ThisEvent

is ButtonUpWeather

// Start start button timer for debounce time


ES_Timer_InitTimer(START_BUTTON_TIMER, DEBOUNCE_TIME);
// Set CurrentState to DebouncingWeather
CurrentState = DebouncingWeather;
}
WAITING

if ( ThisEvent.EventType == WAITING) { // If ThisEvent is


// set the CurrentState to WaitingForStart
CurrentState = WaitingForStart;
}
break;

case DebouncingWeather : // CurrentState is DebouncingWeather


if ( ThisEvent.EventType == ES_TIMEOUT){ // If ThisEvent is ES_TIMEOUT
// set the CurrentState to WaitingForWeather
CurrentState = WaitingForWeather;
}
if ( ThisEvent.EventType == WAITING) { // If ThisEvent is
WAITING

// set the CurrentState to WaitingForStart


CurrentState = WaitingForStart;

}
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{

//If current weather button state is up, post


ButtonUpWeather to this service
ES_Event NotFunEvent;
NotFunEvent.EventType = ButtonUpWeather;
PostStartButton(NotFunEvent);
}
//set ReturnVal equal to true
ReturnVal = true;
}
// Set LastButtonStates to the CurrentStates
LastButtonStateWeather = CurrentButtonStateWeather;
LastButtonStateStart = CurrentButtonStateStart;
return ReturnVal;
}

You might also like