
Changes done in ECG and BT
Dependencies: SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217
Fork of merged_code2_20sept_2017_4th_oct_2017 by
sdcard.cpp
- Committer:
- nikitateggi
- Date:
- 2017-06-24
- Revision:
- 24:d992ee8369f4
- Parent:
- 22:ffa88619551d
- Child:
- 26:53ff13a164f3
File content as of revision 24:d992ee8369f4:
// Exisiting code in Int_Demo_09May2017_suhasini/sdcard.cpp is deleted and replaced by new code //Nidhin 1/6/2017 #include "mbed.h" #include <stdio.h> #include "SDFileSystem.h" #include "sdcard.h" #include "rtc.h" #include "struct.h" Serial snd(USBTX,USBRX); SDFileSystem sd(PTE1, PTE3, PTE2, PTE4, "sd"); FILE *fpecg; // PPG and ECG File pointers for BP FILE *fpppg; //"" "" FILE *fpmain; // FILE pointer for main file FILE *fpbg; // FILE pointer to BG file FILE *fpeecg; // FILE pointer to ECG file FILE *fpbp; // FILE pointer to BP file FILE *fp; // FILE pointer for random use int ecgloc = 0; // PPG and eCG maximum locations int ppgloc = 0; int ebp = 0; // variable for LOOP operation to read value from File i == ECG , j == PPG int pbp = 0; //Function Added on 16/5/2017 by Nidhin void sd_open_mainfile(int32_t pid) { char buffer1[32]; sprintf(buffer1,"/sd/%d.csv",pid); fp = fopen(buffer1,"a"); } //------------------------------------------ /*void sd_open_GLCfile(int pid) // opening the glc file { char buffer[32]; char buffer1[32]; time_t epoch_time1; epoch_time1=rtc_read(); // strftime(buffer, 32, "GLC_%s", pid); sprintf(buffer1,"/sd/%d_GLC.csv",pid); fp = fopen(buffer1,"a"); fprintf(fp,"%s\n",ctime(&epoch_time1) ); //writing the date, time into the file } */ void sd_open_GLCfilee(int32_t pid) // only opening the glc file after entering TEST screen { char buffer1[32]; sprintf(buffer1,"/sd/%d_GLC.csv",pid); fpbg = fopen(buffer1,"a"); fclose(fpbg); } /*void sd_open_ECGfile(int pid) { // char buffer[32]; char buffer1[32]; time_t epoch_time1; epoch_time1=rtc_read(); // strftime(buffer, 32, "ECG_%d", pid); sprintf(buffer1,"/sd/%d_ECG.csv",pid); fp = fopen(buffer1,"a"); fprintf(fp,"%s\n",ctime(&epoch_time1) ); } */ void sd_open_ECGfilee(int32_t pid) // only opening the ECG file after entering TEST screen { char buffer1[32]; sprintf(buffer1,"/sd/%d_ECG.csv",pid); fpeecg = fopen(buffer1,"a"); //fclose(fpeecg); } /*void sd_open_BPfile(int pid) { char buffer[32]; char buffer1[32]; time_t epoch_time1; epoch_time1=rtc_read(); // strftime(buffer, 32, "ECG_%d", pid); sprintf(buffer1,"/sd/%d_BP.csv",pid); fp = fopen(buffer1,"a"); fprintf(fp,"%s\n",ctime(&epoch_time1) ); } */ void sd_open_BPfilee(int32_t pid) // only opening the BP file after entering TEST screen { char buffer1[32]; sprintf(buffer1,"/sd/%d_BP.csv",pid); fpbp = fopen(buffer1,"a"); //fprintf(fp,"PATIENT ID %d\n",pid ); Not required in new code as writing in hex fclose(fpbp); } void sd_write (int value) // writing data into the file { fprintf(fp,"%d\n", value); } void sd_open_read(int32_t pid) // opening the file for read { char buffer1[32]; sprintf(buffer1,"/sd/%d.csv",pid); fp = fopen(buffer1,"r"); if (fp==NULL) { snd.printf("no"); } else { snd.printf("yes"); } } void sd_read(uint32_t *v1, int size) //reading the data from the openend file { fread(v1,sizeof(v1),size,fp); } void setpos() { //fseek ( fp , 1 , SEEK_SET ); rewind (fp); } void setpos1(uint32_t position) { fseek ( fp , position, SEEK_SET ); //rewind (fp); } int file_size() { int file_length; fseek (fp, 0, SEEK_END); file_length=ftell(fp); file_length=sizeof(char)*file_length; rewind(fp); return file_length; } int file_size1() { int file_length; // fseek (fp, 0, SEEK_END); file_length=ftell(fp); // file_length=sizeof(char)*file_length; // rewind(fp); return file_length; } void sd_read1(BLEMsg_info &f) { fread(&f,sizeof(BLEMsg_info),1,fp); //fscanf(fp,"%08x",&v1); } //------------------------------------------------------ //FILE *sd_BP_ecgwrite (int value) // writing data into the file NIDHIN FILE *sd_BP_ecgwrite (int32_t *fpdrum11) // writing data into the file NIDHIN { fwrite(fpdrum11,sizeof(int32_t),1, fpecg); //Change by Nidhin 30/5/2017 Direct writing to SD card. Org Val 64 return fpecg; } FILE* sd_BP_ppgwrite (uint32_t *fpdrum21) // //Change by Nidhin 30/5/2017 Direct writing to SD card. { fwrite(fpdrum21,sizeof(uint32_t),1, fpppg); //Change by Nidhin 30/5/2017 Direct writing to SD card. OR Val 64 return fpppg; } //-----------------------------------------------SD Read void sd_reopen_BPfilee(int32_t pid) // only opening the BP file after entering TEST screen { char buffer1[32]; sprintf(buffer1,"/sd/%d_BP.csv",pid); fp = fopen(buffer1,"r"); //fprintf(fp,"PATIENT ID %d\n",pid ); } //------------------------------------------------- //--------------------------------------- void sd_open_BP_ECGfile(int32_t pid) { //char buffer[32]; char buffer1[32]; //time_t epoch_time1; //epoch_time1=rtc_read(); // strftime(buffer, 32, "ECG_%d", pid); sprintf(buffer1,"/sd/%d_BP_ECG.csv",pid); fpecg = fopen(buffer1,"a"); if (fpecg == NULL) { exit(1); } //fprintf(fpecg,"%s\n",ctime(&epoch_time1) ); } void sd_open_BP_PPGfile(int32_t pid) { char buffer1[32]; //time_t epoch_time1; //epoch_time1=rtc_read(); // strftime(buffer, 32, "ECG_%d", pid); sprintf(buffer1,"/sd/%d_BP_PPG.csv",pid); fpppg = fopen(buffer1,"a"); if(fpppg == NULL) { exit(1); } //fprintf(fpppg,"%s\n",ctime(&epoch_time1) ); } //-------------------------------------------------- //FUNCTIONS TO CLOSE FILES //__________________________________________________ void sd_close() //close the file with fp { fclose(fp); } //______________________________________________________ void sd_close_ecg() // Closes ECG file pinted by fpeecg { fclose(fpeecg); } //______________________________________________________ void sd_close_glc() // Closes GLC file pinted by fpbg { fclose(fpbg); } //_____________________________________________________ //------------------------------------------ //REOPEN FUNCTIONs for reading data Nidhin int sd_BP_ECGMAX(int32_t pid) { char buffer1[32]; sprintf(buffer1,"/sd/%d_BP_ECG.csv",pid); fpecg = fopen(buffer1,"r"); if(fpecg == NULL) { exit(1); } else { int ecgmax = 0; int a[1] = {0}; int samplesecg =0; for(ebp=0; ebp<1024; ebp++) { fread(a,sizeof(uint32_t), 1,fpecg); // snd.printf("\n%d", a[0]); Commented May3_2017 (Nidhin) if(a[0] > ecgmax) { ecgmax=a[0]; ecgloc= ebp +1; } else samplesecg =ebp+1; } } fclose (fpecg); return ecgloc; } // Function to calculate PPG MAx and location int sd_BP_PPGMAX(int32_t pid) { char buffer1[32]; sprintf(buffer1,"/sd/%d_BP_PPG.csv",pid); fpppg = fopen(buffer1,"r"); if(fpppg == NULL) { exit(1); } else { uint32_t ppgmax = 0; uint32_t b[1] ={0}; int samplesppg = 0; for(pbp=0; pbp<1664; pbp++) { fread(b, sizeof(uint32_t),1, fpppg); // snd.printf("\n%d", b[0]); Commented May3_2017 (Nidhin) if((pbp>ecgloc) && (pbp< (ecgloc+350))) { if(b[0] > ppgmax) { ppgmax=b[0]; ppgloc= pbp+1; } else samplesppg =pbp+1; } else samplesppg = pbp+1; } } fclose (fpppg); return ppgloc; } //fread(buffer, 1, size, fp) //fwrite(&buffer, , //----------------------------------------------------28/4/2017--------------------------------- //---------------------------------------------------------------------------------------------- //------- FUNCTION TO CREATE SINGLE BP FILE --------2/5/2017 (NIDHIN)---------------// void create_single_BPfile(int32_t pid) { //int ecgbuf[64] = {0}; //uint32_t ppgbuf[64] = {0}; char buffer3[32]; sprintf(buffer3, "/sd/%d_BP.csv", pid); fpbp = fopen(buffer3, "a"); // Opens BP file with FILE pointer fpbp in APPEND mode //buffer1[32] = ""; sprintf(buffer3, "/sd/%d_BP_ECG.csv", pid); fpecg = fopen(buffer3, "r"); // Opens ECG file of BP with FILE pointer fpecg in READ mode //char buffer1[32] = ""; sprintf(buffer3, "/sd/%d_BP_PPG.csv", pid); fpppg = fopen(buffer3, "r"); // Opens PPG file of BP with FILE pointer fpppg in READ mode for(int i=0; i<16; i++) { int ecgbuf[64] = {0} ; fread(ecgbuf, sizeof(uint32_t), 64, fpecg); fwrite(ecgbuf, sizeof(uint32_t), 64, fpbp); // Copy ECG data of BP into BP file } for(int i=0; i<26; i++) { int ppgbuf[64] = {0} ; fread(ppgbuf, sizeof(uint32_t), 64, fpppg); fwrite(ppgbuf, sizeof(uint32_t), 64, fpbp); // Copy PPG data of BP into BP file } fclose(fpecg); // Closes the ECG file of BP fclose(fpppg); // Closes the PPG file of BP fclose(fpbp); // Closes the BP file //char buffer1[32] = ""; int status = 1 ; sprintf(buffer3, "/sd/%d_BP_ECG.csv", pid); status = remove(buffer3); //Removes ECG file of BP and updates status with 0 if successful if( status == 0 ) snd.printf("\nECG file delete : Successful"); // Checks for SUCCESS else snd.printf("\nECG file delete : UnSuccessful"); //char buffer1[32] = ""; status = 1 ; sprintf(buffer3, "/sd/%d_BP_PPG.csv", pid); status = remove(buffer3); //Removes PPG file of BP and updates status with 0 if successful if( status == 0 ) snd.printf("\nPPG file delete : Successful"); // Checks for SUCCESS else snd.printf("\nPPG file delete : UnSuccessful"); } //Creating strcuture file Common to All Functions void structure_file(BLEMsg_info *ptr, int32_t pid) { char buffer3[32]; sprintf(buffer3, "/sd/%d.csv", pid); //Printing file path to the buffer fpmain = fopen(buffer3, "a"); // open the file PID.csv with FILE pointer fpmain fwrite(ptr, sizeof(uint8_t),26, fpmain ); // writing the Structure into the file fclose(fpmain); // Close File } //Copy Data into file. void bpfile_mainfile(int32_t pid) //Copy data of bp file to main { char buffer3[32]; sprintf(buffer3, "/sd/%d.csv", pid); fpmain = fopen(buffer3, "a"); // Opens Main file with FILE pointer "fpmain" in APPEND mode sprintf(buffer3, "/sd/%d_BP.csv", pid); fpbp = fopen(buffer3, "r"); // Opens BP file with FILE pointer "fpbp" in READ mode for(int i=0; i<21; i++) { int bigbuf[128] = {0} ; fread(bigbuf, sizeof(uint32_t), 128, fpbp); fwrite(bigbuf, sizeof(uint32_t), 128, fpmain); // Copy BP raw data into main file } fclose(fpbp); // Close BP file fclose(fpmain); // Close Main File int success; sprintf(buffer3, "/sd/%d_BP.csv", pid); success = remove(buffer3); // Should return zero on success } //------------New function added to print ECG data to Sd card using fwrite void sd_ecgwrite(int *ecg_ptr) { fwrite(ecg_ptr, sizeof(int), 1, fpeecg); } //------------ Function to write ECG data to main file void ecgfile_mainfile(int32_t pid) { char buffer3[32]; sprintf(buffer3, "/sd/%d.csv", pid); fpmain = fopen(buffer3, "a"); // Main File is opened in Append mode sprintf(buffer3, "/sd/%d_ECG.csv", pid); // ECG file is opened in read mode fpeecg = fopen(buffer3, "r"); for(int i=0; i<10; i++) { int bigbuf[100] = {0} ; fread(bigbuf, sizeof(int),100 , fpeecg); fwrite(bigbuf, sizeof(int), 100, fpmain); } fclose(fpeecg); // Both files are Closed fclose(fpmain); /* sprintf(buffer3, "/sd/%d_ECG.csv", pid); // ECG file is opened in write mode to empty the file fpeecg = fopen(buffer3, "w"); fclose(fpecg); */ // File is closed int success; sprintf(buffer3, "/sd/%d_ECG.csv", pid); success = remove(buffer3); } //---------------------- Function to write GLC data to GLC file void sd_glcwrite(uint32_t *glc_ptr, int32_t pid) { char buffer3[32]; sprintf(buffer3, "/sd/%d_GLC.csv", pid); fpbg = fopen(buffer3, "a"); fwrite(glc_ptr, sizeof(uint32_t), 2, fpbg); fclose(fpbg); } // --------------------------Fuinction to write GLC data to Main file void glcfile_mainfile(int32_t pid) { char buffer3[32]; sprintf(buffer3, "/sd/%d.csv", pid); fpmain = fopen(buffer3, "a"); // Main File is opened in Append mode sprintf(buffer3, "/sd/%d_GLC.csv", pid); // BG file is opened in read mode fpbg = fopen(buffer3, "r"); for(int i=0; i<1; i++) { int bigbuf[2] = {0} ; fread(bigbuf, sizeof(int),2 , fpbg); fwrite(bigbuf, sizeof(int), 2, fpmain); } fclose(fpbg); // Both files are Closed fclose(fpmain); int success; sprintf(buffer3, "/sd/%d_GLC.csv", pid); success = remove(buffer3); // GLC file is deleted } void delete_subfiles(int32_t pid) { char buffer3[32]; int success1 , success2, success3; success1 = 1 ; sprintf(buffer3, "/sd/%d_BP.csv", pid); success1 = remove(buffer3); //Removes PPG file of BP and updates status with 0 if successful if( success1 == 0 ) snd.printf("\nBP file delete : Successful"); // Checks for SUCCESS else snd.printf("\nBP file delete : UnSuccessful"); success2 = 1 ; sprintf(buffer3, "/sd/%d_GLC.csv", pid); success2 = remove(buffer3); //Removes GLC file and updates status with 0 if successful if( success2 == 0 ) snd.printf("\nGLC file delete : Successful"); // Checks for SUCCESS else snd.printf("\nGLC file delete : UnSuccessful"); success3 = 1 ; sprintf(buffer3, "/sd/%d_ECG.csv", pid); success3 = remove(buffer3); //Removes ECG file and updates status with 0 if successful if( success3 == 0 ) snd.printf("\nECG file delete : Successful"); // Checks for SUCCESS else snd.printf("\nECG file delete : UnSuccessful"); } uint16_t heart_cal(int pid) // Function created on 10/6/17, Function modified on 13/6/17 Nidhin // Objective ti understand how file pointers work { //FILE *fpeecg2; // Secondary File char buffer3[32]; int32_t buff1[1] ; int32_t buff2[2]; int32_t buff3[1]; // Buff 12 &3 Stores sample 1, 2 & 10th sample int32_t fppos; // Variable to hold pointer position int32_t val1_val10; // VARIABLE holding difference of sample 1 and 10 int32_t peakpos[10]; //array taht holds the peak position //Changed to 10 (ORG 4) Nidhin 20/6/17 int32_t peakval[10]; // Array holding peak values ////Changed to 10 (ORG 4) Nidhin 20/6/17 int32_t pk = 0; // Variable to access array int32_t no_of_peaks =0; // Variable holding toatl number of peaks ; increments on each peak float hrt_rate; // Heart rate in float varoable uint16_t heart_rate; // Heart rate to be type casted to int sprintf(buffer3, "/sd/%d_ECG.csv", pid); // For opening a specific file fpeecg = fopen(buffer3, "r"); /* fppos = ftell(fpeecg); snd.printf("Main pointer is at Position : %d \n", fppos); */ int32_t m =0; // Variable to move the file pointer in fseek function Org Static Nidhin 22/6/17 int32_t i =0; // Variable to count sample number, ORG Static Nidhin 22/6/17 for ( i = 0; i < 1000; i++) // for 1024 samples in file { rewind(fpeecg); // Go to satrt of file each time fseek(fpeecg, m, SEEK_CUR); // Update the count value according to move pointer fread(buff1, sizeof(int32_t), 1, fpeecg); // Read sample 1 //fpeecg2 = fpeecg; fread(buff2, sizeof(int32_t), 1, fpeecg); // Read Sample 2 int32_t n = 28; // Variable to move to 10th sampple from current fseek(fpeecg, n, SEEK_CUR); // Moving to tenth sample fread(buff3, sizeof(int32_t), 1, fpeecg); // Read 3rd sample snd.printf("buff1 = %d , buff2 = %d, buff3 = %d\n",buff1[0],buff2[0],buff3[0] ); //Test Value heald by buffer each round val1_val10 = buff1[0] - buff3[0]; // Difference of sample 1 and 10 if(buff1[0] > buff2[0] && val1_val10 > 10000) // Condition for -ve gradient and difference of 10000 { peakpos[pk] = i; // Peak detected sample number updated in array peakval[pk] = buff1[0]; // Peak value updated in array pk++; // Array pointer incremented i = i+120; // i incremented to 120 for next 120 samples no_of_peaks = no_of_peaks + 1; // no. of peaks incremented //fseek(fpeecg, 480, SEEK_CUR); // push the pointer to //fppos = ftell(fpeecg); Commenetd Nidhin 20/6/17 //snd.printf("in if : The Pointer is moved to positon %d\n", fppos); m = m + 480; } else { m = m+4; //fpeecg = fpeecg2; //i = i+1; //fppos = ftell(fpeecg); COMMENTED NIDHIN 20/6/17 //snd.printf("in Else : The Pointer is moved to positon %d\n", fppos); } } snd.printf("The No: of Peaks is %d\n", no_of_peaks); for(int j=0; j<no_of_peaks; j++) { snd.printf("The Peak %d, is at %d & value is %d\n",j, peakpos[j], peakval[j]); } // Heart rate calculation //Testing int32_t t_diff; t_diff = peakpos[1] - peakpos[0]; snd.printf("t_diff is %d\n", t_diff); hrt_rate = 60.0/(t_diff*2); //snd.printf("Hear rate in float is %f\n", hrt_rate); COMMENETD NIDHIN 20/6/17 hrt_rate = float (hrt_rate*1000); snd.printf("Hear rate in float is %f\n", hrt_rate); heart_rate = uint16_t(hrt_rate + 0.5); snd.printf("The heart rate after type casting is %d\n", heart_rate); /* fppos = ftell(fpeecg); snd.printf("The first data is = %d\n", buff1); snd.printf("The Position after reading first data is = %d\n\n", fppos); fread(buff2, sizeof(int32_t), 1, fpeecg); fppos = ftell(fpeecg); snd.printf("The Second data is = %d\n", buff2); snd.printf("The Position after reading Second data is = %d\n\n", fppos); fseek(fpeecg, 32,SEEK_CUR); fread(buff3, sizeof(int32_t), 1, fpeecg); fppos = ftell(fpeecg); snd.printf("The third data is = %d\n", buff3); snd.printf("The Position after reading Second data is = %d\n\n", fppos); fseek(fpeecg, -24,SEEK_CUR); fread(buff3, sizeof(int32_t), 1, fpeecg); fppos = ftell(fpeecg); snd.printf("The fourth data is = %d\n", buff3); snd.printf("The Position after reading fourth data is = %d\n", fppos); } /* snd.printf("Printing from 500-999\n\n"); fread(ecgbufnew, sizeof(int), 500, fpeecg); for(int i=0; i<500; i++) { //snd.printf("%d\n", ecgbufnew[i]); } rewind(fpeecg); fseek(fpeecg, 996, SEEK_SET); // Try modifying with 996 = 249*4 if not working snd.printf("Printing from 250-749\n\n"); fread(ecgbufnew, sizeof(int), 500, fpeecg); for(int i=0; i<500; i++) { //snd.printf("%d\n", ecgbufnew[i]); } */ fclose(fpeecg); return heart_rate; }