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

Revision:
19:1650bbc923cc
Child:
22:ffa88619551d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bt.cpp	Fri Jun 09 17:12:21 2017 +0000
@@ -0,0 +1,341 @@
+#include "mbed.h"
+#include "sdcard.h"
+#include "struct.h"
+
+BLEMsg_info *bt_file;              //pointer to structure 
+Point *myBleMsg;                   // pointer to bluetooth structure
+//bt_st *as;
+Serial mc(USBTX,USBRX);            // serial terminal
+Serial blue(PTC4,PTC3);            // bluetooth serial terminal
+                  
+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 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; 
+uint32_t file_pointer_position=0;
+
+
+
+
+uint8_t bt_send(uint8_t state_tx)
+{
+  uint32_t v1[150]; 
+  blue.baud(115200);                  // baud rate of bluetooth
+  bt_file=&bt_file1;                      
+  myBleMsg=&b; 
+  static  uint8_t state_rx =0;
+//t.start();                             // timer start
+  
+
+    switch(state_tx)
+          {
+ 
+       case 1:   sendinit();  
+                 sd_open_read(1);                       // open the file for read
+                 total_file_size=file_size() ;             // determined the file size  
+                // mc.printf("filesize=%d",total_file_size);              
+                 state_rx =1;
+                 state_tx =0;                
+                 break;
+               
+                   
+       case 2:   total_samples=0;   
+                 sd_read1(bt_file1);               // read the sd card file
+                 myBleMsg->bt_msg=bt_file1;         //copy the contents to the structure
+                 send();                             // send the structure
+                 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);
+                 total_file_read=file_size1();         // read the file size
+                 //mc.printf("fileread=%d",total_file_read);
+                 //mc.printf("pointer=%d\n",file_pointer_position);
+                 break;                 
+ 
+ 
+       case 3:      
+                 //mc.printf("kkkk=%d\n",k);     
+                 //mc.printf("samples=%d", total_samples);            // send the raw data
+                if(k<(total_samples/150))
+                  {
+                    sd_read(v1,150);                                  //read 1500 samples
+                    send_data_str1();                                 // send the initial structure
+                    //mc.printf("file_start,j=%d %d\n",file_start,j);                     
+                      for(file_start=j;file_start<(j+150);file_start++)
+                         {     
+                       
+                             blue.printf("%08x", v1[file_start]);
+                            
+                         }
+                            //  blue.printf("\n");
+                       // mc.printf("iiii,hjjjj=%d %d\n",file_start,j);   
+                    state_tx =0;
+                    state_rx =3;
+                    send_data_str2();
+                  
+                   }
+               
+               else 
+                     {
+                        for(int l=0;l<150;l++)
+                          {    
+                            v1[l]=NULL;
+                           }
+                        
+                           send_data_str1();
+                           sd_read(v1,(total_samples-(150*k)));
+                        for(file_start=j;file_start<(total_samples-(150*k));file_start++)
+                          {     
+                             blue.printf("%08x", v1[file_start]);
+                           }
+                           
+                          // blue.printf("\n");
+                          state_tx=0;
+                          state_rx=4;
+                          send_data_str2();
+                    }   
+                     
+                       total_file_read=file_size1();
+                   //   mc.printf("fileread=%d",total_file_read );
+                      //  mc.printf("pointer=%d\n",file_pointer_position);
+                break;        
+               
+         
+        case 4:   total_file_read=file_size1();
+                  mc.printf("fileread=%d",total_file_read);
+                 if(total_file_read==total_file_size)
+                     {
+                    
+                        state_rx=6;
+                        sd_close();
+                      //  mc.printf("exit");
+                     }
+                else   
+                       {
+                         state_rx=5;
+                         total_file_read=file_size1();
+                      //   mc.printf("fileread=%d",total_file_read );
+                        //mc.printf("pointer=%d\n",file_pointer_position);
+                         
+                        }   
+                       
+   
+//t.stop();
+
+         }
+//mc.printf("time=%d\n",t.read_ms());
+   return state_rx;     
+        
+  }           
+ 
+ 
+ 
+ 
+uint8_t bt_receive (uint8_t state_rx)
+{
+       
+static 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;
+t.start();                             // timer start
+
+     switch(state_rx)
+         {
+                
+          case 1:                                       // wait for the ack
+                      state_rx=0; 
+                      myBleMsg->proto=ACK;
+                      scanf("%02x",&ack_rx);            // read the ack/nack from serial terminal
+                      mc.printf("%02x",ack_rx);         
+                   if (ack_rx==myBleMsg->proto)         // check for ack
+                     {
+                       
+                       state_tx=2;
+                      }
+                    
+                    else                                // its a nack
+                    
+                      { 
+                       
+                        state_tx=1;
+                        sd_close();
+                      }
+                   break;                 
+ 
+         case 2:      state_rx =0;                        //wait for the ack
+                      myBleMsg->proto=ACK;
+                
+                      scanf("%02x", &ack_rx);
+                     if (ack_rx==myBleMsg->proto)
+                        {
+                
+                          state_tx=3;
+                          file_pointer_position=total_file_read;
+                        }
+                    
+                    else 
+                    
+                        { 
+                      
+                        state_tx=2;
+                        setpos1(file_pointer_position);          // set the file pointer to send the previous data
+                     //   mc.printf("pointer=%d\n",file_pointer_position);
+                        }
+            
+                    break;
+
+                
+     case 3:       
+                   state_rx =0;
+                   myBleMsg->proto=ACK;
+                   scanf("%02x", &ack_rx);
+                 if (ack_rx==myBleMsg->proto)
+                      {
+                     
+                        state_tx=3;
+                         k=k+1;
+                       file_pointer_position=total_file_read;
+                      }
+                    
+                  else 
+                    
+                      { 
+                       
+                        state_tx=3;
+                      //  mc.printf("j=%d\n",j);
+                        file_start=file_start-150;
+                        //mc.printf("j=%d\n",j); 
+                       // mc.printf("file_pointer_position=%d\n",file_pointer_position);
+                        setpos1(file_pointer_position);
+                      }
+               
+                   break;    
+                                            
+     case 4:       
+                   state_rx =0;
+                   myBleMsg->proto=ACK;
+                   scanf("%02x", &ack_rx);
+                   if (ack_rx==myBleMsg->proto)
+                      {
+                       // bt_r.printf("sent");
+                        state_tx=4;
+                         k=k+1;
+                        file_pointer_position=total_file_read;
+                      }
+                    
+                  else 
+                    
+                      { 
+                        //bt_r.printf("no-ack");
+                        state_tx=3;
+                        file_start=file_start-150;
+                        mc.printf("file_pointer_position=%d\n",file_pointer_position);
+                        setpos1(file_pointer_position);
+                      } 
+                   break;                
+                 
+      case 5:      state_tx=2;
+                   break;
+                   
+      case 6:      state_tx=0;
+                   mc.printf("out");
+                   break;
+   
+   
+                
+//t.stop();
+
+  }
+//bt_r.printf("time=%d\n",t.read_ms());
+   return state_tx;          
+  }           
+   
+ 
+ 
+ 
+ 
+ 
+             
+void send()
+{
+                myBleMsg->start_end_of_string=sos;
+                myBleMsg->length_file=28;
+                myBleMsg->proto=start_of_file;
+                myBleMsg->chk_sum=32;
+                mc.printf("%02x",myBleMsg->start_end_of_string); 
+                mc.printf("%08x",myBleMsg->length_file);
+                mc.printf("%02x",myBleMsg->proto);
+                mc.printf("%08x",myBleMsg->bt_msg.length); 
+                mc.printf("%02x",myBleMsg->bt_msg.device_id); 
+                mc.printf("%08x",myBleMsg->bt_msg.patient_id);    
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.date);     
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.month);  
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.year);  
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.hour);      
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.mins);  
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.sec);  
+                mc.printf("%02x",myBleMsg->bt_msg.test_type);   
+                mc.printf("%04x",myBleMsg->bt_msg.sampling_freq); 
+                mc.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy); 
+                mc.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); 
+                mc.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy);   
+                mc.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp);   
+                myBleMsg->start_end_of_string=eos;  
+                mc.printf("%02x",myBleMsg->start_end_of_string);
+                mc.printf("%02x",myBleMsg->chk_sum);
+    
+    }
+    
+void sendinit()
+{
+                myBleMsg->start_end_of_string=sos;
+                myBleMsg->length_file=3;
+                myBleMsg->proto=start_of_file;
+               //  myBleMsg->bt_msg=NULL;
+                myBleMsg->chk_sum=3;
+                mc.printf("%02x",myBleMsg->start_end_of_string); 
+                mc.printf("%08x",myBleMsg->length_file);
+                mc.printf("%02x",myBleMsg->proto);
+                myBleMsg->start_end_of_string=eos;  
+                mc.printf("%02x",myBleMsg->start_end_of_string);
+                mc.printf("%02x",myBleMsg->chk_sum);
+    
+    }
+    
+    
+    
+ void send_data_str1()
+   {
+                 myBleMsg->start_end_of_string=sos;
+                 myBleMsg->length_file=3;
+                 myBleMsg->proto=start_of_file;
+                 mc.printf("%02x",myBleMsg->start_end_of_string); 
+                 mc.printf("%08x",myBleMsg->length_file);
+                 mc.printf("%02x",myBleMsg->proto);
+    }
+    
+    
+    
+ void send_data_str2()
+    {          
+                myBleMsg->chk_sum=3;
+                myBleMsg->start_end_of_string=eos;  
+                mc.printf("%02x",myBleMsg->start_end_of_string);
+                mc.printf("%02x",myBleMsg->chk_sum);
+    }
+    
+
+
+