Skip to content

Instantly share code, notes, and snippets.

@michaellin
Created December 1, 2015 05:02
Show Gist options
  • Save michaellin/ff9d29b57f2c23d2c207 to your computer and use it in GitHub Desktop.
Save michaellin/ff9d29b57f2c23d2c207 to your computer and use it in GitHub Desktop.
/***Service Module to debounce Pipe1 button***/
/*----------------------------- Include Files -----------------------------*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"
#include "LCDService.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"
#include "driverlib/gpio.h"
#include "Pipe1ButtonDebounce.h"
// All_BITS offset variable
#define ALL_BITS (0xff<<2)
// Data Private to Module
static uint8_t LastButtonState;
static uint8_t MyPriority;
static Button1State_t CurrentState;
/**************************************************************
Function: InitPipe1ButtonDB
Takes: 8 bit integer (priority number)
Returns: true or false
Purpose: Passes in priority number, initializes Tiva pins, and moves into state machine.
/*************************************************************/
bool InitPipe1ButtonDB (uint8_t Priority){
// Store priority value as static variable
MyPriority = Priority;
// Initialize port A
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R0 ;
while((HWREG(SYSCTL_PRGPIO) & SYSCTL_PRGPIO_R0) != SYSCTL_PRGPIO_R0);
// Initialize bit 5
HWREG(GPIO_PORTA_BASE+GPIO_O_DEN) |= (GPIO_PIN_5);
HWREG(GPIO_PORTA_BASE+GPIO_O_DIR) &= (BIT5LO);
// Sample the button port pin
LastButtonState = (HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA+ALL_BITS)) & BIT5HI);
// Set CurrentState to be Debouncing
CurrentState = Debouncing1;
// Start debounce timer
ES_Timer_InitTimer(DB_PIPE1_TIMER,30); // Debounce time of 30 ms
return true;
}
/**************************************************************
Function: PostStartButtonDB
Takes: ES_Event
Returns: true or false
Purpose: Posts events to this service's queue.
/*************************************************************/
bool PostPipe1Button( ES_Event ThisEvent ){
return ES_PostToService( MyPriority, ThisEvent);
}
/**************************************************************
Function: CheckPipe1ButtonEvents
Takes: nothing
Returns: true or false
Purpose: Checks the state of the start button and reports it to this service.
/*************************************************************/
bool CheckPipe1ButtonEvents(void){
// Local Variables
ES_Event ThisEvent;
uint8_t ReturnVal = false;
// Set CurrentButtonState to state read from port pin
uint8_t CurrentButtonState = (HWREG(GPIO_PORTA_BASE+(GPIO_O_DATA+ALL_BITS)) & BIT5HI);
if (CurrentButtonState != LastButtonState){
ReturnVal = true;
if (CurrentButtonState == BIT5HI){
// Tell this service the pipe1 button is down
ThisEvent.EventType = ES_PIPE1_BUTTON_DOWN;
PostPipe1Button(ThisEvent);
}
else{ // button is up
// Tell this service the pipe1 button is up
ThisEvent.EventType = ES_PIPE1_BUTTON_UP;
PostPipe1Button(ThisEvent);
}
}
// Update LastButtonState to CurrentButtonState
LastButtonState = CurrentButtonState;
return ReturnVal;
}
/**************************************************************
Function: RunPipe1ButtonDB
Takes: ES_Event
Returns: ES_Event (ES_NO_EVENT)
Purpose: Runs the start button debounce state machine.
/*************************************************************/
ES_Event RunPipe1ButtonDB( ES_Event ThisEvent ){
if (CurrentState == Debouncing1){
if ( (ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == DB_PIPE1_TIMER) ){ // 0 is debounce timer number
// Move into Ready2Sample state
CurrentState = Ready2Sample1;
}
}
else if (CurrentState == Ready2Sample1){
if (ThisEvent.EventType == ES_PIPE1_BUTTON_UP){
// Start debounce timer
ES_Timer_InitTimer(DB_PIPE1_TIMER,30);
// Move into debouncing state
CurrentState = Debouncing1;
}
else if (ThisEvent.EventType == ES_PIPE1_BUTTON_DOWN){
// Start debounce timer
ES_Timer_InitTimer(DB_PIPE1_TIMER,30);
CurrentState = Debouncing1;
// Post DBButtonDown to all queues
ThisEvent.EventType = ES_PIPE1_BUTTON_DBDOWN;
ES_PostAll(ThisEvent);
}
}
// Return ES_NO_EVENT
ThisEvent.EventType = ES_NO_EVENT;
return ThisEvent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment