テスト

Dependencies:   C12832 USBHost mbed MMA7660 wave_player

Fork of app-board-Speaker by Ryohei Funakoshi

main.cpp

Committer:
toyohara
Date:
2016-12-13
Revision:
17:9e07a9ff6e91
Parent:
16:12f30425ba88
Child:
18:3a7e3401ada3

File content as of revision 17:9e07a9ff6e91:



#define mC 261.626
#define mD 293.665
#define mEb 311.13
#define mE 329.628
#define mF 349.228
#define mGb 369.99
#define mG 391.995
#define mAb 415.3
#define mA 440.000
#define mBb 466.16
#define mB 493.883
#define THRESH 80
#define COUNT_MAX 15
#define DIM 3
#define NUM_OF_NOTES 21
#define NUM_OF_SCALE 5

#include "C12832.h"
#include "mbed.h"
#include "rtos.h"
#include "MMA7660.h"
#include "USBHostMSD.h"
#include "wave_player.h"
#include <string>

using namespace std;

C12832 lcd(p5, p7, p6, p8, p11);
DigitalIn fire(p14);
//DigitalIn up(p15);
//DigitalIn down(p12);
PwmOut sp1(p26);
InterruptIn myup(p15);
InterruptIn mydown(p12);
PwmOut led1(p23);
PwmOut led2(p24);
PwmOut led3(p25);
Semaphore three_slots(1);
Timer for_chat;


//for acceleration sonsor
MMA7660 acc(p28, p27);
float r[DIM];

//for music
AnalogOut DACout (p18);
wave_player waver(&DACout);
AnalogIn volumeMeter(p19);
AnalogIn musicSwitch(p20);
InterruptIn push(p14);
InterruptIn right(p16);
InterruptIn left(p13);
FILE *wave_file;
Mutex stdio_mutex;

int i=0;
int count = 0;
bool pushed = false;
int i_for20mm;
int i_for20mm_old = 0;
int scale = 0;
bool printflag = false;

float mm[]={mC,mD,mE,mF,mG,mA,mB,mC*2,mD*2,mE*2,mF*2,mG*2,mA*2,mB*2,mC*4};

float mmm[]={mC,mD,mE,mF,mG,mA,mB,mC*2,mD*2,mE*2,mF*2,mG*2,mA*2,mB*2,mC*4, mD*4,mE*4,mF*4,mG*4,mA*4,mB*4};//has 21
float notes[NUM_OF_SCALE][NUM_OF_NOTES] = {  {mC,mD,mE,mF,mG,mA,mB,mC*2,mD*2,mE*2,mF*2,mG*2,mA*2,mB*2,mC*4, mD*4,mE*4,mF*4,mG*4,mA*4,mB*4},             //major
                                             {mC,mD,mEb,mF,mG,mA,mBb,mC*2,mD*2,mEb*2,mF*2,mG*2,mA*2,mBb*2,mC*4, mD*4,mEb*4,mF*4,mG*4,mA*4,mBb*4},       //Dorian
                                             {mC,mD,mEb,mF,mG,mAb,mBb,mC*2,mD*2,mEb*2,mF*2,mG*2,mAb*2,mBb*2,mC*4, mD*4,mEb*4,mF*4,mG*4,mAb*4,mBb*4},    //natural minor 
                                             {mC,mEb,mF,mGb,mG,mBb,mC*2,mEb*2,mF*2,mGb*2,mG*2,mBb*2,mC*4,mEb*4,mF*4,mGb*4,mG*4,mBb*4,mC*8,mEb*8,mF*8},  //blues
                                             {mC,mE,mF,mG,mB,mC*2,mE*2,mF*2,mG*2,mB*2,mC*4,mE*4,mF*4,mG*4,mB*4,mC*8,mE*8,mF*8,mG*8,mB*8,mC*16}};        //Okinawa
                                             
string names[] = {"Major", "Dorian", "Minor", "Blues", "Okinawa"};

void printlcd(void)
{
        lcd.cls();
        stdio_mutex.lock();
        lcd.locate(10,10);
        lcd.printf("scale %s", names[scale]);
        printflag=false;
        stdio_mutex.unlock();
}
void intRise(void) 
{    
    if(for_chat.read_ms()>THRESH);
    {
        if(i<=13){
        i++;
        count ++;
        }
        sp1.period(1.0/mm[i]);
        sp1.write(0.5f);
        for_chat.reset();
    }
}
 
void intFall(void) 
{
    if(for_chat.read_ms()>THRESH);
    {
        if(i >=1){
        i--;
        count --;
        }
        sp1.period(1.0/mm[i]);
        sp1.write(0.5f);
        for_chat.reset();
    }        
}

