updated 7seg controls for new 7 seg boards
Dependencies: PixelArray WS2812 mbed
Fork of frdm_pong_table_controller by
Diff: main.cpp
- Revision:
- 9:f3f194982bb3
- Parent:
- 8:66dac2e00cce
- Child:
- 10:554301a1579b
--- a/main.cpp Tue Jul 11 08:51:23 2017 +0000 +++ b/main.cpp Tue Jul 11 13:02:44 2017 +0000 @@ -4,64 +4,24 @@ { Setup(); - pc.printf("\nIDLE"); + pc.printf("\nSETUP(IDLE)"); while(1) { - if (idle_flag == 0) { - - if (idle_button_pressed == 1) { // button just pressed + if (idle_flag == 0) { // IDLE - pc.printf("\nIDLE"); - - idle_button_pressed = 0; - - PlayToIdleTransition(); - - } - - CircleAnimation(true,true,true,true,1); - - wait(0.02); + IdleStateProtocol(); } - if (idle_flag == 1) { - - if (idle_button_pressed == 1) { // button just pressed - - pc.printf("\nPLAY"); - - idle_button_pressed = 0; - - IdleToPlayTransition(); - - } + if (idle_flag == 1) { // PLAY - double rbbValue = robotBreakBeam; // Read Robot Break beam - double pbbValue = playerBreakBeam; // Read Player Break beam - - // IF PLAYER HAS SCORED A GOAL - if (((prevRbbValue - rbbValue)>0.03)|| (PB1==0)) { - //pc.printf("Player has scored a goal \n\r"); - HandleGoal(false); - } - - // IF ROBOT HAS SCORED A GOAL - if (((prevPbbValue - pbbValue) > 0.03)|| (PB2==0)) { - //pc.printf("Robot has scored a goal \n\r"); - HandleGoal(true); - } - - prevRbbValue = rbbValue; - prevPbbValue = pbbValue; - - //pc.printf("PlayerGoal: %f, RobotGoal: %f \r\n",pbbValue,rbbValue); - //pc.printf("Player: %i v %i : Robot \r\n",playerScore,robotScore); - wait(0.02); + PlayStateProtocol(); } - + + wait(0.02); + } } @@ -95,14 +55,65 @@ } +void IdleStateProtocol() +{ + if (idle_button_pressed == 1) { // button just pressed + + pc.printf("\nIDLE"); + + PlayToIdleTransition(); + + } + + FigureOf8Animation(true,true,true,true,1); + CircleAnimation(true,true,true,true,1); + +} + +void PlayStateProtocol() +{ + if (idle_button_pressed == 1) { // button just pressed + + pc.printf("\nPLAY"); + + IdleToPlayTransition(); + + } + + double rbbValue = robotBreakBeam; // Read Robot Break beam + double pbbValue = playerBreakBeam; // Read Player Break beam + + // IF PLAYER HAS SCORED A GOAL + if (((prevRbbValue - rbbValue)>0.03)|| (PB1==0)) { + //pc.printf("Player has scored a goal \n\r"); + HandleGoal(false); + } + + // IF ROBOT HAS SCORED A GOAL + if (((prevPbbValue - pbbValue) > 0.03)|| (PB2==0)) { + //pc.printf("Robot has scored a goal \n\r"); + HandleGoal(true); + } + + prevRbbValue = rbbValue; + prevPbbValue = pbbValue; + + //pc.printf("PlayerGoal: %f, RobotGoal: %f \r\n",pbbValue,rbbValue); + //pc.printf("Player: %i v %i : Robot \r\n",playerScore,robotScore); + +} + void IdleToPlayTransition() { + // reset scores robotScore = 0; playerScore = 0; + // reset array memset(mainArray, 0, sizeof mainArray); - for (int i = 9; i < 115; i++) { // loop to set LEDs around the circle from start of segment A to end of Segment F + // form closed loop of LEDs around exterior of display in opposite directions for robot and player scores + for (int i = 9; i < 115; i++) { if (i < 104) { mainArray[0][i] = 1; @@ -126,29 +137,35 @@ } - DrainAnimation(true,true,robotScore,true,true,playerScore); + // drain closed loop away + DrainAnimation(true,true,true,true); wait(1); + // flash 0-0 on display WriteScores(); idleButton.enable_irq(); + idle_button_pressed = 0; + } void PlayToIdleTransition() { + // reset scores and main array robotScore = 0; playerScore = 0; memset(mainArray, 0, sizeof mainArray); - wait(1); - + FigureOf8Animation(true,true,true,true,1); + idleButton.enable_irq(); + + idle_button_pressed = 0; } - // set segment number patterns void SetNumberPatterns() { @@ -411,7 +428,7 @@ { GoalAnimation(hasRobotScored); - // If either player reaches the score limit, then call HandleWin(). If not, then turn goal LEDs off + // If either player reaches the score limit, then call HandleWin() if(robotScore == scoreLimit || playerScore == scoreLimit) { HandleWin(); } @@ -440,6 +457,7 @@ robotScore = 0; playerScore = 0; + // IDLE state idle_flag = 0; memset(mainArray, 0, sizeof mainArray); @@ -451,6 +469,7 @@ { if(hasRobotScored == true) { + // clear current score switch (robotScore) { case 0: ZeroOutAnimation(true,false,false,false); @@ -466,10 +485,12 @@ default : break; } - + + // increment score robotScore++; wait_ms(250); + // write new score switch (robotScore) { case 0: ZeroInAnimation(true,false,false,false); @@ -481,13 +502,14 @@ TwoInAnimation(true,false,false,false); break; case 3: - ThreeInAnimation(true,true,false,false); + ThreeInAnimation(true,true,false,false); // with random colour pattern default : break; } } else { + // clear current score switch (playerScore) { case 0: ZeroOutAnimation(false,false,true,false); @@ -503,9 +525,12 @@ default : break; } + + // increment score playerScore++; wait_ms(250); + // write new score switch (playerScore) { case 0: ZeroInAnimation(false,false,true,false); @@ -517,7 +542,7 @@ TwoInAnimation(false,false,true,false); break; case 3: - ThreeInAnimation(false,false,true,true); + ThreeInAnimation(false,false,true,true); // with random colour pattern default : break; } @@ -541,7 +566,7 @@ wait_ms(50); } - DrainAnimation(true,false,robotScore,true,true,playerScore); // drain both scores away with the player score in random colours + DrainAnimation(true,false,true,true); // drain both scores away with the player score in random colours // reset robot score brightness robotScoreLED.setII(0xB0); @@ -560,7 +585,7 @@ wait_ms(50); } - DrainAnimation(true,true,robotScore,true,false,playerScore); // drain both scores away with the robot score in random colours + DrainAnimation(true,true,true,false); // drain both scores away with the robot score in random colours // reset player score brightness playerScoreLED.setII(0xB0); @@ -571,7 +596,197 @@ } +// LEDs snake in a figure of 8 pattern in a group of 10 +void FigureOf8Animation(bool robot, bool robotColour, bool player, bool playerColour, int numberOfRepititions) +{ + for (int loops = 0; loops < numberOfRepititions; loops++) { + for (int i = 0; i < 9; i++) { // loop for LED snake to separate from centre of segment A + + mainArray[0][9+i] = 1; + mainArray[1][8-i] = 1; + if (i < 9) { + mainArray[0][i] = 0; + mainArray[1][17-i] = 0; + } + wait_ms(10); + + if (player == true) { + WritePxAnimation(0,false,playerColour); + playerScoreLED.write(playerScorePx.getBuf()); + } + if (robot == true) { + WritePxAnimation(1,true,robotColour); + robotScoreLED.write(robotScorePx.getBuf()); + } + } + + for (int i = 0; i < 17; i++) { // loop for LED snake to go down segments B and F + + mainArray[0][18+i] = 1; + mainArray[1][103-i] = 1; + mainArray[0][9+i] = 0; + if (i < 9) { + mainArray[1][8-i] = 0; + } else { + mainArray[1][112-i] = 0; + } + wait_ms(10); + + if (player == true) { + WritePxAnimation(0,false,playerColour); + playerScoreLED.write(playerScorePx.getBuf()); + } + if (robot == true) { + WritePxAnimation(1,true,robotColour); + robotScoreLED.write(robotScorePx.getBuf()); + } + } + + for (int i = 0; i < 18; i++) { // loop for LED snake to pass through segment G + + mainArray[0][121-i] = 1; + mainArray[1][104+i] = 1; + if (i < 9) { + mainArray[0][26+i] = 0; + mainArray[1][95-i] = 0; + } else { + mainArray[0][130-i] = 0; + mainArray[1][95+i] = 0; + } + wait_ms(10); + + if (player == true) { + WritePxAnimation(0,false,playerColour); + playerScoreLED.write(playerScorePx.getBuf()); + } + if (robot == true) { + WritePxAnimation(1,true,robotColour); + robotScoreLED.write(robotScorePx.getBuf()); + } + } + + for (int i = 0; i < 17; i++) { // loop for LED snake to pass down through segments E and C + + mainArray[0][86-i] = 1; + mainArray[1][35+i] = 1; + if (i < 9) { + mainArray[0][112-i] = 0; + mainArray[1][113+i] = 0; + } else { + mainArray[0][95-i] = 0; + mainArray[1][26+i] = 0; + } + wait_ms(10); + + if (player == true) { + WritePxAnimation(0,false,playerColour); + playerScoreLED.write(playerScorePx.getBuf()); + } + if (robot == true) { + WritePxAnimation(1,true,robotColour); + robotScoreLED.write(robotScorePx.getBuf()); + } + } + + for (int i = 0; i < 35; i++) { // loop for LED snake to pass through segments D and E AND D and C + + mainArray[0][69-i] = 1; + mainArray[1][52+i] = 1; + mainArray[0][80-i] = 0; + mainArray[1][43+i] = 0; + + wait_ms(10); + + if (player == true) { + WritePxAnimation(0,false,playerColour); + playerScoreLED.write(playerScorePx.getBuf()); + } + if (robot == true) { + WritePxAnimation(1,true,robotColour); + robotScoreLED.write(robotScorePx.getBuf()); + } + } + + for (int i = 0; i < 18; i++) { // loop for LED snake to pass through segment G + + mainArray[0][121-i] = 1; + mainArray[1][104+i] = 1; + if (i < 9) { //////////////// + mainArray[0][45-i] = 0; + mainArray[1][78+i] = 0; + if (i == 8) { + mainArray[0][36] = 0; + mainArray[0][35] = 0; + } + } else { + mainArray[0][130-i] = 0; + mainArray[1][95+i] = 0; + } + wait_ms(10); + + if (player == true) { + WritePxAnimation(0,false,playerColour); + playerScoreLED.write(playerScorePx.getBuf()); + } + if (robot == true) { + WritePxAnimation(1,true,robotColour); + robotScoreLED.write(robotScorePx.getBuf()); + } + } + + for (int i = 0; i < 17; i++) { // loop for LED snake to go up segments B and F + + mainArray[0][87+i] = 1; + mainArray[1][34-i] = 1; + if (i < 9) { + mainArray[0][112-i] = 0; + mainArray[1][113+i] = 0; + } else { + mainArray[0][78+i] = 0; + mainArray[1][43-i] = 0; + } + wait_ms(10); + + if (player == true) { + WritePxAnimation(0,false,playerColour); + playerScoreLED.write(playerScorePx.getBuf()); + } + if (robot == true) { + WritePxAnimation(1,true,robotColour); + robotScoreLED.write(robotScorePx.getBuf()); + } + } + + for (int i = 0; i < 9; i++) { // loop for LED snake to pass through the centre of segment A + + mainArray[0][i] = 1; + mainArray[1][17-i] = 1; + if (i < 9) { + mainArray[0][95+i] = 0; + mainArray[1][26-i] = 0; + } else { + mainArray[0][i-9] = 0; + mainArray[1][26-i] = 0; + } + wait_ms(10); + + if (player == true) { + WritePxAnimation(0,false,playerColour); + playerScoreLED.write(playerScorePx.getBuf()); + } + if (robot == true) { + WritePxAnimation(1,true,robotColour); + robotScoreLED.write(robotScorePx.getBuf()); + } + } + + + } + +} + +// LEDs snake in a figure of 8 pattern in a group of 10 void CircleAnimation(bool robot, bool robotColour, bool player, bool playerColour, int numberOfRepititions) { for (int loops = 0; loops < numberOfRepititions; loops++) { @@ -630,11 +845,11 @@ } // animation that drains the display from the top segment A to the bottom segment D -void DrainAnimation(bool robot, bool robotColour, int robotScore, bool player, bool playerColour, int playerScore) +void DrainAnimation(bool robot, bool robotColour,bool player, bool playerColour) { SetNumberPatterns(); - for (int i = 0; i < 10; i++) { // A + for (int i = 0; i < 10; i++) { // drain A from the centre of the segment mainArray[robotScore][9+i] = 0; mainArray[robotScore][9-i] = 0; mainArray[playerScore][9+i] = 0; @@ -649,7 +864,7 @@ robotScoreLED.write(robotScorePx.getBuf()); } } - for (int i = 0; i < 18; i++) { // B and F + for (int i = 0; i < 18; i++) { // drain B and F downwards mainArray[robotScore][18+i] = 0; mainArray[robotScore][103-i] = 0; mainArray[playerScore][18+i] = 0; @@ -666,7 +881,7 @@ } if (robotScore != 0 | playerScore != 0) { // endgame - at least one score is three - for (int i = 0; i < 18; i++) { // G + for (int i = 0; i < 18; i++) { // drain G across mainArray[robotScore][104+i] = 0; mainArray[playerScore][104+i] = 0; if (player == true) { @@ -679,7 +894,7 @@ } } } else { // start of game - both scores are 0 - for (int i = 0; i < 18; i++) { // G + for (int i = 0; i < 18; i++) { // instantly delete G mainArray[robotScore][104+i] = 0; mainArray[playerScore][104+i] = 0; } @@ -693,7 +908,7 @@ } } - for (int i = 0; i < 18; i++) { // C and E + for (int i = 0; i < 18; i++) { // drain C and E downwards mainArray[robotScore][35+i] = 0; mainArray[robotScore][86-i] = 0; mainArray[playerScore][35+i] = 0; @@ -708,7 +923,7 @@ robotScoreLED.write(robotScorePx.getBuf()); } } - for (int i = 0; i < 10; i++) { // D + for (int i = 0; i < 10; i++) { // drain D from edges to centre mainArray[robotScore][52+i] = 0; mainArray[robotScore][70-i] = 0; mainArray[playerScore][52+i] = 0; @@ -735,7 +950,7 @@ if (player == true && robot == true) { - for (int i = 0; i < 10; i++) { // A + for (int i = 0; i < 10; i++) { // write A from centre mainArray[0][9+i] = 1; mainArray[0][9-i] = 1; wait_ms(10); @@ -744,7 +959,7 @@ WritePxAnimation(robotScore,true,robotColour); robotScoreLED.write(robotScorePx.getBuf()); } - for (int i = 0; i < 18; i++) { // B and F + for (int i = 0; i < 18; i++) { // write B and F downwards mainArray[0][18+i] = 1; mainArray[0][103-i] = 1; wait_ms(10); @@ -753,7 +968,7 @@ WritePxAnimation(robotScore,true,robotColour); robotScoreLED.write(robotScorePx.getBuf()); } - for (int i = 0; i < 18; i++) { // C and E + for (int i = 0; i < 18; i++) { // write C and E downwards mainArray[0][35+i] = 1; mainArray[0][86-i] = 1; wait_ms(10); @@ -762,7 +977,7 @@ WritePxAnimation(robotScore,true,robotColour); robotScoreLED.write(robotScorePx.getBuf()); } - for (int i = 0; i < 10; i++) { // D + for (int i = 0; i < 10; i++) { // write D from edges to centre mainArray[0][52+i] = 1; mainArray[0][70-i] = 1; wait_ms(10); @@ -852,10 +1067,7 @@ robotScoreLED.write(robotScorePx.getBuf()); } - } - - SetNumberPatterns(); - + } } @@ -989,10 +1201,7 @@ robotScoreLED.write(robotScorePx.getBuf()); } - } - - SetNumberPatterns(); - + } } @@ -1034,9 +1243,6 @@ } } - - SetNumberPatterns(); - } @@ -1076,8 +1282,6 @@ } } - - SetNumberPatterns(); } @@ -1159,8 +1363,6 @@ } } - - SetNumberPatterns(); } @@ -1240,8 +1442,6 @@ } } - - SetNumberPatterns(); } @@ -1326,8 +1526,6 @@ } } - - SetNumberPatterns(); } @@ -1396,15 +1594,14 @@ } } - - SetNumberPatterns(); } void idleButtonISR() { - idle_flag = !idle_flag; - idle_button_pressed = 1; - pc.printf("\nidle flag = %d",idle_flag); - idleButton.disable_irq(); + if (idle_button_pressed == 0) { + idle_flag = !idle_flag; + idle_button_pressed = 1; + idleButton.disable_irq(); + } }