W R / Mbed 2 deprecated 4180_Final_Project

Dependencies:   mbed wave_player mbed-rtos 4180Final SDFileSystem

Revision:
12:896de8e66292
Parent:
11:962634a738fb
Child:
13:aff3ad7d5afc
--- a/main.cpp	Wed Apr 29 01:50:46 2020 +0000
+++ b/main.cpp	Wed Apr 29 03:17:15 2020 +0000
@@ -1,7 +1,50 @@
-#include "play.h"
-#include "init.h"
+// TODO: 
+//      - Add actual way to calulate scores (in play() or add helper method)
+//      - Add more inputs/outputs
+//      - add pot to increase selector size
+//      - add sound
+#include "mbed.h"
+#include "uLCD_4DGL.h"
+#include "Nav_Switch.h"
+#include "SDFileSystem.h"
+#include "microphone.h"
+#include "fireflies.h"
+#include "wave_player.h"
+#include  "rtos.h"
+
+Thread thread1, thread2; 
 
-int main();
+Nav_Switch myNav(p9, p6, p7, p5, p8); //pin order on Sparkfun breakout
+uLCD_4DGL uLCD(p28, p27, p30); // serial tx, serial rx, reset pin; 
+SDFileSystem sd(p11, p12, p13, p10, "sd");
+microphone mymic1(p15);
+microphone mymic2(p17);
+AnalogIn pot(p16);
+AnalogOut DACout(p18);
+PwmOut myled(LED1);
+PwmOut myled2(LED2);
+PwmOut myled3(LED3);
+BusOut led_strip1(p21,p22,p23); 
+BusOut led_strip2(p24,p25,p26);
+
+// Globals
+int rounds = 3;
+int curRound = 1;
+int score1 = 0;
+int score2 = 0;
+int scores1[7] = {0};
+int scores2[7] = {0};
+int curTime = 0;
+int wonRnds1 = 0;
+int wonRnds2 = 0;
+int highScoresArr[10] = {100, 90, 83, 73, 64, 54, 44, 34, 24, 14};
+int s = 0;
+int timer = 0;
+volatile int micScore1 = 0;
+volatile int micScore2 = 0;
+int playType = 0;
+
+//#include "play.h"
 
 // Stuff for sound
 unsigned char *music = NULL;
@@ -10,15 +53,29 @@
 volatile int sIndx = 0;
 int playMusic = 0;
 
+//stuff for play
+int mic_reading1 = 0; 
+int mic_reading2 = 0; 
+float mic_read_raw = 0; 
+Ticker mic; 
+Timer round; 
+int round_time = 25;
+int loud1 = 0; 
+int loud2 = 0; 
+
+int main();
+void play();
+
+
 // Method for ticker for sound
 void playMenuMusic() {
     if (playMusic) {
         music = (unsigned char*) fireflies;
-        led = 1;
+//        myled2 = 1;
         DACout.write(music[sIndx++] / 255.0);
         if(sIndx > 120000) {
             sIndx = 0;
-            led = 0;
+//            myled2 = 0;
         }
     } else {
         DACout.write(0.0);
@@ -40,7 +97,8 @@
 void mic_thread() {
     while (1) {
         myled = pot;
-        micScore = int(mymic1*1000.0);
+        micScore1 = int(mymic1*500.0);
+        micScore2 = int(mymic2*500.0);
         Thread::wait(100);
     }
 }
@@ -306,7 +364,124 @@
     }
 }
 
+void read_mic(){
+    while(1){
+        mic_reading1 = int(abs((mymic1 - (0.67/3.3)))*500.0); //this value should hover around 15 
+        mic_reading2 = int(abs((mymic2 - (0.67/3.3)))*500.0); 
+        Thread::wait(125);
+    }
+}
 
