sdf

Dependencies:   AvailableMemory mbed-rtos mbed

Committer:
y7jin
Date:
Thu Apr 03 22:56:32 2014 +0000
Revision:
0:1c8f2727e9f5
hello

Who changed what in which revision?

UserRevisionLine numberNew contents of line
y7jin 0:1c8f2727e9f5 1 #include "mbed.h"
y7jin 0:1c8f2727e9f5 2 #include "math.h"
y7jin 0:1c8f2727e9f5 3 #include "rtos.h"
y7jin 0:1c8f2727e9f5 4 #include "Mem.h"
y7jin 0:1c8f2727e9f5 5 #include "Parser.h"
y7jin 0:1c8f2727e9f5 6 #include "AvailableMemory.h"
y7jin 0:1c8f2727e9f5 7
y7jin 0:1c8f2727e9f5 8 #define SAMPLES 1000
y7jin 0:1c8f2727e9f5 9 #define PRINT0(arg0) stdio_mutex.lock();while(!pc.writeable());pc.printf(arg0);stdio_mutex.unlock()
y7jin 0:1c8f2727e9f5 10 #define PRINT1(arg0,arg1) stdio_mutex.lock();while(!pc.writeable());pc.printf(arg0,arg1);stdio_mutex.unlock()
y7jin 0:1c8f2727e9f5 11 #define PRINT2(arg0,arg1,arg2) stdio_mutex.lock();while(!pc.writeable());pc.printf(arg0,arg1,arg2);stdio_mutex.unlock()
y7jin 0:1c8f2727e9f5 12 #define PRINT3(arg0,arg1,arg2,arg3) stdio_mutex.lock();while(!pc.writeable());pc.printf(arg0,arg1,arg2,arg3);stdio_mutex.unlock()
y7jin 0:1c8f2727e9f5 13 #define PRINT4(arg0,arg1,arg2,arg3,arg4) stdio_mutex.lock();while(!pc.writeable());pc.printf(arg0,arg1,arg2,arg3,arg4);stdio_mutex.unlock()
y7jin 0:1c8f2727e9f5 14
y7jin 0:1c8f2727e9f5 15 using namespace segundo::Utilities;
y7jin 0:1c8f2727e9f5 16
y7jin 0:1c8f2727e9f5 17 Serial pc(USBTX, USBRX);
y7jin 0:1c8f2727e9f5 18 LPC_TIM_TypeDef abc;
y7jin 0:1c8f2727e9f5 19 int cntr2 = 0;
y7jin 0:1c8f2727e9f5 20 Mutex stdio_mutex;
y7jin 0:1c8f2727e9f5 21
y7jin 0:1c8f2727e9f5 22 const int SamplingRate = 100;//sampling rate
y7jin 0:1c8f2727e9f5 23
y7jin 0:1c8f2727e9f5 24 PwmOut led1(LED1);
y7jin 0:1c8f2727e9f5 25 PwmOut led2(LED2);
y7jin 0:1c8f2727e9f5 26 PwmOut led3(LED3);
y7jin 0:1c8f2727e9f5 27 PwmOut led4(LED4);
y7jin 0:1c8f2727e9f5 28 LocalFileSystem local("local");
y7jin 0:1c8f2727e9f5 29 short hp = 0;
y7jin 0:1c8f2727e9f5 30 //thread 1: display & filter, thread 2: process commands
y7jin 0:1c8f2727e9f5 31 void control_thread(void const *args) {
y7jin 0:1c8f2727e9f5 32 PRINT0("Swap thread starts...\r\n");
y7jin 0:1c8f2727e9f5 33
y7jin 0:1c8f2727e9f5 34 while(1)
y7jin 0:1c8f2727e9f5 35 {
y7jin 0:1c8f2727e9f5 36 short temp;
y7jin 0:1c8f2727e9f5 37 scanf("%d",&temp);
y7jin 0:1c8f2727e9f5 38 if (!temp)
y7jin 0:1c8f2727e9f5 39 {
y7jin 0:1c8f2727e9f5 40 hp = 0;
y7jin 0:1c8f2727e9f5 41 PRINT0("Low-pass filter activated!\n\r");
y7jin 0:1c8f2727e9f5 42 }
y7jin 0:1c8f2727e9f5 43 else if (temp == 1)
y7jin 0:1c8f2727e9f5 44 {
y7jin 0:1c8f2727e9f5 45 hp = 1;
y7jin 0:1c8f2727e9f5 46 PRINT0("High-pass filter activated!\n\r");
y7jin 0:1c8f2727e9f5 47 }
y7jin 0:1c8f2727e9f5 48 else
y7jin 0:1c8f2727e9f5 49 {
y7jin 0:1c8f2727e9f5 50
y7jin 0:1c8f2727e9f5 51 }
y7jin 0:1c8f2727e9f5 52 }
y7jin 0:1c8f2727e9f5 53 }
y7jin 0:1c8f2727e9f5 54
y7jin 0:1c8f2727e9f5 55
y7jin 0:1c8f2727e9f5 56 void monitor_thread(void const *args)
y7jin 0:1c8f2727e9f5 57 {
y7jin 0:1c8f2727e9f5 58 PRINT0("Monitor thread started...\n\r");
y7jin 0:1c8f2727e9f5 59 RingBuffer* output= (RingBuffer*) args;
y7jin 0:1c8f2727e9f5 60 int checksum = 0, pw = 0;
y7jin 0:1c8f2727e9f5 61 for (int cntr=0;cntr<SAMPLES;cntr++)
y7jin 0:1c8f2727e9f5 62 {
y7jin 0:1c8f2727e9f5 63 while(cntr >= cntr2);
y7jin 0:1c8f2727e9f5 64 pw = output->buf[cntr];
y7jin 0:1c8f2727e9f5 65 checksum ^= pw;
y7jin 0:1c8f2727e9f5 66 PRINT3("cntr: %d\tout: %d\tcs: %08X\n\r", cntr, pw, checksum);
y7jin 0:1c8f2727e9f5 67 }
y7jin 0:1c8f2727e9f5 68
y7jin 0:1c8f2727e9f5 69
y7jin 0:1c8f2727e9f5 70 }
y7jin 0:1c8f2727e9f5 71
y7jin 0:1c8f2727e9f5 72 PwmOut myled(LED1);
y7jin 0:1c8f2727e9f5 73
y7jin 0:1c8f2727e9f5 74 int main() {
y7jin 0:1c8f2727e9f5 75
y7jin 0:1c8f2727e9f5 76 Thread controlThread(control_thread);
y7jin 0:1c8f2727e9f5 77
y7jin 0:1c8f2727e9f5 78 RingBuffer *input=NULL, *output=NULL;
y7jin 0:1c8f2727e9f5 79 input=new RingBuffer(inBuffer, 4096);
y7jin 0:1c8f2727e9f5 80 output=new RingBuffer(outBuffer, 4096);
y7jin 0:1c8f2727e9f5 81 SDFG *lpiir=new SDFG("/local/lpconf.txt",input,output);
y7jin 0:1c8f2727e9f5 82 if (!lpiir) error("ERROR: could not allocate memory for LP IIR filter.\r\n"); else {PRINT0("LP IIR filter instance created...\n\r");}
y7jin 0:1c8f2727e9f5 83 SDFG *hpiir=new SDFG("/local/hpconf.txt",input,output);
y7jin 0:1c8f2727e9f5 84 if (!hpiir) error("ERROR: could not allocate memory for HP IIR filter.\r\n"); else {PRINT0("HP IIR filter instance created...\n\r");}
y7jin 0:1c8f2727e9f5 85
y7jin 0:1c8f2727e9f5 86 Parser::parseInput("/local/input.txt",input);
y7jin 0:1c8f2727e9f5 87
y7jin 0:1c8f2727e9f5 88 // Thread monitorThread(monitor_thread, output);
y7jin 0:1c8f2727e9f5 89 PRINT0("Starting filtering...\n\r");
y7jin 0:1c8f2727e9f5 90
y7jin 0:1c8f2727e9f5 91 int pw;
y7jin 0:1c8f2727e9f5 92 int cntr=0;
y7jin 0:1c8f2727e9f5 93
y7jin 0:1c8f2727e9f5 94 int checksum = 0;
y7jin 0:1c8f2727e9f5 95 while (true) {
y7jin 0:1c8f2727e9f5 96 if (hp)
y7jin 0:1c8f2727e9f5 97 hpiir->execute(1);
y7jin 0:1c8f2727e9f5 98 else
y7jin 0:1c8f2727e9f5 99 lpiir->execute(1);
y7jin 0:1c8f2727e9f5 100 pw = output->next();
y7jin 0:1c8f2727e9f5 101 if (cntr2<SAMPLES)
y7jin 0:1c8f2727e9f5 102 {
y7jin 0:1c8f2727e9f5 103 checksum ^= pw;
y7jin 0:1c8f2727e9f5 104 if (++cntr2 == SAMPLES)
y7jin 0:1c8f2727e9f5 105 {
y7jin 0:1c8f2727e9f5 106 PRINT1("Writing the checksum: 0x%X", checksum);
y7jin 0:1c8f2727e9f5 107 FILE *fp=fopen("/local/output.txt", "w");
y7jin 0:1c8f2727e9f5 108 fprintf(fp, "0x%X\r\n", checksum);
y7jin 0:1c8f2727e9f5 109 fclose(fp);
y7jin 0:1c8f2727e9f5 110 }
y7jin 0:1c8f2727e9f5 111 }
y7jin 0:1c8f2727e9f5 112
y7jin 0:1c8f2727e9f5 113 led1 = (float)input->buf[input->cur]/1000;
y7jin 0:1c8f2727e9f5 114 led4 = (float)pw/1000;
y7jin 0:1c8f2727e9f5 115 wait(0.02);
y7jin 0:1c8f2727e9f5 116 }
y7jin 0:1c8f2727e9f5 117 abc;
y7jin 0:1c8f2727e9f5 118 }
y7jin 0:1c8f2727e9f5 119