Josh Baldwin / Mbed 2 deprecated Dance_Maniax

Dependencies:   4DGL-uLCD-SE DebounceIn PinDetect SDFileSystem mbed-rtos mbed wave_player

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 "uLCD_4DGL.h"
00004 #include "SDFileSystem.h"
00005 #include "wave_player.h"
00006 #include "DebounceIn.h"
00007 #include <string>
00008 
00009 SDFileSystem sd(p5, p6, p7, p8, "sd");
00010 uLCD_4DGL uLCD(p9,p10,p20); // serial tx, serial rx, reset pin;
00011 SPI spi(p11, p12, p13);
00012 DigitalOut latch(p15);
00013 DigitalOut enable(p16);
00014 AnalogIn ain(p17);          //ir sensor pins
00015 AnalogOut DACout(p18);
00016 DebounceIn pb1(p21);
00017 DebounceIn pb2(p22);
00018 Mutex stdio_mutex;
00019 wave_player waver(&DACout);
00020 
00021 bool fileClosed;
00022 void makeSound(void const *args) {
00023     fileClosed = false;
00024     FILE *wave_file;
00025     wave_file=fopen("/sd/mySongs/I_Miss_You2.wav","r");
00026     waver.play(wave_file);
00027     fclose(wave_file);
00028     fileClosed = true;
00029 }
00030 
00031 int highScore = 0;
00032 int highCombo;
00033 int main() {
00034     pb1.mode(PullUp);
00035     pb2.mode(PullUp);
00036     //increase baud rate for faster printing
00037     uLCD.baudrate(3000000);
00038     
00039 Start:
00040     //main menu
00041     uLCD.cls();
00042     while(1) {
00043         uLCD.color(0xFF00FF);
00044         uLCD.text_width(2);
00045         uLCD.text_height(3);
00046         uLCD.locate(1, 1);
00047         uLCD.printf("uDance");
00048         uLCD.locate(1, 2);
00049         uLCD.printf("Maniax");
00050         uLCD.text_width(1);
00051         uLCD.text_height(1);
00052         uLCD.locate(7, 11);
00053         uLCD.printf("START");
00054         wait(0.1);
00055         uLCD.color(0xFFFFFF);
00056         uLCD.locate(7, 11);
00057         uLCD.printf("START");
00058         wait(0.1);
00059         if (!pb1) {
00060             goto Game;
00061         }
00062     }
00063 
00064 Game:
00065     highCombo = 0;
00066     uLCD.cls();
00067     Thread thread2(makeSound);
00068     int orbX1 = SIZE_X / 2;
00069     int orbY1 = SIZE_Y;
00070     int score = 0;
00071     int combo = 0;
00072     //use bools to save previous state for "if" statements
00073     bool preventExcessPrinting = true;
00074     bool preventMultipleScoreIncrement = true;
00075     //use to see if hand has passed over sensor at all
00076     bool sensed = false;
00077     //setup display
00078     stdio_mutex.lock();
00079     uLCD.color(WHITE);
00080     uLCD.locate(SIZE_X - 5, 0);
00081     uLCD.printf("Score:");
00082     uLCD.color(WHITE);
00083     uLCD.locate(SIZE_X - 3, 1);
00084     uLCD.printf("%d", score);
00085     uLCD.circle(SIZE_X / 2, SIZE_Y / 2, 8, WHITE);
00086     uLCD.color(WHITE);
00087     uLCD.text_width(1);
00088     uLCD.text_height(1);
00089     uLCD.locate(0, 0);
00090     uLCD.printf("Top");
00091     uLCD.locate(0, 1);
00092     uLCD.printf("Score:");
00093     uLCD.locate(0, 2);
00094     uLCD.printf("%d", highScore);
00095     stdio_mutex.unlock();
00096     //wait for song to start
00097     Thread::wait((10000)*1.48);
00098     while(1) {
00099         if(fileClosed) {
00100             if (score > highScore) {
00101                 highScore = score;
00102             }
00103             goto End;
00104         }
00105         //draw orb and keep white circle in center
00106         stdio_mutex.lock();
00107         uLCD.circle(SIZE_X / 2, SIZE_Y / 2, 8, WHITE);
00108         uLCD.filled_circle(orbX1, orbY1, 8, RED);
00109         uLCD.filled_circle(orbX1, orbY1, 8, BLACK);
00110         stdio_mutex.unlock();
00111         //update orb's position
00112         orbY1 = orbY1 - 5;
00113         //check sensor while orb is within bounds
00114         if ((ain > 0.6f) && (orbY1 < (SIZE_Y/2) + 5 && orbY1 > (SIZE_Y/2) - 10)) {
00115             sensed = true;
00116             stdio_mutex.lock();
00117             uLCD.filled_circle(SIZE_X / 2, SIZE_Y / 2, 8, BLUE);
00118             //compensate for lag from printing
00119             orbY1 = orbY1 - 2;
00120             //compensate for lag from printing
00121             if (combo >= 5) {
00122                 orbY1 = orbY1 - 5;
00123             }
00124             stdio_mutex.unlock();
00125             if (preventMultipleScoreIncrement) {
00126                 preventMultipleScoreIncrement = false;
00127                 score++;
00128                 combo++;
00129                 //double score if combo is 5
00130                 if (combo >= 5) {
00131                     score++;
00132                 }
00133                 preventExcessPrinting = true;
00134                 //print the current combo and multiplier
00135                 if (preventExcessPrinting && combo >=5 ) {
00136                     preventExcessPrinting = false;
00137                     if (combo == 5) {
00138                         stdio_mutex.lock();
00139                         uLCD.color(0xFFFF00);
00140                         uLCD.text_width(2);
00141                         uLCD.text_height(2);
00142                         uLCD.locate(1, 6);
00143                         uLCD.printf("x2!");
00144                         stdio_mutex.unlock();
00145                     }
00146                     stdio_mutex.lock();
00147                     uLCD.color(WHITE);
00148                     uLCD.text_width(1);
00149                     uLCD.text_height(1);
00150                     uLCD.locate(0, 14);
00151                     uLCD.printf("COMBO");
00152                     uLCD.color(WHITE);
00153                     uLCD.text_width(1);
00154                     uLCD.text_height(1);
00155                     uLCD.locate(0, 15);
00156                     uLCD.printf("%d", combo);
00157                     stdio_mutex.unlock();
00158                 }
00159                 //update printed score
00160                 stdio_mutex.lock();
00161                 uLCD.color(WHITE);
00162                 uLCD.text_width(1);
00163                 uLCD.text_height(1);
00164                 uLCD.locate(SIZE_X - 3, 1);
00165                 uLCD.printf("%d", score);
00166                 stdio_mutex.unlock();
00167             }
00168         } else {
00169             //after hand is sensed and blue circle is printed, draw over it with black
00170             preventMultipleScoreIncrement = true;
00171             stdio_mutex.lock();
00172             uLCD.filled_circle(SIZE_X / 2, SIZE_Y / 2, 8, BLACK);
00173             stdio_mutex.unlock();
00174         }
00175         //C-C-C-COMBOBREAKER if player keeps hand in front of sensor when orb isn't within bounds
00176         //or if player doesn't have their hand in front of the sensor when the orb is within bounds
00177         if ((combo != 0 && (ain > 0.6f) && (orbY1 > (SIZE_Y/2) + 10 || orbY1 < (SIZE_Y/2) - 15))
00178             || (combo != 0 && (orbY1 < (SIZE_Y/2) - 8) && (!sensed))) {
00179             if (combo > highCombo) {
00180                 highCombo = combo;
00181             }
00182             combo = 0;
00183             stdio_mutex.lock();
00184             uLCD.color(BLACK);
00185             uLCD.text_width(1);
00186             uLCD.text_height(1);
00187             uLCD.locate(0, 14);
00188             uLCD.printf("COMBO");
00189             uLCD.color(BLACK);
00190             uLCD.text_width(1);
00191             uLCD.text_height(1);
00192             uLCD.locate(0, 15);
00193             uLCD.printf("%s", "000");
00194             uLCD.color(BLACK);
00195             uLCD.text_width(2);
00196             uLCD.text_height(2);
00197             uLCD.locate(1, 6);
00198             uLCD.printf("x2!");
00199             stdio_mutex.unlock();
00200         }
00201         //when orb reaches top of screen start back at the bottom and check for sensor again
00202         if (orbY1 <= -5) {
00203             orbY1 = SIZE_Y;
00204             sensed = false;
00205         }
00206     }
00207 End:
00208     //select if player wants to play again or not
00209     uLCD.cls();
00210     uLCD.color(0xFF00FF);
00211     uLCD.text_width(2);
00212     uLCD.text_height(3);
00213     uLCD.locate(0, 1);
00214     uLCD.printf("Score:%d", score);
00215     wait(3.0);
00216     uLCD.cls();
00217     uLCD.text_width(1);
00218     uLCD.text_height(2);
00219     uLCD.locate(1, 2);
00220     uLCD.printf("Highest combo:%d", highCombo);
00221     wait(3.0);
00222     uLCD.cls();
00223     uLCD.color(0xFF00FF);
00224     uLCD.text_width(1);
00225     uLCD.text_height(3);
00226     uLCD.locate(3, 1);
00227     uLCD.printf("Play Again?");
00228     uLCD.text_width(1);
00229     uLCD.text_height(1);
00230     uLCD.locate(7, 9);
00231     uLCD.printf("Yes");
00232     uLCD.locate(7, 11);
00233     uLCD.printf("No");
00234     int x = 7;  //use to highlight "Yes" or "No"
00235     int y = 9;
00236     string s = "Yes";
00237     int counter = 0;
00238     int old_pb2 = 1;    //use this so the option only changes once when a button is pressed and held
00239     int new_pb2;
00240     while(1) {
00241         uLCD.color(WHITE);
00242         uLCD.text_width(1);
00243         uLCD.text_height(1);
00244         uLCD.locate(x, y);
00245         uLCD.printf("%s", s.c_str());
00246         wait(0.1);
00247         uLCD.color(0xFF00FF);
00248         uLCD.text_width(1);
00249         uLCD.text_height(1);
00250         uLCD.locate(x, y);
00251         uLCD.printf("%s", s.c_str());
00252         wait(0.1);
00253         new_pb2 = pb2;
00254         if (new_pb2 == 0 && old_pb2 == 1) {
00255             counter++;
00256             if (counter % 2 == 1) {
00257                 x = 7;
00258                 y = 11;
00259                 s = "No";
00260             } else if (counter % 2 == 0) {
00261                 x = 7;
00262                 y = 9;
00263                 s = "Yes";
00264             }
00265         }
00266         old_pb2 = new_pb2;
00267         if (!pb1 && y == 9) {
00268             goto Game;
00269         } else if (!pb1 && y == 11) {
00270             goto Start;
00271         }
00272     }
00273 }