Code used in the table for the Pong Demo
Dependencies: PixelArray WS2812 mbed
The project for the code used in the table of the "Pong Demo"
Revision 1:24bc4d8ed2ae, committed 2017-03-20
- Comitter:
- benswindell
- Date:
- Mon Mar 20 12:12:46 2017 +0000
- Parent:
- 0:f2b739e846ae
- Commit message:
- Publishing the table controller code used at Embedded World;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r f2b739e846ae -r 24bc4d8ed2ae main.cpp --- a/main.cpp Mon Feb 27 16:38:51 2017 +0000 +++ b/main.cpp Mon Mar 20 12:12:46 2017 +0000 @@ -15,7 +15,7 @@ #define BLUE 0x0000ff // Player scored a goal #define PINK 0x2f002f #define OFF 0x000000 -#define WHITE 0xffffff +#define WHITE 0xffffaa #define ARMBLUE 0x128BAB #define PURPLE 0xff0055 // Player has conceded a goal @@ -32,21 +32,26 @@ // The given numbers are for the K64F WS2812 robotScoreLED(D9, WS2812_BUF, 0, 5, 5, 0); WS2812 playerScoreLED(D8,WS2812_BUF, 0, 5, 5, 0); -WS2812 robotGoalLED(D7,WS2812_BUF, 0, 5, 5, 0); -WS2812 playerGoalLED(D6,WS2812_BUF, 0, 5, 5, 0); +WS2812 robotGoalLED(D5,WS2812_BUF, 0, 5, 5, 0); +WS2812 playerGoalLED(D3,WS2812_BUF, 0, 5, 5, 0); // LED Variables bool seg1A, seg1B, seg1C, seg1D, seg1E, seg1F, seg1G; int seg1Array[112]; int mainArray[11][122]; -PixelArray px(WS2812_BUF); +PixelArray robotScorePx(WS2812_BUF); +PixelArray playerScorePx(WS2812_BUF); +PixelArray robotGoalPx(WS2812_BUF); +PixelArray playerGoalPx(WS2812_BUF); +const int PLAYERGOALLEDS = 55; +const int ROBOTGOALLEDS = 55; // Score counters int robotScore; int playerScore; int scoreLimit = 3; bool finishedGame = false; -int endFlashes = 0; +int endFlashes = 3; int numFlashes; // Robot Bream Beam value @@ -60,25 +65,28 @@ void WritePlayerGoal(bool scored); void WriteScores(); void HandleGoal(bool hasRobotScored); -void WritePxScores(int line_num); -void WritePxGoal(unsigned int colour); +void WritePxScores(int line_num, bool isRobot); +void WritePxGoal(unsigned int colour,bool isRobot); +void HandleWin(); +void WriteGoalsOff(); void setup() { - // Set brightness of the 4 LED strips - robotScoreLED.setII(0xFF); - robotScoreLED.useII(WS2812::GLOBAL); - playerScoreLED.setII(0xFF); - playerScoreLED.useII(WS2812::GLOBAL); - robotGoalLED.setII(0xFF); - robotGoalLED.useII(WS2812::GLOBAL); - playerGoalLED.setII(0xFF); - playerGoalLED.useII(WS2812::GLOBAL); // Turn on green LED led_green = 0; + // Set brightness of the 4 LED strips + robotScoreLED.setII(0x19); + robotScoreLED.useII(WS2812::GLOBAL); + playerScoreLED.setII(0x19); + playerScoreLED.useII(WS2812::GLOBAL); + robotGoalLED.setII(0x19); + robotGoalLED.useII(WS2812::GLOBAL); + playerGoalLED.setII(0x80); + playerGoalLED.useII(WS2812::GLOBAL); + // Fills 2D array with data for(int i=0; i<10; i++) { Write7Seg(i); @@ -95,7 +103,10 @@ WritePlayerGoal(true); wait(0.01); WriteRobotGoal(true); - + + wait(5); + + WriteGoalsOff(); // Turn off green LED led_green = 1; } @@ -297,65 +308,97 @@ // Write to player 1 LED (ROBOT) void WriteRobotGoal(bool scored) { - if(scored == true) - { - WritePxGoal(BLUE); + if(scored == true) { + WritePxGoal(BLUE,true); } else { - WritePxGoal(PURPLE); + WritePxGoal(PURPLE,true); } - robotGoalLED.write(px.getBuf()); + robotGoalLED.write(robotGoalPx.getBuf()); } // Write to player 1 LED (ROBOT) void WritePlayerGoal(bool scored) { - if(scored == true) - { - WritePxGoal(BLUE); + if(scored == true) { + WritePxGoal(BLUE,false); } else { - WritePxGoal(PURPLE); + WritePxGoal(PURPLE,false); } - playerGoalLED.write(px.getBuf()); + playerGoalLED.write(playerGoalPx.getBuf()); } +// Write OFF to both goals void WriteGoalsOff() { - WritePxGoal(OFF); - robotGoalLED.write(px.getBuf()); - WritePxGoal(OFF); - playerGoalLED.write(px.getBuf()); + WritePxGoal(OFF,true); + robotGoalLED.write(robotGoalPx.getBuf()); + WritePxGoal(OFF,false); + playerGoalLED.write(playerGoalPx.getBuf()); } +// Update both Score LEDs with the current score void WriteScores() { - WritePxScores(playerScore); - playerScoreLED.write(px.getBuf()); + WritePxScores(playerScore,false); + playerScoreLED.write(playerScorePx.getBuf()); wait(0.01); - WritePxScores(robotScore); - robotScoreLED.write(px.getBuf()); + WritePxScores(robotScore,true); + robotScoreLED.write(robotScorePx.getBuf()); } // Write pixel array -void WritePxScores(int line_num) +void WritePxScores(int line_num,bool isRobot) { - for (int i = 0; i < WS2812_BUF; i++) { - if (mainArray[line_num][i] == 0) { - px.Set(i,OFF); + if(isRobot == true) { + + for (int i = 0; i < WS2812_BUF; i++) { + if (mainArray[line_num][i] == 0) { + robotScorePx.Set(i,OFF); + } + + if (mainArray[line_num][i] == 1) { + robotScorePx.Set(i,WHITE); + } } + } else { + for (int i = 0; i < WS2812_BUF; i++) { + if (mainArray[line_num][i] == 0) { + playerScorePx.Set(i,OFF); + } - if (mainArray[line_num][i] == 1) { - px.Set(i,LIGHTBLUE); + if (mainArray[line_num][i] == 1) { + playerScorePx.Set(i,WHITE); + } + } + } + +} + +// Write Goal pixel array, using input colour for the specified goal +// INPUTS: colour, isRobot +// +// colour - This is the colour that you wish to set the array to +// isRobot - This specifies which goal to set the colours of, based on if it is the Robot side goal or not +// +void WritePxGoal(unsigned int colour,bool isRobot) +{ + if(isRobot == true) { + for (int i = 0; i < WS2812_BUF; i++) { + robotGoalPx.Set(i,colour); + } + } else { + for (int i = 0; i < WS2812_BUF; i++) { + playerGoalPx.Set(i,colour); } } } -void WritePxGoal(unsigned int colour) -{ - for (int i = 0; i < WS2812_BUF; i++) { - px.Set(i,colour); - } -} - +// Write the goal LEDs and score LEDs when a goal is recorded. Also check to see if the goal being scored +// is the final point of the game and the game is over. +// INPUTS: hasRobotScored +// +// hasRobotScored - true if the goal being recorded is from the playerside goal, meaning the robot has scored a point +// void HandleGoal(bool hasRobotScored) { if(hasRobotScored == true) { @@ -373,35 +416,84 @@ wait(0.01); WriteScores(); } - + wait(5); - WriteGoalsOff(); + + // If either player reaches the score limit, then call HandleWin(). If not, then turn goal LEDs off + if(robotScore == scoreLimit || playerScore == scoreLimit) { + HandleWin(); + } else { + WriteGoalsOff(); + } +} + +// Handle behaviour when either the player or robot has won a game. +void HandleWin() +{ + // Initalise variable as true + bool isRobotWinner = true; + + // Decide if the robot is the winner. If not, then change isRobotWinner to false + if(playerScore == scoreLimit) { + isRobotWinner = false; + pc.printf("Player won!"); + + // Animation for goal LEDs. Turns off the robot Goal LEDs, working from in the middle point outwards. + for(int i = 0; i < (ROBOTGOALLEDS / 2); i++) { + robotGoalPx.Set((ROBOTGOALLEDS /2) - i, OFF); + robotGoalPx.Set((ROBOTGOALLEDS /2) + i, OFF); + robotGoalLED.write(robotGoalPx.getBuf()); + wait(0.15); + } + + WriteGoalsOff(); + + } else { + pc.printf("Robot won!"); + + // Animation for goal LEDs. Turns off the player Goal LEDs, working from in the middle point outwards. + for(int i = 0; i < (PLAYERGOALLEDS / 2); i++) { + playerGoalPx.Set((PLAYERGOALLEDS /2) - i, OFF); + playerGoalPx.Set((PLAYERGOALLEDS /2) + i, OFF); + playerGoalLED.write(playerGoalPx.getBuf()); + wait(0.25); + } + + WriteGoalsOff(); + } + + // Reset scores then write to LEDs + robotScore = 0; + playerScore = 0; + WriteScores(); } int main() { + setup(); + while(1) { double rbbValue = robotBreakBeam; // Read Robot Break beam double pbbValue = playerBreakBeam; // Read Player Break beam // IF PLAYER HAS SCORED A GOAL - if ((prevRbbValue - rbbValue)>0.01) { + if ((prevRbbValue - rbbValue)>0.03) { pc.printf("Player has scored a goal \n\r"); HandleGoal(false); } // IF ROBOT HAS SCORED A GOAL - if ((prevPbbValue - pbbValue) > 0.01) { + if ((prevPbbValue - pbbValue) > 0.03) { pc.printf("Robot has scored a goal \n\r"); HandleGoal(true); } - + prevRbbValue = rbbValue; prevPbbValue = pbbValue; - - pc.printf("PlayerGoal: %f, RobotGoal: %f",pbbValue,rbbValue); - pc.printf("Player: %i v %i : Robot",playerScore,robotScore); - wait(0.01); + + pc.printf("PlayerGoal: %f, RobotGoal: %f \r\n",pbbValue,rbbValue); + pc.printf("Player: %i v %i : Robot \r\n",playerScore,robotScore); + wait(0.02); } } \ No newline at end of file