inital commit

Dependencies:   mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem2 PinDetect MMA8452

main.cpp

Committer:
lfink6
Date:
2021-12-12
Revision:
10:5bd6abd66d12
Parent:
9:08886cc06a5d
Child:
11:1a47726ac72a

File content as of revision 10:5bd6abd66d12:

#include "mbed.h"
#include "rtos.h"
#include "SDFileSystem.h"
#include "uLCD_4DGL.h"
#include "wave_player.h"
#include "LSM9DS1.h"
#include "PinDetect.h"
#include <string>
#include <vector>

DigitalOut myled(LED1);
RawSerial blue(p28,p27);

SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
uLCD_4DGL uLCD(p13,p14,p11);

LSM9DS1 imu(p9, p10, 0xD6, 0x3C);
Serial pc(USBTX, USBRX);

PinDetect playpause(p23);
PinDetect menu(p26);
PinDetect skip(p25);
PinDetect back(p30);
Mutex LCD;
Mutex Speaker;
Mutex avgs;
Mutex mentex;

AnalogOut DACout(p18);

wave_player waver(&DACout);
bool playing = false;
bool play = 1;
bool menu1 = 0;
bool inmenu=0;
int currentsong = 0;
int avgIMU;
int avgMic;
int songcount;
vector <string> songList; // vector of songs to index
string dir = "/sd/myMusic/";

class microphone
{
public :
    microphone(PinName pin);
    float read();
    operator float ();
private :
    AnalogIn _pin;
};
microphone::microphone (PinName pin):
    _pin(pin)
{
}
float microphone::read()
{
    return _pin.read();
}
inline microphone::operator float ()
{
    return _pin.read();
}
 
microphone mymicrophone(p16);
void LCDThread(void const *argument)
{
    while(1){
        Speaker.lock();
        if(!menu1)
        {
            Speaker.unlock();
            LCD.lock();
            uLCD.cls();
            uLCD.locate(1,1);
            uLCD.printf("%s",songList[currentsong].substr(0,songList[currentsong].find(".wav")));
            if(play)
            {
                //play
        
                uLCD.filled_rectangle(0,118,280,40,BLACK);
                uLCD.triangle(120, 100, 40, 40, 10, 100, 0x0000FF);
            }
            else
            {
        //pause
                uLCD.filled_rectangle(0,118,110,40,WHITE);
                uLCD.filled_rectangle(50,118,100,40,BLACK);
                uLCD.filled_rectangle(180,118,280,40,WHITE);
            }
            LCD.unlock();
        }
        else
        {
       
            Speaker.unlock();
            mentex.lock();
            LCD.lock();
            uLCD.cls();
            LCD.unlock();
            for(int i=-1; i<2; i++)
            {
                //add code to display and scroll through menu here 
            
                mentex.lock();
                if(currentsong+i>=0)
                {
                  if(currentsong+i<songcount)
                  {
                    LCD.lock();
                    uLCD.printf("%s\r\n\r\n", songList[currentsong+i].substr(0,songList[currentsong + i].find(".wav")));
                    LCD.unlock();
                  }
                    else
                    {
                    LCD.lock();
                    uLCD.printf("%s\r\n\r\n", songList[0].substr(0,songList[0].find(".wav")));
                    LCD.unlock();
                    }
                }
                else if(currentsong+i<0)
                {
                LCD.lock();
                uLCD.printf("%s\r\n\r\n", songList[songcount-1].substr(0,songList[songcount-1].find(".wav")));
                LCD.unlock();
                }
                
           }
           mentex.unlock();
           LCD.lock();
           uLCD.locate(2,3);
           uLCD.printf("%s", "^^^");
           LCD.unlock();
        }   
    Thread::wait(500);
    }
}
void buttonThread()
{
    //add playpause and skip features here 
    Speaker.lock();
    if(!menu1)
    {
    playing=!playing;
    play=!playing;
    }
    else
    {
     playing=!playing;
     Thread::wait(500);
     playing=!playing;   
    }
    myled=play;
    Speaker.unlock();
    Thread::wait(10);
}
void skipThread()
{
    //add skip features
    
    Speaker.lock();
    if(currentsong<songcount-1)
    {
    currentsong++;
    }
    else
    {
        currentsong=0;
        }
        if(!menu1)
        {
        playing=true;
        play=!playing;
        }
        Speaker.unlock();
}
void backThread()
{
    Speaker.lock();
    if(currentsong!=0)
    {
        currentsong--;
    }
    else
    {
       currentsong=songcount-1; 
    }
        if(!menu1)
        {
        playing=true;
        play=!playing;
        }
        Speaker.unlock();
}
void menuThread()
{
    menu1=!menu1;
}
void BlueThread(void const *argument)
{
    //add bluetooth control code here 
    char bnum =0;
    char bhit=0;
    while(1)
    {

        if(blue.readable()&&blue.writeable())
        {
            if (blue.getc()=='!') {
            if (blue.getc()=='B') { //button data
                bnum = blue.getc(); //button number
                bhit=blue.getc();
                if ((bnum>='1')&&(bnum<='4')) //is a number button 1..4
                {
                                        LCD.lock();
        uLCD.printf("%s", "test lol");
        LCD.unlock();
                    switch (bnum)
                    {
                        case '1': //number button 1   //add playpause and skip features here 
                        if (bhit=='1') {
                            Speaker.lock();
                            if(play)
                            {
                                playing = false;
                            }
                            else
                            {
                                playing = true;
                            }
                            play=!play;
                            myled=play;
                            Speaker.unlock();
                            Thread::wait(10);
                            }
                            break;
                        case '2': //number button 2
                        if (bhit=='1') {
                                mentex.lock();
                                if(currentsong<songcount-1)
                                {
                                currentsong++;
                                }
                                else
                                {
                                    currentsong=0;
                                }
                                    if(!menu)
                                    {
                                    playing=true;
                                    play=true;
                                    }
                                    mentex.unlock();
                                }
                            break;
                        case '3': //number button 3
                        if (bhit=='1') {
                                mentex.lock();
                                if(currentsong!=0)
                                {
                                    currentsong--;
                                }
                                else
                                {
                                   currentsong=songcount-1; 
                                }
                                    if(!menu)
                                    {
                                    playing=true;
                                    play=true;
                                    }
                                    mentex.unlock();
                                }
                            break;
                        case '4': //number button 4
                        if (bhit=='1') {
                                   mentex.lock();
                                    menu1=!menu1;
                                    mentex.unlock();
                                    }
                            break;
                        default:
                            break;
                        }
                        }
                
            }
        }
        }
        Thread::wait(1000);
        }
}
void IMUThread(void const *argument)
{
    while(1){
    avgs.lock();
    //put imu averaging and next track selection code here if that is selected 
    avgMic = int(((abs((mymicrophone - (0.67/3.3)))*500.0)+avgMic)/2);
    imu.readAccel();
    avgIMU=int((((imu.ax+imu.az+imu.ay)/3.0)+avgIMU)/2);
    avgs.unlock();
    Thread::wait(5000);
    }
    
}