void On(void)
{
    pushed = true;
}

void Off(void)
{
    pushed = false;
}

void scaleUp(void)
{
    if(for_chat.read_ms()>THRESH) ;
    {
        if (scale == NUM_OF_SCALE - 1)
        {
            scale = 0;
        } else
        {
            scale++;
        }
        printflag=true;
    }
    for_chat.reset();
}

void scaleDown(void)
{
    if(for_chat.read_ms()>THRESH) ;
    {
        if (scale == 0)
        {
            scale = NUM_OF_SCALE - 1;
        } else 
        {
            scale--;
        }
        printflag=true;
    }
    for_chat.reset();
}


void r_thread(void const *argument) 
{
    float r = 0;
    while (true) 
    {
        r=0;
        while(r<=1)
        {
            three_slots.wait();
            switch(count){
                case 0:r = 0.0;break;
                case 1:r=0.07;break;
                case 2:r=0.13;break;
                case 3:r=0.20;break;
                case 4:r=0.27;break;
                case 5:r=0.34;break;
                case 6:r=0.41;break;
                case 7:r=0.48;break;
                case 8:r=0.55;break;
                case 9:r=0.62;break;
                case 10:r=0.69;break;
                case 11:r=0.76;break;
                case 12:r=0.83;break;
                case 13:r=0.90;break;
                case 14:r=0.97;break;
                }
            //r = (float)(count%COUNT_MAX)/COUNT_MAX;
            led1 = r;
            //r += 0.01;
            lcd.locate(0,16);
            lcd.printf("R: %3.2f",r);
            three_slots.release();
            Thread::wait(100);

        }
        while(r>0)
        {
            three_slots.wait();
            switch(count){
                case 0:r = 0.0;break;
                case 1:r=0.07;break;
                case 2:r=0.13;break;
                case 3:r=0.20;break;
                case 4:r=0.27;break;
                case 5:r=0.34;break;
                case 6:r=0.41;break;
                case 7:r=0.48;break;
                case 8:r=0.55;break;
                case 9:r=0.62;break;
                case 10:r=0.69;break;
                case 11:r=0.76;break;
                case 12:r=0.83;break;
                case 13:r=0.90;break;
                case 14:r=0.97;break;
                }
            //r = (float)(count%COUNT_MAX)/COUNT_MAX;
            led1 = r;
            //r -= 0.01;
            lcd.locate(0,16);
            lcd.printf("R: %3.2f",r);
            three_slots.release();
            Thread::wait(100);

        }
    }
} 
void g_thread(void const *argument) 
{
    float g = 0;
    while (true) 
    {
        g=0;
        while(g<=1)
        {
            three_slots.wait();
            switch(count){
                case 0:g = 0.50;break;
                case 1:g = 0.50;break;
                case 2:g=0.63;break;
                case 3:g=0.63;break;
                case 4:g=0.77;break;
                case 5:g=0.77;break;
                case 6:g=0.90;break;
                case 7:g=0.90;break;
                case 8:g=0.47;break;
                case 9:g=0.47;break;
                case 10:g=0.33;break;
                case 11:g=0.33;break;
                case 12:g=0.20;break;
                case 13:g=0.20;break;
                case 14:g=0.07;break;
                }
            led2 = g;
            //g += 0.01;
            lcd.locate(40,16);
            lcd.printf("G: %3.2f",g);
            three_slots.release();
            Thread::wait(200);
        }
        while(g>0)
        {
            three_slots.wait();
            switch(count){
                case 0:g = 0.50;break;
                case 1:g = 0.50;break;
                case 2:g=0.63;break;
                case 3:g=0.63;break;
                case 4:g=0.77;break;
                case 5:g=0.77;break;
                case 6:g=0.90;break;
                case 7:g=0.90;break;
                case 8:g=0.47;break;
                case 9:g=0.47;break;
                case 10:g=0.33;break;
                case 11:g=0.33;break;
                case 12:g=0.20;break;
                case 13:g=0.20;break;
                case 14:g=0.07;break;
                }
            led2 = g;
            //g -= 0.01;
            lcd.locate(40,16);
            lcd.printf("G: %3.2f",g);
            three_slots.release();
            Thread::wait(200);
        }
    }
} 
void b_thread(void const *argument) 
{
    float b = 0;
    while (true) 
    {
        b=0;
        while(b<=1)
        {
            three_slots.wait();
            switch(count){
                case 0:b = 1.00;break;
                case 1:b = 1.00;break;
                case 2:b=1.00;break;
                case 3:b=0.80;break;
                case 4:b=0.80;break;
                case 5:b=0.80;break;
                case 6:b=0.60;break;
                case 7:b=0.60;break;
                case 8:b=0.60;break;
                case 9:b=0.40;break;
                case 10:b=0.40;break;
                case 11:b=0.40;break;
                case 12:b=0.20;break;
                case 13:b=0.20;break;
                case 14:b=0.20;break;
                }
            led3 = b;
            //b += 0.01;
            lcd.locate(80,16);
            lcd.printf("B: %3.2f",b);
            three_slots.release();
            Thread::wait(300);
        }
        while(b>0)
        {
            three_slots.wait();
            switch(count){
                case 0:b = 1.00;break;
                case 1:b = 1.00;break;
                case 2:b=1.00;break;
                case 3:b=0.80;break;
                case 4:b=0.80;break;
                case 5:b=0.80;break;
                case 6:b=0.60;break;
                case 7:b=0.60;break;
                case 8:b=0.60;break;
                case 9:b=0.40;break;
                case 10:b=0.40;break;
                case 11:b=0.40;break;
                case 12:b=0.20;break;
                case 13:b=0.20;break;
                case 14:b=0.20;break;
                }
            led3 = b;
            //b -= 0.01;
            lcd.locate(80,16);
            lcd.printf("B: %3.2f",b);
            three_slots.release();
            Thread::wait(300);
        }
    }
} 
void count_thread(void const *argument) 
{
    while (true) 
    {
        three_slots.wait();
        lcd.locate(0,3);
        lcd.printf("Counting : %d   ",i);
        three_slots.release();
        Thread::wait(100);
    }
} 

