|
anticipation
|
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 |
This class represents a single instance of a game player.
| GamePlayer::GamePlayer | ( | int | buttonPort, |
| int | redLEDPort, | ||
| int | greenLEDPort, | ||
| int | id | ||
| ) |
This constructor will instantiate a new instance of the Game Player class.
| buttonPort | This is the port number to which the button is attached. The port is considered to have a pull up resistor attached. |
| redLEDPort | This is the port number for the red LED that is controlled by this class. |
| greenLEDPort | This is the port number for the green LED that is controlled by this class. |
| id | This is the player ID for this instance. |
Set the total elapsed time to 0.
Setup the id to be the value passed in as a parameter.
|
virtual |
This is the destructor for the class. It will release the GPIO resources that were allocated for this player.
Release the allocated GPIO instances.
| void GamePlayer::designateWinner | ( | ) |
This method will designate the winner of the game.
Turn the green light on.

| 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.

| void GamePlayer::run | ( | ) |
This is the run method. It contains the logic for this thread which deals with running the game.
Start by turning the red light on and the green light off.
Now enable button interrupts on falling edge when the button is pressed. Note: Make sure you understand why it is falling.
Enter into a loop that will run 10 times.
Sleep for at least 1s.
Generate a random number between 0 and 499, and use it to delay for up to 5 seconds.
Grab a system time and populate a timeval structure.
Swap the state of the lights, turning the red light off and the green light on.
Wait for the user to press a button.
Now that the user has pressed a button, determine the difference in time and convert into milliseconds.
Change the lights back to the initial state, with the red light on and the green light off.
Calculate the latency in milliseconds.
Print out the latency to the console.
Add the latency from this round of the game to the total value.
Turn the lights off.

|
private |
This is a reference to the input button for this player.
|
private |
This is the green LED that is driven by this class.
|
private |
This is the player id number, which identifies the LED and pushbutton set that belongs to this player.
|
private |
This is the red LED that is driven by this class.
|
private |
This is the elapsed time that has been taken by this player, given in ms.