Guitar Effector using "mbed application board".

Dependencies:   C12832 mbed

Guitar Effector using "mbed application board".

/media/uploads/vaifreak/dscn0187.jpg

/media/uploads/vaifreak/schematic_00.jpg

/media/uploads/vaifreak/schematic_01.jpg

/media/uploads/vaifreak/schematic_02.jpg

Menu.h

Committer:
vaifreak
Date:
2015-09-04
Revision:
2:25adc1277b3e
Child:
3:1666e2d5bd46

File content as of revision 2:25adc1277b3e:

//=============================================================================
//  @author vaifreak
//  @brief  menu display & effect parameter Control.
//=============================================================================
#pragma once

#include "Filter.h"
#include "Drive.h"
#include "Delay.h"
#include "MyInputUtil.h"

//---------------------------------------------
// 
//---------------------------------------------
class Menu
{
public:
    float masterVolume;

private:
    //function type.
    enum {
        FILTER_PRE,
        DRIVE,
        FILTER_POST,
        DELAY,
        MASTER,
        
        FUNCTION_MAX
    };

    //param for filter pre
    int filter_pre_type;
    float filter_pre_freq;
    float filter_pre_Q;

    //param for filter post
    int filter_post_type;
    float filter_post_freq;
    float filter_post_Q;

    //param for Drive
    float drive_gain;
    float drive_volume;

    //param for delay
    float delay_time;
    float delay_feedback;
    float delay_mix;

    float sample_rate;

    bool isOn[FUNCTION_MAX];

    bool dirtyFlag;
    void SetDirty(){ dirtyFlag = true; }
    bool GetDirty(){ return dirtyFlag; }
    void ClearDirty(){ dirtyFlag = false; }

    C12832 *lcd;
    MyVolume *pot1;
    MyVolume *pot2;
    MyButton *stick_U;
    MyButton *stick_D;
    MyButton *stick_L;
    MyButton *stick_R;
    MyButton *stick_C;

    Filter *filter_pre;
    Filter *filter_post;
    Drive *drive;
    Delay *delay;

    int current_function;
   
public:
    //-----------------------------------------
    //
    //-----------------------------------------
    Menu()
    {
        lcd = new C12832(p5, p7, p6, p8, p11);
        pot1 = new MyVolume(p19);
        pot2 = new MyVolume(p20);
        stick_U = new MyButton(p15);
        stick_D = new MyButton(p12);
        stick_L = new MyButton(p13);
        stick_R = new MyButton(p16);
        stick_C = new MyButton(p14);
        
        printf("Menu constructed.\n");
    }

    //-----------------------------------------
    //
    //-----------------------------------------
    void Init(
        float sample_rate_,
        Filter *filter_pre_,
        Filter *filter_post_,
        Drive *drive_,
        Delay *delay_ )
    {
        sample_rate = sample_rate_;
        
        filter_pre = filter_pre_;
        filter_post = filter_post_;
        drive = drive_;
        delay = delay_;

        for(int i=0; i<FUNCTION_MAX; i++){
            isOn[i] = true;
        }

        current_function = 0;

        filter_pre_type = filter_pre->type;
        filter_pre_freq = 3000.0f;
        filter_pre_Q = 0.5f;
        UpdateFilterPreParam();
    
        filter_post_type = filter_post->type;
        filter_post_freq = 3000.0f;
        filter_post_Q = 0.5f;
        UpdateFilterPostParam();

        drive_gain = 20.0f;
        drive_volume = 0.5f;
        UpdateDriveParam();
        
        delay_time = 0.8f;
        delay_feedback = 0.5f;
        delay_mix = 0.8f;
        UpdateDelayParam();

        masterVolume = 0.5f;

        UpdateDisplay();
    }
        
