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@3:b7df72682b81, 2020-04-01 (annotated)
- Committer:
- lukeocarwright
- Date:
- Wed Apr 01 16:25:32 2020 +0000
- Revision:
- 3:b7df72682b81
- Parent:
- 2:07cef563acdf
- Child:
- 4:9b7ea5528a5c
Produces single sin with variable frequency in uint format. Poor quality sound unsolved. Physical filter built but untested.
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:b7df72682b81 | 39 | volatile unsigned short 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 | 3:b7df72682b81 | 49 | for (int rep=0; rep<10; rep++) { |
lukeocarwright | 3:b7df72682b81 | 50 | sinspeak(); |
lukeocarwright | 2:07cef563acdf | 51 | } |
lukeocarwright | 2:07cef563acdf | 52 | // squareWave(); |
lukeocarwright | 2:07cef563acdf | 53 | } |
lukeocarwright | 2:07cef563acdf | 54 | |
lukeocarwright | 2:07cef563acdf | 55 | |
lukeocarwright | 2:07cef563acdf | 56 | void startup() |
lukeocarwright | 2:07cef563acdf | 57 | { |
lukeocarwright | 2:07cef563acdf | 58 | pad.init(); //initiate Gamepad |
lukeocarwright | 2:07cef563acdf | 59 | pad.leds_on(); //turn LEDS on to show starting up |
lukeocarwright | 3:b7df72682b81 | 60 | printf("Initialising Pad\n"); |
lukeocarwright | 2:07cef563acdf | 61 | pad.play_melody(8,notes,duration,108,0); //play startup tune |
lukeocarwright | 2:07cef563acdf | 62 | lcd.init(); //intitates screen |
lukeocarwright | 2:07cef563acdf | 63 | lcd.clear(); |
lukeocarwright | 2:07cef563acdf | 64 | lcd.setContrast(0.4f); //contrast setting |
lukeocarwright | 2:07cef563acdf | 65 | lcd.inverseMode(); //puts screen in -ve |
lukeocarwright | 2:07cef563acdf | 66 | //positions for CART SYNTH |
lukeocarwright | 2:07cef563acdf | 67 | const int x = 10; |
lukeocarwright | 2:07cef563acdf | 68 | const int y = 2; |
lukeocarwright | 2:07cef563acdf | 69 | const int a = 40; |
lukeocarwright | 2:07cef563acdf | 70 | const int b = 4; |
lukeocarwright | 2:07cef563acdf | 71 | //Prints CART SYNTH to display |
lukeocarwright | 2:07cef563acdf | 72 | lcd.printString(CART,x,y); |
lukeocarwright | 2:07cef563acdf | 73 | lcd.printString(SYNTH,a,b); |
lukeocarwright | 2:07cef563acdf | 74 | lcd.refresh(); |
lukeocarwright | 2:07cef563acdf | 75 | wait_ms(1800); //timer to allow theme to play out |
lukeocarwright | 2:07cef563acdf | 76 | lcd.clear(); |
lukeocarwright | 2:07cef563acdf | 77 | lcd.refresh(); //sets clear screen |
lukeocarwright | 2:07cef563acdf | 78 | pad.leds_off(); //turns of leds to show ready |
eencae | 0:b7f1f47bb26a | 79 | } |
eencae | 0:b7f1f47bb26a | 80 | |
lukeocarwright | 2:07cef563acdf | 81 | |
lukeocarwright | 2:07cef563acdf | 82 | void wavetable() |
lukeocarwright | 2:07cef563acdf | 83 | { |
lukeocarwright | 2:07cef563acdf | 84 | int samples= 4096; |
lukeocarwright | 2:07cef563acdf | 85 | float sinfl[samples]; |
lukeocarwright | 2:07cef563acdf | 86 | int sinf[samples]; |
lukeocarwright | 2:07cef563acdf | 87 | |
lukeocarwright | 2:07cef563acdf | 88 | pad.leds_on(); //shows computating |
lukeocarwright | 2:07cef563acdf | 89 | printf("Generating Wavetable \n"); |
lukeocarwright | 2:07cef563acdf | 90 | wait_ms(500); //shows reaches stage |
lukeocarwright | 2:07cef563acdf | 91 | |
lukeocarwright | 2:07cef563acdf | 92 | float ifl=0.0; //creates fl type incramenter |
lukeocarwright | 2:07cef563acdf | 93 | |
lukeocarwright | 2:07cef563acdf | 94 | for (int i=0; i<4096; i++) { |
lukeocarwright | 3:b7df72682b81 | 95 | sinfl[i] = 65536.0f*sin(2.0f*PI*(ifl/4096.0f)); |
lukeocarwright | 3:b7df72682b81 | 96 | sinf[i] = sinfl[i]; |
lukeocarwright | 3:b7df72682b81 | 97 | o[i]= sinf[i]+32767; //generates wave table in uint |
lukeocarwright | 3:b7df72682b81 | 98 | // printf("o[i]= %u \n", o[i]); // Used for Debug |
lukeocarwright | 2:07cef563acdf | 99 | ifl=i+1.0f; |
lukeocarwright | 2:07cef563acdf | 100 | } |
lukeocarwright | 2:07cef563acdf | 101 | pad.leds_off(); |
lukeocarwright | 2:07cef563acdf | 102 | } |
lukeocarwright | 3:b7df72682b81 | 103 | |
lukeocarwright | 3:b7df72682b81 | 104 | |
lukeocarwright | 2:07cef563acdf | 105 | void sinspeak () |
lukeocarwright | 2:07cef563acdf | 106 | { |
lukeocarwright | 2:07cef563acdf | 107 | float f1=440.0; |
lukeocarwright | 2:07cef563acdf | 108 | float i=0; |
lukeocarwright | 3:b7df72682b81 | 109 | unsigned short v=0; |
lukeocarwright | 2:07cef563acdf | 110 | int inc=0; |
lukeocarwright | 2:07cef563acdf | 111 | pad.reset_buttons(); |
lukeocarwright | 2:07cef563acdf | 112 | f1 = pad.read_pot1(); |
lukeocarwright | 3:b7df72682b81 | 113 | f1 = 440.0f+440.0f*f1; |
lukeocarwright | 3:b7df72682b81 | 114 | //printf("f1= %f \n",f1); // Used for Debug |
lukeocarwright | 2:07cef563acdf | 115 | |
lukeocarwright | 2:07cef563acdf | 116 | while (inc<3000) { |
lukeocarwright | 2:07cef563acdf | 117 | int inti = i; |
lukeocarwright | 2:07cef563acdf | 118 | v = o[inti]; |
lukeocarwright | 3:b7df72682b81 | 119 | //printf("OUTPUT: %u \n", v); |
lukeocarwright | 3:b7df72682b81 | 120 | pad.write_u16(v); |
lukeocarwright | 3:b7df72682b81 | 121 | wait_us(230); //fs= 4k Ts=250us |
lukeocarwright | 3:b7df72682b81 | 122 | |
lukeocarwright | 3:b7df72682b81 | 123 | i = i + ((4096.0f*f1)/4000.0f); //i+((samples*f)*Ts) |
lukeocarwright | 2:07cef563acdf | 124 | if (i>=4096.0f) { |
lukeocarwright | 2:07cef563acdf | 125 | i=i-4096.0f; |
lukeocarwright | 3:b7df72682b81 | 126 | } |
lukeocarwright | 3:b7df72682b81 | 127 | else { |
lukeocarwright | 3:b7df72682b81 | 128 | wait_us(3); |
lukeocarwright | 2:07cef563acdf | 129 | } |
lukeocarwright | 2:07cef563acdf | 130 | inc++; |
lukeocarwright | 2:07cef563acdf | 131 | } |
lukeocarwright | 2:07cef563acdf | 132 | } |
lukeocarwright | 2:07cef563acdf | 133 | |
lukeocarwright | 2:07cef563acdf | 134 | void squareWave() |
lukeocarwright | 2:07cef563acdf | 135 | { |
lukeocarwright | 2:07cef563acdf | 136 | // defines local variables |
lukeocarwright | 2:07cef563acdf | 137 | float v = 0.0; |
lukeocarwright | 2:07cef563acdf | 138 | float f = 440.0; |
lukeocarwright | 2:07cef563acdf | 139 | float p1 = 0; |
lukeocarwright | 2:07cef563acdf | 140 | // continual loop for square production |
lukeocarwright | 2:07cef563acdf | 141 | while (1) { |
lukeocarwright | 2:07cef563acdf | 142 | pad.write_dac(v); //wite out value (1/0) |
lukeocarwright | 2:07cef563acdf | 143 | //Statements for switch of out |
lukeocarwright | 2:07cef563acdf | 144 | if (v == 0.0f) { |
lukeocarwright | 2:07cef563acdf | 145 | v=0.1; |
lukeocarwright | 2:07cef563acdf | 146 | } else { |
lukeocarwright | 2:07cef563acdf | 147 | v=0.0; |
lukeocarwright | 2:07cef563acdf | 148 | } |
lukeocarwright | 2:07cef563acdf | 149 | p1 = pad.read_pot1(); //read pot 1 value |
lukeocarwright | 2:07cef563acdf | 150 | f = 440 + 440*p1; //convert to freq (A4->A5) |
lukeocarwright | 2:07cef563acdf | 151 | float T=1/f; //calc Period of wave |
lukeocarwright | 2:07cef563acdf | 152 | wait(T); //timer for switch value (T) |
lukeocarwright | 2:07cef563acdf | 153 | } |
lukeocarwright | 2:07cef563acdf | 154 | |
lukeocarwright | 2:07cef563acdf | 155 | } |