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

bt.cpp

Committer:
nikitateggi
Date:
2017-06-24
Revision:
24:d992ee8369f4
Parent:
22:ffa88619551d
Child:
27:907f8ce0025e

File content as of revision 24:d992ee8369f4:

// added newline, changed the sequence of cmd and length, added send last function





#include "mbed.h"
#include "sdcard.h"
#include "struct.h"
#define ACK 0x00                 /// changed //16/06 #define all the values removing structure 
#define start_of_file 0x01
#define end_of_file 0x02
#define data_trfr 0x03
#define NACK 0xFF
#define sos 0xc0
#define eos 0xc0
//#define len 6000



BLEMsg_info *bt_file;
Point *myBleMsg;
//bt_st *as;
Serial mc(USBTX,USBRX);
Serial blue(PTC4,PTC3);
                 // buffer for data
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 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[1500];    
  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
  

    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 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 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;
                 
                 state_tx =0;
                 state_rx =2;
                 k=0;
                // mc.printf("samples=%d", total_samples);
                 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))
                  {
                    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++)
                         {     
                       
                             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);
                  
                   }
               
               else 
                     {
                        for(int l=0;l<1500;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++)
                          {     
                             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;        
               
         
        case 4:   total_file_read=file_size1();
                 // mc.printf("fileread=%d",total_file_read);
                 if(total_file_read==total_file_size)
                     {
                    
                        state_rx=6;
                        sendlast();
                        blue.printf("\n"); 
                        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
int x2=0;
int x3=0;
int x4=0;
int x5=0;

     switch(state_rx)
         {
                
          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); 
                   if (ack_rx==myBleMsg->proto)
                     {
                       
                       state_tx=2;
                      }
                    
                    else 
                    
                      { 
                       
                        state_tx=1;
                        sd_close();
                      }
                   break;                 
 
         case 2:      state_rx =0;                        //wait for the ack
                      myBleMsg->proto=ACK;
                
                       blue.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
                     if (ack_rx==myBleMsg->proto)
                        {
                
                          state_tx=3;
                          file_pointer_position=total_file_read;
                        }
                    
                    else 
                    
                        { 
                      
                        state_tx=2;
                        setpos1(file_pointer_position);
                     //   mc.printf("pointer=%d\n",file_pointer_position);
                        }
            
                    break;

                
     case 3:       
                   state_rx =0;
                   myBleMsg->proto=ACK;
                  blue.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
                 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-1500;
                        //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;
                    blue.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
                   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-1500;
                       // 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_of_string=sos;
                myBleMsg->length_file=26;
                myBleMsg->proto=data_trfr;                      // data transfer mode
                blue.printf("%02x",myBleMsg->start_of_string); 
                blue.printf("%02x",myBleMsg->proto);
                blue.printf("%08x",myBleMsg->length_file);      //changed the sequence 
                blue.printf("%08x",myBleMsg->bt_msg.length); 
                blue.printf("%02x",myBleMsg->bt_msg.device_id); 
                blue.printf("%08x",myBleMsg->bt_msg.patient_id);    
                blue.printf("%02x",myBleMsg->bt_msg.date_time.date);     
                blue.printf("%02x",myBleMsg->bt_msg.date_time.month);  
                blue.printf("%02x",myBleMsg->bt_msg.date_time.year);  
                blue.printf("%02x",myBleMsg->bt_msg.date_time.hour);      
                blue.printf("%02x",myBleMsg->bt_msg.date_time.mins);  
                blue.printf("%02x",myBleMsg->bt_msg.date_time.sec);  
                blue.printf("%02x",myBleMsg->bt_msg.test_type);   
                blue.printf("%04x",myBleMsg->bt_msg.sampling_freq); 
                blue.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy); 
                blue.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); 
                blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy);   
                blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp);   
                myBleMsg->end_of_string=eos;  
                myBleMsg->chk_sum=checksum_struct();
                blue.printf("%02x",myBleMsg->end_of_string);
                blue.printf("%02x",myBleMsg->chk_sum);
    
    }
    
