updated 7seg controls for new 7 seg boards
Dependencies: PixelArray WS2812 mbed
Fork of frdm_pong_table_controller by
Diff: main.cpp
- Revision:
- 7:dc6f1f105c52
- Parent:
- 6:5e8e2645cd93
- Child:
- 8:66dac2e00cce
--- a/main.cpp Mon Jul 10 11:05:06 2017 +0000 +++ b/main.cpp Mon Jul 10 16:06:14 2017 +0000 @@ -3,30 +3,68 @@ int main() { Setup(); + + pc.printf("\nIDLE"); 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.03)|| (PB1==0)) { - pc.printf("Player has scored a goal \n\r"); - HandleGoal(false); - } + if (idle_flag == 0) { + + if (idle_button_pressed == 1) { // button just pressed + + pc.printf("\nIDLE"); + + idle_button_pressed = 0; + + PlayToIdleTransition(); + + } + + CircleAnimation(true,true,true,true,1); + + idleButton.enable_irq(); - // IF ROBOT HAS SCORED A GOAL - if (((prevPbbValue - pbbValue) > 0.03)|| (PB2==0)) { - pc.printf("Robot has scored a goal \n\r"); - HandleGoal(true); + wait(0.02); + } + + if (idle_flag == 1) { + + if (idle_button_pressed == 1) { // button just pressed + + pc.printf("\nPLAY"); + + idle_button_pressed = 0; + + IdleToPlayTransition(); + + idleButton.enable_irq(); + + } + + double rbbValue = robotBreakBeam; // Read Robot Break beam + double pbbValue = playerBreakBeam; // Read Player Break beam - prevRbbValue = rbbValue; - prevPbbValue = pbbValue; + // IF PLAYER HAS SCORED A GOAL + if (((prevRbbValue - rbbValue)>0.03)|| (PB1==0)) { + //pc.printf("Player has scored a goal \n\r"); + HandleGoal(false); + } - pc.printf("PlayerGoal: %f, RobotGoal: %f \r\n",pbbValue,rbbValue); - pc.printf("Player: %i v %i : Robot \r\n",playerScore,robotScore); - wait(0.02); + // 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); + + } } @@ -54,19 +92,53 @@ playerScore = 0; numFlashes = 0; - CircleAnimation(true,true,true,true,2); + memset(mainArray, 0, sizeof mainArray); + + // Turn off green LED + led_green = 1; + +} + +void IdleToPlayTransition() +{ + robotScore = 0; + playerScore = 0; + + memset(mainArray, 0, sizeof mainArray); + + for (int i = 0; i < 104; i++) { + + mainArray[0][i] = 1; + + wait_ms(10); + + WritePxAnimation(playerScore,false,true); + playerScoreLED.write(playerScorePx.getBuf()); + + WritePxAnimation(robotScore,true,true); + robotScoreLED.write(robotScorePx.getBuf()); + + } + SetNumberPatterns(); - DrainAnimation(true,true,robotScore,true,true,playerScore); // run animation for both scores with random colour pattern + DrainAnimation(true,true,robotScore,true,true,playerScore); - wait(1.5); + wait(1); + + WriteAnimation(); - ZeroInAnimation(true,false,true,false); +} - // Turn off green LED - led_green = 1; +void PlayToIdleTransition() +{ + robotScore = 0; + playerScore = 0; + memset(mainArray, 0, sizeof mainArray); + } + // set segment number patterns void SetNumberPatterns() { @@ -357,12 +429,10 @@ robotScore = 0; playerScore = 0; - CircleAnimation(true,true,true,true,2); - DrainAnimation(true,true,robotScore,true,true,playerScore); + idle_flag = 0; - wait(2); + memset(mainArray, 0, sizeof mainArray); - ZeroInAnimation(true,false,true,false); } // animation for when a goal is scored @@ -492,54 +562,39 @@ void CircleAnimation(bool robot, bool robotColour, bool player, bool playerColour, int numberOfRepititions) -{ - memset(mainArray, 0, sizeof mainArray); - +{ for (int loops = 0; loops < numberOfRepititions; loops++) { - for (int i = 0; i < 115; i++) { // loop to set LEDs around the circle from start of segment A to end of Segment F + for (int i = 9; i < 115; i++) { // loop to set LEDs around the circle from start of segment A to end of Segment F if ( i < 104) { mainArray[0][i] = 1; + for (int j = 0; j < i-10; j++) { // Loop to clear previously set LEDs so that the chain that animates round the display is 9 LEDs long + mainArray[0][j] = 0; + } } - for (int j = 0; j < i-10; j++) { // Loop to clear previously set LEDs so that the chain that animates round the display is 9 LEDs long - mainArray[0][j] = 0; + if (i > 103) { + mainArray[0][i-104] = 1; + for (int j = 90; j < i-10; j++) { // Loop to clear previously set LEDs so that the chain that animates round the display is 9 LEDs long + mainArray[0][j] = 0; + } } - + wait_ms(10); if (player == true) { - WritePxAnimation(playerScore,false,playerColour); + WritePxAnimation(0,false,playerColour); playerScoreLED.write(playerScorePx.getBuf()); } if (robot == true) { - WritePxAnimation(robotScore,true,robotColour); + WritePxAnimation(0,true,robotColour); robotScoreLED.write(robotScorePx.getBuf()); } } } - for (int i = 0; i < 104; i++) { - - mainArray[0][i] = 1; - - wait_ms(10); - - if (player == true) { - WritePxAnimation(playerScore,false,playerColour); - playerScoreLED.write(playerScorePx.getBuf()); - } - if (robot == true) { - WritePxAnimation(robotScore,true,robotColour); - robotScoreLED.write(robotScorePx.getBuf()); - } - - } - - SetNumberPatterns(); - } @@ -1317,4 +1372,7 @@ void idleButtonISR() { idle_flag = !idle_flag; + idle_button_pressed = 1; + pc.printf("\nidle flag = %d",idle_flag); + idleButton.disable_irq(); }