ELEC2645 (2019/20) / Mbed 2 deprecated el18loc_final

Dependencies:   mbed

main.cpp

Committer:
lukeocarwright
Date:
2020-03-30
Revision:
2:07cef563acdf
Parent:
1:766a293c9b07
Child:
3:b7df72682b81

File content as of revision 2:07cef563acdf:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
2019/20

Name: Luke Cartwright
Username: el18loc
Student ID Number: 201225242
Start Date: 06/02/2020
Last Edited: 12/03/2020
*/

// Includes
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"

// Objects
Gamepad pad;
N5110 lcd;
Timer t;

Serial pc(USBTX, USBRX);

//Functions
void startup();
void squareWave();
void wavetable();
void sinspeak();

//Arrays for startup
const int notes[8] = {330,0,330,294,330,247,294,392};
const int duration[8] = {4,8,4,8,8,4,8,4};
const char CART[4] = {'C','A','R','T'};
const char SYNTH[5] = {'S','Y','N','T','H'};

//Global Variables
volatile int o[4096];

int main()
{
    printf("RUNNING CODE \n");
    startup();
    wavetable();
    pad.leds_on();
    sinspeak();
    pad.leds_off();
    printf("Waiting for button press \n");
    while (1) {
         int a = pad.A_pressed();
        if (pad.A_pressed()==true) {
            printf("A Pressed \n");
            sinspeak();
        }
        //wait_ms(10);
        printf("A= %d \n", a);
    }
    
//  squareWave();
}


void startup()
{
    pad.init(); //initiate Gamepad
    pad.leds_on(); //turn LEDS on to show starting up
    pad.play_melody(8,notes,duration,108,0); //play startup tune
    printf("Initialising Pad\n");
    lcd.init(); //intitates screen
    lcd.clear();
    lcd.setContrast(0.4f); //contrast setting
    lcd.inverseMode(); //puts screen in -ve
    //positions for CART SYNTH
    const int x = 10;
    const int y = 2;
    const int a = 40;
    const int b = 4;
    //Prints CART SYNTH to display
    lcd.printString(CART,x,y);
    lcd.printString(SYNTH,a,b);
    lcd.refresh();
    wait_ms(1800); //timer to allow theme to play out
    lcd.clear();
    lcd.refresh(); //sets clear screen
    pad.leds_off(); //turns of leds to show ready
}




void wavetable()
{
    int samples= 4096;
    float sinfl[samples];
    int sinf[samples];

    pad.leds_on(); //shows computating
    printf("Generating Wavetable \n");
    wait_ms(500); //shows reaches stage

    float ifl=0.0; //creates fl type incramenter

    for (int i=0; i<4096; i++) {
        sinfl[i] = 6553500.0f*sin(2.0f*PI*(ifl/4096.0f));
        sinf[i] = sinfl[i]/100.0f;
        o[i]= sinf[i]+32767; //generates wave table
        //printf("o[i]= %d",o[i], "\n"); // Used for Debug
        ifl=i+1.0f;
    }
    pad.leds_off();
}
void sinspeak ()
{
    float f=440.0;
    float f1=440.0;
    float f2=1.0;
    float i=0;
    //int i1=0;
    float i2=0;
    int v=0;
    int inc=0;
    pad.reset_buttons();
    f1 = pad.read_pot1();
    //f2 = pad.read_pot2();
    f1 = 440.0f+440.0f*f;
    f2 = 5.0f*f2;
    printf("f1= %f \n",f1); // Used for Debug
    printf("f2= %f \n",f2);

    while (inc<3000) {
        int inti = i;
        v = o[inti];
        printf("OUTPUT: %d \n", v);
        //pad.write_u16(v);
        wait_us(190); //200us but 150 for delay (uncalculated)
        
        i2 = i2 + ((4096*f2)/5000); //i2+((samples*f)*Ts)
        if (i2>=4096) {
            i2=i2-4096;
        }
        int inti2 = i2;
        int x = (o[inti2]/6554)-5; //(amp/6554)-offset =sin10f
        float xfloat = x;
        printf("x= %d \n", x); // Used for Debug
        f = f1 + xfloat;
        
        i = i + ((4096.0f*f)/5000.0f); //i+((samples*f)*Ts)
        if (i>=4096.0f) {
            i=i-4096.0f;
            }
            
        inc++;
    }
}

void squareWave()
{
    // defines local variables
    float v = 0.0;
    float f = 440.0;
    float p1 = 0;
    // continual loop for square production
    while (1) {
        pad.write_dac(v); //wite out value (1/0)
        //Statements for switch of out
        if (v == 0.0f) {
            v=0.1;
        } else {
            v=0.0;
        }
        p1 = pad.read_pot1(); //read pot 1 value
        f = 440 + 440*p1; //convert to freq (A4->A5)
        float T=1/f; //calc Period of wave
        wait(T); //timer for switch value (T)
    }

}