2014_winter_EE202A_HM1_UCLA Author:Yujing Qian and Tianlei Tang

Dependencies:   MAG3110 MMA8451Q TSI mbed

Revision:
1:58c266799ba7
Parent:
0:3cc7f3653a5d
--- a/main.cpp	Thu Feb 13 02:00:58 2014 +0000
+++ b/main.cpp	Fri Feb 14 21:04:08 2014 +0000
@@ -1,3 +1,10 @@
+/* This is a mbed program for EE202A, hm1. written by Yujing Qian's team
+ Main idea:
+ Use interrupt to change the default configuration of the device and choose different channels and different sampling rate.
+ The corresponding GUI is host_command.py
+ All the commented-out code is for debugging use, please ignore them
+ Author: Yujing Qian, Tianlei Tang                      Feb.12/2014
+ */
 #include "mbed.h"
 #include "MMA8451Q.h"
 #include "MAG3110.h"
@@ -12,6 +19,7 @@
 char rx_buffer[BUFFER_SIZE];
 int buff=0;
 bool received;
+Timer timer;
  
 void receive_handler(){
     while (pc.readable() && buff< BUFFER_SIZE){
@@ -21,12 +29,12 @@
         if (rx_buffer[buff] == '#'){
             rx_buffer[buff] = '\0';
             /*
-            rx_buffer[6] = '\0';
-            int tag=192;
+            rx_buffer[14] = '\0';
+            int tag=1;
             char *testx=(char*)&tag;
             buff=14;
             rx_buffer[0]=(*testx);
-            tag=2;
+            tag=3;
             testx=(char*)&tag;
             rx_buffer[1]=(*testx);
             rx_buffer[2]=0;
@@ -38,13 +46,13 @@
             rx_buffer[6]=0;
             rx_buffer[7]=0;
             rx_buffer[8]=0;
-            tag=200;
+            tag=100;
             testx=(char*)&(tag);
             rx_buffer[9]=(*testx);
             rx_buffer[10]=0;
             rx_buffer[11]=0;
             rx_buffer[12]=0;
-            tag=400;
+            tag=100;
             testx=(char*)&(tag);
             rx_buffer[13]=(*testx);
             */
@@ -78,12 +86,13 @@
 int tsiDis;
 int analogin;
 char* select;
-int enable=256;
+int enable=32767;
 int tmpf=0;
 int i,j;
-int threshold[15];
+int interval[15];
 int count[15];
-for(i=0;i<15;i++)threshold[i]=1;//BAUDRATE;
+int tmptime;
+for(i=0;i<15;i++)interval[i]=1000;//default:1ms
 for(i=0;i<15;i++)count[i]=0;
 bool output_en=0;
 char *tmp;
@@ -92,6 +101,11 @@
 output=new char;
 pc.attach(&receive_handler,Serial::RxIrq);
 //pc.printf("enable=%d",enable); 
+timer.start();
+tmptime=timer.read_us();
+for(i=0;i<15;i++)count[i]=tmptime;
+//pc.printf("time_written");  
+       
      while (true) { 
      if (received) {
          select=rx_buffer;
@@ -113,26 +127,31 @@
                 }
                 k=k-1;
  
-            threshold[14-k]=BAUDRATE/tmpf;
+            interval[14-k]=1000000/tmpf;//interval us per sample
             i=i+4;
          }
-         for (i=0;i<15;i++)count[i]=0;
+         //for (i=0;i<15;i++)count[i]=0;
          
-         //for(int o=0;o<15;o++){pc.printf("count=%d",count[o]);pc.printf("threshold=%d\n",threshold[o]);}
+         //for(int o=0;o<15;o++){pc.printf("count=%d",count[o]);pc.printf("interval=%d\n",interval[o]);}
          //pc.printf("enable=%d\n",enable&1);
          //pc.putc('$');pc.putc('c');pc.putc('d');pc.putc('_');pc.putc('r');pc.putc('v');pc.putc('d');
          buff=0;
-         for(i=0;i<15;i++)count[i]=0;
-         received=false; 
+         tmptime=timer.read_us();
+         for(i=0;i<15;i++)count[i]=tmptime;
+         received=false;
+         //pc.printf("command,received\n");
          }
      int output_map=0;
      output_en=false;
+     
 
+         
      int opt=0;
 //pc.printf("nscanning\n");
-//for(int o=0;o<15;o++){pc.printf("count=%d",count[o]);pc.printf("threshold=%d\n",threshold[o]);}
+//for(int o=0;o<15;o++){pc.printf("count=%d",count[o]);pc.printf("interval=%d\n",interval[o]);}
 //pc.getc();
-     if((count[0]>=threshold[0])&&(enable&(1))){
+     if((enable&(1))&&(timer.read_us()>=(count[0]+interval[0]))){
+         count[0]=timer.read_us();
          output_map=output_map|(1);
          output_en=true;//output
          light=(int)(lightsensor.read()*10000);
@@ -141,12 +160,13 @@
          output[opt+1]=(*(tmp+2));
          output[opt+2]=(*(tmp+1));
          output[opt+3]=(*tmp);
-         count[0]=1;
+         
          opt=opt+4; }
-     else count[0]++;    
+     //else count[0]++;    
           
      //light sensor
-     if((count[1]>=threshold[1])&&(enable&(1<<1))){
+     if((enable&(1<<1))&&(timer.read_us()>=(count[1]+interval[1]))){
+         count[1]=timer.read_us();
          output_map=output_map|(1<<1);
          output_en=true;//output
          accx=(int)(acc.getAccX()*10000);
@@ -155,12 +175,12 @@
          output[opt+1]=(*(tmp+2));
          output[opt+2]=(*(tmp+1));
          output[opt+3]=(*tmp);
-         count[1]=1;
          opt=opt+4;}
-     else count[1]++;    
+     //else count[1]++;    
      //accx  
 
-     if((count[2]>=threshold[2])&&(enable&(1<<2))){
+     if((enable&(1<<2))&&(timer.read_us()>=(count[2]+interval[2]))){
+         count[2]=timer.read_us();
          output_map=output_map|(1<<2);
          output_en=true;//output
          accy=(int)(acc.getAccY()*10000);
@@ -169,11 +189,12 @@
          output[opt+1]=(*(tmp+2));
          output[opt+2]=(*(tmp+1));
          output[opt+3]=(*tmp);
-         count[2]=1;
+         
          opt=opt+4;} 
-     else count[2]++;   
+    // else count[2]++;   
      //accy 
-     if((count[3]>=threshold[3])&&(enable&(1<<3))){
+     if((enable&(1<<3))&&(timer.read_us()>=(count[3]+interval[3]))){
+         count[3]=timer.read_us();
          output_map=output_map|(1<<3);
          output_en=true;//output
          accz=(int)(acc.getAccZ()*10000);
@@ -182,11 +203,11 @@
          output[opt+1]=(*(tmp+2));
          output[opt+2]=(*(tmp+1));
          output[opt+3]=(*tmp);
-         count[3]=1;
          opt=opt+4;}
-    else count[3]++;      
+    //else count[3]++;      
      //accz
-     if((count[4]>=threshold[4])&&(enable&(1<<4))){
+     if((enable&(1<<4))&&(timer.read_us()>=(count[4]+interval[4]))){
+         count[4]=timer.read_us();
          output_map=output_map|(1<<4);
          output_en=true;//output
          magx=(int)(mag.getX()*10000);
@@ -195,11 +216,12 @@
          output[opt+1]=(*(tmp+2));
          output[opt+2]=(*(tmp+1));
          output[opt+3]=(*tmp);
-         count[4]=1;
+         //count[4]=1;
          opt=opt+4;}
-     else count[4]++;    
+    // else count[4]++;    
      //magx  
-     if((count[5]>=threshold[5])&&(enable&(1<<5))){
+     if((enable&(1<<5))&&(timer.read_us()>=(count[5]+interval[5]))){
+         count[5]=timer.read_us();
          output_map=output_map|(1<<5);
          output_en=true;//output
          magy=(int)(mag.getY()*10000);
@@ -208,11 +230,12 @@
          output[opt+1]=(*(tmp+2));
          output[opt+2]=(*(tmp+1));
          output[opt+3]=(*tmp);
-         count[5]=1;
+         //count[5]=1;
          opt=opt+4;} 
-         else count[5]++;   
+         //else count[5]++;   
      //magy 
-     if((count[6]>=threshold[6])&&(enable&(1<<6))){
+     if((enable&(1<<6))&&(timer.read_us()>=(count[6]+interval[6]))){
+         count[6]=timer.read_us();
          output_map=output_map|(1<<6);
          magz=(int)(mag.getZ()*10000);
          output_en=true;//output
@@ -221,11 +244,11 @@
          output[opt+1]=(*(tmp+2));
          output[opt+2]=(*(tmp+1));
          output[opt+3]=(*tmp);
-         count[6]=1;
          opt=opt+4;}
-         else count[6]++;             
+         //else count[6]++;             
      //magz 
-          if((count[7]>=threshold[7])&&(enable&(1<<7))){
+     if((enable&(1<<7))&&(timer.read_us()>=(count[7]+interval[7]))){
+         count[7]=timer.read_us();
          output_map=output_map|(1<<7);
          tsiPerc=(int)(tsi.readPercentage()*10000); 
          output_en=true;//output
@@ -234,11 +257,12 @@
          output[opt+1]=(*(tmp+2));
          output[opt+2]=(*(tmp+1));
          output[opt+3]=(*tmp);
-         count[7]=1;
+         //count[7]=1;
          opt=opt+4;}
-         else count[7]++;             
+         //else count[7]++;             
      //TIS_perc
-    if((count[8]>=threshold[8])&&(enable&(1<<8))){
+     if((enable&(1<<8))&&(timer.read_us()>=(count[8]+interval[8]))){
+         count[8]=timer.read_us();
          output_map=output_map|(1<<8);
          tsiDis=(int)(tsi.readDistance()*10000); 
          output_en=true;//output
@@ -247,14 +271,15 @@
          output[opt+1]=(*(tmp+2));
          output[opt+2]=(*(tmp+1));
          output[opt+3]=(*tmp);
-         count[8]=1;
+         //count[8]=1;
          opt=opt+4;}
-         else count[8]++;             
+         //else count[8]++;             
      //TIS_perc     
      //touchs1=touch1.read();
      //touchs2=touch2.read();
      
-     if((count[9]>=threshold[9])&&(enable&(1<<9))){
+     if((enable&(1<<9))&&(timer.read_us()>=(count[9]+interval[9]))){
+         count[9]=timer.read_us();
          output_map=output_map|(1<<9);
          analogin=(int)(analoginput.read()*10000);
          output_en=true;//output
@@ -263,9 +288,9 @@
          output[opt+1]=(*(tmp+2));
          output[opt+2]=(*(tmp+1));
          output[opt+3]=(*tmp);
-         count[9]=1;
+         //count[9]=1;
          opt=opt+4;}
-     else count[9]++;      
+     //else count[9]++;      
 
      
      //analog channel 1#
@@ -288,6 +313,7 @@
         /*
         pc.printf("\n");
         pc.printf("c0=%d",count[0]);pc.printf("c1=%d",count[1]);pc.printf("c9=%d",count[9]);
+        pc.printf("time=%d\n",timer.read_us());
         pc.printf("translate=%d",(int)(*(tmp+1)));
         pc.printf("translate=%d",(int)(*(tmp)));
         pc.printf("\n");
@@ -300,14 +326,14 @@
                 }
             pc.printf("output=%d\n",inttmp);
             }
+        
         */
         
-        
         //
        // pc.printf("finish\n");
       }   
      //pc.printf("ck=%d",count[0]);
-     wait_us(1000000/BAUDRATE);
+     //wait_us(1000000/BAUDRATE);
      } 
 
 }