Modification into 17th July New display demo code

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of Ext_Demo_17July2017_newdis by SenseSemi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bt.cpp Source File

bt.cpp

00001 
00002 #include "mbed.h"
00003 #include "sdcard.h"
00004 #include "struct.h"
00005 #define ACK 0x00                 /// changed //16/06 #define all the values removing structure 
00006 #define start_of_file 0x01
00007 #define end_of_file 0x02
00008 #define data_trfr 0x03
00009 #define NACK 0xFF
00010 #define sos 0xc0
00011 #define eos 0xc0
00012 #define no_of_samples 150
00013 #include "eeprom_pgm.h"
00014 //#define len 6000
00015 
00016 BLEMsg_info *bt_file;
00017 Point *myBleMsg;
00018 
00019 Serial mc(USBTX,USBRX);                  // serial terminal
00020 Serial blue(PTC4,PTC3);                 // communicating bluetooth through serial 
00021                  
00022 static uint32_t total_samples=0;              // variable to hold total number of samples
00023 uint8_t ack_rx;                        // varibale to receive ack from the bluetooth 
00024 Point b;                               // structure copy
00025 BLEMsg_info bt_file1;                  // structure copy
00026 static uint32_t total_file_size=0;        // to determine the size of the file 
00027 static uint32_t total_file_read=0; 
00028 static uint32_t file_start=0;           // variable for 'for loop'
00029 static uint32_t j=0;
00030 uint8_t count_send=0; 
00031 static uint32_t file_pointer_position=0;
00032 static int32_t bt_file_tosend=0;           // file to send on bluetooth
00033 static int32_t current_file=0;            // file of the test done for the current pid 
00034 static uint32_t counter=0;
00035 uint8_t file=0;
00036 static  uint32_t v1[no_of_samples];  
00037 
00038 uint8_t bt_send(uint8_t state_tx)
00039 {
00040    
00041   blue.baud(115200);                  // baud rate of bluetooth
00042   bt_file=&bt_file1;                      
00043   myBleMsg=&b; 
00044   int8_t check_sum=0;
00045   uint8_t state_rx =0;
00046  
00047   
00048 
00049   
00050 
00051     switch(state_tx)
00052           {
00053  
00054        case 1:    
00055                   //blue.printf("\n");
00056                    state_tx =0; 
00057                   counter=0;
00058                   total_file_read=0; 
00059                   file_start=0;          
00060                   j=0; count_send=0;  total_samples=0;  
00061                   file_pointer_position=0;
00062                   bt_file_tosend=eprom_read(6);            // reading the file number(pid) from eeprom which has to be sent to bluetooth
00063                   current_file=eprom_read(1);  
00064                  // current_file=2;
00065                   //bt_file_tosend=bt_file_tosend+1;
00066                   //eprom_write(6,bt_file_tosend);            // read the current pid 
00067                   mc.printf("eeprom file=%d",bt_file_tosend);  
00068                   mc.printf("currentfile=%d",current_file);  
00069                   if (bt_file_tosend>current_file)       // if the file sent to bluetooth and current file are same,
00070                   {                                       // then there is no new file to send
00071                       state_rx =6;
00072                       }
00073                   else
00074                     {
00075                        file=sd_open_read(bt_file_tosend);
00076                        if(file==0)
00077                         {
00078                            state_rx =6; 
00079                            }
00080                          else 
00081                          {   
00082                             sendinit(); 
00083                             total_file_size=file_size() ;             // determined the file size  
00084                             state_rx =1;
00085                             blue.printf("\n"); 
00086                          }
00087                         }
00088                                      
00089                     break;
00090                
00091                    
00092        case 2:   total_samples=0;   
00093                  sd_read1(bt_file1);               // read the sd card file
00094                  myBleMsg->bt_msg=bt_file1;         //copy the contents to the structure
00095                  send();                                // send the initial string
00096                  blue.printf("\n");                       
00097                  total_samples= myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp;
00098                  
00099                  state_tx =0;
00100                  state_rx =2;
00101                  count_send=0;
00102                  total_file_read=file_size1();
00103                  break;                 
00104  
00105  
00106        case 3:     counter=0;
00107                 if(count_send<(total_samples/no_of_samples))
00108                   {
00109                      sd_read(v1,no_of_samples);      
00110                      counter=j+no_of_samples;               //read 1500 samples
00111                      send_data_str1(); 
00112                      for(file_start=j;file_start<(j+no_of_samples);file_start++)
00113                          {     
00114                        
00115                              blue.printf("%08x", v1[file_start]);
00116                            
00117                          }
00118                          
00119                          
00120                           check_sum=checksum((uint8_t *)v1,counter);               // calculating checksum            // calculating checksum
00121                           state_tx =0;
00122                           state_rx =3;
00123                           send_data_str2(check_sum);            // send the remaining part of the string
00124                   
00125                    }
00126                
00127                else 
00128                      {
00129                         for(int l=0;l<no_of_samples;l++)
00130                           {    
00131                             v1[l]=NULL;
00132                            }
00133                             counter=(total_samples-( no_of_samples*count_send));
00134                             send_data_str1();
00135                             sd_read(v1,(total_samples-( no_of_samples*count_send)));
00136                         for(file_start=j;file_start<(total_samples-( no_of_samples*count_send));file_start++)
00137                           {     
00138                              blue.printf("%08x", v1[file_start]);
00139                             
00140                            }
00141                                                                    
00142                           state_tx=0;
00143                           state_rx=4;
00144                           check_sum=checksum((uint8_t *)v1,counter);               // calculating checksum
00145                           send_data_str2(check_sum);
00146                     }   
00147                      
00148                        total_file_read=file_size1();
00149                         blue.printf("\n");  
00150                   break;        
00151                
00152          
00153         case 4:   total_file_read=file_size1();
00154                  if(total_file_read==total_file_size)          // if the total file size is same as total file size
00155                      {                                         // then there is no more data to send in that particular file
00156                     
00157                         state_rx=6;
00158                         sendlast();
00159                         blue.printf("\n"); 
00160                       //  sd_close();
00161                      }
00162                 else   
00163                        {
00164                          state_rx=5;
00165                          total_file_read=file_size1();        
00166                         }   
00167                        
00168    
00169 
00170 
00171          }
00172 
00173    return state_rx;     
00174         
00175   }           
00176  
00177  
00178  
00179  
00180 uint8_t bt_receive (uint8_t state_rx)
00181 {
00182        
00183  unsigned char state_tx =1;      //state for transmitting 
00184             
00185 Timer t;                               // timer 
00186 uint8_t ack_rx;                        // varibale to receive ack from the bluetooth 
00187 //Point b;                               // structure copy                     
00188 //myBleMsg=&b;
00189 t.start();                             // timer start
00190 uint8_t sos_rx=0;              // sos received from app
00191 uint8_t eos_rx=0;              // eos received from app
00192 uint8_t chksum_rx=0;          // check sum received from app
00193 
00194 
00195      switch(state_rx)
00196          {
00197                 
00198           case 1:                                       // wait for the ack
00199                       state_rx=0;
00200                       myBleMsg->proto=ACK;
00201                       blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
00202             
00203                    if (ack_rx==myBleMsg->proto)
00204                      {
00205                        
00206                        state_tx=2;
00207                       }
00208                     
00209                     else 
00210                     
00211                       { 
00212                        
00213                         state_tx=1;
00214                         sd_close();
00215                       }
00216                    break;                 
00217  
00218          case 2:      state_rx =0;                        //wait for the ack
00219                       myBleMsg->proto=ACK;
00220                 
00221                     blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
00222                      if (ack_rx==myBleMsg->proto)
00223                         {
00224                 
00225                           state_tx=3;
00226                           file_pointer_position=total_file_read;
00227                         }
00228                     
00229                     else 
00230                     
00231                         { 
00232                       
00233                         state_tx=2;
00234                         setpos1(file_pointer_position);
00235              
00236                         }
00237                     //blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
00238                     break;
00239 
00240                 
00241      case 3:       
00242                    state_rx =0;
00243                    myBleMsg->proto=ACK;
00244                   blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
00245                  if (ack_rx==myBleMsg->proto)
00246                       {
00247                      
00248                         state_tx=3;
00249                          count_send=count_send+1;
00250                        file_pointer_position=total_file_read;
00251                       }
00252                     
00253                   else 
00254                     
00255                       { 
00256                        
00257                         state_tx=3;
00258                         file_start=file_start- no_of_samples;
00259                         setpos1(file_pointer_position);
00260                       }
00261                
00262                    break;    
00263                                             
00264      case 4:       
00265                    state_rx =0;
00266                    myBleMsg->proto=ACK;
00267                     blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
00268                    if (ack_rx==myBleMsg->proto)
00269                       {
00270                         state_tx=4;
00271                          count_send=count_send+1;
00272                         file_pointer_position=total_file_read;
00273                       }
00274                     
00275                   else 
00276                     
00277                       { 
00278                        
00279                         state_tx=3;
00280                         file_start=file_start- no_of_samples;
00281                         setpos1(file_pointer_position);
00282                       } 
00283                    break;                
00284                  
00285       case 5:      state_tx=2;
00286                     break;
00287                    
00288       case 6:       state_rx =0;
00289                   bt_file_tosend=bt_file_tosend+1;
00290                   eprom_write(6,bt_file_tosend);     
00291                    mc.printf("next file");       
00292                    /*myBleMsg->proto=ACK;
00293                     blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
00294                    if (ack_rx==myBleMsg->proto)
00295                       {*/
00296                       if (bt_file_tosend<=current_file)     
00297                             {   
00298                                 if (file==1)
00299                                    {
00300                                          sd_close();
00301                                       }  
00302               
00303                                       
00304                                   state_tx=1;
00305                                   
00306                                   mc.printf("next file");
00307                                }
00308                          
00309                           else
00310                               {      
00311                                  state_tx=0;
00312                                  mc.printf("out");
00313                              }  
00314                                
00315                    // }
00316                     
00317                  /* else 
00318                     
00319                       { 
00320                        
00321                         state_tx=4;
00322                        
00323                       } */
00324                   
00325                    break;
00326 
00327   }
00328 
00329    return state_tx;          
00330   }           
00331    
00332  
00333  
00334  
00335  
00336  
00337              
00338 void send()                                 // function to send the structure to bluetooth
00339 {
00340                 myBleMsg->start_of_string=sos;
00341                 myBleMsg->length_file=26;
00342                 myBleMsg->proto=data_trfr;                      // data transfer mode
00343                 blue.printf("%02x",myBleMsg->start_of_string); 
00344                 blue.printf("%02x",myBleMsg->proto);
00345                 blue.printf("%08x",myBleMsg->length_file);      //changed the sequence 
00346                 blue.printf("%08x",myBleMsg->bt_msg.length); 
00347                 blue.printf("%02x",myBleMsg->bt_msg.device_id); 
00348                 blue.printf("%08x",myBleMsg->bt_msg.patient_id);    
00349                 blue.printf("%02x",myBleMsg->bt_msg.date_time.date);     
00350                 blue.printf("%02x",myBleMsg->bt_msg.date_time.month);  
00351                 blue.printf("%02x",myBleMsg->bt_msg.date_time.year);  
00352                 blue.printf("%02x",myBleMsg->bt_msg.date_time.hour);      
00353                 blue.printf("%02x",myBleMsg->bt_msg.date_time.mins);  
00354                 blue.printf("%02x",myBleMsg->bt_msg.date_time.sec);  
00355                 blue.printf("%02x",myBleMsg->bt_msg.test_type);   
00356                 blue.printf("%04x",myBleMsg->bt_msg.sampling_freq); 
00357                 blue.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy); 
00358                 blue.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); 
00359                 blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy);   
00360                 blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp);   
00361                 myBleMsg->end_of_string=eos;  
00362                 myBleMsg->chk_sum = checksum_struct((uint8_t *)myBleMsg, sizeof(Point));
00363                 blue.printf("%02x",myBleMsg->end_of_string);
00364                 blue.printf("%02x",myBleMsg->chk_sum);
00365     
00366     }
00367     
00368 void sendinit()                // function to send the initial string at the start f communication to bluetooth
00369 {              
00370 
00371                 uint8_t sum_init=0  ;               
00372                 myBleMsg->start_of_string=sos;
00373                 myBleMsg->length_file=0;
00374                 myBleMsg->proto=start_of_file;
00375                //  myBleMsg->bt_msg=NULL;
00376                 sum_init=checksum_init();
00377                 myBleMsg->chk_sum=sum_init;
00378                 blue.printf("%02x",myBleMsg->start_of_string);
00379                 blue.printf("%02x",myBleMsg->proto); 
00380                 blue.printf("%08x",myBleMsg->length_file);     // chnged
00381                 myBleMsg->end_of_string=eos;  
00382                 blue.printf("%02x",myBleMsg->end_of_string);
00383                 blue.printf("%02x",myBleMsg->chk_sum);
00384     
00385     }
00386   
00387    
00388 uint8_t checksum_init()          // function to calculate the checksum for the initial string
00389  {
00390   
00391   uint32_t sum=0;
00392   uint8_t chksum_init=0;
00393   sum=sos+eos+start_of_file; 
00394   chksum_init=sum & 0x000000FF;
00395   return chksum_init;
00396   }
00397 
00398 uint8_t checksum_last()        // function to calculate the checksum for the last string
00399  {
00400   
00401   uint32_t sum=0;
00402   uint8_t chksum_last=0;
00403   sum=sos+eos+end_of_file; // changed //16/06 #define all the values removing structure 
00404   chksum_last=sum & 0x000000FF;
00405   return chksum_last;
00406   }    
00407     
00408  void sendlast()                 // function to send the last string to the bluetooth // which determines the end of file
00409 {
00410                 uint8_t sum_last=0;
00411                 myBleMsg->start_of_string=sos;
00412                 myBleMsg->length_file=0;
00413                 myBleMsg->proto=end_of_file;
00414                //  myBleMsg->bt_msg=NULL;
00415                 sum_last=checksum_last();
00416                 myBleMsg->chk_sum=sum_last;
00417                 blue.printf("%02x",myBleMsg->start_of_string);
00418                 blue.printf("%02x",myBleMsg->proto); 
00419                 blue.printf("%08x",myBleMsg->length_file);     // chnged
00420                 myBleMsg->end_of_string=eos;  
00421                 blue.printf("%02x",myBleMsg->end_of_string);
00422                 blue.printf("%02x",myBleMsg->chk_sum);
00423     
00424     }
00425        
00426  void send_data_str1()       // function to send a part of the structure // after this raw data is sent//
00427    {
00428                  myBleMsg->start_of_string=sos;
00429                  myBleMsg->length_file=(counter*4);
00430                  myBleMsg->proto=data_trfr;
00431                  blue.printf("%02x",myBleMsg->start_of_string); 
00432                  blue.printf("%02x",myBleMsg->proto);                   // chnaged
00433                  blue.printf("%08x",myBleMsg->length_file);
00434                
00435     }
00436     
00437     
00438     
00439  void send_data_str2(uint8_t check_sum)          // after raw data// this last part of structure is sent
00440     {          
00441                 myBleMsg->chk_sum=check_sum;
00442                 myBleMsg->end_of_string=eos;  
00443                 blue.printf("%02x",myBleMsg->end_of_string);
00444                 blue.printf("%02x",myBleMsg->chk_sum);
00445     }
00446     
00447 /*uint8_t checksum(uint32_t  *v1)          // this function to calculate the checksum for the raw data
00448 {
00449 
00450 uint32_t sum=0;
00451 uint8_t chksum_datatrfr=0;
00452  
00453  for (int i=0;i< no_of_samples;i++)
00454  {
00455      sum=sum+v1[i];
00456   }
00457   
00458   sum=sos+eos+sum+counter+data_trfr;
00459   chksum_datatrfr=sum & 0x000000FF;
00460   return chksum_datatrfr;
00461 }
00462 
00463 
00464 
00465 
00466 
00467 uint8_t checksum_struct()         // calculating checksum for sending the file structure
00468 {
00469 
00470  uint32_t sum=0;
00471  uint8_t chksum_struct=0;
00472  
00473   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;
00474   chksum_struct=sum & 0x000000FF;
00475   return chksum_struct;
00476 }*/
00477 
00478 uint8_t checksum_struct(const uint8_t data[], uint16_t numBytes)         // calculating checksum for 
00479 {
00480     uint32_t chksum_struct = 0;
00481     uint16_t index = 0;
00482     
00483     for (index=0U; index < (numBytes-1); index++)
00484     {
00485           chksum_struct += data[index];  
00486           //blue.printf("data=%02x\n",data[index]);
00487         
00488     }
00489   // blue.printf("%08x\n",chksum_struct);  
00490   return chksum_struct;
00491 }
00492 
00493 
00494 uint8_t checksum(const uint8_t data[], uint32_t numBytes)          // this function to calculate the checksum for the raw data
00495 {
00496 
00497 uint32_t sum=0;
00498 uint8_t  chksum_datatrfr=0;
00499 uint8_t  i = 0;
00500 uint32_t index = 0;
00501 uint32_t index1 = 0;
00502     sum += sos;
00503     sum += data_trfr;  
00504    index1=(numBytes*4);
00505    sum +=(index1>>(8*0))&0xFF;
00506     sum +=(index1>>(8*1))&0xFF; 
00507 
00508 for (index=0U; index<(numBytes*4); index++)
00509 { 
00510     sum += data[index];
00511 
00512 }
00513 
00514   sum+=eos;
00515 
00516   chksum_datatrfr = sum & 0x000000FF;
00517  mc.printf("chksum_datatrfr=%02x\n",chksum_datatrfr);
00518   return chksum_datatrfr;
00519 }
00520