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

Revision:
0:5bb514318c64
Child:
2:acd4656312d8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Apr 24 03:23:00 2020 +0000
@@ -0,0 +1,505 @@
+#include "mbed.h"
+#include "uLCD_4DGL.h"
+#include "rgb.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
+#include "rtos.h"
+
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <algorithm> 
+
+using namespace std;
+
+/*
+------------------CONSTS
+*/
+
+volatile bool homescreen = true;
+volatile bool diff_selected = false;
+volatile bool boot_vid = true;
+
+/*
+------------------I/O VARS
+*/
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+RawSerial bluemod(p28, p27);
+Serial pc(USBTX, USBRX);
+uLCD_4DGL uLCD(p9, p10, p11);
+DigitalIn pb(p12);
+
+/*
+------------------LED VARS
+*/
+DigitalOut life1(p16);
+DigitalOut life2(p19);
+DigitalOut life3(p20);
+RGBLed myRGBled(p21, p24, p23);
+DigitalOut onboard_led(LED1);
+
+/*
+------------------AUDIO VARS
+*/
+
+FILE *wave_file;
+AnalogOut DACount(p18);
+wave_player waver(&DACount);
+
+/*
+------------------GENERAL VARS
+*/
+int difficulty = 0;
+string scores;
+int score_val[3];
+volatile char bnum;
+int ans[4];
+char sign;
+
+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()
+{
+
+    //while(true){
+    //        if(homescreen){
+    pc.printf("MAIN_SCREEN\r\n");
+    lcd_mutex.lock();
+    uLCD.cls();
+    uLCD.text_height(1.9);
+    uLCD.text_width(1.9);
+    uLCD.color(WHITE);
+    uLCD.locate(6, 1);
+    uLCD.printf("MATH FUN");
+    //MAKE MATH FUN BLINK
+    uLCD.locate(1, 4);
+    uLCD.text_height(1.3);
+    uLCD.printf("Difficulty");
+    uLCD.text_height(1.3);
+    uLCD.text_width(1.9);
+    uLCD.locate(3, 6);
+    uLCD.color(GREEN);
+    uLCD.printf("1) Easy");
+    uLCD.locate(3, 8);
+    uLCD.color(0xFFFF00);
+    uLCD.printf("2) Not as Easy");
+    uLCD.locate(3, 10);
+    uLCD.color(RED);
+    uLCD.printf("3) Very Uneasy");
+    uLCD.color(BLUE);
+    uLCD.locate(1, 13);
+    uLCD.printf("4) High Scores");
+    lcd_mutex.unlock();
+    //pc.printf("thread 1\r\n");
+    homescreen = false;
+    //  Thread::wait(100);
+    // }
+    // else{
+    //     Thread::yield();
+    // }
+    //}
+}
+
+bool count_distinct(int arr[], int n) 
+{ 
+
+    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;   
+    }
+
+} 
+
+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);
+
+            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;
+        }
+}
+
+void game_questions()
+{
+    int gen1 = rand() % (10 + 1 - 0) + 0;
+    int gen2 = rand() % (10 + 1 - 1) + 1;
+    int gen_sign = rand() % (3 + 1 - 1) + 1;
+    gen_ans(gen1, gen2, gen_sign);
+    bool reroll = count_distinct(ans, 4);
+    while(reroll){
+        gen_ans(gen1, gen2, gen_sign);
+        reroll = count_distinct(ans, 4);
+        pc.printf("reroll\r\n");
+        wait(0.5);
+    }
+    uLCD.cls();
+    uLCD.locate(2, 1);
+    uLCD.text_height(2);
+    uLCD.text_width(2);
+    uLCD.color(0xFC766A);
+    uLCD.printf("Q1");
+    uLCD.text_height(1);
+    uLCD.text_width(1);
+
+    uLCD.rectangle(0, 0, 127, 127, 0xA89C94);
+    uLCD.line(0, 27, 127, 27, 0xA89C94);
+    uLCD.line(0, 55, 127, 55, 0xA89C94);
+
+    uLCD.color(0x669DB2);
+    uLCD.locate(2, 4);
+    uLCD.text_height(2);
+    uLCD.text_width(2);
+    uLCD.printf("%i%c%i=", gen1, sign, gen2);
+    uLCD.text_height(1);
+    uLCD.text_width(1);
+
+    uLCD.color(0xFC766A);
+    uLCD.locate(2, 8);
+    uLCD.printf("1)");
+
+    uLCD.color(0x669DB2);
+    uLCD.locate(8, 8);
+    uLCD.printf("%i",ans[1]);
+
+    uLCD.color(0xFC766A);
+    uLCD.locate(2, 10);
+    uLCD.printf("2)");
+
+    uLCD.color(0x669DB2);
+    uLCD.locate(8, 10);
+    uLCD.printf("%i",ans[2]);
+
+    uLCD.color(0xFC766A);
+    uLCD.locate(2, 12);
+    uLCD.printf("3)");
+
+    uLCD.color(0x669DB2);
+    uLCD.locate(8, 12);
+    uLCD.printf("%i",ans[3]);
+
+    uLCD.color(0xFC766A);
+    uLCD.locate(2, 14);
+    uLCD.printf("4)");
+
+    uLCD.color(0x669DB2);
+    uLCD.locate(8, 14);
+    uLCD.printf("%i",ans[0]);
+
+}
+
+void get_bluetooth_button()
+{
+    {
+        while (true)
+        {
+            //pc.printf("BLUETOOTH\r\n");
+
+            if (bluemod.readable())
+            {
+                pc.printf("MAIN\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)))
+                        {
+                            if (bhit == '1')
+                            {
+                                pc.printf("%c\r\n", bnum);
+                            }
+                        }
+                    }
+                }
+                blue_mutex.unlock();
+            }
+            else
+            {
+                Thread::yield();
+            }
+            Thread::wait(100);
+        }
+    }
+}
+
+void rgb_led_difficulty()
+{
+    difficulty = bnum - 48;
+    //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;
+    }
+    else if (difficulty == 3)
+    {
+        myRGBled.write(1.0, 0.0, 0.0); //red
+        diff_selected = true;
+    }
+}
+
+void wav_thread()
+{
+    while (true)
+    {
+        pc.printf("AUDIO\r\n");
+        wave_file = fopen("/sd/audio/intro.wav", "r");
+        waver.play(wave_file);
+        fclose(wave_file);
+    }
+}
+
+void boot_video_thread()
+{
+    while (true)
+    {
+        pc.printf("BootVID\r\n");
+        //PLAY VIDEO BOOT
+        if (boot_vid)
+        {
+            //lcd_mutex.lock();
+            uLCD.media_init();
+            uLCD.set_sector_address(0x0, 0x0);
+            uLCD.display_video(0, 0);
+            boot_vid = false;
+            //lcd_mutex.unlock();
+        }
+        else
+        {
+            Thread::yield();
+        }
+        Thread::wait(200);
+    }
+}
+
+int main()
+{
+    uLCD.baudrate(3000000);
+    srand(time(NULL));
+    while(1){
+
+        game_questions();
+        wait(2);
+    }
+    //game_questions();
+    pc.printf("MAIN_INIT\r\n\n");
+    thread1.start(boot_video_thread);
+    thread2.start(wav_thread);
+    while (true)
+    {
+        //pc.printf("MAIN_LOOP\r\n");
+        //ONCE VIDEO STOPS PLAYING
+        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");
+            if (diff_selected == false)
+            {
+                bnum = 51;
+                thread3.start(get_bluetooth_button);
+                rgb_led_difficulty();
+            }
+            else{
+                thread3.terminate();
+                game_questions();
+            }
+        }
+        Thread::wait(200);
+    }
+}