Luke Cartwright / Mbed 2 deprecated ELEC2645_Project_el18loc_nearlythere

Dependencies:   mbed

Committer:
lukeocarwright
Date:
Mon May 04 14:27:35 2020 +0000
Revision:
6:3a4e9ce12911
Parent:
5:e785b9cd58c9
Child:
7:33cb5f2db1ee
Menu format created in seperate files

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 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 LEDS INSTALL
lukeocarwright 6:3a4e9ce12911 16 pass variables between functions using pointers
lukeocarwright 6:3a4e9ce12911 17 menu starts select at item 0?
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"
lukeocarwright 6:3a4e9ce12911 24 #include "menu.h"
eencae 0:b7f1f47bb26a 25
lukeocarwright 2:07cef563acdf 26 // Objects
eencae 0:b7f1f47bb26a 27 Gamepad pad;
eencae 0:b7f1f47bb26a 28 N5110 lcd;
lukeocarwright 6:3a4e9ce12911 29 menu menu;
lukeocarwright 4:9b7ea5528a5c 30 Ticker down;
lukeocarwright 4:9b7ea5528a5c 31 Ticker period;
lukeocarwright 4:9b7ea5528a5c 32
lukeocarwright 4:9b7ea5528a5c 33 DigitalOut rca(PTC4);
lukeocarwright 2:07cef563acdf 34
lukeocarwright 2:07cef563acdf 35 Serial pc(USBTX, USBRX);
lukeocarwright 2:07cef563acdf 36
lukeocarwright 2:07cef563acdf 37 //Functions
lukeocarwright 2:07cef563acdf 38 void startup();
lukeocarwright 5:e785b9cd58c9 39 //void squareWave();
lukeocarwright 2:07cef563acdf 40 void wavetable();
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
lukeocarwright 2:07cef563acdf 46 //Global Variables
lukeocarwright 4:9b7ea5528a5c 47 volatile double wavtable[4096]; //Wavetable array
lukeocarwright 4:9b7ea5528a5c 48 // all times drastically slowed to attempt debugging in teraterm
lukeocarwright 4:9b7ea5528a5c 49 float g_period = 0.002;
lukeocarwright 4:9b7ea5528a5c 50 float g_unused= g_period*1000000.0f;
lukeocarwright 4:9b7ea5528a5c 51 int g_period_us=g_unused;
lukeocarwright 4:9b7ea5528a5c 52 volatile uint64_t g_uptime_us = g_period_us/2; // placeholder value
lukeocarwright 4:9b7ea5528a5c 53 //isr interrupt flags
lukeocarwright 4:9b7ea5528a5c 54 volatile int g_upflag=1;
lukeocarwright 4:9b7ea5528a5c 55 volatile int g_downflag=1;
eencae 0:b7f1f47bb26a 56
eencae 0:b7f1f47bb26a 57 int main()
eencae 0:b7f1f47bb26a 58 {
lukeocarwright 2:07cef563acdf 59 printf("RUNNING CODE \n");
lukeocarwright 4:9b7ea5528a5c 60 startup(); //initialises board and displays start screen
lukeocarwright 4:9b7ea5528a5c 61 wavetable(); //generates wavtable array
lukeocarwright 6:3a4e9ce12911 62 menu.mainmenu(lcd, pad); //generates main menu
lukeocarwright 4:9b7ea5528a5c 63 //pad.leds_on();
lukeocarwright 5:e785b9cd58c9 64 //squareWave(); //generates pulse wave modulated by sin wave
lukeocarwright 4:9b7ea5528a5c 65 //pad.leds_off();
lukeocarwright 4:9b7ea5528a5c 66 //sinspeak();
lukeocarwright 2:07cef563acdf 67 }
lukeocarwright 2:07cef563acdf 68
lukeocarwright 2:07cef563acdf 69
lukeocarwright 2:07cef563acdf 70 void startup()
lukeocarwright 2:07cef563acdf 71 {
lukeocarwright 2:07cef563acdf 72 pad.init(); //initiate Gamepad
lukeocarwright 2:07cef563acdf 73 pad.leds_on(); //turn LEDS on to show starting up
lukeocarwright 3:b7df72682b81 74 printf("Initialising Pad\n");
lukeocarwright 2:07cef563acdf 75 lcd.init(); //intitates screen
lukeocarwright 2:07cef563acdf 76 lcd.clear();
lukeocarwright 4:9b7ea5528a5c 77 lcd.setContrast(0.5); //contrast setting
lukeocarwright 2:07cef563acdf 78 lcd.inverseMode(); //puts screen in -ve
lukeocarwright 2:07cef563acdf 79 //Prints CART SYNTH to display
lukeocarwright 6:3a4e9ce12911 80 lcd.printString("CART",12,2);
lukeocarwright 6:3a4e9ce12911 81 lcd.printString("SYNTH",32,3);
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 void wavetable()
lukeocarwright 2:07cef563acdf 91 {
lukeocarwright 4:9b7ea5528a5c 92 double sin_d[4096];
lukeocarwright 2:07cef563acdf 93
lukeocarwright 4:9b7ea5528a5c 94 pad.leds_on(); //shows computating (debug)
lukeocarwright 2:07cef563acdf 95 printf("Generating Wavetable \n");
lukeocarwright 2:07cef563acdf 96 for (int i=0; i<4096; i++) {
lukeocarwright 4:9b7ea5528a5c 97 sin_d[i] = 0.5f*sin(2.0f*PI*(i/4096.0f));
lukeocarwright 4:9b7ea5528a5c 98 wavtable[i]= sin_d[i]+0.5; //generates wave table 0<1 in double
lukeocarwright 4:9b7ea5528a5c 99 //printf("wav[i]= %f \n", wavtable[i]); // Used for Debug
lukeocarwright 2:07cef563acdf 100 }
lukeocarwright 2:07cef563acdf 101 pad.leds_off();
lukeocarwright 2:07cef563acdf 102 }
lukeocarwright 3:b7df72682b81 103
lukeocarwright 3:b7df72682b81 104
lukeocarwright 5:e785b9cd58c9 105 /*
lukeocarwright 4:9b7ea5528a5c 106 void squareWave()
lukeocarwright 4:9b7ea5528a5c 107 {
lukeocarwright 4:9b7ea5528a5c 108 printf("Generating SIN PWM \n");
lukeocarwright 4:9b7ea5528a5c 109
lukeocarwright 4:9b7ea5528a5c 110 int i=0; //int based iterator
lukeocarwright 4:9b7ea5528a5c 111 float ifl=0; //float based itterator
lukeocarwright 4:9b7ea5528a5c 112 float f=50; //frequency of sin wave produced
lukeocarwright 4:9b7ea5528a5c 113 period.attach_us(&up_isr,g_period_us); //ticker to write 1 to rca
lukeocarwright 4:9b7ea5528a5c 114 down.attach_us(&down_isr,g_uptime_us); //ticker to write 0 to rca
lukeocarwright 5:e785b9cd58c9 115
lukeocarwright 5:e785b9cd58c9 116
lukeocarwright 4:9b7ea5528a5c 117 printf("g_period_us: %d \n", g_period_us);
lukeocarwright 4:9b7ea5528a5c 118 printf("sin Frequency: %f \n", f);
lukeocarwright 4:9b7ea5528a5c 119
lukeocarwright 4:9b7ea5528a5c 120 while (1) { //continual loop for pulse production
lukeocarwright 4:9b7ea5528a5c 121 float dutyratio = wavtable[i]; //calcualtes duty ratio of pulse
lukeocarwright 5:e785b9cd58c9 122
lukeocarwright 4:9b7ea5528a5c 123 g_uptime_us= dutyratio*g_period_us; //calculates duty ratio in usecs
lukeocarwright 4:9b7ea5528a5c 124 if (g_uptime_us<1) {
lukeocarwright 4:9b7ea5528a5c 125 g_uptime_us=g_period_us/100;
lukeocarwright 4:9b7ea5528a5c 126 } //sets to be value for timebeing to eliminate 0 error
lukeocarwright 4:9b7ea5528a5c 127 //float f=440*(pad.read_pot1()+1); //removed for simplification
lukeocarwright 5:e785b9cd58c9 128
lukeocarwright 4:9b7ea5528a5c 129 if (g_upflag==0) {
lukeocarwright 5:e785b9cd58c9 130
lukeocarwright 4:9b7ea5528a5c 131 ifl = ifl + (4096*f*g_period); //once rca=1 itterate sin function
lukeocarwright 4:9b7ea5528a5c 132 if (ifl>4096) {
lukeocarwright 4:9b7ea5528a5c 133 ifl= ifl-4096;
lukeocarwright 4:9b7ea5528a5c 134 }
lukeocarwright 4:9b7ea5528a5c 135 i=ifl;
lukeocarwright 5:e785b9cd58c9 136
lukeocarwright 4:9b7ea5528a5c 137 g_upflag=1; //reset flag
lukeocarwright 4:9b7ea5528a5c 138 //printf("iterate i: i= %d, ifl= %f \n", i, ifl);
lukeocarwright 4:9b7ea5528a5c 139 }
lukeocarwright 5:e785b9cd58c9 140
lukeocarwright 4:9b7ea5528a5c 141 //printf("DR: %f \n",dutyratio);
lukeocarwright 4:9b7ea5528a5c 142 //printf("sleep \n");
lukeocarwright 4:9b7ea5528a5c 143 sleep(); //sleeps till next isr
lukeocarwright 4:9b7ea5528a5c 144 }
lukeocarwright 4:9b7ea5528a5c 145 }
lukeocarwright 4:9b7ea5528a5c 146
lukeocarwright 4:9b7ea5528a5c 147 void down_isr() //sets rca to 0
lukeocarwright 4:9b7ea5528a5c 148 {
lukeocarwright 4:9b7ea5528a5c 149 //printf("downISR \n");
lukeocarwright 4:9b7ea5528a5c 150 if (g_downflag==0) {//stops error trigger
lukeocarwright 4:9b7ea5528a5c 151 rca.write(0);
lukeocarwright 4:9b7ea5528a5c 152 g_downflag=1;
lukeocarwright 4:9b7ea5528a5c 153 g_uptime_us= g_uptime_us+g_period_us; // eliminates it calling if =0
lukeocarwright 4:9b7ea5528a5c 154 //printf("0 \n");//print only in while v. slow freguency
lukeocarwright 4:9b7ea5528a5c 155 }
lukeocarwright 4:9b7ea5528a5c 156 }
lukeocarwright 4:9b7ea5528a5c 157
lukeocarwright 4:9b7ea5528a5c 158 void up_isr() //sets rca=1
lukeocarwright 4:9b7ea5528a5c 159 {
lukeocarwright 4:9b7ea5528a5c 160 //printf("upISR \n");
lukeocarwright 4:9b7ea5528a5c 161 rca.write(1);
lukeocarwright 4:9b7ea5528a5c 162 down.attach_us(&down_isr,g_uptime_us); //timer to set rca=0 after elapsed
lukeocarwright 4:9b7ea5528a5c 163 g_upflag=0; //sets flag to iterate
lukeocarwright 4:9b7ea5528a5c 164 g_downflag=0; //sets flag to allow set to 0
lukeocarwright 4:9b7ea5528a5c 165 //printf("1 \n"); //only in while at v low frequency
lukeocarwright 4:9b7ea5528a5c 166 }
lukeocarwright 4:9b7ea5528a5c 167
lukeocarwright 5:e785b9cd58c9 168 */
lukeocarwright 4:9b7ea5528a5c 169 /*
lukeocarwright 2:07cef563acdf 170 void sinspeak ()
lukeocarwright 2:07cef563acdf 171 {
lukeocarwright 2:07cef563acdf 172 float f1=440.0;
lukeocarwright 2:07cef563acdf 173 float i=0;
lukeocarwright 3:b7df72682b81 174 unsigned short v=0;
lukeocarwright 2:07cef563acdf 175 int inc=0;
lukeocarwright 2:07cef563acdf 176 pad.reset_buttons();
lukeocarwright 2:07cef563acdf 177 f1 = pad.read_pot1();
lukeocarwright 3:b7df72682b81 178 f1 = 440.0f+440.0f*f1;
lukeocarwright 3:b7df72682b81 179 //printf("f1= %f \n",f1); // Used for Debug
lukeocarwright 2:07cef563acdf 180
lukeocarwright 2:07cef563acdf 181 while (inc<3000) {
lukeocarwright 2:07cef563acdf 182 int inti = i;
lukeocarwright 2:07cef563acdf 183 v = o[inti];
lukeocarwright 4:9b7ea5528a5c 184 printf("OUTPUT: %u \n", v);
lukeocarwright 4:9b7ea5528a5c 185 //pad.write_u16(v);
lukeocarwright 3:b7df72682b81 186 wait_us(230); //fs= 4k Ts=250us
lukeocarwright 3:b7df72682b81 187
lukeocarwright 3:b7df72682b81 188 i = i + ((4096.0f*f1)/4000.0f); //i+((samples*f)*Ts)
lukeocarwright 2:07cef563acdf 189 if (i>=4096.0f) {
lukeocarwright 2:07cef563acdf 190 i=i-4096.0f;
lukeocarwright 3:b7df72682b81 191 }
lukeocarwright 3:b7df72682b81 192 else {
lukeocarwright 4:9b7ea5528a5c 193 wait_us(3); // used to attempt note stabilisation to match other loop
lukeocarwright 2:07cef563acdf 194 }
lukeocarwright 2:07cef563acdf 195 inc++;
lukeocarwright 2:07cef563acdf 196 }
lukeocarwright 2:07cef563acdf 197 }
lukeocarwright 4:9b7ea5528a5c 198 */