void sendinit()
{              

                uint8_t sum_init=0  ;               
                myBleMsg->start_of_string=sos;
                myBleMsg->length_file=0;
                myBleMsg->proto=start_of_file;
               //  myBleMsg->bt_msg=NULL;
                sum_init=checksum_init();
                myBleMsg->chk_sum=sum_init;
                blue.printf("%02x",myBleMsg->start_of_string);
                blue.printf("%02x",myBleMsg->proto); 
                blue.printf("%08x",myBleMsg->length_file);     // chnged
                myBleMsg->end_of_string=eos;  
                blue.printf("%02x",myBleMsg->end_of_string);
                blue.printf("%02x",myBleMsg->chk_sum);
    
    }
  
   
uint8_t checksum_init()
 {
  
  uint32_t sum=0;
  uint8_t chksum_init=0;
  sum=sos+eos+start_of_file; 
  chksum_init=sum & 0x000000FF;
  return chksum_init;
  }

uint8_t checksum_last()
 {
  
  uint32_t sum=0;
  uint8_t chksum_last=0;
  sum=sos+eos+end_of_file; // changed //16/06 #define all the values removing structure 
  chksum_last=sum & 0x000000FF;
  return chksum_last;
  }    
    
 void sendlast()
{
                uint8_t sum_last=0;
                myBleMsg->start_of_string=sos;
                myBleMsg->length_file=0;
                myBleMsg->proto=end_of_file;
               //  myBleMsg->bt_msg=NULL;
                sum_last=checksum_last();
                myBleMsg->chk_sum=sum_last;
                blue.printf("%02x",myBleMsg->start_of_string);
                blue.printf("%02x",myBleMsg->proto); 
                blue.printf("%08x",myBleMsg->length_file);     // chnged
                myBleMsg->end_of_string=eos;  
                blue.printf("%02x",myBleMsg->end_of_string);
                blue.printf("%02x",myBleMsg->chk_sum);
    
    }
       
 void send_data_str1()
   {
                 myBleMsg->start_of_string=sos;
                 myBleMsg->length_file=(counter*4);
                 myBleMsg->proto=data_trfr;
                 blue.printf("%02x",myBleMsg->start_of_string); 
                 blue.printf("%02x",myBleMsg->proto);                   // chnaged
                 blue.printf("%08x",myBleMsg->length_file);
               
    }
    
    
    
 void send_data_str2(uint8_t check_sum)
    {          
                myBleMsg->chk_sum=check_sum;
                myBleMsg->end_of_string=eos;  
                blue.printf("%02x",myBleMsg->end_of_string);
                blue.printf("%02x",myBleMsg->chk_sum);
    }
    
uint8_t checksum(uint32_t  *v1)
{

uint32_t sum=0;
uint8_t chksum_datatrfr=0;
 
 for (int i=0;i<1500;i++)
 {
     sum=sum+v1[i];
  }
  
  sum=sos+eos+sum+counter+data_trfr;
  chksum_datatrfr=sum & 0x000000FF;
  return chksum_datatrfr;
}





uint8_t checksum_struct()
{

 uint32_t sum=0;
 uint8_t chksum_struct=0;
 
  sum=myBleMsg->start_of_string+myBleMsg->proto+myBleMsg->bt_msg.length+myBleMsg->bt_msg.device_id+myBleMsg->bt_msg.date_time.date+myBleMsg->bt_msg.date_time.month+myBleMsg->bt_msg.date_time.year+myBleMsg->bt_msg.date_time.hour+myBleMsg->bt_msg.date_time.mins+myBleMsg->bt_msg.date_time.sec+myBleMsg->bt_msg.test_type+myBleMsg->bt_msg.sampling_freq+myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp+myBleMsg->bt_msg.cal_data.cal_sbp_dummy+myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp+myBleMsg->end_of_string+myBleMsg->chk_sum;
  chksum_struct=sum & 0x000000FF;
  return chksum_struct;
}