Spaceship

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

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

Module
Spaceship.c

Revision
1.0.1

Description
This is a Service to update space ship location in galaga

Notes

History
When Who What/Why
-------------- --- --------
10/30/20 EM Created File
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* 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 "ES_Configure.h"
#include "ES_Framework.h"
#include "Spaceship.h"
#include "UpdateOLED.h"
#include "PIC32_AD_Lib.h"
#include "Gameplay.h"
/*----------------------------- Module Defines ----------------------------*/
#define DEFAULT_SHIP_POS 3
#define OLED_TEST
#define SPACESHIP 1
#define PROJECTILE 2
#define ENEMY_SHIP 3

#define LENGTH_OF_SCREEN 128


#define WIDTH_OF_SCREEN 64
#define TIME_STEP 50
#define TIME_STEP_JOYSTICK 250
/*---------------------------- Module Functions ---------------------------*/
/* prototypes for private functions for this machine.They should be functions
relevant to the behavior of this state machine
*/

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


// everybody needs a state variable, you may need others as well.
// type of state variable should match htat of enum in header file
static SpaceshipState_t CurrentState;

// with the introduction of Gen2, we need a module level Priority var as well
static uint8_t MyPriority;
static uint32_t CalibrationJoystickLocation[2];
static int16_t ShipPositionX;
static uint8_t deltaT;
static uint8_t TimeDiscretization;

/*------------------------------ Module Code ------------------------------*/


/****************************************************************************
Function
InitSpaceship

Parameters
uint8_t : the priorty of this service

Returns
bool, false if error in initialization, true otherwise

Description
Saves away the priority, initializes input pins to be read for joystick
movement
Notes

Author
Andrew Low
****************************************************************************/
bool InitSpaceship(uint8_t Priority)
{
ES_Event_t ThisEvent;

MyPriority = Priority;
// put us into the Initial PseudoState
CurrentState = InitSpaceshipState;

// Sets ANSEL and TRIS bits on pins B2 and B3 to read analog input values
ANSELBbits.ANSB2 = 1;
TRISBbits.TRISB2 = 1;
ANSELBbits.ANSB3 = 1;
TRISBbits.TRISB3 = 1;

// Configures pins B2 and B3 to sample and read analog inputs


ADC_ConfigAutoScan((BIT4HI | BIT5HI), 2);

// Sets initial position of the ship and time discretizations


ShipPositionX = LENGTH_OF_SCREEN / 2;
deltaT = 1;
TimeDiscretization = 150;

// post the initial transition event


ThisEvent.EventType = ES_INIT;
if (ES_PostToService(MyPriority, ThisEvent) == true)
{
return true;
}
else
{
return false;
}
}

/****************************************************************************
Function
PostSpaceship

Parameters
ES_Event_t ThisEvent, the event to post to the queue

Returns
boolean False if the Enqueue operation failed, True otherwise

Description
Posts an event to this state machine's queue
Notes

Author
Andrew Low
****************************************************************************/
bool PostSpaceship(ES_Event_t ThisEvent)
{
return ES_PostToService(MyPriority, ThisEvent);
}

