32314 mbed / Mbed 2 deprecated helloaabbc

Dependencies:   AvailableMemory mbed-rtos 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         short 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  
00056 void monitor_thread(void const *args)
00057 {
00058     PRINT0("Monitor thread started...\n\r");
00059     RingBuffer* output= (RingBuffer*) args;
00060     int checksum = 0, pw = 0;
00061     for (int cntr=0;cntr<SAMPLES;cntr++)
00062     {
00063         while(cntr >= cntr2);
00064         pw = output->buf[cntr];
00065         checksum ^= pw;
00066         PRINT3("cntr: %d\tout: %d\tcs: %08X\n\r", cntr, pw, checksum);
00067     }
00068     
00069     
00070 }
00071 
00072 PwmOut myled(LED1);
00073 
00074 int main() {
00075     
00076     Thread controlThread(control_thread);
00077         
00078     RingBuffer *input=NULL, *output=NULL;
00079     input=new RingBuffer(inBuffer, 4096);
00080     output=new RingBuffer(outBuffer, 4096);
00081     SDFG *lpiir=new SDFG("/local/lpconf.txt",input,output);
00082     if (!lpiir) error("ERROR: could not allocate memory for LP IIR filter.\r\n"); else {PRINT0("LP IIR filter instance created...\n\r");}
00083     SDFG *hpiir=new SDFG("/local/hpconf.txt",input,output);
00084     if (!hpiir) error("ERROR: could not allocate memory for HP IIR filter.\r\n"); else {PRINT0("HP IIR filter instance created...\n\r");}
00085 
00086     Parser::parseInput("/local/input.txt",input);   
00087    
00088 //    Thread monitorThread(monitor_thread, output);
00089     PRINT0("Starting filtering...\n\r");
00090     
00091     int pw;
00092     int cntr=0;
00093     
00094     int checksum = 0;
00095     while (true) {
00096        if (hp)
00097            hpiir->execute(1);  
00098        else 
00099            lpiir->execute(1);
00100        pw = output->next();  
00101        if (cntr2<SAMPLES) 
00102        { 
00103            checksum ^= pw;
00104            if (++cntr2 == SAMPLES)
00105            {
00106                PRINT1("Writing the checksum: 0x%X", checksum);
00107                FILE *fp=fopen("/local/output.txt", "w");
00108                fprintf(fp, "0x%X\r\n", checksum);
00109                fclose(fp);  
00110            }
00111        }
00112        
00113        led1 = (float)input->buf[input->cur]/1000;
00114        led4 = (float)pw/1000;       
00115        wait(0.02);
00116     }
00117     abc;
00118 }
00119