Beruflich Dokumente
Kultur Dokumente
#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
//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;
}
return true;
}
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)))
{
//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 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);