/****************************************************************************
Function
RunSpaceship

Parameters
ES_Event_t : the event to process

Returns
ES_Event_t, ES_NO_EVENT if no error ES_ERROR otherwise

Description
add your description here
Notes
uses nested switch/case to implement the machine.
Author
Andrew Low
****************************************************************************/
ES_Event_t RunSpaceship(ES_Event_t ThisEvent)
{
ES_Event_t ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors

switch(CurrentState)
{
case InitSpaceshipState: // Initial pseudo state
{
if(ThisEvent.EventType == ES_INIT)
{
// Initializes JOYSTICK_TIMER to a time of 250 ms
ES_Timer_InitTimer(JOYSTICK_TIMER, TIME_STEP_JOYSTICK);

// Reads analog values from joystick and assigns them to


// CalibrationJoystickLocation
ADC_MultiRead(CalibrationJoystickLocation);
CalibrationJoystickLocation[0] = CalibrationJoystickLocation[0] /
TimeDiscretization;
CalibrationJoystickLocation[1] = CalibrationJoystickLocation[1] /
TimeDiscretization;
}
// Sets CurrenState to TitleState
CurrentState = TitleState;
}
break;
case TitleState: // Title Screen state
{
switch(ThisEvent.EventType)
{
case ES_INIT_TITLE_JOYSTICK:
{
// Resets position of ship on the OLED screen
// Initializes JOYSTICK_TIMER to a time of 250 ms
ShipPositionX = LENGTH_OF_SCREEN / 2;
ES_Timer_InitTimer(JOYSTICK_TIMER, TIME_STEP_JOYSTICK);
}
break;
case ES_TIMEOUT: // Samples joystick four times every second
{
if(ThisEvent.EventParam == JOYSTICK_TIMER)
{
// Re-initialize JOYSTICK_TIMER to a time of 250 ms
ES_Timer_InitTimer(JOYSTICK_TIMER, TIME_STEP_JOYSTICK);

// Read the y-axis position of the joystick


uint32_t JoystickLocation[2];
ADC_MultiRead(JoystickLocation);
JoystickLocation[0] = JoystickLocation[0] / TimeDiscretization;

if(JoystickLocation[0] < CalibrationJoystickLocation[0])


{
// If the joystick was moved up from neutral position,
// post corresponding event to Gameplay service
ES_Event_t Event2Post;
Event2Post.EventParam = 0;
Event2Post.EventType = ES_JOYSTICK;
PostGameplay(Event2Post);
}
else if(JoystickLocation[0] > CalibrationJoystickLocation[0])
{
// If the joystick was moved down from neutral position,
// post corresponding event to Gameplay service
ES_Event_t Event2Post;
Event2Post.EventParam = 2;
Event2Post.EventType = ES_JOYSTICK;
PostGameplay(Event2Post);
}
}
}
break;
case ES_START_GAME: // Start the game
{
// Posts an ES_INIT_JOYSTICK event to Spaceship
// Switches states to GameState
CurrentState = GameState;
ES_Event_t Event2Post;
Event2Post.EventType = ES_INIT_JOYSTICK_MOVEMENT;
PostSpaceship(Event2Post);
}
}
}
break;
case GameState:
{
switch(ThisEvent.EventType)
{
case ES_INIT_JOYSTICK_MOVEMENT:
{
// Initializes SPACESHIP_TIMER to a time of 50 ms
ES_Timer_InitTimer(SPACESHIP_TIMER, TIME_STEP);
}
break;
case ES_TIMEOUT: // SPACESHIP_TIMER times out
{
if(ThisEvent.EventParam == SPACESHIP_TIMER)
{
// Initializes SPACESHIP_TIMER to a time of 50 ms
ES_Timer_InitTimer(SPACESHIP_TIMER, TIME_STEP);

// Read x-axis position of joystick


uint32_t JoystickLocation[2];
ADC_MultiRead(JoystickLocation);
JoystickLocation[1] = JoystickLocation[1] / TimeDiscretization;

// Updates the position of the ship based on how far the


// joystick is from its calibrated/neutral position
if(JoystickLocation[1] >= (CalibrationJoystickLocation[1]+1) ||
(JoystickLocation[1] <= (CalibrationJoystickLocation[1]-1)))
{
ShipPositionX = ShipPositionX + deltaT *
(JoystickLocation[1] - CalibrationJoystickLocation[1]);
}

// Sets the edges of the OLED screen as boundaries


// restricting TIE fighter movement
if(ShipPositionX+6 > 128)
{
ShipPositionX = 122;
}
else if(ShipPositionX-6 < 0)
{
ShipPositionX = 6;
}

// Post ES_UPDATE_OLED to update the position of the


// TIE fighter to the UpdateOLED service
ES_Event_t Event2Post;
Event2Post.EventParam = spaceshipID; //prepare event to send
to oled
Event2Post.EventType = ES_UPDATE_OLED;
PostUpdateOLED(Event2Post);

}
}
break;
case ES_GAME_OVER: // Game Over event
{
// Sets CurrentState to TitleState
CurrentState = TitleState;
}
}
}
break;
}

return ReturnEvent;
}

/****************************************************************************
Function
QuerySpaceship

Parameters
None

Returns
SpaceshipState_t variable

Description
Returns the current state of the service
Notes

Author
Andrew Low
****************************************************************************/
SpaceshipState_t QuerySpaceship(void)
{
return CurrentState;
}

/****************************************************************************
Function
QuerySpaceshipPosition

Parameters
None

Returns
uint8_t variable

Description
Returns the current position of the TIE fighter
Notes

Author
Andrew Low
****************************************************************************/
uint8_t QuerySpaceshipPosition(void){
return ShipPositionX;
}

You might also like