test

Dependencies:   mbed

Footile.cpp

Committer:
Uchida0923
Date:
2018-05-30
Revision:
0:03cdee95fb5a

File content as of revision 0:03cdee95fb5a:

#include "Footile.h"
#include <math.h>

//myFootile(P0_8, P0_9, P0_10, P0_11, P0_12, P0_19, P0_18, P0_7, P0_17, P0_5, P0_4, P0_22);
Footile::Footile(PinName led1, PinName led2, PinName led3, PinName trg, PinName ad, PinName tx, PinName rx, PinName io1, PinName io2, PinName sda, PinName scl, PinName en):
    _led1(led1), _led2(led2), _led3(led3), _trg(trg), _prssr(ad), _bt(tx, rx), _btDummy1(io1), _btDummy2(io2), _Vibration(sda, scl, en)
{

    //_Vibration.i2cWriteByte(LIBRARY_SELECTION, _Vibration.LRA_LIB);
    //_Vibration.i2cWriteByte(MODE, _Vibration.PWM_ANALOG);
    

    _led1 = 1;
    _led2 = 1;
    _led3 = 1;
    _volumeBuf=255;
    
    //flag
    /*
    SteadyMode=false;
    SinewaveMode=true;
    ChangeFlag=true;
    CorrespondMode=false;
    ConnectCheck=false;
    snowMode=false;
    grassMode=false;
    */
    SteadyMode=false;
    SinewaveMode=false;
    CorrespondMode=false;
    ChangeFlag=true;
    snowMode=true;
    grassMode=false;
    
    _sens=0.8;
    
    _countSize=0;
    
    _on=0;

    _trans.attach(this,&Footile::_transmission,0.5);
    _bt.attach(this, &Footile::_onReceive, Serial::RxIrq);
    _btDummy1 = 0;
    _btDummy2 = 0;
    
    
    //_Vibration.rtp((char)(255)); 
    
    _heart.attach(this, &Footile::_beat, 2.0);
}



void Footile::doFunctions()
{
    static int prv_on = 0,Volume=0;
    


    // make hysteresis
    if(_prssr > _sens)
        _on = 1;
    if(_prssr < 0.2)
        _on = 0;

    if(_on && !prv_on||ChangeFlag==true) {
        _trg = 1;
        _led1 = 0;
        Volume=_volumeBuf;
        if(SinewaveMode==true){
            _Vibration.rtp((char)(Volume)); 
        }
        _bt.printf("%d\n",_prssr);
    }
    if(!_on && prv_on||ChangeFlag==true) {
        _trg = 0;
        _countSize=0;
        if(SteadyMode==false&&SinewaveMode==true){
            Volume=0;//When Not c=125
            _Vibration.rtp((char)(Volume)); 
        }
    }
    
    prv_on = _on;

    
    if(snowMode==true||grassMode==true){
        _viber.attach(this, &Footile::VibeTimer,0.001);
    } else{
       _viber.detach();     
    }
    
    ChangeFlag=false;
        
    if(CorrespondMode==true){  
        _Vibration.rtp((char)(_volumeBuf*_prssr));
    }
    wait(0.001);
}


void Footile::OnVibe(const float* wave, int size){
    if(_countSize < size){
        _Vibration.rtp((char)(wave[_countSize]*255));
            _countSize++;
    }
    else  {
            _Vibration.rtp((char)0);     
    }
}


void Footile::VibeTimer(){
    if(_on==1&&snowMode==true){
        OnVibe(snowwave500, (int)(sizeof(snowwave500)/sizeof(snowwave500[0])));
    }else if(_on==1&&grassMode==true){
        OnVibe(grasswave, (int)(sizeof(grasswave)/sizeof(grasswave[0])));
    }
    else if(_on==0){
        _Vibration.rtp((char)0);    
    }
}










// interruption on receiving
void Footile::_onReceive(void)
{
    unsigned short num[5];
    unsigned short data_index = 0;
    unsigned short index;
    unsigned short data=0;
    
    ConnectCheck=true;
    
    for(int i=0;i<sizeof(num);i++){
        num[i]=0;    
    }

    while(1) {
        if (!_bt.readable()) continue;
        
        char let = _bt.getc();
        //_bt.printf("%c",let);
        data_index++;
        
        if (let == '\n')
        {
           // num[data_index-1] = 0;
            break;          
        }else{
            num[data_index]=let-'0';
            }
    }
        
    index=data_index-1; 
    
    for(int i=0;i<data_index-1;i++){
        data+=(unsigned short)(pow(10.0,(double)i))*num[index];//get unsigned short values
        index--;
    }
     
     data_index = 0;
    // echo back    
    _bt.printf("%d",data);
    _modeChange(data);
    data=0;
   
}












void Footile:: _modeChange(unsigned short c){
    
        // PWM mode
    if(c == 0) {
        SteadyMode=false;
        SinewaveMode=true;
        ChangeFlag=true;
        CorrespondMode=false;
        snowMode=false;
        grassMode=false;
        /*_Vibration.i2cWriteByte(MODE, _Vibration.PWM_ANALOG);
        _heart.attach(this, &Footile::_beat, 1.0);
        ChangeFlag=true;*/
        // Wavefrom sequencer mode
    } else if (0 < c && c <= 123) { //Change waveform
        SteadyMode=false;
        SinewaveMode=false;
        ChangeFlag=false;
        CorrespondMode=false;
        snowMode=false;
        grassMode=false;
        _Vibration.i2cWriteByte(WAVEFORM_SEQUENCER_1, c);
        _Vibration.i2cWriteByte(MODE, _Vibration.EXTERNAL_LEVEL);
        _heart.attach(this, &Footile::_beat, 1.0);
        // Standby mode
        
    }else if(c==124){   //SteadyMode
        SteadyMode=true;
        SinewaveMode=true;
        ChangeFlag=true;
        CorrespondMode=false;
        snowMode=false;
        grassMode=false;
    }else if (125<=c&&c<=255) {   //Change Magnituide
        if(SteadyMode==false&&SinewaveMode==false){
            ChangeFlag=false;    
        }else{
            ChangeFlag=true;
        }
        _volumeBuf=(c-125)*255/(255-125);
        
    }else if(c==256){ //Change Amplitude Mode
        CorrespondMode=true;
        ChangeFlag=false;
    }else if(257<=c&&c<=261){   //Change Sensitvty(257<=c<=266)
        _sens=(c-257)*0.5/4.0+0.3;
    }else if(262<=c&&c<=266){
        _sens=(c-262)*0.15/4.0+0.8;
    }
    
    else if(c==267){
        SteadyMode=false;
        SinewaveMode=false;
        CorrespondMode=false;
        ChangeFlag=true;
        snowMode=true;
        grassMode=false;
    }else if(c==268){
        SteadyMode=false;
        SinewaveMode=false;
        CorrespondMode=false;
        ChangeFlag=true;
        snowMode=false;
        grassMode=true;        
    }
    else {
        _Vibration.i2cWriteByte(MODE, _Vibration.STANDBY);
        _heart.attach(this, &Footile::_beat, 2.0);
    }
    
    
    
}

void Footile::_transmission(void)
{
    _bt.printf("a");
    
    if(ConnectCheck==true){
        _trans.detach();
    }
}


// heart beat
void Footile::_beat(void)
{
    _led2 = !_led2;
}