Changes done in ECG and BT

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of merged_code2_20sept_2017_4th_oct_2017 by nikita teggi

Files at this revision

API Documentation at this revision

Comitter:
nikitateggi
Date:
Sat Jul 08 04:19:45 2017 +0000
Parent:
26:53ff13a164f3
Child:
28:65f2d80b7eb7
Commit message:
with file_track bluetooth

Changed in this revision

bt.cpp Show annotated file Show diff for this revision Revisions of this file
sdcard.cpp Show annotated file Show diff for this revision Revisions of this file
sdcard.h Show annotated file Show diff for this revision Revisions of this file
--- a/bt.cpp	Wed Jul 05 15:36:58 2017 +0000
+++ b/bt.cpp	Sat Jul 08 04:19:45 2017 +0000
@@ -1,8 +1,3 @@
-// added newline, changed the sequence of cmd and length, added send last function
-
-
-
-
 
 #include "mbed.h"
 #include "sdcard.h"
@@ -14,55 +9,83 @@
 #define NACK 0xFF
 #define sos 0xc0
 #define eos 0xc0
+#define no_of_samples 1500
+#include "eeprom_pgm.h"
 //#define len 6000
 
-
-
 BLEMsg_info *bt_file;
 Point *myBleMsg;
-//bt_st *as;
-Serial mc(USBTX,USBRX);
-Serial blue(PTC4,PTC3);
-                 // buffer for data
+
+Serial mc(USBTX,USBRX);                  // serial terminal
+Serial blue(PTC4,PTC3);                 // communicating bluetooth through serial 
+                 
 uint32_t total_samples=0;              // variable to hold total number of samples
-//Timer t;                               // timer 
 uint8_t ack_rx;                        // varibale to receive ack from the bluetooth 
 Point b;                               // structure copy
 BLEMsg_info bt_file1;                  // structure copy
-uint32_t counter=0;
- uint32_t total_file_size=0;        // to determine the size of the file 
- uint32_t total_file_read=0; 
-                  
+uint32_t total_file_size=0;        // to determine the size of the file 
+uint32_t total_file_read=0; 
 uint32_t file_start=0;           // variable for 'for loop'
 uint32_t j=0;
-uint8_t k=0; 
+uint8_t count_send=0; 
 uint32_t file_pointer_position=0;
