Skip to content

Instantly share code, notes, and snippets.

@michaellin
Created December 1, 2015 05:05
Show Gist options
  • Save michaellin/d67317348854431b742e to your computer and use it in GitHub Desktop.
Save michaellin/d67317348854431b742e to your computer and use it in GitHub Desktop.
/***Service Module to debounce Pipe2 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 "Pipe2ButtonDebounce.h"
// All_BITS offset variable
#define ALL_BITS (0xff<<2)
// Data Private to Module
static uint8_t LastButtonState;
static uint8_t MyPriority;
static Button2State_t CurrentState;
/**************************************************************
Function: InitPipe2ButtonDB
Takes: 8 bit integer (priority number)
Returns: true or false
Purpose: Passes in priority number, initializes Tiva pins, and moves into state machine.
/*************************************************************/
bool InitPipe2ButtonDB (uint8_t Priority){
// Store priority value as static variable
MyPriority = Priority;
// Initialize port F
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R5 ;
while((HWREG(SYSCTL_PRGPIO) & SYSCTL_PRGPIO_R5) != SYSCTL_PRGPIO_R5);
// Initialize bit 7
HWREG(GPIO_PORTF_BASE+GPIO_O_DEN) |= (GPIO_PIN_4);
HWREG(GPIO_PORTF_BASE+GPIO_O_DIR) &= (BIT4LO);
// Sample the button port pin
LastButtonState = (HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS)) & BIT4HI);
// Set CurrentState to be Debouncing
CurrentState = Debouncing2;
// Start debounce timer
ES_Timer_InitTimer(DB_PIPE2_TIMER,30); // Debounce time of 30 ms
return true;
}
/**************************************************************
Function: PostPipe2ButtonDB
Takes: ES_Event
Returns: true or false
Purpose: Posts events to this service's queue.
/*************************************************************/
bool PostPipe2Button( ES_Event ThisEvent ){
return ES_PostToService( MyPriority, ThisEvent);
}
/**************************************************************
Function: CheckPipe2ButtonEvents
Takes: nothing
Returns: true or false
Purpose: Checks the state of the start button and reports it to this service.
/*************************************************************/
bool CheckPipe2ButtonEvents(void){
// Local Variables
ES_Event ThisEvent;
uint8_t ReturnVal = false;
// Set CurrentButtonState to state read from port pin
uint8_t CurrentButtonState = (HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS)) & BIT4HI);
if (CurrentButtonState != LastButtonState){
ReturnVal = true;
if (CurrentButtonState == BIT4HI){
// Tell this service the pipe1 button is down
ThisEvent.EventType = ES_PIPE2_BUTTON_DOWN;
PostPipe2Button(ThisEvent);
}
else{ // button is up
// Tell this service the pipe1 button is up
ThisEvent.EventType = ES_PIPE2_BUTTON_UP;
PostPipe2Button(ThisEvent);
}
}
// Update LastButtonState to CurrentButtonState
LastButtonState = CurrentButtonState;
return ReturnVal;
}
/**************************************************************
Function: RunPipe2ButtonDB
Takes: ES_Event
Returns: ES_Event (ES_NO_EVENT)
Purpose: Runs the start button debounce state machine.
/*************************************************************/
ES_Event RunPipe2ButtonDB( ES_Event ThisEvent ){
if (CurrentState == Debouncing2){
if ( (ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam == DB_PIPE2_TIMER) ){ // 0 is debounce timer number
// Move into Ready2Sample state
CurrentState = Ready2Sample2;
}
}
else if (CurrentState == Ready2Sample2){
if (ThisEvent.EventType == ES_PIPE2_BUTTON_UP){
// Start debounce timer
ES_Timer_InitTimer(DB_PIPE2_TIMER,30);
// Move into debouncing state
CurrentState = Debouncing2;
}
else if (ThisEvent.EventType == ES_PIPE2_BUTTON_DOWN){
// Start debounce timer
ES_Timer_InitTimer(DB_PIPE2_TIMER,30);
CurrentState = Debouncing2;
// Post DBButtonDown to all queues
ThisEvent.EventType = ES_PIPE2_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