Sound update

Dependencies:   4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
jsanchez307
Date:
Thu Dec 08 02:12:14 2022 +0000
Parent:
25:c3eb6c1a2dfb
Child:
27:a09981ebd532
Commit message:
No speaker. LED works for Tetris by going through RGB one at a time. LCD successfully clears the screen after Tetris is in Game Over and goes to the Menu.

Changed in this revision

Speaker.h Show annotated file Show diff for this revision Revisions of this file
globals.h Show annotated file Show diff for this revision Revisions of this file
hockey/hockey.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
tetris/Board.h Show annotated file Show diff for this revision Revisions of this file
tetris/Game.h Show annotated file Show diff for this revision Revisions of this file
tetris/tetris.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Speaker.h	Thu Dec 08 00:37:55 2022 +0000
+++ b/Speaker.h	Thu Dec 08 02:12:14 2022 +0000
@@ -1,3 +1,6 @@
+//#ifndef __SPEAKER.H
+//#define __SPEAKER.H
+
 #include "mbed.h"
 // new class to play a note on Speaker based on PwmOut class
 class Speaker
@@ -18,4 +21,6 @@
 
 private:
     PwmOut _pin;
-};
\ No newline at end of file
+};
+
+//#endif
\ No newline at end of file
--- a/globals.h	Thu Dec 08 00:37:55 2022 +0000
+++ b/globals.h	Thu Dec 08 02:12:14 2022 +0000
@@ -13,6 +13,7 @@
 // Global state flags
 extern volatile bool game1;
 extern volatile bool game2;
+extern volatile int menu_flag;
 
 // Debug systems
 extern Serial pc;
@@ -37,5 +38,5 @@
 extern PwmOut rGb;
 extern PwmOut rgB;
 
-//extern AnalogOut DACout;
+//extern Speaker DACout;
 //extern wave_player waver;
--- a/hockey/hockey.cpp	Thu Dec 08 00:37:55 2022 +0000
+++ b/hockey/hockey.cpp	Thu Dec 08 02:12:14 2022 +0000
@@ -219,6 +219,9 @@
 
         Thread::wait(2000);
 
+        uLCD.cls();
+        menu_flag = 0;
+
         game2 = false;
         uLCD_mutex.unlock();
     }
--- a/main.cpp	Thu Dec 08 00:37:55 2022 +0000
+++ b/main.cpp	Thu Dec 08 02:12:14 2022 +0000
@@ -5,7 +5,7 @@
 #include "hockey.h"
 #include "tetris.h"
 
-#include "Speaker.h"
+//#include "Speaker.h"
 #include "SDFileSystem.h"
 #include "uLCD_4DGL.h"
 
@@ -18,6 +18,7 @@
 // them here:
 volatile bool game1 = false;
 volatile bool game2 = false;
+volatile int menu_flag = 0;
 
 Serial pc(USBTX, USBRX);
 Mutex pc_mutex;
@@ -34,8 +35,8 @@
 PwmOut Rgb(p23);
 PwmOut rGb(p24);
 PwmOut rgB(p25);
-AnalogOut DACout(p18);
-wave_player waver(&DACout);
+//Speaker DACout(p18);
+//wave_player waver(&DACout);
 
 
 
@@ -54,6 +55,7 @@
 //////////////////////
 // MAIN.CPP THREADS //
 //////////////////////
