help :(

Dependencies:   FFT

main.cpp

Committer:
annieluo2
Date:
2020-12-03
Revision:
3:3dbf078989b1
Parent:
2:a6e81c45ef48
Child:
4:7974ef1b1e07
Child:
6:d311a95f1e9b

File content as of revision 3:3dbf078989b1:

#include "mbed.h"
#include "SDFileSystem.h"
#include "wave_player.h"
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "rtos.h"
#include "FFT.h"

SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card

I2C i2c(p28, p27); // LED display 
Adafruit_8x8matrix matrix = Adafruit_8x8matrix(&i2c);

AnalogOut DACout(p18); // speaker
wave_player waver(&DACout);
Mutex speaker_lock;
DigitalOut myled(LED1); // mbed LED
Serial pc (USBTX,USBRX);

#define BUFFER_SIZE 64

// states for display output
const int on[8][8] =
{1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1};

const int high[8][8] = 
{0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 1, 1, 1, 1, 0, 0,
0, 1, 1, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1};

const int med[8][8] =
{0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 1, 0, 0, 0,
0, 0, 1, 1, 1, 1, 0, 0,
0, 0, 1, 1, 1, 1, 0, 0,
0, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1};

const int low[8][8] =
{0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 1, 0, 0,
0, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1};

const int off[8][8] =
{0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0};



void display_fft(float sample[]) {
    
    int state = 0;
    float freq = 0;
    matrix.begin(0x70);
    
    int s = sizeof(sample);
    
    pc.printf("%d\n", s);
    
    for (int a = 0; a < 64; a++) {
            
        freq = sample[a];
        
        pc.printf("%d\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; // HIGHEST
        } 
        
        // 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(1);
                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(1);
        
            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();
                wait(1);
                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);
                        }
                    }
                }
                matrix.writeDisplay();
                wait(1);
                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(1);
                break;
        
            default:
                break;
            }
        
        
        }
        
        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;
    }
    
    float buffer[BUFFER_SIZE];
    int buffer_index = 0;
    int value;

    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, 64);
               display_fft(buffer);
               buffer_index = 0;
               memset(buffer,0,sizeof(buffer));
               
           }
    }
    
    fclose(fp);
}

void speaker_thread(void const* args)
{
    while(1)
    {   
        // check helper function for new song in GUI
        // grab file here and put together string
        // string song_title = "/sd/" + "" + ".wav";
        
        
        /*
        FILE *wave_file;
        wave_file=fopen("/sd/africa-toto.wav","r");
        
        speaker_lock.lock();
        waver.play(wave_file);
        speaker_lock.unlock();
        fclose(wave_file);
        Thread::wait(100);
        */
    }
}
    

int main()
{
   
    Thread th1(speaker_thread);
    Thread th2(display_thread);

    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
        Thread::wait(100);
    }
    
}