filter implement on DiscoTech device

Dependencies:   PwmDAC ShiftReg Terminal adc mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* This program executes an audio to rgb light conversion.
00002  * It is made for an international EESTEC Workshop by LC Hamburg.
00003  * This works on mbed LPC1768 with hardware peripherals on pcb.
00004  * It converts a stereo (to chan. mixed to mono) signal to light animation
00005  * Using digital filters or any alogrithm you prefer to convert music into ligth
00006  * It controlls 3 RGB LED stripes powerlines with 74HC595 shift
00007  * register.
00008  * 
00009  * Project:         DiscoTech
00010  * HDK/ SDK Eng:    Tobias Wulf
00011  * Date:            21.09.2013
00012  *
00013  */
00014  
00015 #include "mbed.h"
00016 #include "Terminal.h"
00017 
00018 
00019 #include "adc.h"
00020 #include "PwmDAC.h"
00021 #include "ShiftReg.h"
00022 
00023 
00024 #define SAMPLE_RATE 48000 //Hz
00025 #define ADC_BIAS 1.65 //Volts
00026 #define ULSB 3.3 / 4095
00027 #define RESOLUTION 256
00028 
00029 #define MASK 0x07 // for shift register
00030 
00031 
00032 #define NUMBER_OF_SAMPLES 1024
00033 
00034 /* declair your functions
00035  * isr routine doesn't have to be declaired
00036  */
00037 void mainInit();
00038 
00039 
00040 PwmDAC rgb;
00041 ADC adc(SAMPLE_RATE, 1);
00042 //AnalogOut dac(p18);
00043 ShiftReg LEDstripes(p30, p29, p28);
00044 
00045 /* Serial communication via usb uplink to pc
00046  * to display what ever you want in the terminal
00047  * or to send what ever you want from the terminal
00048  */
00049 Terminal usbPC(USBTX, USBRX);
00050 
00051 /* array for updating the PwmDAC object
00052  * and pointers for easy work
00053  * we use steps 1 3 5 ...point'em
00054  */
00055 uint32_t channels[6];
00056 
00057 
00058 
00059 /* global variables to work in the adc interrupt
00060  * service routine to get the data from interrupt
00061  */
00062 
00063 unsigned int pattern;
00064 double newSample;
00065 
00066 
00067 bool save;
00068 
00069 double adcSavings[NUMBER_OF_SAMPLES];
00070 double adcSamples[NUMBER_OF_SAMPLES];
00071 
00072 
00073 
00074     double adcSample;
00075     // LP for output pwm channel 1
00076     const double b1[3] = {0.034566506326168603, 0.069133012652337206, 0.034566506326168603}; 
00077     const double a1[2] = { -1.5156844350566925,  0.65395046036136695}; 
00078     double w1[3];
00079     double y1;
00080 
00081 
00082 int main() {
00083 
00084  
00085         // init for saving samples and filter results
00086         for (int i = 0; i < NUMBER_OF_SAMPLES; i++){
00087         adcSavings[i] = 0.0f;  
00088         adcSamples[i] = 0.0f;
00089         }
00090     
00091 
00092     // LP for output pwm channel 1
00093     for(int i= 0; i<3;i++)
00094         w1[i] = 0.0f;
00095     y1 = 0.0f;
00096 
00097 
00098  
00099     
00100     usbPC.printf("...start program\n");
00101     wait(0.2);
00102 
00103     mainInit();
00104     usbPC.printf("...init completed\n");
00105     wait(0.2);
00106 
00107  
00108     while(1) {
00109         
00110         if(save == false) {
00111         
00112            usbPC.locate(0,0);
00113            usbPC.cls();
00114            
00115            
00116            for(int i=0; i<NUMBER_OF_SAMPLES; i++)
00117                 usbPC.printf("%1.4f %1.4f\n",adcSavings[i],adcSamples[i]);
00118         }
00119         
00120         if(save == false) break;
00121         
00122     }        
00123 
00124 }/* end main */
00125 
00126 
00127 
00128 
00129 
00130 
00131 
00132 
00133 /* interrupt service routine from adc object
00134  * here should be done the data processing
00135  * variable and output updates
00136  */
00137 void getADC(int chan, uint32_t value) {
00138     static int cycleCounter = 0;
00139     
00140     adcSample = (double) adc.read(p15);
00141     adcSample = adcSample * ULSB;
00142               // first LP, chan 1 (red)
00143             w1[0] = adcSample - (a1[0] * w1[1]) - (a1[1] * w1[2]);
00144             y1 = 0.89125093813374 * ((b1[0] * w1[0]) +(b1[1] * w1[1]) + (b1[2] * w1[2]));
00145             w1[2] = w1[1]; 
00146             w1[1] = w1[0];
00147        
00148      if(cycleCounter < NUMBER_OF_SAMPLES) {
00149        adcSavings[cycleCounter] = y1;
00150        adcSamples[cycleCounter] = adcSample;
00151        cycleCounter++;
00152        if((cycleCounter == NUMBER_OF_SAMPLES-1) && save) {
00153             save = false;
00154             cycleCounter = 0;
00155        }
00156     }          
00157  
00158 }         
00159 
00160 /* function to initialize the main program, objects and 
00161  * implement all variables
00162  */
00163 void mainInit() {
00164       
00165 
00166 
00167     for(int i=0; i<6; i++) {
00168         channels[i] = 0;
00169     }
00170         
00171 
00172     usbPC.printf("...variables implemented\n");
00173     wait(0.2);
00174     
00175     /* setup shift register (stripe power)
00176      */
00177      LEDstripes.ShiftByte((unsigned int) MASK, ShiftReg::MSBFirst);
00178      LEDstripes.Latch();
00179      wait(0.2);
00180     
00181     usbPC.printf("...shift register initialized\n");
00182     wait(0.2);
00183     
00184     /* setup adc input
00185      */
00186     adc.append(getADC);
00187     
00188     adc.startmode(0,0);
00189     adc.burst(1);
00190    
00191     adc.setup(p15,1);
00192     adc.setup(p16,0);
00193     adc.setup(p17,0);
00194     adc.setup(p18,0);
00195     adc.setup(p19,0);
00196     adc.setup(p20,0);
00197     
00198     wait(0.2);
00199     adc.interrupt_state(p15,1);
00200     
00201   
00202     
00203     
00204     usbPC.printf("%u, %u, %u, %u\n", adc.setup(p15), 
00205                                      adc.burst(),
00206                                      adc.interrupt_state(p15), 
00207                                      adc.actual_sample_rate());   
00208     
00209     usbPC.printf("...ADC initialized\n");
00210     wait(0.2);
00211     
00212     /* setup pwm output
00213      */
00214     rgb.init();
00215     rgb.deactivate(0);
00216     rgb.deactivate(2);
00217     rgb.deactivate(4);
00218     rgb.setResolution(RESOLUTION);
00219     rgb.start();
00220     
00221     usbPC.printf("...PWM output initialized\n");
00222     wait(0.2);
00223 }