+/*
 void audioThread()
 {
     while (true) {
@@ -80,7 +82,7 @@
             led2 = false;
         }
     }
-}
+}*/
 
 void ledThread()
 {
@@ -113,14 +115,15 @@
     Thread thread2(hockeyGame, osPriorityHigh);
     //Thread thread3(audioThread, osPriorityLow);
     Thread thread4(ledThread, osPriorityLow);
-    int x = 0;
+    
     // The main thread goes on to blink LEDs
     while (true) {
         led1 = 1;
         Thread::wait(500);
         led1 = 0;
         Thread::wait(500);
-        if(!game1 && !game2 && x==0){
+        if(!game1 && !game2 && menu_flag==0){
+            uLCD.cls();
             uLCD.color(WHITE);
             uLCD.text_height(2);
             uLCD.text_width(2);
@@ -132,9 +135,11 @@
             uLCD.printf("DOWN:\n");
             uLCD.printf("Air\n");
             uLCD.printf("Hockey");
-            x=1;
+            menu_flag=1;
         }
         
+        //DACout.PlayNote(50, 1.0/2.5, 0.08);
+        
         PRINTF("[MAIN] Thread stacks: %lu %lu %lu %lu\r\n",
             thread1.used_stack(),
             thread2.used_stack(),
@@ -142,11 +147,11 @@
             thread4.used_stack());
         
         // Launch into the air hockey game
-        if (game1 != true && myNav.up()){
+        if (game1 != true && game2 != true && myNav.up()){
             uLCD.cls();
             game1 = true;
             }
-        if (game2 != true && myNav.down()){
+        if (game2 != true && game1 != true && myNav.down()){
             uLCD.cls();
             game2 = true;
             }
--- a/tetris/Board.h	Thu Dec 08 00:37:55 2022 +0000
+++ b/tetris/Board.h	Thu Dec 08 02:12:14 2022 +0000
@@ -34,6 +34,7 @@
     void StorePiece             (int pX, int pY, int pPiece, int pRotation);
     int DeletePossibleLines    ();
     bool IsGameOver             ();
+    void InitBoard();
 
 private:
 
@@ -43,7 +44,7 @@
     Pieces *mPieces;
     int mScreenHeight;
 
-    void InitBoard();
+    //void InitBoard();
     void DeleteLine (int pY);
 };
 
--- a/tetris/Game.h	Thu Dec 08 00:37:55 2022 +0000
+++ b/tetris/Game.h	Thu Dec 08 02:12:14 2022 +0000
@@ -38,6 +38,8 @@
     int points;
     int clearedLineCount;
     
+    void InitGame(int,int,int,int);
+    
 
 private:
 
@@ -50,7 +52,7 @@
     uLCD_4DGL *uLCD;
 
     
-    void InitGame(int,int,int,int);
+    //void InitGame(int,int,int,int);
     void DrawPiece (int pX, int pY, int pPiece, int pRotation, int colorIndex);
     
 };
--- a/tetris/tetris.cpp	Thu Dec 08 00:37:55 2022 +0000
+++ b/tetris/tetris.cpp	Thu Dec 08 02:12:14 2022 +0000
@@ -1,17 +1,13 @@
 #include "mbed.h"
-//#include "uLCD_4DGL.h"
 #include "Game.h"
 #include "globals.h"
-//#include "Nav_Switch.h"
 
-//uLCD_4DGL uLCD(p13, p14, p30);
-//Nav_Switch myNav(p20, p16, p17, p15, p19); // pins(up, down, left, right, fire)
 
 int SinglePiecePoints = 50;
 int LineClearPoints = 200;
 int key_input = 0;
 bool gameStarted = false;
-void input_left()   // FIX
+void input_left()
 {
     if(!gameStarted)
     {   gameStarted=true;
@@ -47,6 +43,15 @@
     key_input=4;
 }
 
+void input_menu()
+{
+    if(!gameStarted)
+    {   gameStarted=true;
+        return;
+    }
+    key_input = 5;
+}
+
 void clear_board()
 {
     uLCD.filled_rectangle(20,0,79,128,0);
@@ -66,15 +71,6 @@
 }
 
 
-/*int RandomGen(char range)
-{
-    pc.printf("%c",range);
-    while(!pc.readable()) wait(0.5);
-    char buffer[4];
-    pc.gets(buffer,4);
-    int i = buffer[0]-'0';
-    return i;
-}*/
 
 void tetrisGame()
 {
@@ -85,38 +81,17 @@
         Thread::wait(500);
     }
     
-    /*while(1){
-        uLCD.printf("test");
-    }*/
-    
-    
-    
-    /*if (myNav.up()) input_rotate();
-    if (myNav.down()) input_down();;
-    if (myNav.left()) input_left();
-    if (myNav.right()) input_right();;
-    //check mouse left button click*/   // FIXQ
-    //if (myNav.fire()) input_down; // FIX, CAN ADD MENU FUNCTIONALITY
-    
-    
     
     uLCD.text_width(2);
     uLCD.text_height(2);
     uLCD.color(WHITE);
+    uLCD.locate(1,0);
     uLCD.printf("TETRIS");
     wait(2);
     
     uLCD.baudrate(3000000);
     
     
-    /*pc.baud(9600);
-    pc.format(8,SerialBase::None,1);
-    pc.printf("0");
-    while(!pc.readable()) wait(0.5);
-    char buffer[4];
-    pc.gets(buffer,4);*/
-    
-    
     bool isGameOver = false;
     int mScreenHeight = 128;
     Pieces mPieces;
@@ -143,6 +118,13 @@
     int piece = 0;
     int rotate = 0;
     
+    //float x = 0.0;
+    
+    Rgb = 1;
+    rGb = 0;
+    rgB = 0;
+    int count = 0;
+    
     UpdateGameStatus(mGame.GetPoints());
     
     while (1)
@@ -156,10 +138,10 @@
         }
         
         if (myNav.up()) input_rotate();
-        if (myNav.down()) input_down();;
+        if (myNav.down()) input_down();
         if (myNav.left()) input_left();
-        if (myNav.right()) input_right();;
-        //if (myNav.fire()) input_down; // FIX, CAN ADD MENU FUNCTIONALITY
+        if (myNav.right()) input_right();
+        //if (myNav.fire()) input_menu();
         
         if(isGameOver) 
         {
@@ -168,7 +150,33 @@
             uLCD.text_width(2);
             uLCD.text_height(2);
             uLCD.printf("GAME OVER");
-            while(1);
+            wait(2);
+            uLCD.cls();
+            
+            //isGameOver = false;
+            
+            Thread::wait(1000);
+            
+            mBoard.InitBoard();
+            uLCD.cls();
+            
+            mGame.points = 0;
+            count = 0;
+            
+            //mGame.InitGame();
+            isGameOver = false;
+            
+            menu_flag = 0;
+            
+            game1 = false;
+            
+            
+            //mBoard.InitBoard();
+            
+            //free(mPieces);
+            //free(mBoard);
+            //free(mGame);
+            
         }
         // ----- Draw ----
         if(needErase)
@@ -212,8 +220,6 @@
                 {
                     mGame.AddClearedLines(linesDeleted);
                     mGame.AddPoints(LineClearPoints*linesDeleted);
-                    //Thread t1(FlashLight);
-                    //PlayClearSound();
                     clear_board();
                 }
                 UpdateGameStatus(mGame.GetPoints()); // FIX
@@ -246,6 +252,25 @@
                 break;
             }
             
+            case (5)://menu
+            {
+                wait(1);
+                uLCD.cls();
+                uLCD.text_width(2);
+                uLCD.text_height(2);
+                uLCD.printf("MENU");
+                wait(2);
+                uLCD.cls();
+                
+                Thread::wait(1000); // FIX?
+                
+                menu_flag = 0;
+                
+                game1 = false;  // FIX? put after Thread::wait()
+            
+                
+            }
+            
             case (0):{break;}
         }
         key_input = 0;
@@ -270,8 +295,6 @@
                     {
                         mGame.AddClearedLines(linesDeleted);
                         mGame.AddPoints(LineClearPoints*linesDeleted);
-                        //Thread t1(FlashLight);
-                        //PlayClearSound();
                         clear_board();
                     }
                     UpdateGameStatus(mGame.GetPoints());
@@ -297,5 +320,47 @@
             timer.reset();
         }
             wait(0.1);
+            
+            //get a new random number for PWM
+        //x = rand() / float(RAND_MAX);
+        //add some exponential brightness scaling
+        //for more of a fast flash effect
+        /*x = x*x*x;
+        Rgb = x;
+        rGb = x;
+        rgB = x;
+        //fast update rate for welding flashes
+        //Thread::wait(20);
+        //add a random pause between welds
+        if (rand() / float(RAND_MAX) > 0.95) {
+            Rgb = 0;
+            rGb = 0;
+            rgB = 0;
+            //Thread::wait(4000.0 * rand() / float(RAND_MAX));
+        }*/
+        if (count % 10 == 0) {
+            if (Rgb == 1) {
+                Rgb = 0;
+                rGb = 1;
+                rgB = 0;
+            }
+            
+            else if (rGb == 1) {
+                Rgb = 0;
+                rGb = 0;
+                rgB = 1;
+            }
+             else if (rgB == 1) {
+                Rgb = 1;
+                rGb = 0;
+                rgB = 0;
+            } else if (Rgb == 0) {
+                Rgb = 0;
+                rGb = 1;
+                rgB = 0;
+            }
+        }
+        
+        count++;
     }
 }