Luke Cartwright / Mbed 2 deprecated ELEC2645_Project_el18loc_nearlythere

Dependencies:   mbed

main.cpp

Committer:
lukeocarwright
Date:
2020-05-08
Revision:
12:7a399a476cfd
Parent:
11:6ae098535da9
Child:
13:27300c533dd1

File content as of revision 12:7a399a476cfd:

/*
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: 04/05/2020
*/

/* TO DO:
add Menu functionality from front.cpp
front.h warning sort
Settings tab
sort printing waveform submenu passing
Generate sound
ADSR
MIDI
LEDS INSTALL
*/
// Includes
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Menu.h"
#include "startup.h"
#include "Front.h"

#ifdef DEBUG
# include "Debug.h"
#endif

// Objects
Gamepad pad;
N5110 lcd;
Menu menu;
startup start;
Ticker down;
Ticker period;

DigitalOut rca(PTC4);
Serial pc(USBTX, USBRX);

//Functions
//void squareWave();
//void sinspeak();
//void down_isr();
//void up_isr();


//Global Variables
// all times drastically slowed to attempt debugging in teraterm
//float g_period = 0.002;
//float g_unused= g_period*1000000.0f;
//int g_period_us=g_unused;
//volatile uint64_t g_uptime_us = g_period_us/2; // placeholder value
//isr interrupt flags
//volatile int g_upflag=1;
//volatile int g_downflag=1;

 //volatile extern uint16_t sin_wavtable[1024];
  //volatile extern double sin_d[1024];


volatile extern uint16_t sin_wavtable[1024];
volatile extern uint16_t tri_wavtable[1024];
volatile extern uint16_t pulse_wavtable[1024];

int main()
{
    printf("RUNNING CODE \n");
    start.initialise(lcd,pad); //initialises board and displays start screen
    
    #ifdef DEBUG
        printf("DEBUG COMPILE RUNNING\n");
        run_LUTs_debug();
    #endif
    
    menu.mainmenu(lcd,pad);
    //squareWave(); //generates pulse wave modulated by sin wave
    //sinspeak();
}

/*
void squareWave()
{
    printf("Generating SIN PWM \n");

    int i=0; //int based iterator
    float ifl=0; //float based itterator
    float f=50; //frequency of sin wave produced
    period.attach_us(&up_isr,g_period_us); //ticker to write 1 to rca
    down.attach_us(&down_isr,g_uptime_us); //ticker to write 0 to rca


    printf("g_period_us: %d \n", g_period_us);
    printf("sin Frequency: %f \n", f);

    while (1) { //continual loop for pulse production
        float dutyratio = wavtable[i]; //calcualtes duty ratio of pulse

        g_uptime_us= dutyratio*g_period_us; //calculates duty ratio in usecs
        if (g_uptime_us<1) {
            g_uptime_us=g_period_us/100;
            } //sets to be value for timebeing to eliminate 0 error
        //float f=440*(pad.read_pot1()+1); //removed for simplification

        if (g_upflag==0) {

            ifl = ifl + (4096*f*g_period); //once rca=1 itterate sin function
            if (ifl>4096) {
                ifl= ifl-4096;
                }
            i=ifl;

            g_upflag=1; //reset flag
            //printf("iterate i: i= %d, ifl= %f \n", i, ifl);
        }

        //printf("DR: %f \n",dutyratio);
        //printf("sleep \n");
        sleep(); //sleeps till next isr
    }
}

void down_isr() //sets rca to 0
{
    //printf("downISR \n");
    if (g_downflag==0) {//stops error trigger
        rca.write(0);
        g_downflag=1;
        g_uptime_us= g_uptime_us+g_period_us; // eliminates it calling if =0
        //printf("0 \n");//print only in while v. slow freguency
    }
}

void up_isr() //sets rca=1
{
    //printf("upISR \n");
    rca.write(1);
    down.attach_us(&down_isr,g_uptime_us); //timer to set rca=0 after elapsed
    g_upflag=0; //sets flag to iterate
    g_downflag=0; //sets flag to allow set to 0
    //printf("1 \n"); //only in while at v low frequency
}

*/
/*
void sinspeak ()
{
    float f1=440.0;
    float i=0;
    unsigned short v=0;
    int inc=0;
    pad.reset_buttons();
    f1 = pad.read_pot1();
    f1 = 440.0f+440.0f*f1;
    //printf("f1= %f \n",f1); // Used for Debug

    while (inc<3000) {
        int inti = i;
        v = o[inti];
        printf("OUTPUT: %u \n", v);
        //pad.write_u16(v);
        wait_us(230); //fs= 4k Ts=250us

        i = i + ((4096.0f*f1)/4000.0f); //i+((samples*f)*Ts)
        if (i>=4096.0f) {
            i=i-4096.0f;
        }
        else {
            wait_us(3); // used to attempt note stabilisation to match other loop
            }
        inc++;
    }
}
*/