ELEC2645 (2019/20) / Mbed 2 deprecated el18loc_final

Dependencies:   mbed

Front/Front.cpp

Committer:
lukeocarwright
Date:
2020-05-21
Revision:
18:204cd747b54a
Parent:
15:1c67f064278e
Child:
19:08862f49cd9e

File content as of revision 18:204cd747b54a:

#include "mbed.h"
#include "Front.h"

//Objects-----------------------------------------------------------------------
Sound sound;
Envelope envelope;

//Sprites-----------------------------------------------------------------------
const int fader[35][5] =   {
    { 1,1,1,1,1 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 1,1,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 1,1,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 1,1,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 1,1,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 1,1,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 1,1,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 0,0,1,0,0 },
    { 1,1,1,1,1 },
};

const int sin_wav[6][28] =   {//sub 1 sin sprite
    { 0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0 },
    { 0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0 },
    { 1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0 },
    { 0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1 },
    { 0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0 },
};
const int tri_wav[6][28] =   {//sub 2 tri sprite
    { 0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0 },
    { 0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0 },
    { 0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0 },
    { 1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0 },
    { 0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1 },
    { 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0 },
};
const int square_wav[6][28] =   {//sub 3 sqr sprite
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
    { 1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1 },
    { 0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0 },
    { 0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0 },
    { 0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,0,0 },
    { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
};
//Gloabal Variables
volatile int g_isr_flag;

//constructor/destructor
Front::Front()
{
}
Front::~Front()
{
}
//PUBLIC------------------------------------------------------------------------

void Front::frontrun(N5110 &lcd, Gamepad &pad, int submenu)
{
    //printf("G_ISR_FLAG= %d\n",g_isr_flag);
    frontsetup(lcd, pad, submenu, true);
    menuflag=1;
    used=1;
    a=17;
    d=17;
    s=17;
    r=17;
    out=sound.sound_main(true,submenu,440);
    out=envelope.env_in(a,d,s,r,out,true);
    noteon=false;
    pad.reset_buttons();
    //printf("starting while loop");
    #ifdef CSV
    itterator=0;
    #endif

    while (menuflag==1) {
        if (noteon==true) {
            if (g_isr_flag == 1) {
                pad.write_u16(out);

                envelope_in=(sound.sound_main(false,submenu,440)-32767); //value around 0
#ifdef SLOW_TIME
                printf("SPEAKER VALUE = %u\n",out);
                printf("\nENVELOPE IN = %d\n",envelope_in);
#endif
                //out = envelope_in+32767; //skips envelope
                out=(envelope.env_in(a,d,s,r,envelope_in,false)+32767);//value around 32767
                g_isr_flag=0;
#ifdef CSV
                printf("%d,%d\n",itterator,out);
                itterator++;
#endif
            }
        }
        dir=pad.get_direction();
        if (dir!=dir_1) {
            printf("dir =%d\n",dir); //debug
            if(dir==3) {
                used++;
                if (used>4) {
                    used=4;
                }
            }
            if (dir==7) {
                used--;
                if(used<1) {
                    used=1;
                }
            }
            if (dir==1) {
                if (used==1) {
                    a=incrament_adsr(used,a,1);
                }
                if (used==2) {
                    d=incrament_adsr(used,d,1);
                }
                if (used==3) {
                    s=incrament_adsr(used,s,1);
                }
                if (used==4) {
                    r=incrament_adsr(used,r,1);
                }
            }

            if (dir==5) {
                if (used==1) {
                    a=incrament_adsr(used,a,-1);
                }
                if (used==2) {
                    d=incrament_adsr(used,d,-1);
                }
                if (used==3) {
                    s=incrament_adsr(used,s,-1);
                }
                if (used==4) {
                    r=incrament_adsr(used,r,-1);
                }
            }
            frontsetup(lcd,pad,submenu,false);
            printsliders(lcd,a,d,s,r,used);
            wait_ms(50);
        } //END if DIR
        if ( pad.A_pressed())  { //NOTE ON
            printf("NOTE_ON\n");
            //wait_ms(200); //debounce
            noteon=true;
            out=envelope.env_in(a,d,s,r,out,true);
        }
        if ( pad.B_pressed())  { //NOTE OFF
            printf("NOTE_OFF\n");
            //wait_ms(200); //debounce
            noteon=false;
            out=envelope.env_in(a,d,s,r,out,true);
        }

        if (pad.start_pressed())  { //go back menu
            // printf("start pressed\n");
            printf("Waveforms Sub selected\n"); //debug
            noteon=false;
            //wait_ms(200); //debounce
            menuflag=0; //sets flag
        }
        dir=dir_1;
        sleep();
    }//end while()
}

//PRIVATE-----------------------------------------------------------------------

void Front::frontsetup(N5110 &lcd, Gamepad &pad, int submenu, bool initial)
{
    if (initial==true) {
        printsliders(lcd,17,17,17,17,1);
        printf("front setup complete\n\n");
    }
    lcd.clear();
    lcd.printString("A D S R",3,5);
    printfader(lcd,3,3);
    printfader(lcd,15,3);
    printfader(lcd,27,3);
    printfader(lcd,39,3);
    lcd.drawRect(50,3,30,8,FILL_TRANSPARENT);
    printwav(lcd,submenu);
    lcd.refresh();
}

void Front::printsliders(N5110 &lcd, int a, int d, int s, int r, int used)
{
    if(used==0) {
        printslider(lcd,3,a,false);
        printslider(lcd,15,d,false);
        printslider(lcd,27,s,false);
        printslider(lcd,39,r,false);
    }
    if(used==1) {
        printslider(lcd,3,a,true);
        printslider(lcd,15,d,false);
        printslider(lcd,27,s,false);
        printslider(lcd,39,r,false);
    }
    if(used==2) {
        printslider(lcd,3,a,false);
        printslider(lcd,15,d,true);
        printslider(lcd,27,s,false);
        printslider(lcd,39,r,false);
    }
    if(used==3) {
        printslider(lcd,3,a,false);
        printslider(lcd,15,d,false);
        printslider(lcd,27,s,true);
        printslider(lcd,39,r,false);
    }
    if(used==4) {
        printslider(lcd,3,a,false);
        printslider(lcd,15,d,false);
        printslider(lcd,27,s,false);
        printslider(lcd,39,r,true);
    }
    lcd.refresh();
}

void Front::printslider(N5110 &lcd, int x_val, int y_val, bool use)
{
    if (use==true) {
        lcd.drawRect(x_val,37-y_val,5,2,FILL_BLACK);
    }
    if (use==false) {
        lcd.drawRect(x_val,37-y_val,5,1,FILL_BLACK);
    }
}


void Front::printfader(N5110 &lcd, int x, int y)
{
    // x origin, y origin, rows, cols, sprite
    lcd.drawSprite(x,y,35,5,(int *)fader);
}

void Front::printwav(N5110 &lcd, int submenu)
{
    if (submenu==1) {
        // x origin, y origin, rows, cols, sprite
        lcd.drawSprite(51,4,6,28,(int *)sin_wav);
    }
    if (submenu==2) {
        // x origin, y origin, rows, cols, sprite
        lcd.drawSprite(51,4,6,28,(int *)tri_wav);
    }
    if (submenu==3) {
        // x origin, y origin, rows, cols, sprite
        lcd.drawSprite(51,4,6,28,(int *)square_wav);
    }
}

int Front::incrament_adsr(int used, int adsr, int ud)
{
    if (ud==1) {
        adsr++;
        if (adsr>35) {
            adsr--;
        }
    }
    if (ud==-1) {
        adsr--;
        if (adsr<1) {
            adsr++;
        }
    }
    return(adsr);
}