Luke Cartwright / Mbed 2 deprecated ELEC2645_Project_el18loc_nearlythere

Dependencies:   mbed

Committer:
lukeocarwright
Date:
Tue May 26 10:17:47 2020 +0000
Revision:
30:08cc4ec58d07
Parent:
29:207111ffd6e6
Child:
31:cfdb014ff086
Sorted Settings Menu Final Touches. More Code Tidying.

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 9:f6ba53e355a0 24 for (int i=0; i<1024; i++) {
lukeocarwright 9:f6ba53e355a0 25 sin_d= 65535*(0.5*sin(2.0*PI*(i/1024.0))+0.5);
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 9:f6ba53e355a0 34 sin_u=((uint16_t)sin_d);
lukeocarwright 9:f6ba53e355a0 35 //printf("sin_u= %u \n", sin_u);
lukeocarwright 9:f6ba53e355a0 36 sin_wavtable[i]=sin_u;
lukeocarwright 9:f6ba53e355a0 37 //sin_wavtable[i]=get_sin(i);
lukeocarwright 9:f6ba53e355a0 38 }
lukeocarwright 10:258a1eca02cc 39 /*//DEBUG
lukeocarwright 9:f6ba53e355a0 40 for (int i=0; i<1024; i=i+128) {
lukeocarwright 9:f6ba53e355a0 41 printf("sin_wav[%d]= %u \n", i, sin_wavtable);
lukeocarwright 30:08cc4ec58d07 42 } */
lukeocarwright 9:f6ba53e355a0 43 }
lukeocarwright 9:f6ba53e355a0 44
lukeocarwright 10:258a1eca02cc 45 void LUTs::tri_wavetable(int pulsewidth)
lukeocarwright 10:258a1eca02cc 46 {
lukeocarwright 30:08cc4ec58d07 47 //printf("Generating Tri-wavetable\n");
lukeocarwright 10:258a1eca02cc 48 tri_wavtable[0]=0;
lukeocarwright 10:258a1eca02cc 49 rise_t=(pulsewidth*1024/100);
lukeocarwright 11:6ae098535da9 50 rise_tu=(uint16_t)rise_t;
lukeocarwright 15:1c67f064278e 51 fall_tu=1024-rise_tu;
lukeocarwright 10:258a1eca02cc 52 dif=65536/rise_t;
lukeocarwright 10:258a1eca02cc 53 dif_u=(uint16_t)dif;
lukeocarwright 15:1c67f064278e 54
lukeocarwright 30:08cc4ec58d07 55 #ifdef SLOW_TIME
lukeocarwright 15:1c67f064278e 56 printf("PRINTING TRI WAVETABLE Values\n");
lukeocarwright 15:1c67f064278e 57 printf("PW= %d \n",pulsewidth);
lukeocarwright 15:1c67f064278e 58 printf("Rise Samples= %u\n", rise_tu);
lukeocarwright 15:1c67f064278e 59 printf("Fall Samples= %u\n", fall_tu);
lukeocarwright 15:1c67f064278e 60 printf("UP sample dif= %u\n", dif_u);
lukeocarwright 30:08cc4ec58d07 61 #endif
lukeocarwright 11:6ae098535da9 62 for (int i=1; i<=rise_tu; i++) {
lukeocarwright 10:258a1eca02cc 63 tri_wavtable[i]=tri_wavtable[i-1]+dif_u;
lukeocarwright 10:258a1eca02cc 64 }
lukeocarwright 11:6ae098535da9 65 dif=65536/fall_tu;
lukeocarwright 10:258a1eca02cc 66 dif_u=(uint16_t)dif;
lukeocarwright 15:1c67f064278e 67
lukeocarwright 15:1c67f064278e 68 #ifdef SLOW_TIME
lukeocarwright 19:08862f49cd9e 69 //printf("down sample dif= %u\n", dif_u);
lukeocarwright 30:08cc4ec58d07 70 #endif
lukeocarwright 15:1c67f064278e 71 for (int i=rise_tu; i<1024; i++) {
lukeocarwright 15:1c67f064278e 72 tri_wavtable[i]=65535-((i-rise_tu)*dif_u);
lukeocarwright 10:258a1eca02cc 73 }
lukeocarwright 30:08cc4ec58d07 74
lukeocarwright 15:1c67f064278e 75 #ifdef SLOW_TIME
lukeocarwright 19:08862f49cd9e 76 //tri_wav_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 11:6ae098535da9 83 printf("Generating Pulse-wavetable\n");
lukeocarwright 11:6ae098535da9 84 up_t=(pulsewidth*1024/100);
lukeocarwright 11:6ae098535da9 85 up_tu=(uint16_t)up_t;
lukeocarwright 11:6ae098535da9 86 for (int i=0; i<=up_tu; i++) {
lukeocarwright 29:207111ffd6e6 87 pulse_wavtable[i]=58981;
lukeocarwright 11:6ae098535da9 88 //printf("up itt = %d\n",i);
lukeocarwright 11:6ae098535da9 89 }
lukeocarwright 14:9cfe0041cc4e 90
lukeocarwright 11:6ae098535da9 91 for (int i=up_tu+1; i<1024; i++) {
lukeocarwright 29:207111ffd6e6 92 pulse_wavtable[i]=6553;
lukeocarwright 11:6ae098535da9 93 //printf("down itt = %d\n",i);
lukeocarwright 11:6ae098535da9 94 }
lukeocarwright 11:6ae098535da9 95 }
lukeocarwright 11:6ae098535da9 96
lukeocarwright 15:1c67f064278e 97 void LUTs::tri_wav_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 }