-
-
+int32_t bt_file_tosend=0;           // file to send on bluetooth
+int32_t current_file=0;            // file of the test done for the current pid 
+uint32_t counter=0;
+uint8_t file=0;
 
 
 uint8_t bt_send(uint8_t state_tx)
 {
-  uint32_t v1[1500];    
+  uint32_t v1[no_of_samples];    
   blue.baud(115200);                  // baud rate of bluetooth
   bt_file=&bt_file1;                      
   myBleMsg=&b; 
   int8_t check_sum=0;
-  static  uint8_t state_rx =0;
-//t.start();                             // timer start
+  uint8_t state_rx =0;
+ 
+  
+
   
 
     switch(state_tx)
           {
  
-       case 1:   sendinit();  
-                  blue.printf("\n");
-                 sd_open_read(36);                       // open the file for read
-                 total_file_size=file_size() ;             // determined the file size  
-              //   mc.printf("filesize=%d",total_file_size);                // TO SEND INITIAL STRING
-                 state_rx =1;
-                 state_tx =0;                
-                 break;
+       case 1:    
+                  //blue.printf("\n");
+                   state_tx =0; 
+                  counter=0;
+                  total_file_read=0; 
+                  file_start=0;          
+                  j=0; count_send=0;  total_samples=0;  
+                  file_pointer_position=0;
+                  bt_file_tosend=eprom_read(6);            // reading the file number(pid) from eeprom which has to be sent to bluetooth
+                  current_file=eprom_read(1);  
+                  bt_file_tosend=bt_file_tosend+1;
+                  eprom_write(6,bt_file_tosend);            // read the current pid 
+                  mc.printf("eeprom file=%d",bt_file_tosend);  
+                  mc.printf("currentfile=%d",current_file);  
+                  if (bt_file_tosend>current_file)       // if the file sent to bluetooth and current file are same,
+                  {                                       // then there is no new file to send
+                      state_rx =6;
+                      }
+                  else
+                    {
+                       file=sd_open_read(bt_file_tosend);
+                       if(file==0)
+                        {
+                           state_tx =6; 
+                           }
+                         else 
+                         {   
+                            sendinit(); 
+                            total_file_size=file_size() ;             // determined the file size  
+                            state_rx =1;
+                            blue.printf("\n"); 
+                         }
+                        }
+                                     
+                    break;
                
                    
        case 2:   total_samples=0;   
@@ -70,99 +93,81 @@
                  myBleMsg->bt_msg=bt_file1;         //copy the contents to the structure
                  send();                                // send the initial string
                  blue.printf("\n");                       
-                 total_samples= myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp;
+                 total_samples= myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp;
                  
                  state_tx =0;
                  state_rx =2;
-                 k=0;
-                // mc.printf("samples=%d", total_samples);
+                 count_send=0;
                  total_file_read=file_size1();
-                 //mc.printf("fileread=%d",total_file_read);
-                 //mc.printf("pointer=%d\n",file_pointer_position);
                  break;                 
  
  
-       case 3:      counter=0;
-                 //mc.printf("kkkk=%d\n",k);     
-                 //mc.printf("samples=%d", total_samples);         // send the raw data
-                if(k<(total_samples/1500))
+       case 3:     counter=0;
+                if(count_send<(total_samples/no_of_samples))
                   {
-                    sd_read(v1,1500);      
-                     counter=j+1500;               //read 1500 samples
-                    send_data_str1(); 
-                   
-                    //mc.printf("file_start,j=%d %d\n",file_start,j);                    //send the strt of the string 
-                      for(file_start=j;file_start<(j+1500);file_start++)
+                     sd_read(v1,no_of_samples);      
+                     counter=j+no_of_samples;               //read 1500 samples
+                     send_data_str1(); 
+                     for(file_start=j;file_start<(j+no_of_samples);file_start++)
                          {     
                        
                              blue.printf("%08x", v1[file_start]);
-                           // counter++;
+                           
                          }
                          
                          
                           check_sum=checksum(v1);               // calculating checksum
-                         
-                            //  blue.printf("\n");
-                       // mc.printf("iiii,hjjjj=%d %d\n",file_start,j);   
-                    state_tx =0;
-                    state_rx =3;
-                    send_data_str2(check_sum);
+                          state_tx =0;
+                          state_rx =3;
+                          send_data_str2(check_sum);            // send the remaining part of the string
                   
                    }
                
                else 
                      {
-                        for(int l=0;l<1500;l++)
+                        for(int l=0;l<no_of_samples;l++)
                           {    
                             v1[l]=NULL;
                            }
-                            counter=(total_samples-(1500*k));
-                           send_data_str1();
-                           sd_read(v1,(total_samples-(1500*k)));
-                        for(file_start=j;file_start<(total_samples-(1500*k));file_start++)
+                            counter=(total_samples-( no_of_samples*count_send));
+                            send_data_str1();
+                            sd_read(v1,(total_samples-( no_of_samples*count_send)));
+                        for(file_start=j;file_start<(total_samples-( no_of_samples*count_send));file_start++)
                           {     
                              blue.printf("%08x", v1[file_start]);
-                             //counter++;
+                            
                            }
-                           
-                          // blue.printf("\n");
+                                                                   
                           state_tx=0;
                           state_rx=4;
                           send_data_str2(check_sum);
                     }   
                      
                        total_file_read=file_size1();
-                        blue.printf("\n"); 
-                   //   mc.printf("fileread=%d",total_file_read );
-                      //  mc.printf("pointer=%d\n",file_pointer_position);
-                break;        
+                        blue.printf("\n");  
+                  break;        
                
          
         case 4:   total_file_read=file_size1();
-                 // mc.printf("fileread=%d",total_file_read);
-                 if(total_file_read==total_file_size)
-                     {
+                 if(total_file_read==total_file_size)          // if the total file size is same as total file size
+                     {                                         // then there is no more data to send in that particular file
                     
                         state_rx=6;
                         sendlast();
                         blue.printf("\n"); 
-                        sd_close();
-                      //  mc.printf("exit");
+                      //  sd_close();
                      }
                 else   
                        {
                          state_rx=5;
-                         total_file_read=file_size1();
-                      //   mc.printf("fileread=%d",total_file_read );
-                        //mc.printf("pointer=%d\n",file_pointer_position);
-                         
+                         total_file_read=file_size1();        
                         }   
                        
    
-//t.stop();
+
 
          }
-//mc.printf("time=%d\n",t.read_ms());
+
    return state_rx;     
         
   }           
@@ -173,17 +178,17 @@
 uint8_t bt_receive (uint8_t state_rx)
 {
        
-static unsigned char state_tx =1;      //state for transmitting 
+ unsigned char state_tx =1;      //state for transmitting 
             
 Timer t;                               // timer 
 uint8_t ack_rx;                        // varibale to receive ack from the bluetooth 
-Point b;                               // structure copy                     
-myBleMsg=&b;
+//Point b;                               // structure copy                     
+//myBleMsg=&b;
 t.start();                             // timer start
-int x2=0;
-int x3=0;
-int x4=0;
-int x5=0;
+uint8_t sos_rx=0;              // sos received from app
+uint8_t eos_rx=0;              // eos received from app
+uint8_t chksum_rx=0;          // check sum received from app
+
 
      switch(state_rx)
          {
@@ -191,8 +196,8 @@
           case 1:                                       // wait for the ack
                       state_rx=0;
                       myBleMsg->proto=ACK;
-                    blue.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
-                //  mc.printf("%02x%02x",ack_rx, x2); 
+                      blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
+            
                    if (ack_rx==myBleMsg->proto)
                      {
                        
@@ -211,7 +216,7 @@
          case 2:      state_rx =0;                        //wait for the ack
                       myBleMsg->proto=ACK;
                 
-                       blue.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
+                    blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
                      if (ack_rx==myBleMsg->proto)
                         {
                 
@@ -225,21 +230,21 @@
                       
                         state_tx=2;
                         setpos1(file_pointer_position);
-                     //   mc.printf("pointer=%d\n",file_pointer_position);
+             
                         }
-            
+                    //blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
                     break;
 
                 
      case 3:       
                    state_rx =0;
                    myBleMsg->proto=ACK;
-                  blue.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
+                  blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
                  if (ack_rx==myBleMsg->proto)
                       {
                      
                         state_tx=3;
-                         k=k+1;
+                         count_send=count_send+1;
                        file_pointer_position=total_file_read;
                       }
                     
@@ -248,11 +253,7 @@
                       { 
                        
                         state_tx=3;
-                      //  mc.printf("j=%d\n",j);
-                        file_start=file_start-1500;
-                        //mc.printf("j=%d\n",j);
-                      //  
-                       // mc.printf("file_pointer_position=%d\n",file_pointer_position);
+                        file_start=file_start- no_of_samples;
                         setpos1(file_pointer_position);
                       }
                
@@ -261,42 +262,63 @@
      case 4:       
                    state_rx =0;
                    myBleMsg->proto=ACK;
-                    blue.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
+                    blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
                    if (ack_rx==myBleMsg->proto)
                       {
-                       // bt_r.printf("sent");
                         state_tx=4;
-                         k=k+1;
+                         count_send=count_send+1;
                         file_pointer_position=total_file_read;
                       }
                     
                   else 
                     
                       { 
-                        //bt_r.printf("no-ack");
+                       
                         state_tx=3;
-                        file_start=file_start-1500;
-                       // mc.printf("file_pointer_position=%d\n",file_pointer_position);
+                        file_start=file_start- no_of_samples;
                         setpos1(file_pointer_position);
                       } 
                    break;                
                  
       case 5:      state_tx=2;
-                   
-                   break;
-                   
-      case 6:      state_tx=0;
+                    break;
                    
-      
-                   mc.printf("out");
+      case 6:       state_rx =0;
+                   /*myBleMsg->proto=ACK;
+                    blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
+                   if (ack_rx==myBleMsg->proto)
+                      {*/
+                      if (bt_file_tosend!=current_file)     
+                            {      sd_close();
+                                // bt_file_tosend=bt_file_tosend+1;
+                                 // eprom_write(6,bt_file_tosend);
+                                 // sd_open_read(bt_file_tosend);
+                                 // total_file_size=file_size() ;  
+                                  state_tx=1;
+                                  
+                                  mc.printf("next file");
+                               }
+                         
+                          else
+                              {      
+                                 state_tx=0;
+                                 mc.printf("out");
+                             }  
+                               
+                   // }
+                    
+                 /* else 
+                    
+                      { 
+                       
+                        state_tx=4;
+                       
+                      } */
+                  
                    break;
-   
-   
-                
-//t.stop();
 
   }
-//bt_r.printf("time=%d\n",t.read_ms());
+
    return state_tx;          
   }           
    
@@ -306,7 +328,7 @@
  
  
              
-void send()
+void send()                                 // function to send the structure to bluetooth
 {
                 myBleMsg->start_of_string=sos;
                 myBleMsg->length_file=26;
@@ -336,7 +358,7 @@
     
     }
     
-void sendinit()
+void sendinit()                // function to send the initial string at the start f communication to bluetooth
 {              
 
                 uint8_t sum_init=0  ;               
@@ -356,7 +378,7 @@
     }
   
    
-uint8_t checksum_init()
+uint8_t checksum_init()          // function to calculate the checksum for the initial string
  {
   
   uint32_t sum=0;
@@ -366,7 +388,7 @@
   return chksum_init;
   }
 
-uint8_t checksum_last()
+uint8_t checksum_last()        // function to calculate the checksum for the last string
  {
   
   uint32_t sum=0;
@@ -376,7 +398,7 @@
   return chksum_last;
   }    
     
- void sendlast()
+ void sendlast()                 // function to send the last string to the bluetooth // which determines the end of file
 {
                 uint8_t sum_last=0;
                 myBleMsg->start_of_string=sos;
@@ -394,7 +416,7 @@
     
     }
        
- void send_data_str1()
+ void send_data_str1()       // function to send a part of the structure // after this raw data is sent//
    {
                  myBleMsg->start_of_string=sos;
                  myBleMsg->length_file=(counter*4);
@@ -407,7 +429,7 @@
     
     
     
- void send_data_str2(uint8_t check_sum)
+ void send_data_str2(uint8_t check_sum)          // after raw data// this last part of structure is sent
     {          
                 myBleMsg->chk_sum=check_sum;
                 myBleMsg->end_of_string=eos;  
@@ -415,13 +437,13 @@
                 blue.printf("%02x",myBleMsg->chk_sum);
     }
     
-uint8_t checksum(uint32_t  *v1)
+uint8_t checksum(uint32_t  *v1)          // this function to calculate the checksum for the raw data
 {
 
 uint32_t sum=0;
 uint8_t chksum_datatrfr=0;
  
- for (int i=0;i<1500;i++)
+ for (int i=0;i< no_of_samples;i++)
  {
      sum=sum+v1[i];
   }
@@ -435,7 +457,7 @@
 
 
 
-uint8_t checksum_struct()
+uint8_t checksum_struct()         // calculating checksum for sending the file structure
 {
 
  uint32_t sum=0;
--- a/sdcard.cpp	Wed Jul 05 15:36:58 2017 +0000
+++ b/sdcard.cpp	Sat Jul 08 04:19:45 2017 +0000
@@ -127,7 +127,7 @@
  }
  
  
- void sd_open_read(int32_t pid)          // opening the file for read
+uint8_t sd_open_read(int32_t pid)          // opening the file for read    // CHNAGED to return value
  {
      
   char buffer1[32];
@@ -136,11 +136,13 @@
   if (fp==NULL)
   {
       snd.printf("no");
+      return 0;
       }
       
       else 
       {
           snd.printf("yes");
+          return 1;
           }
  }
  
--- a/sdcard.h	Wed Jul 05 15:36:58 2017 +0000
+++ b/sdcard.h	Sat Jul 08 04:19:45 2017 +0000
@@ -16,7 +16,7 @@
 int file_size() ;
 int file_size1() ;
 void sd_read(uint32_t  *v1, int size);
-void sd_open_read(int32_t pid);
+uint8_t sd_open_read(int32_t pid);
 void sd_read1(BLEMsg_info &f);
 void setpos();
 void setpos1(uint32_t position);