Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem
Diff: main.cpp
- Revision:
- 2:acd4656312d8
- Parent:
- 0:5bb514318c64
- Child:
- 3:6e41a5ce16c2
--- a/main.cpp Fri Apr 24 03:25:20 2020 +0000 +++ b/main.cpp Sun Apr 26 00:47:37 2020 +0000 @@ -8,7 +8,7 @@ #include <string> #include <iostream> #include <fstream> -#include <algorithm> +#include <algorithm> using namespace std; @@ -19,6 +19,8 @@ volatile bool homescreen = true; volatile bool diff_selected = false; volatile bool boot_vid = true; +bool timeout = true; +bool game_is_over = false; /* ------------------I/O VARS @@ -32,9 +34,8 @@ /* ------------------LED VARS */ -DigitalOut life1(p16); -DigitalOut life2(p19); -DigitalOut life3(p20); + +DigitalOut life[] = {(p16), (p19), (p20)}; RGBLed myRGBled(p21, p24, p23); DigitalOut onboard_led(LED1); @@ -54,144 +55,14 @@ int score_val[3]; volatile char bnum; int ans[4]; +int corr_ans; char sign; +int live_left = 3; Mutex lcd_mutex; Mutex blue_mutex; Thread thread1, thread2, thread3; -void pb_test() -{ - pb.mode(PullUp); - while (1) - { - onboard_led = pb; - } -} - -void rgb_led_check() -{ - while (1) - { - myRGBled.write(1.0, 0.0, 0.0); //red - wait(2.0); - myRGBled.write(0.0, 1.0, 0.0); //green - wait(2.0); - myRGBled.write(0.0, 0.0, 1.0); //blue - wait(2.0); - myRGBled.write(1.0, 0.2, 0.0); //yellow = red + some green - wait(2.0); - } -} - -void lcd_check() -{ - uLCD.cls(); - uLCD.text_width(2); //4X size text - uLCD.text_height(2); - uLCD.printf("Hello"); //Default Green on black text -} - -void life_count_check() -{ - life1 = 1; // LED is ON - wait(0.2); // 200 ms - life1 = 0; // LED is OFF - wait(0.8); // 800 ms - life2 = 1; // LED is ON - wait(0.2); // 200 ms - life2 = 0; // LED is OFF - wait(0.8); // 800 ms - life3 = 1; // LED is ON - wait(0.2); // 200 ms - life3 = 0; // LED is OFF - wait(0.8); // 800 ms -} - -void sd_card_check() -{ - printf("Hello World!\n\r"); - while (1) - { - mkdir("/sd/mydir", 0777); - ofstream score_file; - score_file.open("/sd/mydir/score.txt"); - if (score_file.is_open()) - { - score_file << "100\n110\n120\n"; - score_file.close(); - } - wait(1); - ifstream myfile2("/sd/mydir/score.txt"); - if (myfile2.is_open()) - { - for (int i = 0; i < 3; i++) - { - getline(myfile2, scores); - score_val[i] = atoi(scores.c_str()); - }; - myfile2.close(); - } - pc.printf("%i \n\r", score_val[0]); - pc.printf("%i \n\r", score_val[1]); - wait(1); - } -} - -void sound_check() -{ - while (1) - { - printf("\n\n\nHello, wave world!\n"); - FILE *wave_file = fopen("/sd/piano.wav", "r"); - waver.play(wave_file); - fclose(wave_file); - } -} - -void bluetooth_test() -{ - while (1) - { - if (bluemod.getc() == '!') - { - //char x = ; - if (bluemod.getc() == 'B') - { //button data - bnum = bluemod.getc(); //button number - if (bnum == '1') - { - uLCD.cls(); - uLCD.text_width(2); //4X size text - uLCD.text_height(2); - uLCD.printf("\n%c \npressed\n", bnum); //Default Green on black text - } - if (bnum == '2') - { - uLCD.cls(); - uLCD.text_width(2); //4X size text - uLCD.text_height(2); - uLCD.printf("\n%c \npressed\n", bnum); //Default Green on black text - } - if (bnum == '3') - { - uLCD.cls(); - uLCD.text_width(2); //4X size text - uLCD.text_height(2); - uLCD.printf("\n%c \npressed\n", bnum); //Default Green on black text - } - if (bnum == '4') - { - uLCD.cls(); - uLCD.text_width(2); //4X size text - uLCD.text_height(2); - uLCD.printf("\n%c \npressed\n", bnum); //Default Green on black textd - } - } - } - } -} - void main_screen() { @@ -234,65 +105,70 @@ //} } -bool count_distinct(int arr[], int n) -{ +bool count_distinct(int arr[], int n) +{ + + int res = 1; - int res = 1; - - // Pick all elements one by one - for (int i = 1; i < n; i++) { - int j = 0; - for (j = 0; j < i; j++) - if (arr[i] == arr[j]) - break; - - // If not printed earlier, then print it - if (i == j) - res++; - } - - if (res == 4){ - return false; - } - else{ - return true; + // Pick all elements one by one + for (int i = 1; i < n; i++) + { + int j = 0; + for (j = 0; j < i; j++) + if (arr[i] == arr[j]) + break; + + // If not printed earlier, then print it + if (i == j) + res++; } -} + if (res == 4) + { + return false; + } + else + { + return true; + } +} -void gen_ans(int num1, int num2, int sign_val){ - - switch(sign_val){ - case 1: - sign='*'; - ans[0] = num1 * num2; - ans[1] = (num1 + (rand() % (10 + 1 - 1) + 1)) * num2; - ans[2] = (num1 * num2) + (rand() % (20 + 1 - 1) + 1) ; - ans[3] = (num1 * num2)+ (rand() % (20 + 1 - 1) + 1); +void gen_ans(int &num1, int &num2, int sign_val) +{ - break; - case 2: - sign='+'; - ans[0] = num1 + num2; - ans[1] = (num1 + (rand() % (10 + 1 - 1) + 1)) + num2; - ans[2] = (num1 + num2) - (rand() % ((num1+num2) + 1 - 1) + 1); - ans[3] = (num1 * num2) + (rand() % (10 + 1 - 1) + 1); - break; - case 3: - sign='-'; - if(num1 == num2){ - num1 = (rand() % ((num1) -1 + 1 - 1) + 1); - } - ans[0] = num1 - num2; - ans[1] = (num1 - num2)* -1 + (rand() % (10 + 1 - 1) + 1) ; - ans[2] = num1 + num2 - (rand() % (10 + 1 - 1) + 1); - ans[3] = num1 * num2 - (rand() % (20 + 1 - 1) + 1); - break; - case 4: - sign='/'; - ans[0] = num1 - num2; - break; + switch (sign_val) + { + case 1: + sign = '*'; + ans[0] = num1 * num2; + ans[1] = (num1 + (rand() % (10 + 1 - 1) + 1)) * num2; + ans[2] = (num1 * num2) + (rand() % (20 + 1 - 1) + 1); + ans[3] = (num1 * num2) + (rand() % (20 + 1 - 1) + 1); + break; + case 2: + sign = '+'; + ans[0] = num1 + num2; + ans[1] = (num1 + (rand() % (10 + 1 - 1) + 1)) + num2; + ans[2] = (num1 + num2) - (rand() % ((num1 + num2) + 1 - 1) + 1); + ans[3] = (num1 * num2) + (rand() % (10 + 1 - 1) + 1); + break; + case 3: + sign = '-'; + //rand()%(max-min + 1) + min; + while (num1 == num2) + { + num1 = (rand() % ((num1 + 2) - 1 + 1) + 1); } + ans[0] = num1 - num2; + ans[1] = (num1 - num2) * -1 + (rand() % (10 + 1 - 1) + 1); + ans[2] = num1 + num2 - (rand() % (10 + 1 - 1) + 1); + ans[3] = num1 * num2 - (rand() % (20 + 1 - 1) + 1); + break; + case 4: + sign = '/'; + ans[0] = num1 - num2; + break; + } } void game_questions() @@ -302,12 +178,16 @@ int gen_sign = rand() % (3 + 1 - 1) + 1; gen_ans(gen1, gen2, gen_sign); bool reroll = count_distinct(ans, 4); - while(reroll){ + while (reroll) + { gen_ans(gen1, gen2, gen_sign); reroll = count_distinct(ans, 4); - pc.printf("reroll\r\n"); - wait(0.5); + //pc.printf("reroll\r\n"); } + + corr_ans = ans[0]; + random_shuffle(&ans[0], &ans[3]); + uLCD.cls(); uLCD.locate(2, 1); uLCD.text_height(2); @@ -317,9 +197,13 @@ uLCD.text_height(1); uLCD.text_width(1); + uLCD.locate(9, 1); + uLCD.printf("TIME:"); + uLCD.line(55, 0, 55, 27, 0xA89C94); + uLCD.rectangle(0, 0, 127, 127, 0xA89C94); uLCD.line(0, 27, 127, 27, 0xA89C94); - uLCD.line(0, 55, 127, 55, 0xA89C94); + uLCD.line(0, 50, 127, 50, 0xA89C94); uLCD.color(0x669DB2); uLCD.locate(2, 4); @@ -330,75 +214,181 @@ uLCD.text_width(1); uLCD.color(0xFC766A); - uLCD.locate(2, 8); + uLCD.locate(2, 7); uLCD.printf("1)"); uLCD.color(0x669DB2); - uLCD.locate(8, 8); - uLCD.printf("%i",ans[1]); + uLCD.locate(8, 7); + uLCD.printf("%i", ans[0]); uLCD.color(0xFC766A); - uLCD.locate(2, 10); + uLCD.locate(2, 9); uLCD.printf("2)"); uLCD.color(0x669DB2); - uLCD.locate(8, 10); - uLCD.printf("%i",ans[2]); + uLCD.locate(8, 9); + uLCD.printf("%i", ans[1]); uLCD.color(0xFC766A); - uLCD.locate(2, 12); + uLCD.locate(2, 11); uLCD.printf("3)"); uLCD.color(0x669DB2); - uLCD.locate(8, 12); - uLCD.printf("%i",ans[3]); + uLCD.locate(8, 11); + uLCD.printf("%i", ans[2]); uLCD.color(0xFC766A); - uLCD.locate(2, 14); + uLCD.locate(2, 13); uLCD.printf("4)"); uLCD.color(0x669DB2); - uLCD.locate(8, 14); - uLCD.printf("%i",ans[0]); + uLCD.locate(8, 13); + uLCD.printf("%i", ans[3]); + uLCD.line(0, 115, 127, 115, 0xA89C94); +} +void game_over() +{ + uLCD.cls(); + uLCD.locate(0, 7); + uLCD.text_height(2); + uLCD.text_width(2); + uLCD.color(0xFC776A); + uLCD.printf("GAME OVER"); + game_is_over = true; + wait(2); +} + +void life_count_check() +{ + life[0] = 0; + life[1] = 0; + life[2] = 0; + + for (int i = 0; i < live_left; i++) + { + life[i] = 1; + } + if (live_left == 0) + { + game_over(); + } } -void get_bluetooth_button() +void check_correct_ans() { + if (timeout) { - while (true) + //pc.printf("TIMEOUT\r\n"); + uLCD.cls(); + uLCD.locate(5, 7); + uLCD.text_height(2); + uLCD.text_width(2); + uLCD.color(0xF0F6F7); + uLCD.printf("TIME"); + live_left--; + //wait(2); + } + else + { + //pc.printf("%i\r\n", corr_ans); + //pc.printf("%i\r\n", ans[(bnum-'0' -1)]); + + if (corr_ans == ans[(bnum - '0') - 1]) { - //pc.printf("BLUETOOTH\r\n"); - if (bluemod.readable()) + uLCD.cls(); + uLCD.locate(2, 7); + uLCD.text_height(2); + uLCD.text_width(2); + uLCD.color(GREEN); + uLCD.printf("CORRECT"); + //wait(2); + //pc.printf("CORRECT\r\n"); + } + else + { + //pc.printf("INCORRECT\r\n"); + uLCD.cls(); + uLCD.locate(0, 7); + uLCD.text_height(2); + uLCD.text_width(2); + uLCD.color(RED); + uLCD.printf("INCORRECT"); + live_left--; + //wait(2); + } + } +} + + +bool get_bluetooth_button() +{ + pc.printf("BLUETOOTH\r\n"); + blue_mutex.lock(); + if (bluemod.getc() == '!') + { + if (bluemod.getc() == 'B') + { + //button number + bnum = bluemod.getc(); + //button data + char bhit = bluemod.getc(); + if (bluemod.getc() == char(~('!' + 'B' + bnum + bhit))) { - pc.printf("MAIN\r\n"); - blue_mutex.lock(); - if (bluemod.getc() == '!') + if (bhit == '1') { - if (bluemod.getc() == 'B') - { - //button number - bnum = bluemod.getc(); - //button data - char bhit = bluemod.getc(); - if (bluemod.getc() == char(~('!' + 'B' + bnum + bhit))) - { - if (bhit == '1') - { - pc.printf("%c\r\n", bnum); - } - } - } + pc.printf("%c\r\n", bnum); + blue_mutex.unlock(); + return false; } - blue_mutex.unlock(); + } + } + } + blue_mutex.unlock(); + return true; +} + +void get_button() +{ + int timer = 10; + timeout = true; + while (timer != 0) + { + uLCD.locate(15, 1); + uLCD.color(0xF0F6F7); + uLCD.printf("%i", timer); + if (bluemod.readable()) + { + timeout = get_bluetooth_button(); + if(timeout == false){ + break; } - else - { - Thread::yield(); - } - Thread::wait(100); } + wait(1); + uLCD.locate(15, 1); + uLCD.color(BLACK); + uLCD.printf("%i", timer); + timer = timer - 1; + //pc.printf("%i\r\n", x); + } +} + + + +void bluetooth_thread() +{ + while (true) + { + if (bluemod.readable()) + { + bool n_timeout = get_bluetooth_button(); + } + else + { + Thread::yield(); + } + Thread::wait(100); } } @@ -408,13 +398,11 @@ //pc.printf("%i\r\n",difficulty); if (difficulty == 1) { - myRGBled.write(0.0, 1.0, 0.0); //green diff_selected = true; } else if (difficulty == 2) { - myRGBled.write(1.0, 0.2, 0.0); //yellow = red + some green diff_selected = true; } @@ -440,7 +428,7 @@ { while (true) { - pc.printf("BootVID\r\n"); + //pc.printf("BootVID\r\n"); //PLAY VIDEO BOOT if (boot_vid) { @@ -455,22 +443,24 @@ { Thread::yield(); } - Thread::wait(200); } } +void init() +{ + uLCD.baudrate(3000000); + srand(time(NULL)); + life[0] = 1; + life[1] = 1; + life[2] = 1; + thread1.start(boot_video_thread); +} + int main() { - uLCD.baudrate(3000000); - srand(time(NULL)); - while(1){ - game_questions(); - wait(2); - } - //game_questions(); + init(); pc.printf("MAIN_INIT\r\n\n"); - thread1.start(boot_video_thread); thread2.start(wav_thread); while (true) { @@ -479,27 +469,32 @@ if (boot_vid == false) { thread1.terminate(); - //DACount=0.0f; - //fclose(wave_file); - //thread2.terminate(); - //IF HOMESCREEN PRINTED ONCE DONT PRINT AGAIN if (homescreen) { main_screen(); } //pc.printf("MAIN_PRINT\r\n"); + //break; if (diff_selected == false) { - bnum = 51; - thread3.start(get_bluetooth_button); + thread3.start(bluetooth_thread); rgb_led_difficulty(); } - else{ + else + { thread3.terminate(); - game_questions(); + break; } } Thread::wait(200); } -} + while (!game_is_over) + { + game_questions(); + get_button(); + check_correct_ans(); + wait(2); + life_count_check(); + } +} \ No newline at end of file