ELEC2645 (2019/20) / Mbed 2 deprecated el18loc_final

Dependencies:   mbed

Revision:
10:258a1eca02cc
Parent:
9:f6ba53e355a0
Child:
11:6ae098535da9
diff -r f6ba53e355a0 -r 258a1eca02cc LUTs/LUTS.cpp
--- a/LUTs/LUTS.cpp	Wed May 06 14:37:34 2020 +0000
+++ b/LUTs/LUTS.cpp	Wed May 06 19:51:22 2020 +0000
@@ -7,11 +7,19 @@
 LUTs::~LUTs()
 {
 }
-    volatile uint16_t sin_wavtable[1024];
+volatile uint16_t sin_wavtable[1024];
+volatile uint16_t tri_wavtable[1024];
+
+
+void LUTs::initial_wavetables()
+{
+    sin_wavetable();
+    tri_wavetable(50);
+}
 
 void LUTs::sin_wavetable()
 {
-    printf("Generating Wavetable \n");
+    printf("Generating sin Wavetable \n");
     for (int i=0; i<1024; i++) {
         sin_d= 65535*(0.5*sin(2.0*PI*(i/1024.0))+0.5);
         //calculates remainder for rounding
@@ -22,21 +30,36 @@
         } else {
             sin_d= floor(sin_d-rem); //round DOWN
         }
-       // printf("Postround= %g -",sin_d); //DEBUG
+        // printf("Postround= %g -",sin_d); //DEBUG
         sin_u=((uint16_t)sin_d);
         //printf("sin_u= %u \n", sin_u);
         sin_wavtable[i]=sin_u;
         //sin_wavtable[i]=get_sin(i);
     }
-     /*//DEBUG
+    /*//DEBUG
     for (int i=0; i<1024; i=i+128) {
     printf("sin_wav[%d]= %u \n", i, sin_wavtable);
     }
     */
 }
 
-
-
+void LUTs::tri_wavetable(int pulsewidth)
+{
+    printf("Generating Tri-wavetable\n");
+    tri_wavtable[0]=0;
+    rise_t=(pulsewidth*1024/100);
+    fall_t=1-rise_t;
+    dif=65536/rise_t;
+    dif_u=(uint16_t)dif;
+    for (int i=1; i<rise_t; i++) {
+        tri_wavtable[i]=tri_wavtable[i-1]+dif_u;
+    }
+    dif=65536/fall_t;
+    dif_u=(uint16_t)dif;
+    for (int i=rise_t; i<1024; i++) {
+        tri_wavtable[i]=tri_wavtable[i-1]-dif_u;
+    }
+}
 
 
 /*