updated 7seg controls for new 7 seg boards

Dependencies:   PixelArray WS2812 mbed

Fork of frdm_pong_table_controller by Demo Team

Files at this revision

API Documentation at this revision

Comitter:
DanGibbons
Date:
Mon Jul 10 16:06:14 2017 +0000
Parent:
6:5e8e2645cd93
Child:
8:66dac2e00cce
Commit message:
Start/Idle push button added.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- 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();
 }
--- a/main.h	Mon Jul 10 11:05:06 2017 +0000
+++ b/main.h	Mon Jul 10 16:06:14 2017 +0000
@@ -32,10 +32,10 @@
 //K64F On-board Switches
 DigitalIn PB1(PTC6);
 DigitalIn PB2(PTA4);
-InterruptIn idleButton(PTB2);
+InterruptIn idleButton(PTC11);
 
 // SERIAL
-Serial pc(USBTX, USBRX); // tx, rx
+RawSerial pc(USBTX, USBRX); // tx, rx
 
 // LED STRIPS
 // See the program page for information on the timing numbers
@@ -61,6 +61,7 @@
 
 // Flags
 volatile int idle_flag = 0;
+volatile int idle_button_pressed = 0;
 
 // Robot Bream Beam value
 double prevRbbValue; // Previous Robot break beam value
@@ -91,6 +92,9 @@
 void ThreeInAnimation(bool robot, bool robotColour,bool player, bool playerColour);
 void ThreeOutAnimation(bool robot, bool robotColour,bool player, bool playerColour);
 
+void PlayToIdleTransition();
+void IdleToPlayTransition();
+
 // ISRs
 void idleButtonISR();