help :(

Dependencies:   FFT

Revision:
8:24c18eecc442
Parent:
7:0d62545e6d73
Child:
9:97abd5e954f1
--- a/main.cpp	Thu Dec 03 18:15:35 2020 +0000
+++ b/main.cpp	Fri Dec 04 03:48:15 2020 +0000
@@ -6,6 +6,9 @@
 #include "rtos.h"
 #include "FFT.h"
 #include <string>
+#include "around_the_world-atc-array.h"
+#include "dont_speak-no_doubt-array.h"
+#include "my-love-array.h"
 
 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card
 
@@ -15,14 +18,16 @@
 AnalogOut DACout(p18); // speaker
 wave_player waver(&DACout);
 Mutex speaker_lock;
+Mutex file_lock;
 DigitalOut myled(LED1); // mbed LED
 Serial pc (USBTX,USBRX);
 string dir;         // "/sd/" + song + ".wav"
+//string song = "around_the_world-atc";
 string song;
-bool play;
+int song_data_length;
+int song_data[];
+bool play = false;
 
-<<<<<<< working copy
-#define BUFFER_SIZE 50
 
 // delete later
 /*int sample[12][8] = {
@@ -39,9 +44,9 @@
  41483,37964,39215,35908,31570,28332,24492,23121,
  22874,18154,15541,15263,12463,12198,13301,9951
 };*/
-=======
+
+
 #define BUFFER_SIZE 64
->>>>>>> merge rev
 
 // states for display output
 const int on[8][8] = {
@@ -101,192 +106,143 @@
 
 
 
-<<<<<<< working copy
-void display_fft(int sample[])
+
+void display_fft(float sample[])
 {
 
-=======
-void display_fft(float sample[]) {
-    
->>>>>>> merge rev
     int state = 0;
     float freq = 0;
     matrix.begin(0x70);
-<<<<<<< working copy
 
-    for (int a = 0; a < 50; a++) {
-
-        freq = sample[a];
 
-=======
-    
-    int s = sizeof(sample);
-    
-    pc.printf("%d\n", s);
+    //  for (int a = 0; a < 64; a++) {
+
+    freq = sample[1]/4;
     
-    for (int a = 0; a < 64; a++) {
-            
-        freq = sample[a];
-        
-        pc.printf("%f\n",freq);
-        
->>>>>>> merge rev
-        // determine state based on the FFT output range
-        if (freq < 10000) {
-            state = 5; // OFF
-        } else if (freq >= 10000 && freq < 25000) {
-            state = 4; // LOW
-        } else if (freq >= 25000 && freq < 40000) {
-            state = 3; // MED
-        } else if (freq >= 40000 && freq < 55000) {
-            state = 2; // HIGH
-        } else if (freq >= 55000) {
-<<<<<<< working copy
-            state = 1; // ON
-        }
+
+    //pc.printf("%f\n",freq);
+
+    // determine state based on the FFT output range
+    if (freq < 10000) {
+        state = 5; // OFF
+    } else if (freq >= 10000 && freq < 25000) {
+        state = 4; // LOW
+    } else if (freq >= 25000 && freq < 40000) {
+        state = 3; // MED
+    } else if (freq >= 40000 && freq < 55000) {
+        state = 2; // HIGH
+    } else if (freq >= 55000) {
+        state = 1; // ON
+    }
 
-=======
-            state = 1; // HIGHEST
-        } 
-        
->>>>>>> merge rev
-        // switch statement based on the state
-        switch (state) {
-            case(1): // CASE HIGHEST
-                matrix.clear();
-                for (int i = 0; i < 8; i++) {
-                    for (int j = 0; j < 8; j++) {
-                        if (on[i][j] == 1) {
-                            matrix.drawPixel(i, j, LED_ON);
-                        }
+    // switch statement based on the state
+    switch (state) {
+        case(1): // CASE HIGHEST
+            matrix.clear();
+            for (int i = 0; i < 8; i++) {
+                for (int j = 0; j < 8; j++) {
+                    if (on[i][j] == 1) {
+                        matrix.drawPixel(i, j, LED_ON);
+                    }
+                }
+            }
+            matrix.writeDisplay();
+            wait(.15);
+            break;
+
+        case(2): // CASE HIGH
+            matrix.clear();
+            for (int i = 0; i < 8; i++) {
+                for (int j = 0; j < 8; j++) {
+                    if (high[i][j] == 1) {
+                        matrix.drawPixel(i, j, LED_ON);
                     }
                 }
-                matrix.writeDisplay();
-                wait(.2);
-                break;
+            }
+            matrix.writeDisplay();
+            wait(.15);
+
+
 
-            case(2): // CASE HIGH
-                matrix.clear();
-                for (int i = 0; i < 8; i++) {
-                    for (int j = 0; j < 8; j++) {
-                        if (high[i][j] == 1) {
-                            matrix.drawPixel(i, j, LED_ON);
-                        }
+        case(3): // CASE MEDIUM
+            matrix.clear();
+            for (int i = 0; i < 8; i++) {
+                for (int j = 0; j < 8; j++) {
+                    if (med[i][j] == 1) {
+                        matrix.drawPixel(i, j, LED_ON);
                     }
                 }
-                matrix.writeDisplay();
-<<<<<<< working copy
-                wait(1);
+            }
+            matrix.writeDisplay();
+            wait(.15);
+            break;
 
-=======
-                wait(.2);
-        
->>>>>>> merge rev
-            case(3): // CASE MEDIUM
-                matrix.clear();
-                for (int i = 0; i < 8; i++) {
-                    for (int j = 0; j < 8; j++) {
-                        if (med[i][j] == 1) {
-                            matrix.drawPixel(i, j, LED_ON);
-                        }
+        case(4): // CASE LOW
+            matrix.clear();
+            for (int i = 0; i < 8; i++) {
+                for (int j = 0; j < 8; j++) {
+                    if (low[i][j] == 1) {
+                        matrix.drawPixel(i, j, LED_ON);
                     }
                 }
-                matrix.writeDisplay();
-                wait(.2);
-                break;
+            }
+            matrix.writeDisplay();
+            wait(.15);
+            break;
 
-            case(4): // CASE LOW
-                matrix.clear();
-                for (int i = 0; i < 8; i++) {
-                    for (int j = 0; j < 8; j++) {
-                        if (low[i][j] == 1) {
-                            matrix.drawPixel(i, j, LED_ON);
-                        }
+        case(5): // CASE OFF
+            matrix.clear();
+            for (int i = 0; i < 8; i++) {
+                for (int j = 0; j < 8; j++) {
+                    if (off[i][j] == 1) {
+                        matrix.drawPixel(i, j, LED_ON);
                     }
                 }
-                matrix.writeDisplay();
-                wait(.2);
-                break;
+            }
+            matrix.writeDisplay();
+            wait(.15);
+            break;
 
-            case(5): // CASE OFF
-                matrix.clear();
-                for (int i = 0; i < 8; i++) {
-                    for (int j = 0; j < 8; j++) {
-                        if (off[i][j] == 1) {
-                            matrix.drawPixel(i, j, LED_ON);
-                        }
-                    }
-                }
-                matrix.writeDisplay();
-                wait(.2);
-                break;
-
-            default:
-                break;
-        }
-<<<<<<< working copy
-
-
+        default:
+            break;
     }
 
-=======
-        
-        pc.printf("done");
-    
->>>>>>> merge rev
+    //}
+
+    //pc.printf("done");
+
 }
 
 //Reads SD card and passes value to frequency display
 void display_thread(void const* args)
 {
-    FILE *fp = fopen("/sd/africa-toto-array.txt","r");
-    if(fp == NULL) {
-        pc.printf("Error opening file\n");
-        return;
-    }
-<<<<<<< working copy
+    while(1) {
 
-    int buffer[BUFFER_SIZE];
-=======
-    
-    float buffer[BUFFER_SIZE];
->>>>>>> merge rev
-    int buffer_index = 0;
-    int value;
-<<<<<<< working copy
-    pc.printf("Thread start\n");
-    while(fscanf(fp,"%d,",&value) == 1) {
-        buffer[buffer_index] = value;
-        buffer_index++;
-        pc.printf("%d\n",value);
-        buffer_index++;
-        if(buffer_index == BUFFER_SIZE) {
-            //fast fourier tranform function here
-            display_fft(buffer);
-            buffer_index = 0;
-            memset(buffer,0,sizeof(buffer));
+        float buffer[BUFFER_SIZE];
+        int buffer_index = 0;
+        
+        //int num_elements = NUM_ELEMENTS_AROUND_THE_WORLD;
+        int num_elements = 16979;
+        for(int i=0; i<num_elements; ++i ) {
+            buffer[buffer_index] = song_data[i];
+           // pc.printf("%f", buffer[buffer_index]);
+            buffer_index++; 
+            //pc.printf("%d\n",value);
+            //buffer_index++;
+            if(buffer_index == BUFFER_SIZE) {
+                //fast fourier tranform function here
+                vRealFFT(buffer, 4);
+                display_fft(buffer);
+                buffer_index = 0;
+                memset(buffer,0,sizeof(buffer));
+
+            }
 
         }
-=======
 
-    while(fscanf(fp,"%d,",&value) == 1){
-           buffer[buffer_index] = (float)value;
-           buffer_index++;
-           //pc.printf("%d\n",value);
-           //buffer_index++;
-           if(buffer_index == BUFFER_SIZE)
-           {
-               //fast fourier tranform function here
-               vRealFFT(buffer, 4);
-               display_fft(buffer);
-               buffer_index = 0;
-               memset(buffer,0,sizeof(buffer));
-               
-           }
->>>>>>> merge rev
+        Thread::wait(300);
     }
 
-    fclose(fp);
 }
 
 void speaker_thread(void const* args)
@@ -297,35 +253,41 @@
         // string song_title = "/sd/" + "" + ".wav";
 
 
-        /*
+    if(play == true)
+        {
+        //speaker_lock.lock();
         FILE *wave_file;
         dir = "/sd/" + song + ".wav";
+
+        file_lock.lock();
         wave_file=fopen(dir.c_str(),"r");
+        file_lock.unlock();
+
+        waver.play(wave_file);
 
-        speaker_lock.lock();
-        if (play)
-        {
-            waver.play(wave_file);
-        }
-        speaker_lock.unlock();
+        file_lock.lock();
         fclose(wave_file);
-        Thread::wait(100);
-        */
+        file_lock.unlock();
+        //speaker_lock.unlock();
+    }
+        Thread::wait(1000);
     }
 }
 
 
 int main()
 {
-
+    //sd.mount();
+    pc.printf("I have updated\n");
     Thread th1(speaker_thread);
     Thread th2(display_thread);
     char c;
     while(1) {
-        myled = 1;
+        /*myled = 1;
         wait(0.2);
         myled = 0;
         wait(0.2);
+        */
         while(!pc.readable()) {
             Thread::wait(1);
         }
@@ -334,18 +296,25 @@
             switch(c) {
                 case '1':
                     song = "africa-toto";
+
                     break;
                 case '2':
                     song = "around_the_world-atc";
+                    song_data_length = NUM_ELEMENTS_AROUND_THE_WORLD;
+                    song_data = data_around_the_world;
                     break;
                 case '3':
                     song = "beautiful_life-ace_of_base";
                     break;
                 case '4':
                     song = "dont_speak-no_doubt";
+                    song_data_length = NUM_ELEMENTS_DONT_SPEAK_NO_DOUBT;
+                    song_data = data_dont_speak_no_doubt;
                     break;
                 case '5':
                     song = "my-love";
+                    song_data_length = NUM_ELEMENTS_MY_LOVE;
+                    song_data = data_my_love;
                     break;
                 case '6':
                     song = "Song1_test";
@@ -353,10 +322,12 @@
                 case 'P':
                     // wave_player plays
                     play = true;
+                    led =1;
                     break;
                 case 'S':
                     // wave_player stops
                     play = false;
+                    //led = 0;
                     break;
                 default:
                     break;