Luke Cartwright / Mbed 2 deprecated ELEC2645_Project_el18loc_nearlythere

Dependencies:   mbed

Committer:
lukeocarwright
Date:
Sat May 09 13:45:23 2020 +0000
Revision:
13:27300c533dd1
Parent:
12:7a399a476cfd
Child:
14:9cfe0041cc4e
ADSR faders moving

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 7:33cb5f2db1ee 11 Last Edited: 04/05/2020
eencae 0:b7f1f47bb26a 12 */
eencae 0:b7f1f47bb26a 13
lukeocarwright 5:e785b9cd58c9 14 /* TO DO:
lukeocarwright 8:f305ea78b2b1 15 add Menu functionality from front.cpp
lukeocarwright 8:f305ea78b2b1 16 front.h warning sort
lukeocarwright 7:33cb5f2db1ee 17 Settings tab
lukeocarwright 7:33cb5f2db1ee 18 Generate sound
lukeocarwright 7:33cb5f2db1ee 19 ADSR
lukeocarwright 8:f305ea78b2b1 20 MIDI
lukeocarwright 8:f305ea78b2b1 21 LEDS INSTALL
lukeocarwright 5:e785b9cd58c9 22 */
lukeocarwright 2:07cef563acdf 23 // Includes
eencae 0:b7f1f47bb26a 24 #include "mbed.h"
eencae 0:b7f1f47bb26a 25 #include "Gamepad.h"
eencae 0:b7f1f47bb26a 26 #include "N5110.h"
lukeocarwright 8:f305ea78b2b1 27 #include "Menu.h"
lukeocarwright 7:33cb5f2db1ee 28 #include "startup.h"
lukeocarwright 7:33cb5f2db1ee 29 #include "Front.h"
eencae 0:b7f1f47bb26a 30
lukeocarwright 9:f6ba53e355a0 31 #ifdef DEBUG
lukeocarwright 9:f6ba53e355a0 32 # include "Debug.h"
lukeocarwright 9:f6ba53e355a0 33 #endif
lukeocarwright 9:f6ba53e355a0 34
lukeocarwright 2:07cef563acdf 35 // Objects
eencae 0:b7f1f47bb26a 36 Gamepad pad;
eencae 0:b7f1f47bb26a 37 N5110 lcd;
lukeocarwright 8:f305ea78b2b1 38 Menu menu;
lukeocarwright 7:33cb5f2db1ee 39 startup start;
lukeocarwright 4:9b7ea5528a5c 40 Ticker down;
lukeocarwright 4:9b7ea5528a5c 41 Ticker period;
lukeocarwright 4:9b7ea5528a5c 42
lukeocarwright 4:9b7ea5528a5c 43 DigitalOut rca(PTC4);
lukeocarwright 2:07cef563acdf 44 Serial pc(USBTX, USBRX);
lukeocarwright 2:07cef563acdf 45
lukeocarwright 2:07cef563acdf 46 //Functions
lukeocarwright 5:e785b9cd58c9 47 //void squareWave();
lukeocarwright 4:9b7ea5528a5c 48 //void sinspeak();
lukeocarwright 5:e785b9cd58c9 49 //void down_isr();
lukeocarwright 5:e785b9cd58c9 50 //void up_isr();
lukeocarwright 2:07cef563acdf 51
lukeocarwright 2:07cef563acdf 52
lukeocarwright 2:07cef563acdf 53 //Global Variables
lukeocarwright 4:9b7ea5528a5c 54 // all times drastically slowed to attempt debugging in teraterm
lukeocarwright 7:33cb5f2db1ee 55 //float g_period = 0.002;
lukeocarwright 7:33cb5f2db1ee 56 //float g_unused= g_period*1000000.0f;
lukeocarwright 7:33cb5f2db1ee 57 //int g_period_us=g_unused;
lukeocarwright 7:33cb5f2db1ee 58 //volatile uint64_t g_uptime_us = g_period_us/2; // placeholder value
lukeocarwright 4:9b7ea5528a5c 59 //isr interrupt flags
lukeocarwright 7:33cb5f2db1ee 60 //volatile int g_upflag=1;
lukeocarwright 7:33cb5f2db1ee 61 //volatile int g_downflag=1;
eencae 0:b7f1f47bb26a 62
lukeocarwright 9:f6ba53e355a0 63 //volatile extern uint16_t sin_wavtable[1024];
lukeocarwright 9:f6ba53e355a0 64 //volatile extern double sin_d[1024];
lukeocarwright 9:f6ba53e355a0 65
lukeocarwright 9:f6ba53e355a0 66
lukeocarwright 10:258a1eca02cc 67 volatile extern uint16_t sin_wavtable[1024];
lukeocarwright 10:258a1eca02cc 68 volatile extern uint16_t tri_wavtable[1024];
lukeocarwright 11:6ae098535da9 69 volatile extern uint16_t pulse_wavtable[1024];
lukeocarwright 10:258a1eca02cc 70
eencae 0:b7f1f47bb26a 71 int main()
eencae 0:b7f1f47bb26a 72 {
lukeocarwright 2:07cef563acdf 73 printf("RUNNING CODE \n");
lukeocarwright 7:33cb5f2db1ee 74 start.initialise(lcd,pad); //initialises board and displays start screen
lukeocarwright 9:f6ba53e355a0 75
lukeocarwright 9:f6ba53e355a0 76 #ifdef DEBUG
lukeocarwright 9:f6ba53e355a0 77 printf("DEBUG COMPILE RUNNING\n");
lukeocarwright 9:f6ba53e355a0 78 run_LUTs_debug();
lukeocarwright 9:f6ba53e355a0 79 #endif
lukeocarwright 10:258a1eca02cc 80
lukeocarwright 7:33cb5f2db1ee 81 menu.mainmenu(lcd,pad);
lukeocarwright 5:e785b9cd58c9 82 //squareWave(); //generates pulse wave modulated by sin wave
lukeocarwright 4:9b7ea5528a5c 83 //sinspeak();
lukeocarwright 2:07cef563acdf 84 }
lukeocarwright 2:07cef563acdf 85
lukeocarwright 5:e785b9cd58c9 86 /*
lukeocarwright 4:9b7ea5528a5c 87 void squareWave()
lukeocarwright 4:9b7ea5528a5c 88 {
lukeocarwright 4:9b7ea5528a5c 89 printf("Generating SIN PWM \n");
lukeocarwright 4:9b7ea5528a5c 90
lukeocarwright 4:9b7ea5528a5c 91 int i=0; //int based iterator
lukeocarwright 4:9b7ea5528a5c 92 float ifl=0; //float based itterator
lukeocarwright 4:9b7ea5528a5c 93 float f=50; //frequency of sin wave produced
lukeocarwright 4:9b7ea5528a5c 94 period.attach_us(&up_isr,g_period_us); //ticker to write 1 to rca
lukeocarwright 4:9b7ea5528a5c 95 down.attach_us(&down_isr,g_uptime_us); //ticker to write 0 to rca
lukeocarwright 5:e785b9cd58c9 96
lukeocarwright 5:e785b9cd58c9 97
lukeocarwright 4:9b7ea5528a5c 98 printf("g_period_us: %d \n", g_period_us);
lukeocarwright 4:9b7ea5528a5c 99 printf("sin Frequency: %f \n", f);
lukeocarwright 4:9b7ea5528a5c 100
lukeocarwright 4:9b7ea5528a5c 101 while (1) { //continual loop for pulse production
lukeocarwright 4:9b7ea5528a5c 102 float dutyratio = wavtable[i]; //calcualtes duty ratio of pulse
lukeocarwright 5:e785b9cd58c9 103
lukeocarwright 4:9b7ea5528a5c 104 g_uptime_us= dutyratio*g_period_us; //calculates duty ratio in usecs
lukeocarwright 4:9b7ea5528a5c 105 if (g_uptime_us<1) {
lukeocarwright 4:9b7ea5528a5c 106 g_uptime_us=g_period_us/100;
lukeocarwright 4:9b7ea5528a5c 107 } //sets to be value for timebeing to eliminate 0 error
lukeocarwright 4:9b7ea5528a5c 108 //float f=440*(pad.read_pot1()+1); //removed for simplification
lukeocarwright 5:e785b9cd58c9 109
lukeocarwright 4:9b7ea5528a5c 110 if (g_upflag==0) {
lukeocarwright 5:e785b9cd58c9 111
lukeocarwright 4:9b7ea5528a5c 112 ifl = ifl + (4096*f*g_period); //once rca=1 itterate sin function
lukeocarwright 4:9b7ea5528a5c 113 if (ifl>4096) {
lukeocarwright 4:9b7ea5528a5c 114 ifl= ifl-4096;
lukeocarwright 4:9b7ea5528a5c 115 }
lukeocarwright 4:9b7ea5528a5c 116 i=ifl;
lukeocarwright 5:e785b9cd58c9 117
lukeocarwright 4:9b7ea5528a5c 118 g_upflag=1; //reset flag
lukeocarwright 4:9b7ea5528a5c 119 //printf("iterate i: i= %d, ifl= %f \n", i, ifl);
lukeocarwright 4:9b7ea5528a5c 120 }
lukeocarwright 5:e785b9cd58c9 121
lukeocarwright 4:9b7ea5528a5c 122 //printf("DR: %f \n",dutyratio);
lukeocarwright 4:9b7ea5528a5c 123 //printf("sleep \n");
lukeocarwright 4:9b7ea5528a5c 124 sleep(); //sleeps till next isr
lukeocarwright 4:9b7ea5528a5c 125 }
lukeocarwright 4:9b7ea5528a5c 126 }
lukeocarwright 4:9b7ea5528a5c 127
lukeocarwright 4:9b7ea5528a5c 128 void down_isr() //sets rca to 0
lukeocarwright 4:9b7ea5528a5c 129 {
lukeocarwright 4:9b7ea5528a5c 130 //printf("downISR \n");
lukeocarwright 4:9b7ea5528a5c 131 if (g_downflag==0) {//stops error trigger
lukeocarwright 4:9b7ea5528a5c 132 rca.write(0);
lukeocarwright 4:9b7ea5528a5c 133 g_downflag=1;
lukeocarwright 4:9b7ea5528a5c 134 g_uptime_us= g_uptime_us+g_period_us; // eliminates it calling if =0
lukeocarwright 4:9b7ea5528a5c 135 //printf("0 \n");//print only in while v. slow freguency
lukeocarwright 4:9b7ea5528a5c 136 }
lukeocarwright 4:9b7ea5528a5c 137 }
lukeocarwright 4:9b7ea5528a5c 138
lukeocarwright 4:9b7ea5528a5c 139 void up_isr() //sets rca=1
lukeocarwright 4:9b7ea5528a5c 140 {
lukeocarwright 4:9b7ea5528a5c 141 //printf("upISR \n");
lukeocarwright 4:9b7ea5528a5c 142 rca.write(1);
lukeocarwright 4:9b7ea5528a5c 143 down.attach_us(&down_isr,g_uptime_us); //timer to set rca=0 after elapsed
lukeocarwright 4:9b7ea5528a5c 144 g_upflag=0; //sets flag to iterate
lukeocarwright 4:9b7ea5528a5c 145 g_downflag=0; //sets flag to allow set to 0
lukeocarwright 4:9b7ea5528a5c 146 //printf("1 \n"); //only in while at v low frequency
lukeocarwright 4:9b7ea5528a5c 147 }
lukeocarwright 4:9b7ea5528a5c 148
lukeocarwright 5:e785b9cd58c9 149 */
lukeocarwright 4:9b7ea5528a5c 150 /*
lukeocarwright 2:07cef563acdf 151 void sinspeak ()
lukeocarwright 2:07cef563acdf 152 {
lukeocarwright 2:07cef563acdf 153 float f1=440.0;
lukeocarwright 2:07cef563acdf 154 float i=0;
lukeocarwright 3:b7df72682b81 155 unsigned short v=0;
lukeocarwright 2:07cef563acdf 156 int inc=0;
lukeocarwright 2:07cef563acdf 157 pad.reset_buttons();
lukeocarwright 2:07cef563acdf 158 f1 = pad.read_pot1();
lukeocarwright 3:b7df72682b81 159 f1 = 440.0f+440.0f*f1;
lukeocarwright 3:b7df72682b81 160 //printf("f1= %f \n",f1); // Used for Debug
lukeocarwright 2:07cef563acdf 161
lukeocarwright 2:07cef563acdf 162 while (inc<3000) {
lukeocarwright 2:07cef563acdf 163 int inti = i;
lukeocarwright 2:07cef563acdf 164 v = o[inti];
lukeocarwright 4:9b7ea5528a5c 165 printf("OUTPUT: %u \n", v);
lukeocarwright 4:9b7ea5528a5c 166 //pad.write_u16(v);
lukeocarwright 3:b7df72682b81 167 wait_us(230); //fs= 4k Ts=250us
lukeocarwright 3:b7df72682b81 168
lukeocarwright 3:b7df72682b81 169 i = i + ((4096.0f*f1)/4000.0f); //i+((samples*f)*Ts)
lukeocarwright 2:07cef563acdf 170 if (i>=4096.0f) {
lukeocarwright 2:07cef563acdf 171 i=i-4096.0f;
lukeocarwright 3:b7df72682b81 172 }
lukeocarwright 3:b7df72682b81 173 else {
lukeocarwright 4:9b7ea5528a5c 174 wait_us(3); // used to attempt note stabilisation to match other loop
lukeocarwright 2:07cef563acdf 175 }
lukeocarwright 2:07cef563acdf 176 inc++;
lukeocarwright 2:07cef563acdf 177 }
lukeocarwright 2:07cef563acdf 178 }
lukeocarwright 4:9b7ea5528a5c 179 */