+void update_leds(){
+    switch (loud1){
+        case 10: led_strip1 = 1; break;
+        case 20: led_strip1 = 3; break;
+        case 30: led_strip1 = 7; break; 
+    }
+    switch (loud2){
+        case 10: led_strip2 = 1; break;
+        case 20: led_strip2 = 3; break;
+        case 30: led_strip2 = 7; break; 
+    }
+    //Thread::wait(200);
+}
+
+void update_score(){
+    if (mic_reading1 > 16) {
+        loud1++; //if the player screams loudly for enough times it increases your score
+        score1++;
+    }
+    if (mic_reading2 > 16) {
+        loud2++;
+        score2++; 
+    }
+}
+
+int getScore1() {
+    myled3 = ((double) micScore1) / 500.0;
+    return micScore1; //int(mymic1*1000.0);
+}
+int getScore2() {
+    // 2nd player active
+    if (playType) {
+        return micScore2;
+    } else {
+        return rand() % 650; // put rand
+    }
+}
+
+void play() {
+     srand(time(NULL));
+     uLCD.background_color(GREEN);
+     uLCD.cls();
+     uLCD.filled_rectangle(0, 0, 200, 15, RED);
+     // header
+     uLCD.color(BLACK);
+     uLCD.locate(5,0);
+     uLCD.text_height(2);
+     uLCD.text_width(2);
+     uLCD.textbackground_color(RED);
+     uLCD.printf("PLAY");
+     uLCD.text_height(1);
+     uLCD.text_width(1);
+     uLCD.locate(14,1);
+     uLCD.printf("Rnd%1d", curRound);
+     // Scores headers
+     uLCD.textbackground_color(GREEN);
+     uLCD.color(BLUE);
+     uLCD.locate(5,3);
+     uLCD.printf("Player 1:");
+     uLCD.color(PURPLE);
+     uLCD.locate(5,11);
+     uLCD.printf("Player 2:");
+     // Scores
+     uLCD.text_height(4);
+     uLCD.text_width(4);
+     Thread t1(read_mic);
+     while (1) {
+        timer = 0;
+        // Blinking pause button
+        while (timer<25) {
+            //mic.attach(&read_mic, 1.0/8000.0);
+            //Threread_mic();
+            if (myNav.fire()) {
+                uLCD.filled_rectangle(60, 68, 65, 82, GREEN);
+                uLCD.filled_rectangle(70, 68, 75, 82, GREEN);
+                wait(.1);
+                pause();
+            }
+            if (timer % 25 == 0) {
+                //score1 += getScore1(); 
+                //score2 += getScore2(); 
+                update_score();
+                update_leds();
+                uLCD.color(BLUE);
+                uLCD.locate(0,1);
+                uLCD.printf("%4d", mic_reading1);
+                uLCD.color(PURPLE);
+                uLCD.locate(0,3);
+                uLCD.printf("%4d", mic_reading2);
+                // Time bar
+                double roundTime = 15.0;  // in secs
+                int length = (int) ( (double)curTime / roundTime * 127.0);
+                uLCD.filled_rectangle(0, 65, length, 85, DGREEN);
+                if (length >= 127) {
+                    nextRound();
+                }
+                curTime++;
+            }
+            if (timer < 20){
+                uLCD.filled_rectangle(60, 68, 65, 82, LGREY);
+                uLCD.filled_rectangle(70, 68, 75, 82, LGREY);
+            } else if (timer >= 20) {
+                uLCD.filled_rectangle(60, 68, 65, 82, BLACK);
+                uLCD.filled_rectangle(70, 68, 75, 82, BLACK);
+            }
+            Thread::wait(100);
+            timer++;
+         }
+     }
+}
 
 void highScores() {
      uLCD.background_color(ORANGE);
@@ -361,6 +536,7 @@
      uLCD.text_width(2);
      uLCD.textbackground_color(RED);
      uLCD.printf("Settings");
+     // Rounds to play
      uLCD.text_height(1);
      uLCD.text_width(1);
      uLCD.locate(0,2);
@@ -374,15 +550,27 @@
      uLCD.printf("5");
      uLCD.locate(9,6);
      uLCD.printf("7");
+     uLCD.locate(4,7);
      uLCD.line(0, 32, 200, 32, BLUE);
-     uLCD.locate(4,7);
      uLCD.printf("Rounds = %1d", rounds);
-     uLCD.locate(7,14);
+     // Number of players
+     uLCD.color(DGREEN);
+     uLCD.locate(0,9);
+     uLCD.printf("Choose how many   players:");
+     uLCD.locate(9,11);
+     uLCD.printf("1");
+     uLCD.locate(9,12);
+     uLCD.printf("2");
+     uLCD.locate(4,13);
+     uLCD.line(0, 88, 200, 88, DGREEN);
+     uLCD.printf("Players = %1d", playType+1);
+     // Back
+     uLCD.locate(7,15);
      uLCD.color(BLACK);
      uLCD.printf("Back");
      uLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display
-     int xPos[4] = {57, 57, 57, 45};
-     int yPos[4] = {36, 42, 50, 115};
+     int xPos[6] = {57, 57, 57, 57, 57, 45};
+     int yPos[6] = {36, 42, 50, 91, 99, 123};
      int ticker = 0;
      int oldTicker = 1;
      for (int i=0; i<sizeof(yPos)/4; i++) {
@@ -418,11 +606,20 @@
                         rounds = 7;
                         break;
                      case 3:
+                        playType = 0;
+                        break;
+                     case 4:
+                        playType = 1;
+                        break;
+                     case 5:
                         main();
                   }
                   uLCD.color(BLUE);
                   uLCD.locate(4,7);
                   uLCD.printf("Rounds = %1d", rounds);
+                  uLCD.color(DGREEN);
+                  uLCD.locate(4,13);
+                  uLCD.printf("Players = %1d", playType+1);
              } else if (myNav.up() || myNav.left()) {
                  wait(.2);
                  ticker = ticker - 1;
@@ -441,7 +638,6 @@
 
 int main() {
      // read in highScoresArr from SD card
-     
      FILE *fp = fopen("/sd/finalProj/highscores.txt", "r");
      if(fp == NULL) {
      uLCD.printf("Error Open \n");
@@ -450,7 +646,6 @@
         fscanf(fp, "%4d", &highScoresArr[i]);
      }
      fclose(fp);
-     
      // create menu interface
      uLCD.background_color(LBLUE);
      uLCD.cls();