void acc_thread(void const *argument) 
{
    while (true) 
    {
        acc.readData(r);
        lcd.locate(0,0);
        lcd.printf("x = %f", r[0]);
        lcd.locate(0,10);
        lcd.printf("y = %f", r[1]);
        lcd.locate(0,20);
        lcd.printf("z = %f", r[2]);
        Thread::wait(10);
    }
} 

void disp_thread(void const *argument) 
{
    float volume;
    float mswitch;
    
    while (true) 
    {
        volume = volumeMeter.read();
        mswitch = musicSwitch.read();
        i_for20mm = (int)(mswitch*20);//change 20 param
        //wait(mpmeter);
        //lcd.locate(0,10);
        //lcd.printf("mpmeter = %f", mpmeter);
        
        // for debug
        //lcd.locate(0,10);
        //lcd.printf("i = %d  ", i_for20mm);
        
        if (i_for20mm != i_for20mm_old)
        {
            sp1.period(1.0/(notes[scale][i_for20mm]));
        }
        i_for20mm_old = i_for20mm;
        
        if(pushed)
        {
            sp1.write(volume);
        }
        else
        {
            sp1.write(0.0f);
        }   
        
        for_chat.reset();
        if(printflag)
        {
            printlcd();   
        }
        /////////////////////////////
        
        //for debug
        //lcd.locate(0,0);
        //lcd.printf("volume = %f", volume);
        //wait(mpmeter);
        Thread::wait(50);
    }
} 
void wave_file_player(void const *argument)
 {
    waver.play(wave_file);
    fclose(wave_file);   
    //wave_finished = true;
}    
//void lcd_thread(void const *argument) 
//{
//        stdio_mutex.lock();
//        lcd.locate(0,0);
//        lcd.printf("scale = %d", scale);
//        //lcd.cls();
//        stdio_mutex.unlock();
//        Thread::wait(50);
//    
//}
int main() 
{
    wait(0.01);
    for_chat.start();
    myup.fall(&intRise);
    mydown.fall(&intFall);
    push.rise(&On);
    push.fall(&Off);
    right.rise(&scaleUp);
    left.rise(&scaleDown);
    
    
    //Thread thread1(r_thread,(void *)"Th 1");
    //Thread thread2(g_thread,(void *)"Th 2");
    //Thread thread3(b_thread,(void *)"Th 3");
    //Thread thread4(count_thread,(void *)"Th 4");    
    Thread thread6(disp_thread);
    //Thread thread4(lcd_thread);
    //Thread thread7(sound_thread, (void *)"Th 7");
    
    //for USB
    USBHostMSD msd("usb");
    while(!msd.connect())
    {
        Thread::wait(200);
    }
    
    //for music file
    //FILE *wave_file;
    //sp1.period(1.0 / 16000.0);
    stdio_mutex.lock();
    wave_file = fopen("/usb/music/BGMforMBED2.wav", "r");

    stdio_mutex.unlock();
    waver.play(wave_file);
    fclose(wave_file);   

    
    lcd.cls();
    wait(0.01);
    while(1)
    {
        

    }
}