skeleton for lab1

Dependencies:   AvailableMemory mbed-rtos mbed

Fork of helloaabbc by 32314 mbed

Committer:
mbed36372
Date:
Fri Apr 04 21:31:22 2014 +0000
Revision:
1:55e99f6e2aa5
Parent:
0:1c8f2727e9f5
SP14_lab1

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
mbed36372 1:55e99f6e2aa5 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 {
mbed36372 1:55e99f6e2aa5 36 int 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 int main() {
y7jin 0:1c8f2727e9f5 56
y7jin 0:1c8f2727e9f5 57 Thread controlThread(control_thread);
y7jin 0:1c8f2727e9f5 58
y7jin 0:1c8f2727e9f5 59 RingBuffer *input=NULL, *output=NULL;
y7jin 0:1c8f2727e9f5 60 input=new RingBuffer(inBuffer, 4096);
y7jin 0:1c8f2727e9f5 61 output=new RingBuffer(outBuffer, 4096);
y7jin 0:1c8f2727e9f5 62 SDFG *lpiir=new SDFG("/local/lpconf.txt",input,output);
y7jin 0:1c8f2727e9f5 63 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 64 SDFG *hpiir=new SDFG("/local/hpconf.txt",input,output);
y7jin 0:1c8f2727e9f5 65 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 66
y7jin 0:1c8f2727e9f5 67 Parser::parseInput("/local/input.txt",input);
y7jin 0:1c8f2727e9f5 68
y7jin 0:1c8f2727e9f5 69 // Thread monitorThread(monitor_thread, output);
y7jin 0:1c8f2727e9f5 70 PRINT0("Starting filtering...\n\r");
y7jin 0:1c8f2727e9f5 71
y7jin 0:1c8f2727e9f5 72 int pw;
mbed36372 1:55e99f6e2aa5 73 //int cntr=0;
y7jin 0:1c8f2727e9f5 74
y7jin 0:1c8f2727e9f5 75 int checksum = 0;
y7jin 0:1c8f2727e9f5 76 while (true) {
y7jin 0:1c8f2727e9f5 77 if (hp)
y7jin 0:1c8f2727e9f5 78 hpiir->execute(1);
y7jin 0:1c8f2727e9f5 79 else
y7jin 0:1c8f2727e9f5 80 lpiir->execute(1);
y7jin 0:1c8f2727e9f5 81 pw = output->next();
y7jin 0:1c8f2727e9f5 82 if (cntr2<SAMPLES)
y7jin 0:1c8f2727e9f5 83 {
y7jin 0:1c8f2727e9f5 84 checksum ^= pw;
y7jin 0:1c8f2727e9f5 85 if (++cntr2 == SAMPLES)
y7jin 0:1c8f2727e9f5 86 {
y7jin 0:1c8f2727e9f5 87 PRINT1("Writing the checksum: 0x%X", checksum);
y7jin 0:1c8f2727e9f5 88 FILE *fp=fopen("/local/output.txt", "w");
y7jin 0:1c8f2727e9f5 89 fprintf(fp, "0x%X\r\n", checksum);
y7jin 0:1c8f2727e9f5 90 fclose(fp);
y7jin 0:1c8f2727e9f5 91 }
y7jin 0:1c8f2727e9f5 92 }
y7jin 0:1c8f2727e9f5 93
y7jin 0:1c8f2727e9f5 94 led1 = (float)input->buf[input->cur]/1000;
mbed36372 1:55e99f6e2aa5 95 led2 = (float)pw/1000;
mbed36372 1:55e99f6e2aa5 96 //PRINT1("LED1 = %d\r\n", input->cur);
mbed36372 1:55e99f6e2aa5 97 //PRINT1("LED2 = %d\r\n", output->cur);
y7jin 0:1c8f2727e9f5 98 wait(0.02);
y7jin 0:1c8f2727e9f5 99 }
mbed36372 1:55e99f6e2aa5 100
y7jin 0:1c8f2727e9f5 101 }
y7jin 0:1c8f2727e9f5 102