    //-----------------------------------------
    //
    //-----------------------------------------
    void Update()
    {
        pot1->Update();
        pot2->Update();
        stick_U->Update();
        stick_D->Update();
        stick_L->Update();
        stick_R->Update();
        stick_C->Update();
        
        //---- select function 
        if( stick_U->trig_on ){
            current_function --;
            if(current_function < 0) current_function = FUNCTION_MAX - 1;
            SetDirty();
        }
        if( stick_D->trig_on ){
            current_function ++;
            if(current_function >= FUNCTION_MAX) current_function = 0;
            SetDirty();
        }

        //---- effect ON/OFF 
        if( stick_C->trig_on ){
            isOn[ current_function ] = !isOn[ current_function ];
            SetDirty();
        }

        UpdateParameter();

        // update display.        
        if( GetDirty() )
        {
            UpdateDisplay();
            ClearDirty();
        }
    }

private:
    //-----------------------------------------
    //
    //-----------------------------------------
    void UpdateDisplay()
    {
        lcd->cls();
        lcd->locate(0,0);

        switch( current_function )
        {
        case FILTER_PRE:
            lcd->printf("FilterPre : %s", (isOn[FILTER_PRE]?"ON ":"off") );
            lcd->locate(0,10);
            lcd->printf("Freq.:%.0f Q:%.2f ", filter_pre_freq, filter_pre_Q );
            break;
            
        case DRIVE:
            lcd->printf("Drive     : %s", (isOn[DRIVE]?"ON ":"off") );
            lcd->locate(0,10);
            lcd->printf("Gain:%.2f Vol.:%.2f ", drive_gain, drive_volume );
            break;
            
        case FILTER_POST:
            lcd->printf("FilterPost: %s", (isOn[FILTER_POST]?"ON ":"off") );
            lcd->locate(0,10);
            lcd->printf("Freq.:%.2f Q:%.2f ", filter_post_freq, filter_post_Q );
            break;
            
        case DELAY:
            lcd->printf("Delay     : %s", (isOn[DELAY]?"ON ":"off") );
            lcd->locate(0,10);
            lcd->printf("Time:%4dmsec Mix:%.2f ", delay->GetDelayTimeInMSec(sample_rate), delay_mix);
            break;
            
        case MASTER:
            lcd->locate(0,10);
            lcd->printf("MasterVol.: %.2f", masterVolume );
            break;
        }

    }
    
    //-----------------------------------------
    //
    //-----------------------------------------
    void UpdateParameter()
    {
        switch( current_function )
        {
        case FILTER_PRE:
            {
                if( pot1->dirtyFlag ) {
                    filter_pre_freq = RateToParam( 2000.0f, 6000.0f, pot1->val );
                    SetDirty();
                }
                if( pot2->dirtyFlag ) {
                    filter_pre_Q = RateToParam( 0.3f, 2.0f, pot2->val );
                    SetDirty();
                }
                if( GetDirty() ){
                    UpdateFilterPreParam();
                }
            }
            break;
            
        case DRIVE:
            {
                if( pot1->dirtyFlag ) {
                    drive_gain = pot1->val * 20.0f;
                    SetDirty();
                }
                if( pot2->dirtyFlag ) {
                    drive_volume = pot2->val * 2.0f;
                    SetDirty();
                }
                if( GetDirty() ){
                    UpdateDriveParam();
                }
            }
            break;
            
        case FILTER_POST:
            {
                if( pot1->dirtyFlag ) {
                    filter_post_freq = RateToParam( 2000.0f, 6000.0f, pot1->val );
                    SetDirty();
                }
                if( pot2->dirtyFlag ) {
                    filter_post_Q = RateToParam( 0.3f, 2.0f, pot2->val );
                    SetDirty();
                }
                if( GetDirty() ){
                    UpdateFilterPostParam();
                }
            }
            break;
            
        case DELAY:
            {
                if( pot1->dirtyFlag ) {
                    delay_time = pot1->val;
                    SetDirty();
                }
                if( pot2->dirtyFlag ) {
                    delay_mix = pot2->val;
                    SetDirty();
                }
                if( GetDirty() ){
                    UpdateDelayParam();
                }
            }
            break;
            
        case MASTER:
            if( pot1->dirtyFlag )
            {
                masterVolume = pot1->val;
                SetDirty();
            }
            break;
        }
    }

    //-----------------------------------------
    //
    //-----------------------------------------
    void UpdateFilterPreParam()
    {
        filter_pre->isBypass = !isOn[FILTER_PRE];
        filter_pre->set_LPF( sample_rate, filter_pre_freq, filter_pre_Q );
    }

    //-----------------------------------------
    //
    //-----------------------------------------
    void UpdateFilterPostParam()
    {
        filter_post->isBypass = !isOn[FILTER_POST];
        filter_post->set_LPF( sample_rate, filter_post_freq, filter_post_Q );
    }

    //-----------------------------------------
    //
    //-----------------------------------------
    void UpdateDriveParam()
    {
        drive->isBypass = !isOn[DRIVE];
        drive->gain = drive_gain;
        drive->vol  = drive_volume;
    }
    
    //-----------------------------------------
    //
    //-----------------------------------------
    void UpdateDelayParam()
    {
        delay->isBypass = !isOn[DELAY];
        delay->delay_time = delay_time;
        delay->feedback = delay_feedback;
        delay->effect_level = delay_mix;
    }
    
    //-----------------------------------------
    //
    //-----------------------------------------
    float RateToParam( float min, float max, float rate )
    {
        return (max - min) * rate + min;
    }
};