Sie sind auf Seite 1von 6

//This module handles determining whether the user

//has pressed only the correct buttons

// the common headers for C99 types


#include <stdio.h> //added
#include <stdint.h>
#include <stdbool.h>

// the headers to access the GPIO subsystem


#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"

// the headers to access the TivaWare Library


#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"

#include "BITDEFS.H"

#include "GameplayService.h"
#include "ShiftRegisterWrite.h"
#include "MotorService.h"
#include "LEDServiceSM.h"

#include "ADMulti.h"

#define MAP_COUNTER_MAX 1
#define TEST_BANK_SIZE 6
#define NUM_ROWS 96
#define FIRST_LED_ROW 15
#define NUM_TIMEOUTS_FIRST_PATTERN 3
#define NUM_TIMEOUTS_SECOND_PATTERN 8

//Parameters from the fence checker


#define PLAYER_START_LINE 0
#define PLAYER_FINISH_LINE 1
#define OPPONENT_START_LINE 2
#define OPPONENT_FINISH_LINE 3
#define FLAG_START_POSITION 4
#define FLAG_FINISH_POSITION 5

static uint8_t MyPriority;


static GameplayServiceState_t CurrentState;
static uint8_t FirstButton;
static uint8_t SecondButton;
static uint8_t ThirdButton;
static uint8_t FourthButton;
static uint8_t ButtonPattern;
static uint8_t MapCounter;
static uint8_t LastButtonStates;
static uint8_t LastButtonState_Button1;
static uint8_t LastButtonState_Button2;
static uint8_t LastButtonState_Button3;
static uint8_t LastButtonState_Button4;
static uint8_t NumTimeouts;
static uint8_t FourBitOutputPattern;
static uint8_t NumLEDsOn;
static bool AnyButtonsPressed;
static uint8_t LEDRowBank[NUM_ROWS];

//Get states of buttons. Returns a value that encodes which buttons are on
static uint8_t GetCurrentButtonStates(void)
{
uint8_t ReturnVal;
uint8_t Button2 = (BIT6HI)&HWREG(GPIO_PORTA_BASE + (GPIO_O_DATA + ALL_BITS));
uint8_t Button1 = (BIT7HI)&HWREG(GPIO_PORTA_BASE + (GPIO_O_DATA + ALL_BITS));
uint8_t C_bits = (BIT6HI | BIT7HI) & HWREG(GPIO_PORTC_BASE + (GPIO_O_DATA +
ALL_BITS));
ReturnVal = (C_bits | (Button2 >> 1) | (Button1 >> 3));
return ReturnVal;
}

bool InitializeGameplayService(uint8_t Priority)


{
MyPriority = Priority;
CurrentState = InitialGameState;
FourBitOutputPattern = FIRST_LED_ROW;
SR_WriteMapAllOff();
LastButtonState_Button1 = 0;
LastButtonState_Button2 = 0;
LastButtonState_Button3 = 0;
LastButtonState_Button4 = 0;
LastButtonStates = 0;
FirstButton = 0;
SecondButton = 0;
ThirdButton = 0;
FourthButton = 0;
ButtonPattern = 0;
MapCounter = 0;
NumTimeouts = 0;
NumLEDsOn = 0;
AnyButtonsPressed = false;

//Turn off button backlights


HWREG(GPIO_PORTF_BASE + (GPIO_O_DATA + ALL_BITS)) &= BIT3LO;

return true;
}

ES_Event RunGameplayService(ES_Event ThisEvent)


