Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- lukeocarwright
- Date:
- 2020-04-01
- Revision:
- 3:b7df72682b81
- Parent:
- 2:07cef563acdf
- Child:
- 4:9b7ea5528a5c
File content as of revision 3:b7df72682b81:
/* 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 unsigned short o[4096]; int main() { printf("RUNNING CODE \n"); startup(); wavetable(); pad.leds_on(); sinspeak(); pad.leds_off(); for (int rep=0; rep<10; rep++) { sinspeak(); } // squareWave(); } void startup() { pad.init(); //initiate Gamepad pad.leds_on(); //turn LEDS on to show starting up printf("Initialising Pad\n"); pad.play_melody(8,notes,duration,108,0); //play startup tune 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] = 65536.0f*sin(2.0f*PI*(ifl/4096.0f)); sinf[i] = sinfl[i]; o[i]= sinf[i]+32767; //generates wave table in uint // printf("o[i]= %u \n", o[i]); // Used for Debug ifl=i+1.0f; } pad.leds_off(); } 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); } 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) } }