inital commit

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

main.cpp

Committer:
lfink6
Date:
2021-12-12
Revision:
3:8a1fd3450cb5
Parent:
2:c2afd0c426af
Child:
5:afdd7d5b432a

File content as of revision 3:8a1fd3450cb5:

#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);
Serial 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 play = 0;
bool menu1 = 0;
int currentsong = 0;
int avgIMU;
int avgMic;
int songcount;
vector <string> songList; // vector of songs to index

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 TrackSelect()
{
    //Add code here to use the avg to set the next track, or set the next track based on the menu. 
    }
void LCDThread(void const *argument)
{
    while(1){
        if(!menu1)
        {
    LCD.lock();
    uLCD.locate(1,1);
    uLCD.printf("Put Song Name Here");
    uLCD.printf("%4D", avgIMU);
    uLCD.printf("%4D", avgMic);
    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
    {
       mentex.lock();
       if(currentsong<songcount+3)
       {
       for(int i=0; i<3; i++)
       {
           //add code to display and scroll through menu here 
           LCD.lock();
           uLCD.printf("\r%d. %s\r\n\r\n", (i+1), songList[currentsong+i].substr(0,songList[currentsong + i].find(".wav")));
           LCD.unlock();
           
    }
    currentsong= currentsong+3;
    }
    else
    {
        for(int i=0; i<songcount-currentsong; i++)
       {
           //add code to display and scroll through menu here 
           LCD.lock();
           uLCD.printf("\r%d. %s\r\n\r\n", (i+1), songList[currentsong+i].substr(0,songList[currentsong + i].find(".wav")));
           LCD.unlock();
       }
       for(int i=0; i<3-(songcount-currentsong); i++)
       {
           //add code to display and scroll through menu here 
           LCD.lock();
           uLCD.printf("\r%d. %s\r\n\r\n", (i+1), songList[currentsong+i].substr(0,songList[currentsong + i].find(".wav")));
           LCD.unlock();
       }
    }
    mentex.unlock();
    }
        
    Thread::wait(1000);
    }
    
}
void buttonThread()
{
    //add playpause and skip features here 
    Speaker.lock();
    if(play)
    {
        //playing = false;
        }
        else
        {
            //playing = true;
        }
    play=!play;
    myled=play;
    Speaker.unlock();
    Thread::wait(100);
}
void skipThread()
{
    //add skip features
    mentex.lock();
    if(currentsong<songcount)
    {
    currentsong++;
    }
    else
    {
        currentsong=0;
        }
    mentex.unlock();
    Thread::wait(100);
}
void backThread()
{
    //add skip features
    
    mentex.lock();
    if(currentsong!=0)
    {
        currentsong--;
    }
    else
    {
       currentsong=songcount; 
    }
        
    mentex.unlock();
    Thread::wait(100);
}
void menuThread()
{
    Speaker.lock();
    menu1=!menu1;
    Speaker.unlock();
    Thread::wait(100);
}
void BlueThread(void const *argument)
{
    //add bluetooth control code here 
    while(1)
    {
        if(blue.readable())
        {
            
        }
        }
}
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(2500);
    }
    
}

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);
    wave_file=fopen("/sd/sample.wav","r");
    if(wave_file==NULL) printf("file open error!\n\n\r");
    waver.play(wave_file);
    fclose(wave_file);
    }
}