{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT;
GameplayServiceState_t NextState = CurrentState;
switch (CurrentState)
{
case InitialGameState:
{
if (ThisEvent.EventType == WELCOME_DONE)
{
NextState = ActiveState;
uint32_t results[2];
ADC_MultiRead(results);
ES_Event ThisEvent;
ThisEvent.EventType = SEED_RECEIVED;
ThisEvent.EventParam = results[1];
PostLEDServiceSM(ThisEvent);

//Seed for random number generator is determined by ADC count of knob


//pin
srand(results[1]);

//Populate LED pattern array with numbers between 0 and 15


for (int i = 0; i < NUM_ROWS; i++)
{
LEDRowBank[i] = (rand() % 15);
}

//Turn on button backlights since game is starting


HWREG(GPIO_PORTF_BASE + (GPIO_O_DATA + ALL_BITS)) |= BIT3HI;
}
}
break;

case ActiveState:
{
//If game is over
if (((ThisEvent.EventType == FENCE_HIT) && (ThisEvent.EventParam ==
PLAYER_FINISH_LINE))
|| ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
GAME_TIMER)) ||
((ThisEvent.EventType == FENCE_HIT) && (ThisEvent.EventParam ==
OPPONENT_FINISH_LINE)))
{

//Turn off button backlights since game is finished


HWREG(GPIO_PORTF_BASE + (GPIO_O_DATA + ALL_BITS)) &= BIT3LO;

//If no buttons are pressed by the end of the game, reset system
if (!AnyButtonsPressed)
{
LastButtonState_Button1 = 0;
LastButtonState_Button2 = 0;
LastButtonState_Button3 = 0;
LastButtonState_Button4 = 0;
LastButtonStates = 0;
FirstButton = 0;
SecondButton = 0;
ThirdButton = 0;
FourthButton = 0;
ButtonPattern = 0;
MapCounter = 0;
NumTimeouts = 0;
NumLEDsOn = 0;
NextState = InitialGameState;
ES_Event ThisEvent;
ThisEvent.EventType = USER_DEPARTED;
ES_PostList03(ThisEvent);
}
else
{
NextState = FinishedGameState;
if ((ThisEvent.EventType == FENCE_HIT) && (ThisEvent.EventParam ==
PLAYER_FINISH_LINE))
{
//If player hits the finish line, player wins the game so the
//last LED on the map lights up
SR_WriteMapNextOn();
}
}
}
if (ThisEvent.EventType == BUTTONPRESSED)
{
//Button 1 pressed
if (ThisEvent.EventParam == 1)
{
FirstButton = 1;
}

//Button 2 pressed
if (ThisEvent.EventParam == 2)
{
SecondButton = 1;
}

//Button 3 pressed
if (ThisEvent.EventParam == 3)
{
ThirdButton = 1;
}

//Button 4 pressed
if (ThisEvent.EventParam == 4)
{
FourthButton = 1;
}
}
//If LED pattern expires, process user interactions during the life of
//that pattern
if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
ROW_TIMER))
{
uint8_t CurrentBStates = (GetCurrentButtonStates() >> 4);
NumTimeouts++;
ButtonPattern = ((FourthButton << 3) | (ThirdButton << 2) |
(SecondButton << 1) | FirstButton);
if (ButtonPattern != 0)
{
AnyButtonsPressed = true;
}
ButtonPattern |= CurrentBStates;

//Check if pattern of buttons matches pattern of LEDs for the row that
//just expired
if (((ButtonPattern == LEDRowBank[NumTimeouts -
NUM_TIMEOUTS_SECOND_PATTERN - 1]) &&
(NumTimeouts > NUM_TIMEOUTS_SECOND_PATTERN)) || ((ButtonPattern ==
FourBitOutputPattern) && (NumTimeouts == NUM_TIMEOUTS_FIRST_PATTERN)))
{
MotorPlayerForward();
MapCounter++;
if ((MapCounter == MAP_COUNTER_MAX) && (NumLEDsOn < 6))
{
SR_WriteMapNextOn();
NumLEDsOn++;
MapCounter = 0;
}
}
else
{
//Buzz buttons that user pressed to give user feedback that user's
//button pattern did not match LED pattern
MotorBuzzStart(ButtonPattern);
}
ButtonPattern = 0;
FirstButton = 0;
SecondButton = 0;
ThirdButton = 0;
FourthButton = 0;
}
}
break;
case FinishedGameState:
{
//If flag has reached bottom of trajectory
if ((ThisEvent.EventType == FENCE_HIT) && (ThisEvent.EventParam ==
FLAG_START_POSITION))
{
LastButtonState_Button1 = 0;
LastButtonState_Button2 = 0;
LastButtonState_Button3 = 0;
LastButtonState_Button4 = 0;
LastButtonStates = 0;
FirstButton = 0;
SecondButton = 0;
ThirdButton = 0;
FourthButton = 0;
ButtonPattern = 0;
MapCounter = 0;
NumTimeouts = 0;
NumLEDsOn = 0;
NextState = InitialGameState;
}
}
break;
}
CurrentState = NextState;
return ReturnEvent;
}

bool PostGameplayService(ES_Event ThisEvent)


{
return ES_PostToService(MyPriority, ThisEvent);
}

bool CheckButtonEvents()
{
bool ReturnValue = false;
uint8_t CurrentButtonStates = GetCurrentButtonStates();
uint8_t CurrentButtonState_Button1 = (CurrentButtonStates & BIT4HI);
uint8_t CurrentButtonState_Button2 = (CurrentButtonStates & BIT5HI);
uint8_t CurrentButtonState_Button3 = (CurrentButtonStates & BIT6HI);
uint8_t CurrentButtonState_Button4 = (CurrentButtonStates & BIT7HI);

//If a button has changed states


if (CurrentButtonStates != LastButtonStates)
{
ES_Event ThisEvent;
ThisEvent.EventType = BUTTONPRESSED;
if (CurrentButtonState_Button1 != LastButtonState_Button1)
{
if (CurrentButtonState_Button1 != 0)
{
//Button 1 has been pressed
ThisEvent.EventParam = 1;
PostGameplayService(ThisEvent);
ReturnValue = true;
}
}
if (CurrentButtonState_Button2 != LastButtonState_Button2)
{
if (CurrentButtonState_Button2 != 0)
{
//Button 2 has been pressed
ThisEvent.EventParam = 2;
PostGameplayService(ThisEvent);
ReturnValue = true;
}
}
if (CurrentButtonState_Button3 != LastButtonState_Button3)
{
if (CurrentButtonState_Button3 != 0)
{
//Button 3 has been pressed
ThisEvent.EventParam = 3;
PostGameplayService(ThisEvent);
ReturnValue = true;
}
}
if (CurrentButtonState_Button4 != LastButtonState_Button4)
{
if (CurrentButtonState_Button4 != 0)
{
//Button 4 has been pressed
ThisEvent.EventParam = 4;
PostGameplayService(ThisEvent);
ReturnValue = true;
}
}
}
LastButtonStates = CurrentButtonStates;
LastButtonState_Button1 = CurrentButtonState_Button1;
LastButtonState_Button2 = CurrentButtonState_Button2;
LastButtonState_Button3 = CurrentButtonState_Button3;
LastButtonState_Button4 = CurrentButtonState_Button4;
return ReturnValue;
}

Das könnte Ihnen auch gefallen