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@5:e785b9cd58c9, 2020-05-03 (annotated)
- Committer:
- lukeocarwright
- Date:
- Sun May 03 13:01:59 2020 +0000
- Revision:
- 5:e785b9cd58c9
- Parent:
- 4:9b7ea5528a5c
- Child:
- 6:3a4e9ce12911
Strip back of functions due to hardwaare change (created own physical gamepad3). Inclusion of main menu (yet to be user activated)
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 | 4:9b7ea5528a5c | 11 | Last Edited: 07/04/2020 |
eencae | 0:b7f1f47bb26a | 12 | */ |
eencae | 0:b7f1f47bb26a | 13 | |
lukeocarwright | 5:e785b9cd58c9 | 14 | /* TO DO: |
lukeocarwright | 5:e785b9cd58c9 | 15 | string continues at line ends |
lukeocarwright | 5:e785b9cd58c9 | 16 | LEDS INSTALL |
lukeocarwright | 5:e785b9cd58c9 | 17 | STICK INSTALL |
lukeocarwright | 5:e785b9cd58c9 | 18 | |
lukeocarwright | 5:e785b9cd58c9 | 19 | */ |
lukeocarwright | 2:07cef563acdf | 20 | // Includes |
eencae | 0:b7f1f47bb26a | 21 | #include "mbed.h" |
eencae | 0:b7f1f47bb26a | 22 | #include "Gamepad.h" |
eencae | 0:b7f1f47bb26a | 23 | #include "N5110.h" |
eencae | 0:b7f1f47bb26a | 24 | |
lukeocarwright | 2:07cef563acdf | 25 | // Objects |
eencae | 0:b7f1f47bb26a | 26 | Gamepad pad; |
eencae | 0:b7f1f47bb26a | 27 | N5110 lcd; |
lukeocarwright | 4:9b7ea5528a5c | 28 | Ticker down; |
lukeocarwright | 4:9b7ea5528a5c | 29 | Ticker period; |
lukeocarwright | 4:9b7ea5528a5c | 30 | |
lukeocarwright | 4:9b7ea5528a5c | 31 | DigitalOut rca(PTC4); |
lukeocarwright | 2:07cef563acdf | 32 | |
lukeocarwright | 2:07cef563acdf | 33 | Serial pc(USBTX, USBRX); |
lukeocarwright | 2:07cef563acdf | 34 | |
lukeocarwright | 2:07cef563acdf | 35 | //Functions |
lukeocarwright | 2:07cef563acdf | 36 | void startup(); |
lukeocarwright | 5:e785b9cd58c9 | 37 | //void squareWave(); |
lukeocarwright | 2:07cef563acdf | 38 | void wavetable(); |
lukeocarwright | 5:e785b9cd58c9 | 39 | int mainmenu(); |
lukeocarwright | 5:e785b9cd58c9 | 40 | void printmainmenu(); |
lukeocarwright | 4:9b7ea5528a5c | 41 | //void sinspeak(); |
lukeocarwright | 5:e785b9cd58c9 | 42 | //void down_isr(); |
lukeocarwright | 5:e785b9cd58c9 | 43 | //void up_isr(); |
lukeocarwright | 2:07cef563acdf | 44 | |
lukeocarwright | 2:07cef563acdf | 45 | //Arrays for startup |
lukeocarwright | 5:e785b9cd58c9 | 46 | char CARTSYNTH[9] = {'C','A','R','T','S','Y','N','T','H'}; |
lukeocarwright | 5:e785b9cd58c9 | 47 | char MAIN_MENU[9] = {'M','A','I','N',' ','M','E','N','U'}; |
lukeocarwright | 5:e785b9cd58c9 | 48 | char Waveforms[9] = {'W','a','v','e','f','o','r','m','s'}; |
lukeocarwright | 5:e785b9cd58c9 | 49 | char Credits[7] = {'C','r','e','d','i','t','s'}; |
lukeocarwright | 2:07cef563acdf | 50 | |
lukeocarwright | 2:07cef563acdf | 51 | //Global Variables |
lukeocarwright | 4:9b7ea5528a5c | 52 | volatile double wavtable[4096]; //Wavetable array |
lukeocarwright | 4:9b7ea5528a5c | 53 | // all times drastically slowed to attempt debugging in teraterm |
lukeocarwright | 4:9b7ea5528a5c | 54 | float g_period = 0.002; |
lukeocarwright | 4:9b7ea5528a5c | 55 | float g_unused= g_period*1000000.0f; |
lukeocarwright | 4:9b7ea5528a5c | 56 | int g_period_us=g_unused; |
lukeocarwright | 4:9b7ea5528a5c | 57 | volatile uint64_t g_uptime_us = g_period_us/2; // placeholder value |
lukeocarwright | 4:9b7ea5528a5c | 58 | //isr interrupt flags |
lukeocarwright | 4:9b7ea5528a5c | 59 | volatile int g_upflag=1; |
lukeocarwright | 4:9b7ea5528a5c | 60 | volatile int g_downflag=1; |
eencae | 0:b7f1f47bb26a | 61 | |
eencae | 0:b7f1f47bb26a | 62 | int main() |
eencae | 0:b7f1f47bb26a | 63 | { |
lukeocarwright | 2:07cef563acdf | 64 | printf("RUNNING CODE \n"); |
lukeocarwright | 4:9b7ea5528a5c | 65 | startup(); //initialises board and displays start screen |
lukeocarwright | 4:9b7ea5528a5c | 66 | wavetable(); //generates wavtable array |
lukeocarwright | 5:e785b9cd58c9 | 67 | mainmenu(); //generates main menu |
lukeocarwright | 4:9b7ea5528a5c | 68 | //pad.leds_on(); |
lukeocarwright | 5:e785b9cd58c9 | 69 | //squareWave(); //generates pulse wave modulated by sin wave |
lukeocarwright | 4:9b7ea5528a5c | 70 | //pad.leds_off(); |
lukeocarwright | 4:9b7ea5528a5c | 71 | //sinspeak(); |
lukeocarwright | 2:07cef563acdf | 72 | } |
lukeocarwright | 2:07cef563acdf | 73 | |
lukeocarwright | 2:07cef563acdf | 74 | |
lukeocarwright | 2:07cef563acdf | 75 | void startup() |
lukeocarwright | 2:07cef563acdf | 76 | { |
lukeocarwright | 2:07cef563acdf | 77 | pad.init(); //initiate Gamepad |
lukeocarwright | 2:07cef563acdf | 78 | pad.leds_on(); //turn LEDS on to show starting up |
lukeocarwright | 3:b7df72682b81 | 79 | printf("Initialising Pad\n"); |
lukeocarwright | 2:07cef563acdf | 80 | lcd.init(); //intitates screen |
lukeocarwright | 2:07cef563acdf | 81 | lcd.clear(); |
lukeocarwright | 4:9b7ea5528a5c | 82 | lcd.setContrast(0.5); //contrast setting |
lukeocarwright | 2:07cef563acdf | 83 | lcd.inverseMode(); //puts screen in -ve |
lukeocarwright | 5:e785b9cd58c9 | 84 | //position for CARTSYNTH |
lukeocarwright | 5:e785b9cd58c9 | 85 | const int x = 6; |
lukeocarwright | 2:07cef563acdf | 86 | const int y = 2; |
lukeocarwright | 2:07cef563acdf | 87 | //Prints CART SYNTH to display |
lukeocarwright | 5:e785b9cd58c9 | 88 | lcd.printString(CARTSYNTH,x,y); |
lukeocarwright | 2:07cef563acdf | 89 | lcd.refresh(); |
lukeocarwright | 2:07cef563acdf | 90 | wait_ms(1800); //timer to allow theme to play out |
lukeocarwright | 2:07cef563acdf | 91 | lcd.clear(); |
lukeocarwright | 2:07cef563acdf | 92 | lcd.refresh(); //sets clear screen |
lukeocarwright | 2:07cef563acdf | 93 | pad.leds_off(); //turns of leds to show ready |
eencae | 0:b7f1f47bb26a | 94 | } |
eencae | 0:b7f1f47bb26a | 95 | |
lukeocarwright | 2:07cef563acdf | 96 | |
lukeocarwright | 2:07cef563acdf | 97 | void wavetable() |
lukeocarwright | 2:07cef563acdf | 98 | { |
lukeocarwright | 4:9b7ea5528a5c | 99 | double sin_d[4096]; |
lukeocarwright | 2:07cef563acdf | 100 | |
lukeocarwright | 4:9b7ea5528a5c | 101 | pad.leds_on(); //shows computating (debug) |
lukeocarwright | 2:07cef563acdf | 102 | printf("Generating Wavetable \n"); |
lukeocarwright | 2:07cef563acdf | 103 | |
lukeocarwright | 2:07cef563acdf | 104 | for (int i=0; i<4096; i++) { |
lukeocarwright | 4:9b7ea5528a5c | 105 | sin_d[i] = 0.5f*sin(2.0f*PI*(i/4096.0f)); |
lukeocarwright | 4:9b7ea5528a5c | 106 | wavtable[i]= sin_d[i]+0.5; //generates wave table 0<1 in double |
lukeocarwright | 4:9b7ea5528a5c | 107 | //printf("wav[i]= %f \n", wavtable[i]); // Used for Debug |
lukeocarwright | 2:07cef563acdf | 108 | } |
lukeocarwright | 2:07cef563acdf | 109 | pad.leds_off(); |
lukeocarwright | 2:07cef563acdf | 110 | } |
lukeocarwright | 3:b7df72682b81 | 111 | |
lukeocarwright | 5:e785b9cd58c9 | 112 | int mainmenu() |
lukeocarwright | 5:e785b9cd58c9 | 113 | { |
lukeocarwright | 5:e785b9cd58c9 | 114 | printmainmenu(); |
lukeocarwright | 3:b7df72682b81 | 115 | |
lukeocarwright | 5:e785b9cd58c9 | 116 | |
lukeocarwright | 5:e785b9cd58c9 | 117 | |
lukeocarwright | 5:e785b9cd58c9 | 118 | return(1); |
lukeocarwright | 5:e785b9cd58c9 | 119 | } |
lukeocarwright | 5:e785b9cd58c9 | 120 | |
lukeocarwright | 5:e785b9cd58c9 | 121 | void printmainmenu() |
lukeocarwright | 5:e785b9cd58c9 | 122 | { |
lukeocarwright | 5:e785b9cd58c9 | 123 | printf("Printing Main Menu \n"); |
lukeocarwright | 5:e785b9cd58c9 | 124 | lcd.clear(); |
lukeocarwright | 5:e785b9cd58c9 | 125 | lcd.printString(MAIN_MENU,3,0); |
lukeocarwright | 5:e785b9cd58c9 | 126 | lcd.printString(Waveforms,3,1); |
lukeocarwright | 5:e785b9cd58c9 | 127 | lcd.printString(Credits,3,2); |
lukeocarwright | 5:e785b9cd58c9 | 128 | lcd.refresh(); |
lukeocarwright | 5:e785b9cd58c9 | 129 | |
lukeocarwright | 5:e785b9cd58c9 | 130 | |
lukeocarwright | 5:e785b9cd58c9 | 131 | } |
lukeocarwright | 5:e785b9cd58c9 | 132 | /* |
lukeocarwright | 4:9b7ea5528a5c | 133 | void squareWave() |
lukeocarwright | 4:9b7ea5528a5c | 134 | { |
lukeocarwright | 4:9b7ea5528a5c | 135 | printf("Generating SIN PWM \n"); |
lukeocarwright | 4:9b7ea5528a5c | 136 | |
lukeocarwright | 4:9b7ea5528a5c | 137 | int i=0; //int based iterator |
lukeocarwright | 4:9b7ea5528a5c | 138 | float ifl=0; //float based itterator |
lukeocarwright | 4:9b7ea5528a5c | 139 | float f=50; //frequency of sin wave produced |
lukeocarwright | 4:9b7ea5528a5c | 140 | period.attach_us(&up_isr,g_period_us); //ticker to write 1 to rca |
lukeocarwright | 4:9b7ea5528a5c | 141 | down.attach_us(&down_isr,g_uptime_us); //ticker to write 0 to rca |
lukeocarwright | 5:e785b9cd58c9 | 142 | |
lukeocarwright | 5:e785b9cd58c9 | 143 | |
lukeocarwright | 4:9b7ea5528a5c | 144 | printf("g_period_us: %d \n", g_period_us); |
lukeocarwright | 4:9b7ea5528a5c | 145 | printf("sin Frequency: %f \n", f); |
lukeocarwright | 4:9b7ea5528a5c | 146 | |
lukeocarwright | 4:9b7ea5528a5c | 147 | while (1) { //continual loop for pulse production |
lukeocarwright | 4:9b7ea5528a5c | 148 | float dutyratio = wavtable[i]; //calcualtes duty ratio of pulse |
lukeocarwright | 5:e785b9cd58c9 | 149 | |
lukeocarwright | 4:9b7ea5528a5c | 150 | g_uptime_us= dutyratio*g_period_us; //calculates duty ratio in usecs |
lukeocarwright | 4:9b7ea5528a5c | 151 | if (g_uptime_us<1) { |
lukeocarwright | 4:9b7ea5528a5c | 152 | g_uptime_us=g_period_us/100; |
lukeocarwright | 4:9b7ea5528a5c | 153 | } //sets to be value for timebeing to eliminate 0 error |
lukeocarwright | 4:9b7ea5528a5c | 154 | //float f=440*(pad.read_pot1()+1); //removed for simplification |
lukeocarwright | 5:e785b9cd58c9 | 155 | |
lukeocarwright | 4:9b7ea5528a5c | 156 | if (g_upflag==0) { |
lukeocarwright | 5:e785b9cd58c9 | 157 | |
lukeocarwright | 4:9b7ea5528a5c | 158 | ifl = ifl + (4096*f*g_period); //once rca=1 itterate sin function |
lukeocarwright | 4:9b7ea5528a5c | 159 | if (ifl>4096) { |
lukeocarwright | 4:9b7ea5528a5c | 160 | ifl= ifl-4096; |
lukeocarwright | 4:9b7ea5528a5c | 161 | } |
lukeocarwright | 4:9b7ea5528a5c | 162 | i=ifl; |
lukeocarwright | 5:e785b9cd58c9 | 163 | |
lukeocarwright | 4:9b7ea5528a5c | 164 | g_upflag=1; //reset flag |
lukeocarwright | 4:9b7ea5528a5c | 165 | //printf("iterate i: i= %d, ifl= %f \n", i, ifl); |
lukeocarwright | 4:9b7ea5528a5c | 166 | } |
lukeocarwright | 5:e785b9cd58c9 | 167 | |
lukeocarwright | 4:9b7ea5528a5c | 168 | //printf("DR: %f \n",dutyratio); |
lukeocarwright | 4:9b7ea5528a5c | 169 | //printf("sleep \n"); |
lukeocarwright | 4:9b7ea5528a5c | 170 | sleep(); //sleeps till next isr |
lukeocarwright | 4:9b7ea5528a5c | 171 | } |
lukeocarwright | 4:9b7ea5528a5c | 172 | } |
lukeocarwright | 4:9b7ea5528a5c | 173 | |
lukeocarwright | 4:9b7ea5528a5c | 174 | void down_isr() //sets rca to 0 |
lukeocarwright | 4:9b7ea5528a5c | 175 | { |
lukeocarwright | 4:9b7ea5528a5c | 176 | //printf("downISR \n"); |
lukeocarwright | 4:9b7ea5528a5c | 177 | if (g_downflag==0) {//stops error trigger |
lukeocarwright | 4:9b7ea5528a5c | 178 | rca.write(0); |
lukeocarwright | 4:9b7ea5528a5c | 179 | g_downflag=1; |
lukeocarwright | 4:9b7ea5528a5c | 180 | g_uptime_us= g_uptime_us+g_period_us; // eliminates it calling if =0 |
lukeocarwright | 4:9b7ea5528a5c | 181 | //printf("0 \n");//print only in while v. slow freguency |
lukeocarwright | 4:9b7ea5528a5c | 182 | } |
lukeocarwright | 4:9b7ea5528a5c | 183 | } |
lukeocarwright | 4:9b7ea5528a5c | 184 | |
lukeocarwright | 4:9b7ea5528a5c | 185 | void up_isr() //sets rca=1 |
lukeocarwright | 4:9b7ea5528a5c | 186 | { |
lukeocarwright | 4:9b7ea5528a5c | 187 | //printf("upISR \n"); |
lukeocarwright | 4:9b7ea5528a5c | 188 | rca.write(1); |
lukeocarwright | 4:9b7ea5528a5c | 189 | down.attach_us(&down_isr,g_uptime_us); //timer to set rca=0 after elapsed |
lukeocarwright | 4:9b7ea5528a5c | 190 | g_upflag=0; //sets flag to iterate |
lukeocarwright | 4:9b7ea5528a5c | 191 | g_downflag=0; //sets flag to allow set to 0 |
lukeocarwright | 4:9b7ea5528a5c | 192 | //printf("1 \n"); //only in while at v low frequency |
lukeocarwright | 4:9b7ea5528a5c | 193 | } |
lukeocarwright | 4:9b7ea5528a5c | 194 | |
lukeocarwright | 5:e785b9cd58c9 | 195 | */ |
lukeocarwright | 4:9b7ea5528a5c | 196 | /* |
lukeocarwright | 2:07cef563acdf | 197 | void sinspeak () |
lukeocarwright | 2:07cef563acdf | 198 | { |
lukeocarwright | 2:07cef563acdf | 199 | float f1=440.0; |
lukeocarwright | 2:07cef563acdf | 200 | float i=0; |
lukeocarwright | 3:b7df72682b81 | 201 | unsigned short v=0; |
lukeocarwright | 2:07cef563acdf | 202 | int inc=0; |
lukeocarwright | 2:07cef563acdf | 203 | pad.reset_buttons(); |
lukeocarwright | 2:07cef563acdf | 204 | f1 = pad.read_pot1(); |
lukeocarwright | 3:b7df72682b81 | 205 | f1 = 440.0f+440.0f*f1; |
lukeocarwright | 3:b7df72682b81 | 206 | //printf("f1= %f \n",f1); // Used for Debug |
lukeocarwright | 2:07cef563acdf | 207 | |
lukeocarwright | 2:07cef563acdf | 208 | while (inc<3000) { |
lukeocarwright | 2:07cef563acdf | 209 | int inti = i; |
lukeocarwright | 2:07cef563acdf | 210 | v = o[inti]; |
lukeocarwright | 4:9b7ea5528a5c | 211 | printf("OUTPUT: %u \n", v); |
lukeocarwright | 4:9b7ea5528a5c | 212 | //pad.write_u16(v); |
lukeocarwright | 3:b7df72682b81 | 213 | wait_us(230); //fs= 4k Ts=250us |
lukeocarwright | 3:b7df72682b81 | 214 | |
lukeocarwright | 3:b7df72682b81 | 215 | i = i + ((4096.0f*f1)/4000.0f); //i+((samples*f)*Ts) |
lukeocarwright | 2:07cef563acdf | 216 | if (i>=4096.0f) { |
lukeocarwright | 2:07cef563acdf | 217 | i=i-4096.0f; |
lukeocarwright | 3:b7df72682b81 | 218 | } |
lukeocarwright | 3:b7df72682b81 | 219 | else { |
lukeocarwright | 4:9b7ea5528a5c | 220 | wait_us(3); // used to attempt note stabilisation to match other loop |
lukeocarwright | 2:07cef563acdf | 221 | } |
lukeocarwright | 2:07cef563acdf | 222 | inc++; |
lukeocarwright | 2:07cef563acdf | 223 | } |
lukeocarwright | 2:07cef563acdf | 224 | } |
lukeocarwright | 4:9b7ea5528a5c | 225 | */ |