Sie sind auf Seite 1von 7

/*

LEAF_STATE_MACHINE

This state machine detecs the insertion of the leaf card, during which the game
should run

First Version: Nov 11, Sunday


Second Version: Nov 12, Sunday

Data private to the module:


MyPriority

States:
LeafInserted
LeafNotInserted

Intaking Events:
LEAF_VALID (From HARDWARE event checker)
LEAF_REMOVED (From HARDWARE event checker)

Posting Events:
LEAF_ACTIVATED
LEAF_RESET

*/

/*----------------------------- Include Files -----------------------------*/


// Basic includes for a program using the Events and Services Framework

/* 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"

// Event & Services Framework


#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"

/*----------------------------- Module Defines ----------------------------*/


// define constants for the states for this machine
// and any other local defines

//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

//The pin for Leaf LED. Takes PB5


#define LEAF_LED_PORT_HI BIT1HI
#define LEAF_LED_PORT_LO BIT1LO
#define LEAF_LED_PIN_HI BIT5HI
#define LEAF_LED_PIN_LO BIT5LO
#define LEAF_LED_SYSCTL_PRGPIO SYSCTL_PRGPIO_R1
#define LEAF_LED_GPIO_BASE GPIO_PORTB_BASE

/*---------------------------- Module Functions ---------------------------*/


/* prototypes for private functions for this machine, things like during
functions, entry & exit functions.They should be functions relevant to the
behavior of this state machine
*/
static void Leaf_Led_Init(void);

/*---------------------------- Module Variables ---------------------------*/


// everybody needs a state variable, you may need others as well
static uint8_t MyPriority;
static uint8_t LastPinState;
static LeafSM_States_t CurrentState;

/****************************************************************************
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");

//Initialize the hardware pin to read the analog input


//Initialize the port line to monitor the button
HWREG(SYSCTL_RCGCGPIO) |= LEAF_PORT_HI;
while ((HWREG(SYSCTL_PRGPIO) & LEAF_SYSCTL_PRGPIO) != LEAF_SYSCTL_PRGPIO)
{}
//enable portb bit4 as digital input for dutton debouncing
HWREG(LEAF_GPIO_BASE + GPIO_O_DEN) |= LEAF_SENSOR_PIN_HI; //Setting it as
Digital IO
HWREG(LEAF_GPIO_BASE + GPIO_O_DIR) &= LEAF_SENSOR_PIN_LO; //Setting it as
Digital Input

//Sample the button port pin and use it to initialize LastButtonState


LastPinState = HWREG(LEAF_GPIO_BASE + (GPIO_O_DATA + ALL_BITS)) &
LEAF_SENSOR_PIN_HI;
printf("\r \n LastState for Leaf is %d", LastPinState);

//setting current state as the pseudo state


CurrentState = PseudoState_Leaf;

//initializing leaf led


Leaf_Led_Init();

//starting with the Leaf LED off


Leaf_Led_Off();

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

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

ES_Event_t RunLeafSM(ES_Event_t ThisEvent)


{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors

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();

// now put the machine into the actual initial state


puts("\r \n Pseudo --> LeafNotInserted");
CurrentState = LeafNotInserted;
}
}
break;

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);

puts("\r \n LeafNotInserted --> LeafInserted");


CurrentState = LeafInserted;
}
*/
if (ThisEvent.EventType == LEAF_VALID)
{
//actions: LEAF_ACTIVATED needs to be posted to just the ControlSM
puts("\r \n Post LEAF_ACTIVATED to services");
ES_Event_t PostingEvent;
PostingEvent.EventType = LEAF_ACTIVATED;
PostControlSM(PostingEvent);

puts("\r \n LeafNotInserted --> LeafInserted");


CurrentState = LeafInserted;

//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");

puts("\r \n LeafInserted --> LeafNotInserted");


CurrentState = LeafNotInserted;
}
*/
if (ThisEvent.EventType == LEAF_REMOVED)
{
//actions
puts("\r \n Post LEAF_RESET to services");
ES_Event_t PostingEvent;
PostingEvent.EventType = LEAF_RESET;
ES_PostList00(PostingEvent);

puts("\r \n LeafInserted --> LeafNotInserted");


CurrentState = LeafNotInserted;

//switch on the leaf LED


Leaf_Led_On();
}
}
break;
}
return ReturnEvent;
}

/****************************************************************************
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;

//If it is a rising edge, post LEAF_VALID


/*
Naturally, the tape sensor gives a falling edge for valid leaf insertion.
However, this is inverted by the Schmidt Trigger
*/
if (CurrentPinState == LEAF_SENSOR_PIN_HI)
{
//puts("\r \n Rising edge detected for Leaf -- Leaf Inserted");
ThisEvent.EventType = LEAF_VALID;
PostLeafSM(ThisEvent);
}
//Else, post LEAF_REMOVED
else
{
//puts("\r \n Falling edge detected for Leaf -- Leaf Removed");
ThisEvent.EventType = LEAF_REMOVED;
PostLeafSM(ThisEvent);
}
}
LastPinState = CurrentPinState;
return ReturnVal;
}

/****************************************************************************
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

printf("\r \n Leaf LED hardware initialized");


}

/****************************************************************************
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;
}

Das könnte Ihnen auch gefallen