int main()
{
    uLCD.cls();
    uLCD.baudrate(3000000);
    uLCD.background_color(BLACK);
    uLCD.text_width(1);
    uLCD.text_height(1);
    imu.begin();
    if (!imu.begin()) {
        //set fail flag for imu here
    }
    imu.calibrate();
    blue.baud(9600);
    
    DIR *dp;
    struct dirent *dirp;
    dp = opendir("/sd/myMusic");
    songcount = 0;
    if(dp !=NULL)
    {
       while ((dirp = readdir(dp)) != NULL) {
            songList.push_back(string(dirp->d_name));
            //uLCD.printf("\r%s\r\n", string(dirp->d_name));
            songcount++;
        }
    }
    playpause.mode(PullUp);
    playpause.attach_deasserted(&buttonThread);
    playpause.setSampleFrequency();
    
    menu.mode(PullUp);
    menu.attach_deasserted(&menuThread);
    menu.setSampleFrequency();   
    skip.mode(PullUp);
    skip.attach_deasserted(&skipThread);
    skip.setSampleFrequency();
    back.mode(PullUp);
    back.attach_deasserted(&backThread);
    back.setSampleFrequency();
    //LCD, Player, button Interrupt, bluetooth, imu+mic
    Thread thread1(LCDThread);
    Thread thread4(BlueThread);
    Thread thread5(IMUThread);
    while(1){
    FILE *wave_file;
    Thread::wait(1000);
    mentex.lock();
    string selectedSong= "/sd/myMusic/" + songList[currentsong];
    mentex.unlock();
    const char* song = selectedSong.c_str();
    wave_file=fopen(song,"r");
    if(wave_file==NULL) uLCD.printf("file open error!\n\n\r");
    waver.play(wave_file);
    fclose(wave_file);
    }
}