Beruflich Dokumente
Kultur Dokumente
LEAF_STATE_MACHINE
This state machine detecs the insertion of the leaf card, during which the game
should run
States:
LeafInserted
LeafNotInserted
Intaking Events:
LEAF_VALID (From HARDWARE event checker)
LEAF_REMOVED (From HARDWARE event checker)
Posting Events:
LEAF_ACTIVATED
LEAF_RESET
*/
/* 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 "LeafSM.h"
#include "ControlSM.h"
// Hardware
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
//The tape sensor from the leaf takes Bit 6 on Port D (BIT 3) (PIN 17 on JP6)
#define LEAF_PORT_HI BIT3HI
#define LEAF_PORT_LO BIT3LO
#define LEAF_SENSOR_PIN_HI BIT6HI
#define LEAF_SENSOR_PIN_LO BIT6LO
#define LEAF_SYSCTL_PRGPIO SYSCTL_PRGPIO_R3
#define LEAF_GPIO_BASE GPIO_PORTD_BASE
/****************************************************************************
Function
InitLeafSM
Parameters
uint8_t : the priorty 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
Author
****************************************************************************/
bool InitLeafSM(uint8_t Priority)
{
MyPriority = Priority;
printf("\r \n Initializing LeafSM");
ES_Event_t ThisEvent;
ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}
/****************************************************************************
Function
PostLeafSM
Parameters
ES_Event_t 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
Author
****************************************************************************/
bool PostLeafSM(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunLeafSM
Parameters
ES_Event_t : the event from its service queue to process in this State
Machine
Types of ES_Event_t: LEAF_ACTIVATED, LEAF_NOT_ACTIVATED
Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise
Description
add your description here
Notes
Author
****************************************************************************/
switch (CurrentState)
{
case PseudoState_Leaf: // If current state is initial Psedudo State
{
if (ThisEvent.EventType == ES_INIT) // only respond to ES_Init
{
// this is where you would put any actions associated with the
// transition from the initial pseudo-state into the actual
// initial state
//switch on the LeafLED
Leaf_Led_On();
case LeafNotInserted:
{
//checking for the event StartGame
/*
if (ThisEvent.EventParam == 'V') //Leaf removed
{
//actions
puts("\r \n Working with the keyboard");
puts("\r \n Post LEAF_ACTIVATED to services");
ES_Event_t PostingEvent;
PostingEvent.EventType = LEAF_ACTIVATED
PostControlSM(PostingEvent);
//switch off the LeaF LED once the leaf has been inserted
Leaf_Led_Off();
}
}
break;
case LeafInserted:
{
//checking for the event StartGame
/*
if (ThisEvent.EventParam == 'R') //Leaf removed
{
//actions
puts("\r \n Working with the keyboard");
puts("\r \n Post LEAF_RESET to services");
/****************************************************************************
Function
CheckLeaf
Parameters
No Parameters
Returns
bool true if there is an event else False
Posts:
LEAF_ACTIVATED (falling edge)
LEAF_REMOVED (rising edge)
Description
add your description here
Notes
Author
****************************************************************************/
bool CheckLeaf(void)
{
bool ReturnVal = false;
uint8_t CurrentPinState;
ES_Event_t ThisEvent;
//Set CurrentState to state read from port pin
CurrentPinState = HWREG(LEAF_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &
LEAF_SENSOR_PIN_HI;
//printf("\r \n CurrentPinState = %d", CurrentPinState);
//If the CurrentState is different from the LastState, Set ReturnVal = True
if (CurrentPinState != LastPinState)
{
ReturnVal = true;
/****************************************************************************
Function
Leaf_Led_Init
Parameters
No Parameters
Returns
Nothing
Description
Hardware initialization for the LeafLED
Notes
Author
****************************************************************************/
static void Leaf_Led_Init(void)
{
HWREG(SYSCTL_RCGCGPIO) |= LEAF_LED_PORT_HI;
while ((HWREG(SYSCTL_PRGPIO) & LEAF_LED_SYSCTL_PRGPIO) !=
LEAF_LED_SYSCTL_PRGPIO)
{}
//enable leaf led pin as digital output
HWREG(LEAF_LED_GPIO_BASE + GPIO_O_DEN) |= LEAF_LED_PIN_HI; //Setting it as
Digital IO
HWREG(LEAF_LED_GPIO_BASE + GPIO_O_DIR) |= LEAF_LED_PIN_HI; //Setting it as
Digital Input
/****************************************************************************
Function
Leaf_Led_On
Parameters
No Parameters
Returns
Nothing
Description
a public function which other services can access as well to switch on the
Leaf Led
Notes
Author
****************************************************************************/
void Leaf_Led_On(void)
{
HWREG(LEAF_LED_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) |= LEAF_LED_PIN_HI;
}
/****************************************************************************
Function
Leaf_Led_Off
Parameters
No Parameters
Returns
Nothing
Description
a public function which other state machines can access as well to switch off
the LeafLed
Notes
Author
****************************************************************************/
void Leaf_Led_Off(void)
{
HWREG(LEAF_LED_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &= LEAF_LED_PIN_LO;
}