anticipation
Loading...
Searching...
No Matches
GamePlayer Class Reference

This class represents a single instance of a game player. More...

#include <GamePlayer.h>

Public Member Functions

 GamePlayer (int buttonPort, int redLEDPort, int greenLEDPort, int id)
 
virtual ~GamePlayer ()
 
void run ()
 
void designateWinner ()
 
long getTotalPlayerElapsedTime ()
 

Private Attributes

GPIO & button
 
GPIO & redLight
 
GPIO & greenLight
 
long totalPlayerElapsedTime
 
int id
 

Detailed Description

This class represents a single instance of a game player.

Constructor & Destructor Documentation

◆ GamePlayer()

GamePlayer::GamePlayer ( int  buttonPort,
int  redLEDPort,
int  greenLEDPort,
int  id 
)

This constructor will instantiate a new instance of the Game Player class.

Parameters
buttonPortThis is the port number to which the button is attached. The port is considered to have a pull up resistor attached.
redLEDPortThis is the port number for the red LED that is controlled by this class.
greenLEDPortThis is the port number for the green LED that is controlled by this class.
idThis is the player ID for this instance.
  1. Set the total elapsed time to 0.

  2. Setup the id to be the value passed in as a parameter.

◆ ~GamePlayer()

GamePlayer::~GamePlayer ( )
virtual

This is the destructor for the class. It will release the GPIO resources that were allocated for this player.

  1. Release the allocated GPIO instances.

Member Function Documentation

◆ designateWinner()

void GamePlayer::designateWinner ( )

This method will designate the winner of the game.

  1. Turn the green light on.

  2. End method.
Here is the caller graph for this function:

◆ getTotalPlayerElapsedTime()

long GamePlayer::getTotalPlayerElapsedTime ( )

This method will return the total amount of time in ms that has been spent waiting for button presses. At the start of the game it will be zero, and it will continue to grow until the end of the game.

Returns
The total elapsed time spent waiting for buttons to be pressed will be returned in ms.
Here is the caller graph for this function:

◆ run()

void GamePlayer::run ( )

This is the run method. It contains the logic for this thread which deals with running the game.

  1. Start by turning the red light on and the green light off.

  2. Now enable button interrupts on falling edge when the button is pressed. Note: Make sure you understand why it is falling.

  3. Enter into a loop that will run 10 times.

    1. Sleep for at least 1s.

    2. Generate a random number between 0 and 499, and use it to delay for up to 5 seconds.

    3. Grab a system time and populate a timeval structure.

    4. Swap the state of the lights, turning the red light off and the green light on.

    5. Wait for the user to press a button.

    6. Now that the user has pressed a button, determine the difference in time and convert into milliseconds.

    7. Change the lights back to the initial state, with the red light on and the green light off.

    8. Calculate the latency in milliseconds.

    9. Print out the latency to the console.

    10. Add the latency from this round of the game to the total value.

  4. Turn the lights off.

  5. End method.
Here is the caller graph for this function:

Member Data Documentation

◆ button

GPIO& GamePlayer::button
private

This is a reference to the input button for this player.

◆ greenLight

GPIO& GamePlayer::greenLight
private

This is the green LED that is driven by this class.

◆ id

int GamePlayer::id
private

This is the player id number, which identifies the LED and pushbutton set that belongs to this player.

◆ redLight

GPIO& GamePlayer::redLight
private

This is the red LED that is driven by this class.

◆ totalPlayerElapsedTime

long GamePlayer::totalPlayerElapsedTime
private

This is the elapsed time that has been taken by this player, given in ms.


The documentation for this class was generated from the following files: