this hurts

Dependencies:   FFT

Revision:
2:a6e81c45ef48
Parent:
1:5dd6801bb0d6
Child:
3:3dbf078989b1
Child:
5:598d94b45bad
--- a/main.cpp	Wed Dec 02 23:39:20 2020 +0000
+++ b/main.cpp	Thu Dec 03 00:33:19 2020 +0000
@@ -14,11 +14,13 @@
 AnalogOut DACout(p18); // speaker
 wave_player waver(&DACout);
 Mutex speaker_lock;
+DigitalOut myled(LED1); // mbed LED
+Serial pc (USBTX,USBRX);
 
-DigitalOut myled(LED1); // mbed LED
+#define BUFFER_SIZE 50
 
 // delete later
-int sample[12][8] = {
+/*int sample[12][8] = {
  34085,32666,29810,29823,27514,24175,24468,32559, 
  36401,33075,29037,21933,20189,21751,20975,20087, 
  17530,27723,30891,21128,19982,15446,12413,13597, 
@@ -31,7 +33,7 @@
  57452,51210,52462,59096,56711,52268,49364,42865, 
  41483,37964,39215,35908,31570,28332,24492,23121, 
  22874,18154,15541,15263,12463,12198,13301,9951
-};
+};*/
 
 // states for display output
 const int on[8][8] =
@@ -84,17 +86,17 @@
 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0};
 
-void display_thread(void const* args) {
+
+
+void display_fft(int sample[]) {
     
     int state = 0;
+    int freq = 0;
     matrix.begin(0x70);
     
-    while(1) {
-        for (int a = 0; a < 12; a++) {
-            for (int b = 0; b < 8; b++) {
-        
-        // grab the FFT frequencies;
-        int freq = sample[a][b];
+        for (int a = 0; a < 50; a++) {
+            
+            freq = sample[a];
         
         // determine state based on the FFT output range
         if (freq >= 0 && freq < 10000) {
@@ -177,12 +179,42 @@
         
             default:
                 break;
-        }
+            }
         
         
         }
-        }
+    
+}
+
+//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;
     }
+    
+    int buffer[BUFFER_SIZE];
+    int buffer_index = 0;
+    int value;
+    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));
+               
+           }
+    }
+    
+    fclose(fp);
 }
 
 void speaker_thread(void const* args)
@@ -193,6 +225,8 @@
         // grab file here and put together string
         // string song_title = "/sd/" + "" + ".wav";
         
+        
+        /*
         FILE *wave_file;
         wave_file=fopen("/sd/africa-toto.wav","r");
         
@@ -201,7 +235,7 @@
         speaker_lock.unlock();
         fclose(wave_file);
         Thread::wait(100);
-        
+        */
     }
 }