36372 mbed / Mbed 2 deprecated SP14P1_skeleton

Dependencies:   AvailableMemory mbed-rtos mbed

Fork of helloaabbc by 32314 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "math.h"
00003 #include "rtos.h"
00004 #include "Mem.h"
00005 #include "Parser.h"
00006 #include "AvailableMemory.h"
00007 
00008 #define SAMPLES 1000
00009 #define PRINT0(arg0) stdio_mutex.lock();while(!pc.writeable());pc.printf(arg0);stdio_mutex.unlock()
00010 #define PRINT1(arg0,arg1) stdio_mutex.lock();while(!pc.writeable());pc.printf(arg0,arg1);stdio_mutex.unlock()
00011 #define PRINT2(arg0,arg1,arg2) stdio_mutex.lock();while(!pc.writeable());pc.printf(arg0,arg1,arg2);stdio_mutex.unlock()
00012 #define PRINT3(arg0,arg1,arg2,arg3) stdio_mutex.lock();while(!pc.writeable());pc.printf(arg0,arg1,arg2,arg3);stdio_mutex.unlock()
00013 #define PRINT4(arg0,arg1,arg2,arg3,arg4) stdio_mutex.lock();while(!pc.writeable());pc.printf(arg0,arg1,arg2,arg3,arg4);stdio_mutex.unlock()
00014 
00015 using namespace segundo::Utilities;
00016 
00017 Serial pc(USBTX, USBRX);
00018 LPC_TIM_TypeDef abc;
00019 int cntr2 = 0;
00020 Mutex stdio_mutex; 
00021 
00022 //const int SamplingRate = 100;//sampling rate
00023 
00024 PwmOut led1(LED1);
00025 PwmOut led2(LED2);
00026 PwmOut led3(LED3);
00027 PwmOut led4(LED4);
00028 LocalFileSystem local("local");
00029 short hp = 0;    
00030  //thread 1: display & filter, thread 2: process commands
00031 void control_thread(void const *args) {
00032     PRINT0("Swap thread starts...\r\n");
00033     
00034     while(1)
00035     {
00036         int temp;
00037         scanf("%d",&temp);
00038         if (!temp)
00039         {
00040             hp = 0;
00041             PRINT0("Low-pass filter activated!\n\r");
00042         }
00043         else if (temp == 1)
00044         {
00045             hp = 1;
00046             PRINT0("High-pass filter activated!\n\r");
00047         }
00048         else 
00049         {
00050             
00051         }
00052     }
00053 }
00054  
00055 int main() {
00056     
00057     Thread controlThread(control_thread);
00058         
00059     RingBuffer *input=NULL, *output=NULL;
00060     input=new RingBuffer(inBuffer, 4096);
00061     output=new RingBuffer(outBuffer, 4096);
00062     SDFG *lpiir=new SDFG("/local/lpconf.txt",input,output);
00063     if (!lpiir) error("ERROR: could not allocate memory for LP IIR filter.\r\n"); else {PRINT0("LP IIR filter instance created...\n\r");}
00064     SDFG *hpiir=new SDFG("/local/hpconf.txt",input,output);
00065     if (!hpiir) error("ERROR: could not allocate memory for HP IIR filter.\r\n"); else {PRINT0("HP IIR filter instance created...\n\r");}
00066 
00067     Parser::parseInput("/local/input.txt",input);   
00068    
00069 //    Thread monitorThread(monitor_thread, output);
00070     PRINT0("Starting filtering...\n\r");
00071     
00072     int pw;
00073     //int cntr=0;
00074     
00075     int checksum = 0;
00076     while (true) {
00077        if (hp)
00078            hpiir->execute(1);  
00079        else 
00080            lpiir->execute(1);
00081        pw = output->next();  
00082        if (cntr2<SAMPLES) 
00083        { 
00084            checksum ^= pw;
00085            if (++cntr2 == SAMPLES)
00086            {
00087                PRINT1("Writing the checksum: 0x%X", checksum);
00088                FILE *fp=fopen("/local/output.txt", "w");
00089                fprintf(fp, "0x%X\r\n", checksum);
00090                fclose(fp);  
00091            }
00092        }
00093        
00094        led1 = (float)input->buf[input->cur]/1000;
00095        led2 = (float)pw/1000;       
00096        //PRINT1("LED1 = %d\r\n", input->cur);
00097        //PRINT1("LED2 = %d\r\n", output->cur);
00098        wait(0.02);
00099     }
00100     
00101 }
00102