Code for the first mbed for the drum hero project

Dependencies:   4DGL SDFileSystem mbed

Fork of drums2 by Yusuf Kuris

Files at this revision

API Documentation at this revision

Comitter:
ckabuloglu
Date:
Mon May 01 11:05:52 2017 +0000
Parent:
4:d135d66c55e2
Commit message:
final push

Changed in this revision

Note/note.cpp Show annotated file Show diff for this revision Revisions of this file
Song/song.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
mbed-rtos.lib Show diff for this revision Revisions of this file
--- a/Note/note.cpp	Mon May 01 00:31:47 2017 +0000
+++ b/Note/note.cpp	Mon May 01 11:05:52 2017 +0000
@@ -30,7 +30,7 @@
     y = 130.0;
     w = 12.0;
     h = w * RATIO;
-    speed = 38.0;
+    speed = 49.0;
     consumed = 0;
 }
 
--- a/Song/song.cpp	Mon May 01 00:31:47 2017 +0000
+++ b/Song/song.cpp	Mon May 01 11:05:52 2017 +0000
@@ -4,12 +4,21 @@
 Song::Song(int id) {
     songId = id;
 
-    static const int arr[192] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0,
-                                1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0,
-                                1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0,
-                                1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0,
-                                1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0, 1, 0, 1, 0, 2, 0, 0, 0,
-                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
+    static const int arr[352] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+//                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+//                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+//                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+//                                1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 3, 0,
+                                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
                                   
     vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
     length = vec.size();
--- a/main.cpp	Mon May 01 00:31:47 2017 +0000
+++ b/main.cpp	Mon May 01 11:05:52 2017 +0000
@@ -1,29 +1,24 @@
 #include "mbed.h"
-#include "rtos.h"
 #include "TFT_4DGL.h"
 #include "note.h"
 #include "song.h"
 #include <vector>
 
-
 // Define all the ports
 // Serial pc(USBTX, USBRX);
 TFT_4DGL screen(p9,p10,p11); // serial tx, serial rx, reset pin;
-AnalogIn r1(p18);
-AnalogIn r2(p19);
-AnalogIn r3(p20);
+AnalogIn drum1(p18);
+AnalogIn drum2(p19);
+AnalogIn drum3(p20);
 DigitalOut musicOn(p22);
 DigitalIn button(p21);
 
 // Define objects
-// Ticker beatCounter;
+Ticker t1;
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 
-//Thread t1;
-//Mutex stdio_mutex;
-
 // Define and initiate global variables
 std::vector<Note> noteArr; 
 int beatNumber = 0;
@@ -32,10 +27,16 @@
 int temp;
 int score = 0;
 Song s = Song(1);
-//float r1;
-//float r2;
-//float r3;
 
+// Define the drum variables
+volatile int r1;
+volatile int r2;
+volatile int r3;
+volatile int r11;
+volatile int r22;
+volatile int r33;
+
+// Function that draws the initial game frame
 void initiateScreen() {
     // Initiate the screen and the background
     screen.baudrate(3000000);
@@ -56,66 +57,95 @@
     screen.rectangle(296, 100, 332, 120, 0x00FF00);
     screen.rectangle(332, 100, 372, 120, 0x0000FF);
     
-//    screen.rectangle(192,450,277,470,0xFF0000);
-//    screen.rectangle(277,450,362,470,0x00FF00);
-//    screen.rectangle(362,450,447,470,0x0000FF);
-    
     // Define the points
     screen.graphic_string("POINTS:", 30, 100, FONT_8X8, WHITE, 2, 2);
-    screen.graphic_string("ROCK YOU", 430, 100, FONT_8X8, WHITE, 2, 2);
     
+    screen.graphic_string("OB-LA-DI", 430, 100, FONT_8X8, WHITE, 2, 2);    
 }
 
-//void beatMover() {
-//    
-//}   
+// The function that the ticker will be running, it checks if the piezo sensors
+// sense a hit two cycles (0.002 sec) in a row. Checking for two consecutive
+// high values discards the noise in the piezo sensors.
+void checkDrums() {
+    if (drum1 == 1) {
+        if (r1 == 1) {
+            r11 = 1;
+        } else {
+            r1 = 1;
+        };   
+    } else if (drum2 == 1) {
+        if (r2 == 1) {
+            r22 = 1;
+        } else {
+            r2 = 1;
+        };   
+    } else if (drum3 == 1) {
+        if (r3 == 1) {
+            r33 = 1;
+        } else {
+            r3 = 1;
+        }; 
+    }
+}
 
-//void checkHits() {
-//
-//}
-
+// Main methods that runs the main loop that checks the drums hits and draws the
+// next set of notes on the board.
 int main() {
     button.mode(PullDown);    
     screen.text_string("DRUM HERO 1.0", 15, 8, FONT_8X8, WHITE);  
     screen.text_string("PRESS THE BUTTON", 14, 16, FONT_8X8, WHITE);
     screen.text_string("TO START", 18, 19, FONT_8X8, WHITE);    
+    
     musicOn = 0;
     while(!musicOn) {
         if(button) {
             musicOn = 1;
         }    
-    }    
+    }  
+      
     // Initiate the screen and the main graphics
-    initiateScreen();    
-    //t1.start(beatMover);
-    // t2.start(checkHits);
+    initiateScreen();   
+     
+    t1.attach(&checkDrums, 0.001);
+    
     while (true) {        
         // Check if the song is ended
         if (beatNumber < s.length) {
-            if (r1 > 0.1) {
+            // Check if the drum ticker sensed any hits
+            if (r11) {
+                // Draw hit drums for user feedback
                 screen.rectangle(192,450,277,470,0xFF0000);
                 led1 = 1;
-//              if (screen.read_pixel(230, 420) == 0xFF0000) {
-//                  score++;
-//              }
-            } else if (r2 > 0.1) {
+                // Notes move down 5 times, check the note array in the index of 
+                // 5 notes before and see if it matches with the right drum
+                if (s.notes[beatNumber - 5] == 1) {
+                    score++;
+                }
+                r1 = 0;
+                r11 = 0;
+            } else if (r22) {
                 screen.rectangle(277,450,362,470,0x00FF00);
                 led2 = 1;
-//              if (screen.read_pixel(320, 420) == 0x00FF00) {
-//                  score++;
-//              }
-            } else if (r3 > 0.1) {
+                if (s.notes[beatNumber - 5] == 2) {
+                    score++;
+                }
+                r2 = 0;
+                r22 = 0;
+            } else if (r33) {
                 screen.rectangle(362,450,447,470,0x0000FF);
                 led3 = 1;
-//              if (screen.read_pixel(405, 420) == 0x0000FF) {
-//                  score++;
-//              }
+                if (s.notes[beatNumber - 5] == 3) {
+                    score++;
+                }
+                r3 = 0;
+                r33 = 0;
             } else {
                 screen.rectangle(192,450,447,470,0x000000);
                 led1 = 0;
                 led2 = 0;
                 led3 = 0;
             }
+            
             // Read the next beat (0,1,2,3)
             temp = s.notes[beatNumber++];
             // Check if there's a beat/note (not 0)
@@ -125,27 +155,24 @@
                 Note n1 = Note(temp);
                 noteArr.push_back(n1);
             }   
-            //beatNumber++;                
+                         
             for (int i = skipped; i < noteArr.size(); i++) {
                 if (noteArr[i].consumed) {
                     skipped += 1;
                 } else {
-                    //stdio_mutex.lock();
                     noteArr[i].drawNote();
-                    //stdio_mutex.unlock();
                 }
             }
         } else {
-             //stdio_mutex.lock();
+             // Game over screen
              screen.rectangle(0,0,640,480,0x000000);
-             screen.graphic_string("GAME OVER", 20, 200, FONT_8X8, WHITE, 2, 2);
-             //stdio_mutex.unlock();
+             screen.graphic_string("GAME OVER", 240, 170, FONT_8X8, WHITE, 2, 2);
+             screen.graphic_string("YOUR SCORE IS:", 210, 230, FONT_8X8, WHITE, 2, 2);
+             screen.graphic_string(buffer, 290, 260, FONT_8X8, WHITE, 2, 2);
              wait(30);               
         }
+        // Update the score after each iteration
         sprintf(buffer,"%d",score);
-        //stdio_mutex.lock();
-        screen.text_string(buffer, 5, 7, FONT_8X8, WHITE);
-        //stdio_mutex.unlock();
-        wait(0.4);
+        screen.graphic_string(buffer, 30, 120, FONT_8X8, WHITE, 2, 2);
     }
 }
\ No newline at end of file
--- a/mbed-rtos.lib	Mon May 01 00:31:47 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e