![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Guitar Hero Project on LPC1768 using push buttons, analog joystick. speaker and LCD Display
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SparkfunAnalogJoystick SDFileSystem_OldbutworkswithRTOS PinDetect
main.cpp
- Committer:
- rpatel397
- Date:
- 2020-11-20
- Revision:
- 5:2f09b457f690
- Parent:
- 4:577404e4d53f
- Child:
- 6:4bc137ec1022
File content as of revision 5:2f09b457f690:
#include "mbed.h" #include "rtos.h" #include "SDFileSystem.h" #include "uLCD_4DGL.h" #include "PinDetect.h" #include "song1.h" #define sample_freq 8000.0 //code change Mutex lcd_mut; //SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; //AnalogOut DACout(p18); //wave_player waver(&DACout); AnalogOut speaker(p18); PinDetect pb_1(p9, PullUp); PinDetect pb_2(p10, PullUp); PinDetect pb_3(p11, PullUp); PinDetect pb_4(p12, PullUp); #define Main_Menu 0 // the first screen being shown #define Choose_Difficulty 1 // after hitting start, player chooses difficult #define Choose_Song 2 // after choosing difficullyt, choose song #define Start_Game 3 // the game has started #define Game_Over 4 // the game is done //colors #define YELLOW 16709376 //speed based on difficulty #define Easy 2 #define Medium 5 #define Hard 8 DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); DigitalOut myled4(LED4); Ticker sampletick; volatile int Game_State = Game_Over; volatile bool draw_enable = true; volatile int Speed = Easy; int i=0; void audio_sample () { speaker.write_u16(sound_data[i]); i++; if (i>= NUM_ELEMENTS) { i = 0; sampletick.detach(); Game_State = Game_Over; } } void LCD(void const *args) { while(1) { switch (Game_State) { case Start_Game : if (draw_enable) { lcd_mut.lock(); uLCD.cls(); uLCD.printf("Start_Game"); lcd_mut.unlock(); } break; case Game_Over : if (draw_enable) { draw_enable = false; lcd_mut.lock(); uLCD.cls(); uLCD.line(0, 0, 127, 0, RED); uLCD.line(127, 0, 127, 127, BLUE); uLCD.line(127, 127, 0, 127, GREEN); uLCD.line(0, 127, 0, 0, YELLOW); uLCD.text_width(1); uLCD.text_height(2); uLCD.text_string("Game Over",4,0,FONT_8X8, BLUE); uLCD.text_width(1); uLCD.text_height(1); uLCD.text_string("Score:",2,6,FONT_8X8, YELLOW); uLCD.text_string("Hits:",2,8,FONT_8X8, GREEN); uLCD.text_string("Misses:",2,10,FONT_8X8, RED); uLCD.text_height(2); uLCD.text_string("High Score:",2,6,FONT_8X8, WHITE); uLCD.text_height(1); lcd_mut.unlock(); } break; case Choose_Song : if (draw_enable) { draw_enable = false; lcd_mut.lock(); uLCD.cls(); uLCD.text_width(1); uLCD.text_height(1); uLCD.locate(1,1); uLCD.color(BLUE); uLCD.text_underline(ON); uLCD.text_string("Choose Song",4,1,FONT_8X8, BLUE); uLCD.text_underline(OFF); uLCD.set_font(FONT_7X8); uLCD.triangle(64, 32,58, 40, 70, 40, WHITE); uLCD.triangle(64, 92 ,58, 84, 70, 84, WHITE); uLCD.color(GREEN); uLCD.rectangle(10,44,118, 80, LGREY); uLCD.text_width(2); uLCD.text_height(2); uLCD.text_string("Schools\nOut",1,3,FONT_8X8, GREEN); uLCD.text_width(1); uLCD.text_height(1); lcd_mut.unlock(); } break; case Choose_Difficulty : if (draw_enable) { draw_enable = false; lcd_mut.lock(); uLCD.cls(); uLCD.text_width(1.5); uLCD.text_height(1.5); uLCD.locate(1,1); uLCD.color(BLUE); uLCD.text_underline(ON); uLCD.text_string("Choose Difficulty",1,1,FONT_8X8, BLUE); uLCD.text_underline(OFF); uLCD.set_font(FONT_7X8); uLCD.text_width(2); uLCD.text_height(2); uLCD.color(GREEN); uLCD.locate(1,2); uLCD.printf("1|EASY"); uLCD.locate(1,4); uLCD.color(YELLOW); uLCD.printf("2|MEDIUM"); uLCD.locate(1,6); uLCD.color(RED); uLCD.printf("3|HARD"); uLCD.text_width(1); uLCD.text_height(1); lcd_mut.unlock(); } break; case Main_Menu : if (draw_enable) { draw_enable = false; lcd_mut.lock(); uLCD.cls(); uLCD.line(0, 0, 127, 0, RED); uLCD.line(127, 0, 127, 127, BLUE); uLCD.line(127, 127, 0, 127, GREEN); uLCD.line(0, 127, 0, 0, YELLOW); uLCD.text_width(2); uLCD.text_height(2); uLCD.locate(2,2); uLCD.color(RED); uLCD.printf("Guitar"); uLCD.color(BLUE); uLCD.locate(3,4); uLCD.printf("Hero"); uLCD.text_width(1); uLCD.text_height(1); uLCD.locate(3,12); uLCD.color(GREEN); uLCD.printf("Press to start"); lcd_mut.unlock(); } break; default: break; } Thread::wait(10); } } void lights(void const *args) { while(1) { switch (Game_State) { case Start_Game : myled1 = 0; myled2 = 0; myled3 = 0; myled4 = 1; break; case Game_Over : myled1 = 1; myled2 = 1; myled3 = 1; myled4 = 1; break; case Choose_Song : myled1 = 0; myled2 = 0; myled3 = 1; myled4 = 0; break; case Choose_Difficulty : myled1 = 0; myled2 = 1; myled3 = 0; myled4 = 0; break; case Main_Menu : myled1 = 1; myled2 = 0; myled3 = 0; myled4 = 0; break; default: break; } Thread::wait(500); } } void Thread3(void const *args) { while(1) { Thread::wait(1000); } } void Thread4(void const *args) { while(1){ Thread::wait(1000); } } void startGame() { Game_State = Start_Game; sampletick.attach(&audio_sample, 1.0 / sample_freq); } void pb_1_pressed(void) { switch (Game_State) { case Start_Game : draw_enable = true; Game_State = Game_Over; break; case Game_Over : draw_enable = true; Game_State = Main_Menu; break; case Choose_Song : draw_enable = true; startGame(); break; case Choose_Difficulty : draw_enable = true; Game_State = Choose_Song; Speed = Easy; break; case Main_Menu : draw_enable = true; Game_State = Choose_Difficulty; break; default: break; } } void pb_2_pressed(void) { switch (Game_State) { case Start_Game : draw_enable = true; Game_State = Game_Over; break; case Game_Over : draw_enable = true; Game_State = Main_Menu; break; case Choose_Song : draw_enable = true; startGame(); break; case Choose_Difficulty : draw_enable = true; Game_State = Choose_Song; Speed = Medium; break; case Main_Menu : draw_enable = true; Game_State = Choose_Difficulty; break; default: break; } } void pb_3_pressed(void) { switch (Game_State) { case Start_Game : draw_enable = true; Game_State = Game_Over; break; case Game_Over : draw_enable = true; Game_State = Main_Menu; break; case Choose_Song : draw_enable = true; startGame(); break; case Choose_Difficulty : draw_enable = true; Game_State = Choose_Song; Speed = Hard; break; case Main_Menu : draw_enable = true; Game_State = Choose_Difficulty; break; default: break; } } void pb_4_pressed(void) { switch (Game_State) { case Start_Game : draw_enable = true; Game_State = Game_Over; break; case Game_Over : draw_enable = true; Game_State = Main_Menu; break; case Choose_Song : draw_enable = true; startGame(); break; case Choose_Difficulty : //not used break; case Main_Menu : draw_enable = true; Game_State = Choose_Difficulty; break; default: break; } } int main() { speaker.write_u16(0); pb_1.attach_deasserted( &pb_1_pressed ); pb_2.attach_deasserted( &pb_2_pressed ); pb_3.attach_deasserted( &pb_3_pressed ); pb_4.attach_deasserted( &pb_4_pressed ); pb_1.setSampleFrequency(); pb_2.setSampleFrequency(); pb_3.setSampleFrequency(); pb_4.setSampleFrequency(); lcd_mut.lock(); uLCD.cls(); uLCD.printf("starting..."); uLCD.background_color(BLACK); uLCD.baudrate(3000000); lcd_mut.unlock(); Thread::wait(1000); Thread Thread1(LCD); Thread Thread2(lights); while(1) { Thread::wait(1000); } }