Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217
Fork of merged_code2_20sept_2017_4th_oct_2017 by
bt.cpp
00001 #include "mbed.h" 00002 #include "sdcard.h" 00003 #include "struct.h" 00004 #include "eeprom_pgm.h" 00005 #include "main.h" 00006 00007 #define ACK 0x00 /// changed //16/06 #define all the values removing structure 00008 #define START_OF_FILE 0x01 00009 #define END_OF_FILE 0x02 00010 #define DATA_TRFR 0x03 00011 #define NACK 0xFF 00012 #define SOS 0xc0 00013 #define EOS 0xc0 00014 #define NO_OF_SAMPLES 600 //changed the buffer size from 150- 22/7/17- suhasini 00015 #define CHKSUM 0x80 00016 #define STRUCTURE_LENGTH 26 00017 00018 BLEMsg_info *bt_file; 00019 Point *myBleMsg; 00020 00021 Serial mc(USBTX,USBRX); // serial terminal 00022 Serial blue(PTC4,PTC3); // communicating bluetooth through serial 00023 00024 static uint32_t total_samples=0; // variable to hold total number of samples 00025 Point b; // structure copy 00026 BLEMsg_info bt_file1; // structure copy 00027 static uint32_t total_file_size=0; // to determine the size of the file 00028 static uint32_t total_file_size_sent=0; 00029 static uint32_t file_start=0; // variable for 'for loop' 00030 //static uint32_t j=0; 00031 uint8_t count_send=0; 00032 static uint32_t file_pointer_position=0; 00033 static uint32_t counter=0; 00034 static uint32_t v1[NO_OF_SAMPLES]; 00035 uint8_t file=0; 00036 static uint32_t bt_pid = 0; 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 switch(state_tx) 00048 { 00049 00050 case 1: 00051 state_tx =0; 00052 counter=0; 00053 total_file_size_sent=0; 00054 file_start=0; 00055 count_send=0; total_samples=0; 00056 file_pointer_position=0; 00057 bt_pid = get_btpid()+1; //added on 22/09/2017 nikita 00058 mc.printf("bt_pid = %d\n",get_btpid()); 00059 if(get_filepid()>=bt_pid) //check for pid and bluetooth pid by reading from file// modified to send till the current pid, 3rd nov// 00060 { 00061 send_initial_string(); 00062 blue.printf("\n"); 00063 file=sd_open_read(bt_pid); // open the file for reading 00064 total_file_size=file_size(); // determined the file size 00065 state_rx = 1; 00066 } 00067 00068 else 00069 { 00070 state_rx =0; //no files to send 00071 mc.printf("no file to send\n"); 00072 } 00073 break; 00074 00075 00076 case 2: total_samples=0; 00077 //file=sd_open_read(get_btpid()); 00078 // total_file_size=file_size() ; // determined the file size 00079 sd_read1(bt_file1); // read the sd card file 00080 mc.printf("sd file opened to read data in it\n"); 00081 myBleMsg->bt_msg=bt_file1; //copy the contents to the structure 00082 send_structure(); // send the structure string 00083 blue.printf("\n"); 00084 mc.printf("sent initial string\n"); 00085 total_samples= myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp; 00086 state_tx =0; 00087 state_rx =2; 00088 count_send=0; 00089 total_file_size_sent=file_size1(); 00090 break; 00091 00092 00093 case 3: 00094 counter=0; 00095 if(count_send<(total_samples/NO_OF_SAMPLES)) 00096 { 00097 sd_read(v1,NO_OF_SAMPLES); 00098 counter=NO_OF_SAMPLES; //read 1500 samples 00099 send_data_str1(); 00100 for(file_start=0;file_start<(NO_OF_SAMPLES);file_start++) 00101 { 00102 00103 blue.printf("%08x", v1[file_start]); 00104 00105 } 00106 00107 check_sum=checksum_rawdata((uint8_t *)v1,counter); // calculating checksum // calculating checksum 00108 state_tx =0; 00109 state_rx =3; 00110 send_data_str2(check_sum); // send the remaining part of the string 00111 00112 } 00113 00114 else 00115 { 00116 for(uint16_t l=0;l<NO_OF_SAMPLES;l++) 00117 { 00118 v1[l]=NULL; 00119 } 00120 counter=(total_samples-( NO_OF_SAMPLES*count_send)); 00121 send_data_str1(); 00122 sd_read(v1,(total_samples-( NO_OF_SAMPLES*count_send))); 00123 00124 for(file_start=0;file_start<(total_samples-( NO_OF_SAMPLES*count_send));file_start++) 00125 { 00126 blue.printf("%08x", v1[file_start]); 00127 00128 } 00129 00130 state_tx=0; 00131 state_rx=4; 00132 check_sum=checksum_rawdata((uint8_t *)v1,counter); // calculating checksum 00133 send_data_str2(check_sum); 00134 } 00135 00136 total_file_size_sent=file_size1(); 00137 blue.printf("\n"); 00138 break; 00139 00140 00141 case 4: // total_file_size_sent=file_size1(); 00142 if(total_file_size_sent==total_file_size) // if the total file size is same as total file size 00143 { // then there is no more data to send in that particular file 00144 00145 state_rx=5; 00146 send_last_string(); 00147 blue.printf("\n"); 00148 00149 } 00150 else 00151 { 00152 00153 total_samples=0; 00154 sd_read1(bt_file1); // read the sd card file 00155 // mc.printf("sd file opened to read data in it\n"); 00156 myBleMsg->bt_msg=bt_file1; //copy the contents to the structure 00157 send_structure(); // send the initial string 00158 blue.printf("\n"); 00159 // mc.printf("sent initial string\n"); 00160 total_samples= myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp; 00161 state_tx =0; 00162 state_rx =2; 00163 count_send=0; 00164 total_file_size_sent=file_size1(); 00165 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 uint8_t string_length_rx=0; 00185 char ack_rx[2]; // varibale to receive ack from the bluetooth 00186 char chksum_rx[3]; 00187 char btdata_received[17]; 00188 00189 00190 while (blue.readable()!= NULL) // reading string from bluetooth app 00191 { 00192 blue.scanf("%17s", btdata_received); 00193 mc.printf("%s\r\n", btdata_received); 00194 00195 } 00196 00197 string_length_rx = strlen(btdata_received); // storing the length of the string received 00198 memcpy(ack_rx, btdata_received+1, 2); // reading the ack received 00199 memcpy(chksum_rx, btdata_received+(string_length_rx-2), 3); // reading the checksum received 00200 mc.printf("%s\r\n",ack_rx); 00201 mc.printf("%s\r\n",chksum_rx); 00202 00203 switch(state_rx) 00204 { 00205 00206 case 1: // wait for the ack 00207 state_rx=0; 00208 myBleMsg->proto=ACK; 00209 if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL)) // check for ack and checksum 00210 00211 { 00212 00213 state_tx=2; 00214 } 00215 00216 else 00217 00218 { 00219 00220 state_tx=1; 00221 sd_close(); 00222 } 00223 break; 00224 00225 case 2: state_rx =0; //wait for the ack 00226 00227 if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL)) 00228 { 00229 00230 state_tx=3; 00231 file_pointer_position=total_file_size_sent; 00232 } 00233 00234 else 00235 00236 { 00237 00238 state_tx=2; 00239 setpos1(file_pointer_position); 00240 //sd_close(); //file closed which was opened earlier because it opens again in bt_send case 1 00241 00242 } 00243 //blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx); 00244 break; 00245 00246 00247 case 3: 00248 state_rx =0; 00249 if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL)) 00250 { 00251 00252 state_tx=3; 00253 count_send=count_send+1; 00254 file_pointer_position=total_file_size_sent; 00255 } 00256 00257 else 00258 00259 { 00260 00261 state_tx=3; 00262 file_start=file_start- NO_OF_SAMPLES; 00263 setpos1(file_pointer_position); 00264 } 00265 00266 break; 00267 00268 case 4: 00269 state_rx =0; 00270 if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL)) 00271 { 00272 state_tx=4; 00273 count_send=count_send+1; 00274 file_pointer_position=total_file_size_sent; 00275 } 00276 00277 else 00278 00279 { 00280 00281 state_tx=3; 00282 file_start=file_start- NO_OF_SAMPLES; 00283 setpos1(file_pointer_position); 00284 } 00285 break; 00286 00287 00288 case 5: state_rx = 0; 00289 if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL)) 00290 { 00291 sd_close(); 00292 increment_btpid(); // increment the bluetooth pid and write in the file 00293 mc.printf("next file"); 00294 mc.printf("bt_pid = %d\n",get_btpid()); 00295 state_tx=1; 00296 00297 } 00298 00299 else 00300 { 00301 state_tx = 4; 00302 00303 } 00304 break; 00305 00306 } 00307 00308 return state_tx; 00309 } 00310 00311 00312 00313 00314 00315 00316 00317 void send_structure() // function to send the structure to bluetooth 00318 { 00319 myBleMsg->start_of_string=SOS; 00320 myBleMsg->length_file = STRUCTURE_LENGTH; 00321 myBleMsg->proto=DATA_TRFR; // data transfer mode 00322 blue.printf("%02x",myBleMsg->start_of_string); 00323 blue.printf("%02x",myBleMsg->proto); 00324 blue.printf("%08x",myBleMsg->length_file); //changed the sequence 00325 blue.printf("%08x",myBleMsg->bt_msg.length); 00326 blue.printf("%02x",myBleMsg->bt_msg.device_id); 00327 blue.printf("%08x",myBleMsg->bt_msg.patient_id); 00328 blue.printf("%02x",myBleMsg->bt_msg.date_time.date); 00329 blue.printf("%02x",myBleMsg->bt_msg.date_time.month); 00330 blue.printf("%02x",myBleMsg->bt_msg.date_time.year); 00331 blue.printf("%02x",myBleMsg->bt_msg.date_time.hour); 00332 blue.printf("%02x",myBleMsg->bt_msg.date_time.mins); 00333 blue.printf("%02x",myBleMsg->bt_msg.date_time.sec); 00334 blue.printf("%02x",myBleMsg->bt_msg.test_type); 00335 blue.printf("%04x",myBleMsg->bt_msg.sampling_freq); 00336 blue.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy); 00337 blue.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); 00338 blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy); 00339 blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp); 00340 myBleMsg->end_of_string=EOS; 00341 myBleMsg->chk_sum = checksum_struct((uint8_t *)myBleMsg, sizeof(Point)); 00342 blue.printf("%02x",myBleMsg->end_of_string); 00343 blue.printf("%02x",myBleMsg->chk_sum); 00344 00345 00346 } 00347 00348 void send_initial_string() // function to send the initial string at the start f communication to bluetooth 00349 { 00350 00351 uint8_t sum_init=0 ; 00352 myBleMsg->start_of_string=SOS; 00353 myBleMsg->length_file=0; 00354 myBleMsg->proto=START_OF_FILE; 00355 sum_init=checksum_init(); 00356 myBleMsg->chk_sum=sum_init; 00357 blue.printf("%02x",myBleMsg->start_of_string); 00358 blue.printf("%02x",myBleMsg->proto); 00359 blue.printf("%08x",myBleMsg->length_file); // chnged 00360 myBleMsg->end_of_string=EOS; 00361 blue.printf("%02x",myBleMsg->end_of_string); 00362 blue.printf("%02x",myBleMsg->chk_sum); 00363 00364 } 00365 00366 00367 uint8_t checksum_init() // function to calculate the checksum for the initial string 00368 { 00369 00370 uint32_t sum = 0; 00371 uint8_t chksum_init = 0; 00372 sum=SOS+EOS+START_OF_FILE; 00373 chksum_init = sum & 0x000000FF; 00374 return chksum_init; 00375 } 00376 00377 uint8_t checksum_last() // function to calculate the checksum for the last string 00378 { 00379 00380 uint32_t sum = 0; 00381 uint8_t chksum_last = 0; 00382 sum=SOS+EOS+END_OF_FILE; // changed //16/06 #define all the values removing structure 00383 chksum_last=sum & 0x000000FF; 00384 return chksum_last; 00385 } 00386 00387 void send_last_string() // function to send the last string to the bluetooth // which determines the end of file 00388 { 00389 uint8_t sum_last=0; 00390 myBleMsg->start_of_string = SOS; 00391 myBleMsg->length_file = 0; 00392 myBleMsg->proto=END_OF_FILE; 00393 sum_last=checksum_last(); 00394 myBleMsg->chk_sum=sum_last; 00395 blue.printf("%02x",myBleMsg->start_of_string); 00396 blue.printf("%02x",myBleMsg->proto); 00397 blue.printf("%08x",myBleMsg->length_file); // chnged 00398 myBleMsg->end_of_string=EOS; 00399 blue.printf("%02x",myBleMsg->end_of_string); 00400 blue.printf("%02x",myBleMsg->chk_sum); 00401 00402 } 00403 00404 void send_data_str1() // function to send a part of the structure // after this raw data is sent// 00405 { 00406 myBleMsg->start_of_string=SOS; 00407 myBleMsg->length_file=(counter*4); 00408 myBleMsg->proto=DATA_TRFR; 00409 blue.printf("%02x",myBleMsg->start_of_string); 00410 blue.printf("%02x",myBleMsg->proto); // chnaged 00411 blue.printf("%08x",myBleMsg->length_file); 00412 00413 } 00414 00415 00416 00417 void send_data_str2(uint8_t check_sum) // after raw data// this last part of structure is sent 00418 { 00419 myBleMsg->chk_sum=check_sum; 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 00427 uint8_t checksum_struct(const uint8_t data[], uint16_t numBytes) // calculating checksum for 00428 { 00429 uint32_t chksum_struct = 0; 00430 uint16_t index = 0; 00431 00432 for (index=0U; index < (numBytes-1); index++) 00433 { 00434 chksum_struct += data[index]; 00435 //blue.printf("data=%02x\n",data[index]); 00436 00437 } 00438 // blue.printf("%08x\n",chksum_struct); 00439 return chksum_struct; 00440 } 00441 00442 00443 uint8_t checksum_rawdata(const uint8_t data[], uint32_t numBytes) // this function to calculate the checksum for the raw data 00444 { 00445 00446 uint32_t sum=0; 00447 uint8_t chksum_datatrfr=0; 00448 uint32_t index = 0; 00449 uint32_t index1 = 0; 00450 sum += SOS; 00451 sum += DATA_TRFR; 00452 index1=(numBytes*4); 00453 sum +=(index1>>(8*0))&0xFF; 00454 sum +=(index1>>(8*1))&0xFF; 00455 00456 for (index=0U; index<(numBytes*4); index++) 00457 { 00458 sum += data[index]; 00459 } 00460 00461 sum+=EOS; 00462 00463 chksum_datatrfr = sum & 0x000000FF; 00464 //mc.printf("chksum_datatrfr=%02x\n",chksum_datatrfr); 00465 return chksum_datatrfr; 00466 } 00467 00468
Generated on Thu Jul 14 2022 21:53:47 by
 1.7.2
 1.7.2 
    