ELEC2645 (2019/20) / Mbed 2 deprecated el18loc_final

Dependencies:   mbed

Committer:
lukeocarwright
Date:
Mon Mar 30 15:03:36 2020 +0000
Revision:
2:07cef563acdf
Parent:
1:766a293c9b07
Child:
3:b7df72682b81
Sin generated to onboard speaker and secondary sin modulation signal generated but not implemented. Square wve function also there but not implemented.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lukeocarwright 2:07cef563acdf 1 /*
eencae 0:b7f1f47bb26a 2 ELEC2645 Embedded Systems Project
eencae 0:b7f1f47bb26a 3 School of Electronic & Electrical Engineering
eencae 0:b7f1f47bb26a 4 University of Leeds
eencae 0:b7f1f47bb26a 5 2019/20
eencae 0:b7f1f47bb26a 6
lukeocarwright 1:766a293c9b07 7 Name: Luke Cartwright
lukeocarwright 1:766a293c9b07 8 Username: el18loc
lukeocarwright 1:766a293c9b07 9 Student ID Number: 201225242
lukeocarwright 2:07cef563acdf 10 Start Date: 06/02/2020
lukeocarwright 2:07cef563acdf 11 Last Edited: 12/03/2020
eencae 0:b7f1f47bb26a 12 */
eencae 0:b7f1f47bb26a 13
lukeocarwright 2:07cef563acdf 14 // Includes
eencae 0:b7f1f47bb26a 15 #include "mbed.h"
eencae 0:b7f1f47bb26a 16 #include "Gamepad.h"
eencae 0:b7f1f47bb26a 17 #include "N5110.h"
eencae 0:b7f1f47bb26a 18
lukeocarwright 2:07cef563acdf 19 // Objects
eencae 0:b7f1f47bb26a 20 Gamepad pad;
eencae 0:b7f1f47bb26a 21 N5110 lcd;
lukeocarwright 2:07cef563acdf 22 Timer t;
lukeocarwright 2:07cef563acdf 23
lukeocarwright 2:07cef563acdf 24 Serial pc(USBTX, USBRX);
lukeocarwright 2:07cef563acdf 25
lukeocarwright 2:07cef563acdf 26 //Functions
lukeocarwright 2:07cef563acdf 27 void startup();
lukeocarwright 2:07cef563acdf 28 void squareWave();
lukeocarwright 2:07cef563acdf 29 void wavetable();
lukeocarwright 2:07cef563acdf 30 void sinspeak();
lukeocarwright 2:07cef563acdf 31
lukeocarwright 2:07cef563acdf 32 //Arrays for startup
lukeocarwright 2:07cef563acdf 33 const int notes[8] = {330,0,330,294,330,247,294,392};
lukeocarwright 2:07cef563acdf 34 const int duration[8] = {4,8,4,8,8,4,8,4};
lukeocarwright 2:07cef563acdf 35 const char CART[4] = {'C','A','R','T'};
lukeocarwright 2:07cef563acdf 36 const char SYNTH[5] = {'S','Y','N','T','H'};
lukeocarwright 2:07cef563acdf 37
lukeocarwright 2:07cef563acdf 38 //Global Variables
lukeocarwright 2:07cef563acdf 39 volatile int o[4096];
eencae 0:b7f1f47bb26a 40
eencae 0:b7f1f47bb26a 41 int main()
eencae 0:b7f1f47bb26a 42 {
lukeocarwright 2:07cef563acdf 43 printf("RUNNING CODE \n");
lukeocarwright 2:07cef563acdf 44 startup();
lukeocarwright 2:07cef563acdf 45 wavetable();
lukeocarwright 2:07cef563acdf 46 pad.leds_on();
lukeocarwright 2:07cef563acdf 47 sinspeak();
lukeocarwright 2:07cef563acdf 48 pad.leds_off();
lukeocarwright 2:07cef563acdf 49 printf("Waiting for button press \n");
lukeocarwright 2:07cef563acdf 50 while (1) {
lukeocarwright 2:07cef563acdf 51 int a = pad.A_pressed();
lukeocarwright 2:07cef563acdf 52 if (pad.A_pressed()==true) {
lukeocarwright 2:07cef563acdf 53 printf("A Pressed \n");
lukeocarwright 2:07cef563acdf 54 sinspeak();
lukeocarwright 2:07cef563acdf 55 }
lukeocarwright 2:07cef563acdf 56 //wait_ms(10);
lukeocarwright 2:07cef563acdf 57 printf("A= %d \n", a);
lukeocarwright 2:07cef563acdf 58 }
eencae 0:b7f1f47bb26a 59
lukeocarwright 2:07cef563acdf 60 // squareWave();
lukeocarwright 2:07cef563acdf 61 }
lukeocarwright 2:07cef563acdf 62
lukeocarwright 2:07cef563acdf 63
lukeocarwright 2:07cef563acdf 64 void startup()
lukeocarwright 2:07cef563acdf 65 {
lukeocarwright 2:07cef563acdf 66 pad.init(); //initiate Gamepad
lukeocarwright 2:07cef563acdf 67 pad.leds_on(); //turn LEDS on to show starting up
lukeocarwright 2:07cef563acdf 68 pad.play_melody(8,notes,duration,108,0); //play startup tune
lukeocarwright 2:07cef563acdf 69 printf("Initialising Pad\n");
lukeocarwright 2:07cef563acdf 70 lcd.init(); //intitates screen
lukeocarwright 2:07cef563acdf 71 lcd.clear();
lukeocarwright 2:07cef563acdf 72 lcd.setContrast(0.4f); //contrast setting
lukeocarwright 2:07cef563acdf 73 lcd.inverseMode(); //puts screen in -ve
lukeocarwright 2:07cef563acdf 74 //positions for CART SYNTH
lukeocarwright 2:07cef563acdf 75 const int x = 10;
lukeocarwright 2:07cef563acdf 76 const int y = 2;
lukeocarwright 2:07cef563acdf 77 const int a = 40;
lukeocarwright 2:07cef563acdf 78 const int b = 4;
lukeocarwright 2:07cef563acdf 79 //Prints CART SYNTH to display
lukeocarwright 2:07cef563acdf 80 lcd.printString(CART,x,y);
lukeocarwright 2:07cef563acdf 81 lcd.printString(SYNTH,a,b);
lukeocarwright 2:07cef563acdf 82 lcd.refresh();
lukeocarwright 2:07cef563acdf 83 wait_ms(1800); //timer to allow theme to play out
lukeocarwright 2:07cef563acdf 84 lcd.clear();
lukeocarwright 2:07cef563acdf 85 lcd.refresh(); //sets clear screen
lukeocarwright 2:07cef563acdf 86 pad.leds_off(); //turns of leds to show ready
eencae 0:b7f1f47bb26a 87 }
eencae 0:b7f1f47bb26a 88
lukeocarwright 2:07cef563acdf 89
lukeocarwright 2:07cef563acdf 90
lukeocarwright 2:07cef563acdf 91
lukeocarwright 2:07cef563acdf 92 void wavetable()
lukeocarwright 2:07cef563acdf 93 {
lukeocarwright 2:07cef563acdf 94 int samples= 4096;
lukeocarwright 2:07cef563acdf 95 float sinfl[samples];
lukeocarwright 2:07cef563acdf 96 int sinf[samples];
lukeocarwright 2:07cef563acdf 97
lukeocarwright 2:07cef563acdf 98 pad.leds_on(); //shows computating
lukeocarwright 2:07cef563acdf 99 printf("Generating Wavetable \n");
lukeocarwright 2:07cef563acdf 100 wait_ms(500); //shows reaches stage
lukeocarwright 2:07cef563acdf 101
lukeocarwright 2:07cef563acdf 102 float ifl=0.0; //creates fl type incramenter
lukeocarwright 2:07cef563acdf 103
lukeocarwright 2:07cef563acdf 104 for (int i=0; i<4096; i++) {
lukeocarwright 2:07cef563acdf 105 sinfl[i] = 6553500.0f*sin(2.0f*PI*(ifl/4096.0f));
lukeocarwright 2:07cef563acdf 106 sinf[i] = sinfl[i]/100.0f;
lukeocarwright 2:07cef563acdf 107 o[i]= sinf[i]+32767; //generates wave table
lukeocarwright 2:07cef563acdf 108 //printf("o[i]= %d",o[i], "\n"); // Used for Debug
lukeocarwright 2:07cef563acdf 109 ifl=i+1.0f;
lukeocarwright 2:07cef563acdf 110 }
lukeocarwright 2:07cef563acdf 111 pad.leds_off();
lukeocarwright 2:07cef563acdf 112 }
lukeocarwright 2:07cef563acdf 113 void sinspeak ()
lukeocarwright 2:07cef563acdf 114 {
lukeocarwright 2:07cef563acdf 115 float f=440.0;
lukeocarwright 2:07cef563acdf 116 float f1=440.0;
lukeocarwright 2:07cef563acdf 117 float f2=1.0;
lukeocarwright 2:07cef563acdf 118 float i=0;
lukeocarwright 2:07cef563acdf 119 //int i1=0;
lukeocarwright 2:07cef563acdf 120 float i2=0;
lukeocarwright 2:07cef563acdf 121 int v=0;
lukeocarwright 2:07cef563acdf 122 int inc=0;
lukeocarwright 2:07cef563acdf 123 pad.reset_buttons();
lukeocarwright 2:07cef563acdf 124 f1 = pad.read_pot1();
lukeocarwright 2:07cef563acdf 125 //f2 = pad.read_pot2();
lukeocarwright 2:07cef563acdf 126 f1 = 440.0f+440.0f*f;
lukeocarwright 2:07cef563acdf 127 f2 = 5.0f*f2;
lukeocarwright 2:07cef563acdf 128 printf("f1= %f \n",f1); // Used for Debug
lukeocarwright 2:07cef563acdf 129 printf("f2= %f \n",f2);
lukeocarwright 2:07cef563acdf 130
lukeocarwright 2:07cef563acdf 131 while (inc<3000) {
lukeocarwright 2:07cef563acdf 132 int inti = i;
lukeocarwright 2:07cef563acdf 133 v = o[inti];
lukeocarwright 2:07cef563acdf 134 printf("OUTPUT: %d \n", v);
lukeocarwright 2:07cef563acdf 135 //pad.write_u16(v);
lukeocarwright 2:07cef563acdf 136 wait_us(190); //200us but 150 for delay (uncalculated)
lukeocarwright 2:07cef563acdf 137
lukeocarwright 2:07cef563acdf 138 i2 = i2 + ((4096*f2)/5000); //i2+((samples*f)*Ts)
lukeocarwright 2:07cef563acdf 139 if (i2>=4096) {
lukeocarwright 2:07cef563acdf 140 i2=i2-4096;
lukeocarwright 2:07cef563acdf 141 }
lukeocarwright 2:07cef563acdf 142 int inti2 = i2;
lukeocarwright 2:07cef563acdf 143 int x = (o[inti2]/6554)-5; //(amp/6554)-offset =sin10f
lukeocarwright 2:07cef563acdf 144 float xfloat = x;
lukeocarwright 2:07cef563acdf 145 printf("x= %d \n", x); // Used for Debug
lukeocarwright 2:07cef563acdf 146 f = f1 + xfloat;
lukeocarwright 2:07cef563acdf 147
lukeocarwright 2:07cef563acdf 148 i = i + ((4096.0f*f)/5000.0f); //i+((samples*f)*Ts)
lukeocarwright 2:07cef563acdf 149 if (i>=4096.0f) {
lukeocarwright 2:07cef563acdf 150 i=i-4096.0f;
lukeocarwright 2:07cef563acdf 151 }
lukeocarwright 2:07cef563acdf 152
lukeocarwright 2:07cef563acdf 153 inc++;
lukeocarwright 2:07cef563acdf 154 }
lukeocarwright 2:07cef563acdf 155 }
lukeocarwright 2:07cef563acdf 156
lukeocarwright 2:07cef563acdf 157 void squareWave()
lukeocarwright 2:07cef563acdf 158 {
lukeocarwright 2:07cef563acdf 159 // defines local variables
lukeocarwright 2:07cef563acdf 160 float v = 0.0;
lukeocarwright 2:07cef563acdf 161 float f = 440.0;
lukeocarwright 2:07cef563acdf 162 float p1 = 0;
lukeocarwright 2:07cef563acdf 163 // continual loop for square production
lukeocarwright 2:07cef563acdf 164 while (1) {
lukeocarwright 2:07cef563acdf 165 pad.write_dac(v); //wite out value (1/0)
lukeocarwright 2:07cef563acdf 166 //Statements for switch of out
lukeocarwright 2:07cef563acdf 167 if (v == 0.0f) {
lukeocarwright 2:07cef563acdf 168 v=0.1;
lukeocarwright 2:07cef563acdf 169 } else {
lukeocarwright 2:07cef563acdf 170 v=0.0;
lukeocarwright 2:07cef563acdf 171 }
lukeocarwright 2:07cef563acdf 172 p1 = pad.read_pot1(); //read pot 1 value
lukeocarwright 2:07cef563acdf 173 f = 440 + 440*p1; //convert to freq (A4->A5)
lukeocarwright 2:07cef563acdf 174 float T=1/f; //calc Period of wave
lukeocarwright 2:07cef563acdf 175 wait(T); //timer for switch value (T)
lukeocarwright 2:07cef563acdf 176 }
lukeocarwright 2:07cef563acdf 177
lukeocarwright 2:07cef563acdf 178 }