Final Game. Have fun!!! :-)
Dependencies: 4DGL-uLCD-SE PinDetect mbed-rtos mbed
Diff: main.cpp
- Revision:
- 3:0d678aefc1d0
- Parent:
- 2:fae2bc19d19d
- Child:
- 4:c136aa81f52a
diff -r fae2bc19d19d -r 0d678aefc1d0 main.cpp --- a/main.cpp Wed May 02 04:01:35 2018 +0000 +++ b/main.cpp Wed May 02 05:18:30 2018 +0000 @@ -5,8 +5,37 @@ #include "wave_player.h" #include <string> +//***** HW and signal setup *****// +Serial bluemod(p28,p27); //serial tx, serial rx + +//Bus for buttons +BusIn nibble(p9, p10, p11, p12); + +//Flips the LEDs when you hit one of the buttons +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; +Mutex ledMutex; + +SDFileSystem sd(p5, p6, p7, p8, "sd"); + +PwmOut speakerPWM(p26); +AnalogOut DACout(p18); +wave_player waver(&DACout, &speakerPWM); + +//*********************// + + + +//***** Constants *****// const int songLength = 600; //adjust as needed +//*********************// + + //Arrays for notes int L1[songLength]; int L2[songLength]; @@ -14,30 +43,19 @@ int L4[songLength]; int timespace = -6; //count for drawing the circles +int value = 0; //keypad input value +int score = 0; //score +int playGame = 1; //game state toggle to start game +int restartGame = 1; //game state toggle to restart or quit + +int red = 0; +int green = 0; char blueIn = '0'; //Bluetooth setup int selectedSong = 0; -Serial bluemod(p13,p14); //serial tx, serial rx - -//Flips the LEDs when you hit one of the buttons -DigitalOut myled(LED1); -DigitalOut myled2(LED2); -DigitalOut myled3(LED3); -DigitalOut myled4(LED4); - -AnalogOut DACout(p18); //speaker analog out - -uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; - -SDFileSystem sd(p5, p6, p7, p8, "sd"); FILE *wav_file; // song file -wave_player player(&DACout); // the player -int value = 0; //keypad input value -int points = 0; //points -int playGame = 1; -int restartGame = 1; void mainMenu() { @@ -80,7 +98,7 @@ void endScreen() { uLCD.cls(); - uLCD.printf("Score: %d\n\n", points); + uLCD.printf("Score: %d\n\n", score); uLCD.printf("1: Play Again\n"); uLCD.printf("2: Quit."); @@ -104,7 +122,69 @@ /******Interrupt functions go here********/ - +void buttonsThread(void const* args) { + while (playGame) { + if (timespace > -1) { + if ((nibble != 0x8) || (nibble != 0x4) || (nibble != 0x2) || (nibble != 0x1) || (nibble == 0)) { //check if multiple buttons or no buttons got pressed + red = 1; + green = 0; + } else { + switch(nibble) { + case 0x8: + led4 = 1; + if (L4[timespace] == 1) { + score = score + 1; + red = 0; + green = 1; + } else { + red = 1; + green = 0; + } + break; + case 0x4: + led3 = 1; + if (L3[timespace] == 1) { + score = score + 1; + red = 0; + green = 1; + } else { + red = 1; + green = 0; + } + break; + case 0x2: + led2 = 1; + if (L2[timespace] == 1) { + score = score + 1; + red = 0; + green = 1; + } else { + red = 1; + green = 0; + } + break; + case 0x1: + led1 = 1; + if (L1[timespace] == 1) { + score = score + 1; + red = 0; + green = 1; + } else { + red = 1; + green = 0; + } + break; + default: + break; + } + } + } + led1 = 0; + led2 = 0; + led3 = 0; + led4 = 0; + } +} /*****************************************/ void lcdThread1(void const *args) { @@ -202,7 +282,8 @@ { playGame = 0; } - //draw yellow circles + + //draw circles at base uLCD.circle(110,20,12, 0xFFFF00); uLCD.circle(110,50,12, 0xFFFF00); uLCD.circle(110,80,12, 0xFFFF00); @@ -214,8 +295,9 @@ } //Thread to play song, song is selected in mainMenu() -void wavThread(void const *args) { - Thread::wait(1200); +void musicThread(void const *args) { + + speakerPWM.period(1.0/400000.0); switch(selectedSong) { case 1: @@ -235,7 +317,7 @@ break; } - player.play(wav_file); + waver.play(wav_file); while (true) { Thread::wait(100); } @@ -310,6 +392,26 @@ fclose(fp); } +void readSong(int selection) +{ + switch(selection) { + case 1: + readFile("/sd/", "GTShake"); + break; + case 2: + readFile("/sd/", "DarkHorse"); + break; + case 3: + readFile("/sd/", "OneRepublic"); + break; + case 4: + readFile("/sd/", "ShakeItOff"); + break; + default: + readFile("/sd/", "ShakeItOff"); + break; + } +} int main() { @@ -319,38 +421,24 @@ { mainMenu(); //Game stays at menu until player chooses song - switch(selectedSong) { - case 1: - readFile("/sd/", "GTShake"); - break; - case 2: - readFile("/sd/", "DarkHorse"); - break; - case 3: - readFile("/sd/", "OneRepublic"); - break; - case 4: - readFile("/sd/", "ShakeItOff"); - break; - default: - readFile("/sd/", "ShakeItOff"); - break; - } + readSong(selectedSong); //create note arrays based on chosen song //start threads Thread thread1(lcdThread1); //thread that displays the circles - Thread thread2(wavThread); //thread that plays the song + Thread thread2(musicThread); //thread that plays the song + Thread thread3(buttonsThread); //thread to get user input //declare interrupt //interrupt.mode(PullUp); while (playGame) { - Thread::wait(100); + Thread::wait(10); } //End Threads thread1.terminate(); thread2.terminate(); + thread3.terminate(); //reset game state switches playGame = 1; @@ -360,4 +448,3 @@ uLCD.cls(); } } -