Você está na página 1de 3

//This module governs the LED matrix, including the

//patterns that stream down during gameplay

// 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 "LEDServiceSM.h"
#include "ShiftRegisterWrite.h"
#include "MarathonServiceSM.h"
#include "ADMulti.h"

#define FIRST_LED_ROW 15
#define ROW_TIMER_DURATION_BASE 500
#define ROW_TIMER_DURATION_1 2000
#define ROW_TIMER_DURATION_2 1000
#define ROW_TIMER_DURATION_QUICK 500
#define NUM_ROWS 96
#define GAME_TIMER_DURATION 24000
#define KNOB_THRESHOLD 2000
#define NUM_TIMEOUTS_FIRST_PATTERN 4
#define NUM_PST_TIMEOUTS_FIRST_PATTERN 3
#define PATTERN_MAX_VALUE 15

#define PLAYER_FINISH_LINE 1
#define OPPONENT_FINISH_LINE 3
#define FLAG_START_POSITION 4

static uint8_t MyPriority;


static uint8_t NumberOfTimeouts;
static LEDServiceSMState_t CurrentState;
static uint8_t FourBitOutputPattern;
static uint8_t NumberOfPSTimeouts;
static uint8_t LEDRowBank[NUM_ROWS];

bool InitializeLEDServiceSM(uint8_t Priority)


{
MyPriority = Priority;
NumberOfTimeouts = 0;
NumberOfPSTimeouts = 0;
CurrentState = InitialLEDState;
FourBitOutputPattern = FIRST_LED_ROW;
return true;
}
bool PostLEDServiceSM(ES_Event ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

ES_Event RunLEDServiceSM(ES_Event ThisEvent)


{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT;
LEDServiceSMState_t NextState = CurrentState;

switch (CurrentState)
{
case InitialLEDState:
{
if (ThisEvent.EventType == WELCOME_DONE)
{
SR_Write16(0);
SR_Write4(FourBitOutputPattern);
NextState = OneRowState;
ES_Timer_InitTimer(ROW_TIMER, ROW_TIMER_DURATION_BASE);
}
}
break;
case OneRowState:
{
//If seed for random number generator is received, populate array with
//numbers between 0 and 15
if (ThisEvent.EventType == SEED_RECEIVED)
{
uint32_t seed = ThisEvent.EventParam;
srand(seed);
for (int i = 0; i < NUM_ROWS; i++)
{
LEDRowBank[i] = (rand() % PATTERN_MAX_VALUE);
}
}
if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
ROW_TIMER))
{
NumberOfTimeouts++;
SR_Write4(0);
ES_Timer_InitTimer(ROW_TIMER, ROW_TIMER_DURATION_BASE);
if (NumberOfTimeouts == NUM_TIMEOUTS_FIRST_PATTERN)
{
NextState = PlayState;
ES_Timer_InitTimer(GAME_TIMER, GAME_TIMER_DURATION);
}
}
}
break;
case PlayState:
{
//If game is finished
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)))
{
ES_Timer_StopTimer(ROW_TIMER);
SR_Write16(0);
NextState = FinishedState;
MotorOpponentStop();
MotorPlayerStop();
}
if ((ThisEvent.EventType == ES_TIMEOUT) && (ThisEvent.EventParam ==
ROW_TIMER))
{
if (NumberOfPSTimeouts > NUM_PST_TIMEOUTS_FIRST_PATTERN)
{
MotorOpponentForward();
}
NumberOfPSTimeouts++;
SR_Write4(LEDRowBank[NumberOfPSTimeouts - 1]);
uint32_t results[2];
ADC_MultiRead(results);
uint16_t RowTimerDuration = (results[1] < KNOB_THRESHOLD) ?
ROW_TIMER_DURATION_1 : ROW_TIMER_DURATION_2;
if (NumberOfPSTimeouts < NUM_PST_TIMEOUTS_FIRST_PATTERN)
{
ES_Timer_InitTimer(ROW_TIMER, ROW_TIMER_DURATION_QUICK);
}
else
{
ES_Timer_InitTimer(ROW_TIMER, RowTimerDuration);
}
}
}
break;

case FinishedState:
{
if ((ThisEvent.EventType == FENCE_HIT) && (ThisEvent.EventParam ==
FLAG_START_POSITION))
{
NextState = InitialLEDState;
NumberOfTimeouts = 0;
NumberOfPSTimeouts = 0;
}
}
break;
}
CurrentState = NextState;

return ReturnEvent;
}

Você também pode gostar