drum

Dependencies:   4DGL SDFileSystem mbed-rtos mbed

Fork of drums by Can Kabuloglu

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "TFT_4DGL.h"
00004 #include "note.h"
00005 #include "song.h"
00006 #include <vector>
00007 
00008 
00009 // Define all the ports
00010 // Serial pc(USBTX, USBRX);
00011 TFT_4DGL screen(p9,p10,p11); // serial tx, serial rx, reset pin;
00012 AnalogIn r1(p18);
00013 AnalogIn r2(p19);
00014 AnalogIn r3(p20);
00015 DigitalOut musicOn(p22);
00016 DigitalIn button(p21);
00017 
00018 // Define objects
00019 // Ticker beatCounter;
00020 DigitalOut led1(LED1);
00021 DigitalOut led2(LED2);
00022 DigitalOut led3(LED3);
00023 
00024 //Thread t1;
00025 //Mutex stdio_mutex;
00026 
00027 // Define and initiate global variables
00028 std::vector<Note> noteArr; 
00029 int beatNumber = 0;
00030 char buffer[2];
00031 int skipped = 0;
00032 int temp;
00033 int score = 0;
00034 Song s = Song(1);
00035 //float r1;
00036 //float r2;
00037 //float r3;
00038 
00039 void initiateScreen() {
00040     // Initiate the screen and the background
00041     screen.baudrate(3000000);
00042     screen.display_control(0x0c, 0x01);
00043     screen.background_color(0x000000);
00044     
00045     // Draw the frame
00046     screen.rectangle(192, 120, 447, 450, 0xD38A41);
00047     screen.line(256, 120, 192, 450, 0x96411F);
00048     screen.line(296, 120, 277, 450, 0x96411F);
00049     screen.line(332, 120, 362, 450, 0x96411F);
00050     screen.line(372, 120, 447, 450, 0x96411F);
00051     screen.triangle(192, 120, 192, 450, 256, 120, 0x000000);
00052     screen.triangle(372, 120, 447, 450, 447, 120, 0x000000);
00053     screen.rectangle(447, 120, 450, 450, 0x000000);
00054     
00055     screen.rectangle(256, 100, 296, 120, 0xFF0000);
00056     screen.rectangle(296, 100, 332, 120, 0x00FF00);
00057     screen.rectangle(332, 100, 372, 120, 0x0000FF);
00058     
00059 //    screen.rectangle(192,450,277,470,0xFF0000);
00060 //    screen.rectangle(277,450,362,470,0x00FF00);
00061 //    screen.rectangle(362,450,447,470,0x0000FF);
00062     
00063     // Define the points
00064     screen.graphic_string("POINTS:", 30, 100, FONT_8X8, WHITE, 2, 2);
00065     screen.graphic_string("ROCK YOU", 430, 100, FONT_8X8, WHITE, 2, 2);
00066     
00067 }
00068 
00069 //void beatMover() {
00070 //    
00071 //}   
00072 
00073 //void checkHits() {
00074 //
00075 //}
00076 
00077 int main() {
00078     button.mode(PullDown);    
00079     screen.text_string("DRUM HERO 1.0", 15, 8, FONT_8X8, WHITE);  
00080     screen.text_string("PRESS THE BUTTON", 14, 16, FONT_8X8, WHITE);
00081     screen.text_string("TO START", 18, 19, FONT_8X8, WHITE);    
00082     musicOn = 0;
00083     while(!musicOn) {
00084         if(button) {
00085             musicOn = 1;
00086         }    
00087     }    
00088     // Initiate the screen and the main graphics
00089     initiateScreen();    
00090     //t1.start(beatMover);
00091     // t2.start(checkHits);
00092     while (true) {        
00093         // Check if the song is ended
00094         if (beatNumber < s.length) {
00095             if (r1 > 0.1) {
00096                 screen.rectangle(192,450,277,470,0xFF0000);
00097                 led1 = 1;
00098 //              if (screen.read_pixel(230, 420) == 0xFF0000) {
00099 //                  score++;
00100 //              }
00101             } else if (r2 > 0.1) {
00102                 screen.rectangle(277,450,362,470,0x00FF00);
00103                 led2 = 1;
00104 //              if (screen.read_pixel(320, 420) == 0x00FF00) {
00105 //                  score++;
00106 //              }
00107             } else if (r3 > 0.1) {
00108                 screen.rectangle(362,450,447,470,0x0000FF);
00109                 led3 = 1;
00110 //              if (screen.read_pixel(405, 420) == 0x0000FF) {
00111 //                  score++;
00112 //              }
00113             } else {
00114                 screen.rectangle(192,450,447,470,0x000000);
00115                 led1 = 0;
00116                 led2 = 0;
00117                 led3 = 0;
00118             }
00119             // Read the next beat (0,1,2,3)
00120             temp = s.notes[beatNumber++];
00121             // Check if there's a beat/note (not 0)
00122             if (temp) {
00123                 // If beat/note exists, create the object for it and push
00124                 // it to the array of current notes on the screen
00125                 Note n1 = Note(temp);
00126                 noteArr.push_back(n1);
00127             }   
00128             //beatNumber++;                
00129             for (int i = skipped; i < noteArr.size(); i++) {
00130                 if (noteArr[i].consumed) {
00131                     skipped += 1;
00132                 } else {
00133                     //stdio_mutex.lock();
00134                     noteArr[i].drawNote();
00135                     //stdio_mutex.unlock();
00136                 }
00137             }
00138         } else {
00139              //stdio_mutex.lock();
00140              screen.rectangle(0,0,640,480,0x000000);
00141              screen.graphic_string("GAME OVER", 20, 200, FONT_8X8, WHITE, 2, 2);
00142              //stdio_mutex.unlock();
00143              wait(30);               
00144         }
00145         sprintf(buffer,"%d",score);
00146         //stdio_mutex.lock();
00147         screen.text_string(buffer, 5, 7, FONT_8X8, WHITE);
00148         //stdio_mutex.unlock();
00149         wait(0.4);
00150     }
00151 }