Luke Cartwright / Mbed 2 deprecated ELEC2645_Project_el18loc_nearlythere

Dependencies:   mbed

Committer:
lukeocarwright
Date:
Tue May 26 14:21:36 2020 +0000
Revision:
31:cfdb014ff086
Parent:
30:08cc4ec58d07
Final Pad edit (-API check)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lukeocarwright 7:33cb5f2db1ee 1 #include "mbed.h"
lukeocarwright 7:33cb5f2db1ee 2 #include "LUTs.h"
lukeocarwright 7:33cb5f2db1ee 3
lukeocarwright 7:33cb5f2db1ee 4 LUTs::LUTs()
lukeocarwright 7:33cb5f2db1ee 5 {
lukeocarwright 7:33cb5f2db1ee 6 }
lukeocarwright 7:33cb5f2db1ee 7 LUTs::~LUTs()
lukeocarwright 7:33cb5f2db1ee 8 {
lukeocarwright 7:33cb5f2db1ee 9 }
lukeocarwright 10:258a1eca02cc 10 volatile uint16_t sin_wavtable[1024];
lukeocarwright 10:258a1eca02cc 11 volatile uint16_t tri_wavtable[1024];
lukeocarwright 11:6ae098535da9 12 volatile uint16_t pulse_wavtable[1024];
lukeocarwright 10:258a1eca02cc 13
lukeocarwright 10:258a1eca02cc 14 void LUTs::initial_wavetables()
lukeocarwright 10:258a1eca02cc 15 {
lukeocarwright 10:258a1eca02cc 16 sin_wavetable();
lukeocarwright 10:258a1eca02cc 17 tri_wavetable(50);
lukeocarwright 11:6ae098535da9 18 pulse_wavetable(50);
lukeocarwright 10:258a1eca02cc 19 }
lukeocarwright 7:33cb5f2db1ee 20
lukeocarwright 7:33cb5f2db1ee 21 void LUTs::sin_wavetable()
lukeocarwright 7:33cb5f2db1ee 22 {
lukeocarwright 30:08cc4ec58d07 23 //printf("Generating sin Wavetable \n");
lukeocarwright 31:cfdb014ff086 24 for (int i=0; i<1024; i++) { //Size of table
lukeocarwright 31:cfdb014ff086 25 sin_d= 65535*(0.5*sin(2.0*PI*(i/1024.0))+0.5); //Genrate Value
lukeocarwright 30:08cc4ec58d07 26 rem= fmod(sin_d,1); //calculates remainder for rounding
lukeocarwright 9:f6ba53e355a0 27 //printf("preround= %g -", sin_d); //DEBUG
lukeocarwright 9:f6ba53e355a0 28 if (rem>=0.5) {
lukeocarwright 9:f6ba53e355a0 29 sin_d=ceil(sin_d); //round UP
lukeocarwright 9:f6ba53e355a0 30 } else {
lukeocarwright 9:f6ba53e355a0 31 sin_d= floor(sin_d-rem); //round DOWN
lukeocarwright 9:f6ba53e355a0 32 }
lukeocarwright 10:258a1eca02cc 33 // printf("Postround= %g -",sin_d); //DEBUG
lukeocarwright 31:cfdb014ff086 34 sin_u=((uint16_t)sin_d); //Value converted to unsigned
lukeocarwright 9:f6ba53e355a0 35 //printf("sin_u= %u \n", sin_u);
lukeocarwright 31:cfdb014ff086 36 sin_wavtable[i]=sin_u;//Sets in Global Variable
lukeocarwright 9:f6ba53e355a0 37 //sin_wavtable[i]=get_sin(i);
lukeocarwright 9:f6ba53e355a0 38 }
lukeocarwright 31:cfdb014ff086 39 #ifdef SLOW_TIME
lukeocarwright 9:f6ba53e355a0 40 for (int i=0; i<1024; i=i+128) {
lukeocarwright 31:cfdb014ff086 41 printf("sin_wav[%d]= %u \n", i, sin_wavtable); //prints Key values
lukeocarwright 31:cfdb014ff086 42 }
lukeocarwright 31:cfdb014ff086 43 #endif
lukeocarwright 9:f6ba53e355a0 44 }
lukeocarwright 9:f6ba53e355a0 45
lukeocarwright 10:258a1eca02cc 46 void LUTs::tri_wavetable(int pulsewidth)
lukeocarwright 10:258a1eca02cc 47 {
lukeocarwright 30:08cc4ec58d07 48 //printf("Generating Tri-wavetable\n");
lukeocarwright 31:cfdb014ff086 49 tri_wavtable[0]=0; //sets 0 value
lukeocarwright 31:cfdb014ff086 50 rise_t=(pulsewidth*1024/100); //Calculates rise samples
lukeocarwright 11:6ae098535da9 51 rise_tu=(uint16_t)rise_t;
lukeocarwright 31:cfdb014ff086 52 fall_tu=1024-rise_tu; //Calculates fall samples
lukeocarwright 31:cfdb014ff086 53 dif=65536/rise_t; //Calculates Rise Dif
lukeocarwright 31:cfdb014ff086 54 dif_u=(uint16_t)dif; //converts to uint
lukeocarwright 15:1c67f064278e 55
lukeocarwright 31:cfdb014ff086 56 #ifdef SLOW_TIME //Prints Values in SLOW_TIME case
lukeocarwright 15:1c67f064278e 57 printf("PRINTING TRI WAVETABLE Values\n");
lukeocarwright 15:1c67f064278e 58 printf("PW= %d \n",pulsewidth);
lukeocarwright 15:1c67f064278e 59 printf("Rise Samples= %u\n", rise_tu);
lukeocarwright 15:1c67f064278e 60 printf("Fall Samples= %u\n", fall_tu);
lukeocarwright 15:1c67f064278e 61 printf("UP sample dif= %u\n", dif_u);
lukeocarwright 30:08cc4ec58d07 62 #endif
lukeocarwright 31:cfdb014ff086 63 for (int i=1; i<=rise_tu; i++) { //Generates Rise Points
lukeocarwright 10:258a1eca02cc 64 tri_wavtable[i]=tri_wavtable[i-1]+dif_u;
lukeocarwright 10:258a1eca02cc 65 }
lukeocarwright 31:cfdb014ff086 66 dif=65536/fall_tu; //Calcualtes Fall Diff
lukeocarwright 10:258a1eca02cc 67 dif_u=(uint16_t)dif;
lukeocarwright 15:1c67f064278e 68
lukeocarwright 15:1c67f064278e 69 #ifdef SLOW_TIME
lukeocarwright 31:cfdb014ff086 70 printf("down sample dif= %u\n", dif_u);
lukeocarwright 30:08cc4ec58d07 71 #endif
lukeocarwright 31:cfdb014ff086 72 for (int i=rise_tu; i<1024; i++) { //Generates Fall Points
lukeocarwright 15:1c67f064278e 73 tri_wavtable[i]=65535-((i-rise_tu)*dif_u);
lukeocarwright 10:258a1eca02cc 74 }
lukeocarwright 15:1c67f064278e 75 #ifdef SLOW_TIME
lukeocarwright 31:cfdb014ff086 76 tri_wav_results();//Prints Key Results
lukeocarwright 15:1c67f064278e 77 #endif
lukeocarwright 10:258a1eca02cc 78 }
lukeocarwright 9:f6ba53e355a0 79
lukeocarwright 9:f6ba53e355a0 80
lukeocarwright 11:6ae098535da9 81 void LUTs::pulse_wavetable(int pulsewidth)
lukeocarwright 11:6ae098535da9 82 {
lukeocarwright 31:cfdb014ff086 83 //printf("Generating Pulse-wavetable\n");
lukeocarwright 31:cfdb014ff086 84 up_t=(pulsewidth*1024/100); //Samples Up
lukeocarwright 11:6ae098535da9 85 up_tu=(uint16_t)up_t;
lukeocarwright 31:cfdb014ff086 86 for (int i=0; i<=up_tu; i++) { //Until Down
lukeocarwright 31:cfdb014ff086 87 pulse_wavtable[i]=58981; //Headroom to normalise Volume
lukeocarwright 11:6ae098535da9 88 //printf("up itt = %d\n",i);
lukeocarwright 11:6ae098535da9 89 }
lukeocarwright 14:9cfe0041cc4e 90
lukeocarwright 31:cfdb014ff086 91 for (int i=up_tu+1; i<1024; i++) { //Until Over
lukeocarwright 31:cfdb014ff086 92 pulse_wavtable[i]=6553; //HEadroom Included
lukeocarwright 11:6ae098535da9 93 //printf("down itt = %d\n",i);
lukeocarwright 11:6ae098535da9 94 }
lukeocarwright 11:6ae098535da9 95 }
lukeocarwright 11:6ae098535da9 96
lukeocarwright 31:cfdb014ff086 97 void LUTs::tri_wav_results() { //Prints Results
lukeocarwright 15:1c67f064278e 98 printf("TRI_WAV_RESULTS:\n");
lukeocarwright 15:1c67f064278e 99 printf("i,out\n");
lukeocarwright 15:1c67f064278e 100 for (i=0; i<=1024; i=i+64) {
lukeocarwright 15:1c67f064278e 101 printf("%d, %u\n",i,tri_wavtable[i]);
lukeocarwright 15:1c67f064278e 102 }
lukeocarwright 30:08cc4ec58d07 103 }