Modification into 17th July New display demo code
Dependencies: SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217
Fork of Ext_Demo_17July2017_newdis by
sdcard.cpp
00001 // Exisiting code in Int_Demo_09May2017_suhasini/sdcard.cpp is deleted and replaced by new code 00002 //Nidhin 1/6/2017 00003 00004 00005 #include "mbed.h" 00006 #include <stdio.h> 00007 #include "SDFileSystem.h" 00008 #include "sdcard.h" 00009 #include "rtc.h" 00010 #include "struct.h" 00011 00012 Serial snd(USBTX,USBRX); 00013 SDFileSystem sd(PTE1, PTE3, PTE2, PTE4, "sd"); 00014 00015 00016 00017 FILE *fpecg; // PPG and ECG File pointers for BP 00018 FILE *fpppg; //"" "" 00019 00020 FILE *fpmain; // FILE pointer for main file 00021 00022 FILE *fpbg; // FILE pointer to BG file 00023 FILE *fpeecg; // FILE pointer to ECG file 00024 FILE *fpbp; // FILE pointer to BP file 00025 00026 FILE *fp; // FILE pointer for random use 00027 00028 00029 00030 int ecgloc = 0; // PPG and eCG maximum locations 00031 int ppgloc = 0; 00032 00033 int ebp = 0; // variable for LOOP operation to read value from File i == ECG , j == PPG 00034 int pbp = 0; 00035 00036 //Function Added on 16/5/2017 by Nidhin 00037 void sd_open_mainfile(int32_t pid) 00038 { 00039 char buffer1[32]; 00040 sprintf(buffer1,"/sd/%d.csv",pid); 00041 fp = fopen(buffer1,"a"); 00042 } 00043 //------------------------------------------ 00044 00045 00046 00047 /*void sd_open_GLCfile(int pid) // opening the glc file 00048 { 00049 00050 char buffer[32]; 00051 char buffer1[32]; 00052 time_t epoch_time1; 00053 epoch_time1=rtc_read(); 00054 // strftime(buffer, 32, "GLC_%s", pid); 00055 00056 sprintf(buffer1,"/sd/%d_GLC.csv",pid); 00057 fp = fopen(buffer1,"a"); 00058 fprintf(fp,"%s\n",ctime(&epoch_time1) ); //writing the date, time into the file 00059 00060 00061 } */ 00062 00063 void sd_open_GLCfilee(int32_t pid) // only opening the glc file after entering TEST screen 00064 { 00065 char buffer1[32]; 00066 sprintf(buffer1,"/sd/%d_GLC.csv",pid); 00067 fpbg = fopen(buffer1,"a"); 00068 fclose(fpbg); 00069 } 00070 00071 00072 00073 /*void sd_open_ECGfile(int pid) 00074 { 00075 00076 // char buffer[32]; 00077 char buffer1[32]; 00078 time_t epoch_time1; 00079 epoch_time1=rtc_read(); 00080 // strftime(buffer, 32, "ECG_%d", pid); 00081 00082 sprintf(buffer1,"/sd/%d_ECG.csv",pid); 00083 fp = fopen(buffer1,"a"); 00084 fprintf(fp,"%s\n",ctime(&epoch_time1) ); 00085 00086 } 00087 */ 00088 void sd_open_ECGfilee(int32_t pid) // only opening the ECG file after entering TEST screen 00089 { 00090 00091 char buffer1[32]; 00092 sprintf(buffer1,"/sd/%d_ECG.csv",pid); 00093 fpeecg = fopen(buffer1,"a"); 00094 //fclose(fpeecg); 00095 00096 } 00097 00098 00099 /*void sd_open_BPfile(int pid) 00100 { 00101 00102 char buffer[32]; 00103 char buffer1[32]; 00104 time_t epoch_time1; 00105 epoch_time1=rtc_read(); 00106 // strftime(buffer, 32, "ECG_%d", pid); 00107 00108 sprintf(buffer1,"/sd/%d_BP.csv",pid); 00109 fp = fopen(buffer1,"a"); 00110 fprintf(fp,"%s\n",ctime(&epoch_time1) ); 00111 } */ 00112 00113 void sd_open_BPfilee(int32_t pid) // only opening the BP file after entering TEST screen 00114 { 00115 00116 00117 char buffer1[32]; 00118 sprintf(buffer1,"/sd/%d_BP.csv",pid); 00119 fpbp = fopen(buffer1,"a"); 00120 //fprintf(fp,"PATIENT ID %d\n",pid ); Not required in new code as writing in hex 00121 fclose(fpbp); 00122 } 00123 00124 void sd_write (int value) // writing data into the file 00125 { 00126 fprintf(fp,"%d\n", value); 00127 } 00128 00129 00130 uint8_t sd_open_read(int32_t pid) // opening the file for read // CHNAGED to return value 00131 { 00132 00133 char buffer1[32]; 00134 sprintf(buffer1,"/sd/%d.csv",pid); 00135 fp = fopen(buffer1,"r"); 00136 if (fp==NULL) 00137 { 00138 snd.printf("no"); 00139 return 0; 00140 } 00141 00142 else 00143 { 00144 snd.printf("yes"); 00145 return 1; 00146 } 00147 } 00148 00149 void sd_read(uint32_t *v1, int size) //reading the data from the openend file 00150 { 00151 fread(v1,sizeof(v1),size,fp); 00152 } 00153 00154 void setpos() 00155 { 00156 //fseek ( fp , 1 , SEEK_SET ); 00157 rewind (fp); 00158 } 00159 00160 void setpos1(uint32_t position) 00161 { 00162 fseek ( fp , position, SEEK_SET ); 00163 //rewind (fp); 00164 } 00165 00166 00167 int file_size() 00168 { 00169 int file_length; 00170 fseek (fp, 0, SEEK_END); 00171 file_length=ftell(fp); 00172 file_length=sizeof(char)*file_length; 00173 rewind(fp); 00174 return file_length; 00175 } 00176 00177 00178 int file_size1() 00179 { 00180 int file_length; 00181 // fseek (fp, 0, SEEK_END); 00182 00183 file_length=ftell(fp); 00184 // file_length=sizeof(char)*file_length; 00185 // rewind(fp); 00186 return file_length; 00187 } 00188 00189 00190 00191 00192 00193 void sd_read1(BLEMsg_info &f) 00194 { 00195 fread(&f,sizeof(BLEMsg_info),1,fp); 00196 //fscanf(fp,"%08x",&v1); 00197 } 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 //------------------------------------------------------ 00208 00209 //FILE *sd_BP_ecgwrite (int value) // writing data into the file NIDHIN 00210 FILE *sd_BP_ecgwrite (int32_t *fpdrum11) // writing data into the file NIDHIN 00211 { 00212 fwrite(fpdrum11,sizeof(int32_t),1, fpecg); //Change by Nidhin 30/5/2017 Direct writing to SD card. Org Val 64 00213 return fpecg; 00214 } 00215 00216 FILE* sd_BP_ppgwrite (uint32_t *fpdrum21) // //Change by Nidhin 30/5/2017 Direct writing to SD card. 00217 { 00218 fwrite(fpdrum21,sizeof(uint32_t),1, fpppg); //Change by Nidhin 30/5/2017 Direct writing to SD card. OR Val 64 00219 return fpppg; 00220 } 00221 //-----------------------------------------------SD Read 00222 00223 void sd_reopen_BPfilee(int32_t pid) // only opening the BP file after entering TEST screen 00224 { 00225 00226 00227 char buffer1[32]; 00228 sprintf(buffer1,"/sd/%d_BP.csv",pid); 00229 fp = fopen(buffer1,"r"); 00230 //fprintf(fp,"PATIENT ID %d\n",pid ); 00231 00232 00233 } 00234 //------------------------------------------------- 00235 00236 00237 //--------------------------------------- 00238 00239 00240 void sd_open_BP_ECGfile(int32_t pid) 00241 { 00242 00243 //char buffer[32]; 00244 char buffer1[32]; 00245 //time_t epoch_time1; 00246 //epoch_time1=rtc_read(); 00247 // strftime(buffer, 32, "ECG_%d", pid); 00248 00249 sprintf(buffer1,"/sd/%d_BP_ECG.csv",pid); 00250 fpecg = fopen(buffer1,"a"); 00251 if (fpecg == NULL) 00252 { 00253 exit(1); 00254 } 00255 //fprintf(fpecg,"%s\n",ctime(&epoch_time1) ); 00256 00257 } 00258 00259 00260 void sd_open_BP_PPGfile(int32_t pid) 00261 { 00262 00263 char buffer1[32]; 00264 //time_t epoch_time1; 00265 //epoch_time1=rtc_read(); 00266 // strftime(buffer, 32, "ECG_%d", pid); 00267 00268 sprintf(buffer1,"/sd/%d_BP_PPG.csv",pid); 00269 fpppg = fopen(buffer1,"a"); 00270 if(fpppg == NULL) 00271 { 00272 exit(1); 00273 } 00274 00275 //fprintf(fpppg,"%s\n",ctime(&epoch_time1) ); 00276 00277 } 00278 00279 00280 00281 //-------------------------------------------------- 00282 00283 //FUNCTIONS TO CLOSE FILES 00284 //__________________________________________________ 00285 void sd_close() //close the file with fp 00286 { 00287 00288 fclose(fp); 00289 } 00290 //______________________________________________________ 00291 void sd_close_ecg() // Closes ECG file pinted by fpeecg 00292 { 00293 00294 fclose(fpeecg); 00295 } 00296 //______________________________________________________ 00297 void sd_close_glc() // Closes GLC file pinted by fpbg 00298 { 00299 00300 fclose(fpbg); 00301 } 00302 //_____________________________________________________ 00303 00304 00305 00306 00307 00308 00309 //------------------------------------------ 00310 //REOPEN FUNCTIONs for reading data Nidhin 00311 00312 int sd_BP_ECGMAX(int32_t pid) 00313 { 00314 00315 char buffer1[32]; 00316 00317 sprintf(buffer1,"/sd/%d_BP_ECG.csv",pid); 00318 fpecg = fopen(buffer1,"r"); 00319 if(fpecg == NULL) 00320 { 00321 exit(1); 00322 } 00323 00324 else 00325 { 00326 int ecgmax = 0; 00327 int a[1] = {0}; 00328 int samplesecg =0; 00329 for(ebp=0; ebp<1024; ebp++) 00330 { 00331 00332 00333 fread(a,sizeof(uint32_t), 1,fpecg); 00334 // snd.printf("\n%d", a[0]); Commented May3_2017 (Nidhin) 00335 if(a[0] > ecgmax) 00336 { 00337 ecgmax=a[0]; 00338 ecgloc= ebp +1; 00339 } 00340 else 00341 samplesecg =ebp+1; 00342 00343 } 00344 } 00345 fclose (fpecg); 00346 return ecgloc; 00347 } 00348 00349 // Function to calculate PPG MAx and location 00350 00351 int sd_BP_PPGMAX(int32_t pid) 00352 { 00353 00354 char buffer1[32]; 00355 00356 sprintf(buffer1,"/sd/%d_BP_PPG.csv",pid); 00357 fpppg = fopen(buffer1,"r"); 00358 if(fpppg == NULL) 00359 { 00360 exit(1); 00361 } 00362 00363 else 00364 { 00365 uint32_t ppgmax = 0; 00366 uint32_t b[1] ={0}; 00367 int samplesppg = 0; 00368 for(pbp=0; pbp<1664; pbp++) 00369 { 00370 fread(b, sizeof(uint32_t),1, fpppg); 00371 // snd.printf("\n%d", b[0]); Commented May3_2017 (Nidhin) 00372 00373 if((pbp>ecgloc) && (pbp< (ecgloc+350))) 00374 { 00375 if(b[0] > ppgmax) 00376 { 00377 ppgmax=b[0]; 00378 ppgloc= pbp+1; 00379 } 00380 else 00381 samplesppg =pbp+1; 00382 } 00383 else 00384 samplesppg = pbp+1; 00385 } 00386 } 00387 fclose (fpppg); 00388 return ppgloc; 00389 } 00390 00391 //fread(buffer, 1, size, fp) 00392 //fwrite(&buffer, , 00393 //----------------------------------------------------28/4/2017--------------------------------- 00394 //---------------------------------------------------------------------------------------------- 00395 00396 00397 00398 //------- FUNCTION TO CREATE SINGLE BP FILE --------2/5/2017 (NIDHIN)---------------// 00399 void create_single_BPfile(int32_t pid) 00400 { 00401 00402 //int ecgbuf[64] = {0}; 00403 //uint32_t ppgbuf[64] = {0}; 00404 00405 char buffer3[32]; 00406 sprintf(buffer3, "/sd/%d_BP.csv", pid); 00407 fpbp = fopen(buffer3, "a"); // Opens BP file with FILE pointer fpbp in APPEND mode 00408 00409 //buffer1[32] = ""; 00410 sprintf(buffer3, "/sd/%d_BP_ECG.csv", pid); 00411 fpecg = fopen(buffer3, "r"); // Opens ECG file of BP with FILE pointer fpecg in READ mode 00412 00413 //char buffer1[32] = ""; 00414 sprintf(buffer3, "/sd/%d_BP_PPG.csv", pid); 00415 fpppg = fopen(buffer3, "r"); // Opens PPG file of BP with FILE pointer fpppg in READ mode 00416 00417 for(int i=0; i<16; i++) 00418 { 00419 int ecgbuf[64] = {0} ; 00420 fread(ecgbuf, sizeof(uint32_t), 64, fpecg); 00421 fwrite(ecgbuf, sizeof(uint32_t), 64, fpbp); // Copy ECG data of BP into BP file 00422 } 00423 00424 for(int i=0; i<26; i++) 00425 { 00426 int ppgbuf[64] = {0} ; 00427 fread(ppgbuf, sizeof(uint32_t), 64, fpppg); 00428 fwrite(ppgbuf, sizeof(uint32_t), 64, fpbp); // Copy PPG data of BP into BP file 00429 } 00430 00431 fclose(fpecg); // Closes the ECG file of BP 00432 fclose(fpppg); // Closes the PPG file of BP 00433 fclose(fpbp); // Closes the BP file 00434 00435 //char buffer1[32] = ""; 00436 int status = 1 ; 00437 sprintf(buffer3, "/sd/%d_BP_ECG.csv", pid); 00438 status = remove(buffer3); //Removes ECG file of BP and updates status with 0 if successful 00439 00440 00441 if( status == 0 ) 00442 snd.printf("\nECG file delete : Successful"); // Checks for SUCCESS 00443 else 00444 snd.printf("\nECG file delete : UnSuccessful"); 00445 00446 00447 //char buffer1[32] = ""; 00448 status = 1 ; 00449 sprintf(buffer3, "/sd/%d_BP_PPG.csv", pid); 00450 status = remove(buffer3); //Removes PPG file of BP and updates status with 0 if successful 00451 00452 if( status == 0 ) 00453 snd.printf("\nPPG file delete : Successful"); // Checks for SUCCESS 00454 else 00455 snd.printf("\nPPG file delete : UnSuccessful"); 00456 00457 } 00458 00459 //Creating strcuture file Common to All Functions 00460 void structure_file(BLEMsg_info *ptr, int32_t pid) 00461 { 00462 char buffer3[32]; 00463 sprintf(buffer3, "/sd/%d.csv", pid); //Printing file path to the buffer 00464 fpmain = fopen(buffer3, "a"); // open the file PID.csv with FILE pointer fpmain 00465 fwrite(ptr, sizeof(uint8_t),26, fpmain ); // writing the Structure into the file 00466 fclose(fpmain); // Close File 00467 } 00468 00469 //Copy Data into file. 00470 00471 void bpfile_mainfile(int32_t pid) //Copy data of bp file to main 00472 { 00473 char buffer3[32]; 00474 sprintf(buffer3, "/sd/%d.csv", pid); 00475 fpmain = fopen(buffer3, "a"); // Opens Main file with FILE pointer "fpmain" in APPEND mode 00476 00477 sprintf(buffer3, "/sd/%d_BP.csv", pid); 00478 fpbp = fopen(buffer3, "r"); // Opens BP file with FILE pointer "fpbp" in READ mode 00479 00480 00481 for(int i=0; i<21; i++) 00482 { 00483 int bigbuf[128] = {0} ; 00484 fread(bigbuf, sizeof(uint32_t), 128, fpbp); 00485 fwrite(bigbuf, sizeof(uint32_t), 128, fpmain); // Copy BP raw data into main file 00486 } 00487 fclose(fpbp); // Close BP file 00488 fclose(fpmain); // Close Main File 00489 00490 int success; 00491 sprintf(buffer3, "/sd/%d_BP.csv", pid); 00492 success = remove(buffer3); // Should return zero on success 00493 } 00494 00495 //------------New function added to print ECG data to Sd card using fwrite 00496 void sd_ecgwrite(uint32_t *ecg_ptr) 00497 { 00498 fwrite(ecg_ptr, sizeof(int), 1, fpeecg); 00499 } 00500 00501 00502 //------------ Function to write ECG data to main file 00503 void ecgfile_mainfile(int32_t pid) 00504 { 00505 char buffer3[32]; 00506 sprintf(buffer3, "/sd/%d.csv", pid); 00507 fpmain = fopen(buffer3, "a"); // Main File is opened in Append mode 00508 snd.printf("opening main file\n"); 00509 sprintf(buffer3, "/sd/%d_ECG.csv", pid); // ECG file is opened in read mode 00510 fpeecg = fopen(buffer3, "r"); 00511 snd.printf("opening temp file\n"); 00512 for(int i=0; i<20; i++) //the loop runs as 20 times of 100 i.e., 2k 00513 { 00514 int bigbuf[100] = {0} ; 00515 fread(bigbuf, sizeof(int),100 , fpeecg); 00516 fwrite(bigbuf, sizeof(int), 100, fpmain); 00517 } 00518 snd.printf("to close temp file\n"); 00519 fclose(fpeecg); // Both files are Closed 00520 snd.printf("to come out of main file\n"); 00521 fclose(fpmain); 00522 00523 snd.printf("COPY complete Files Closed\n"); 00524 00525 sprintf(buffer3, "/sd/%d_ECG.csv", pid); // ECG file is opened in write mode to empty the file 00526 fpeecg = fopen(buffer3, "r"); 00527 fclose(fpeecg); 00528 // File is closed 00529 int success; 00530 sprintf(buffer3, "/sd/%d_ECG.csv", pid); 00531 success = remove(buffer3); 00532 snd.printf("DELETE STATUS = %d\n", success); 00533 } 00534 00535 00536 //---------------------- Function to write GLC data to GLC file 00537 void sd_glcwrite(uint32_t *glc_ptr, int32_t pid) 00538 { 00539 char buffer3[32]; 00540 sprintf(buffer3, "/sd/%d_GLC.csv", pid); 00541 fpbg = fopen(buffer3, "a"); 00542 00543 fwrite(glc_ptr, sizeof(uint32_t), 2, fpbg); 00544 fclose(fpbg); 00545 00546 } 00547 00548 // --------------------------Fuinction to write GLC data to Main file 00549 00550 void glcfile_mainfile(int32_t pid) 00551 { 00552 char buffer3[32]; 00553 sprintf(buffer3, "/sd/%d.csv", pid); 00554 fpmain = fopen(buffer3, "a"); // Main File is opened in Append mode 00555 00556 sprintf(buffer3, "/sd/%d_GLC.csv", pid); // BG file is opened in read mode 00557 fpbg = fopen(buffer3, "r"); 00558 00559 for(int i=0; i<1; i++) 00560 { 00561 int bigbuf[2] = {0} ; 00562 fread(bigbuf, sizeof(int),2 , fpbg); 00563 fwrite(bigbuf, sizeof(int), 2, fpmain); 00564 } 00565 fclose(fpbg); // Both files are Closed 00566 fclose(fpmain); 00567 00568 int success; 00569 sprintf(buffer3, "/sd/%d_GLC.csv", pid); 00570 success = remove(buffer3); // GLC file is deleted 00571 } 00572 00573 00574 void delete_subfiles(int32_t pid) 00575 { 00576 char buffer3[32]; 00577 int success1 , success2, success3; 00578 success1 = 1 ; 00579 sprintf(buffer3, "/sd/%d_BP.csv", pid); 00580 success1 = remove(buffer3); //Removes PPG file of BP and updates status with 0 if successful 00581 00582 if( success1 == 0 ) 00583 snd.printf("\nBP file delete : Successful"); // Checks for SUCCESS 00584 else 00585 snd.printf("\nBP file delete : UnSuccessful"); 00586 00587 success2 = 1 ; 00588 sprintf(buffer3, "/sd/%d_GLC.csv", pid); 00589 success2 = remove(buffer3); //Removes GLC file and updates status with 0 if successful 00590 00591 if( success2 == 0 ) 00592 snd.printf("\nGLC file delete : Successful"); // Checks for SUCCESS 00593 else 00594 snd.printf("\nGLC file delete : UnSuccessful"); 00595 00596 success3 = 1 ; 00597 sprintf(buffer3, "/sd/%d_ECG.csv", pid); 00598 success3 = remove(buffer3); //Removes ECG file and updates status with 0 if successful 00599 00600 if( success3 == 0 ) 00601 snd.printf("\nECG file delete : Successful"); // Checks for SUCCESS 00602 else 00603 snd.printf("\nECG file delete : UnSuccessful"); 00604 00605 } 00606 00607
Generated on Wed Jul 13 2022 16:15:21 by 1.7.2