Added 4 point or 8 point averaging function .

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of merged_code2_3rd_nov_2017 by nikita teggi

Committer:
nidhinvarghese
Date:
Mon Jun 05 09:14:44 2017 +0000
Revision:
15:208b146151ba
Parent:
14:f5c62d30c6fc
Child:
19:1650bbc923cc
Heart rate is calculated; GLC - Not working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nidhinvarghese 14:f5c62d30c6fc 1 //-----------------------------------------------NEW BP code starts--------------------------------///
nikitateggi 3:9a06c2bed650 2
nikitateggi 3:9a06c2bed650 3
nidhinvarghese 8:adf2ba7e6412 4
nikitateggi 3:9a06c2bed650 5 #include "mbed.h"
nikitateggi 3:9a06c2bed650 6 #include "MAX30102.h" // BP ADD
nikitateggi 3:9a06c2bed650 7 #include <string.h>
nikitateggi 3:9a06c2bed650 8 #include <stdio.h>
nidhinvarghese 14:f5c62d30c6fc 9
nidhinvarghese 14:f5c62d30c6fc 10 #include <time.h>
nidhinvarghese 14:f5c62d30c6fc 11 //#include <iostream>
nikitateggi 3:9a06c2bed650 12 #include "ds3231.h"
nikitateggi 3:9a06c2bed650 13 #include "SDFileSystem.h"
nikitateggi 3:9a06c2bed650 14 #include "rtc.h"
nikitateggi 3:9a06c2bed650 15 #include "sdcard.h"
nikitateggi 3:9a06c2bed650 16 #include "ec_bp.h"
nikitateggi 3:9a06c2bed650 17 #include "bp.h"
nikitateggi 3:9a06c2bed650 18 #include "display_modules.h"
nikitateggi 3:9a06c2bed650 19
nikitateggi 3:9a06c2bed650 20
nikitateggi 3:9a06c2bed650 21
nidhinvarghese 14:f5c62d30c6fc 22 #include "struct.h"
nidhinvarghese 14:f5c62d30c6fc 23 #include "test_type.h"
nidhinvarghese 8:adf2ba7e6412 24
nidhinvarghese 8:adf2ba7e6412 25 using namespace std;
nidhinvarghese 8:adf2ba7e6412 26
nidhinvarghese 14:f5c62d30c6fc 27 Serial bpr(USBTX,USBRX);
nidhinvarghese 8:adf2ba7e6412 28 DigitalIn INTR(PTC7);
nidhinvarghese 8:adf2ba7e6412 29
nidhinvarghese 8:adf2ba7e6412 30 Timer t;
nidhinvarghese 8:adf2ba7e6412 31
nidhinvarghese 14:f5c62d30c6fc 32
nidhinvarghese 8:adf2ba7e6412 33 //FILE *ecg;
nidhinvarghese 8:adf2ba7e6412 34 //FILE *ppg;
nidhinvarghese 8:adf2ba7e6412 35
nidhinvarghese 14:f5c62d30c6fc 36 void bp(int pid) {
nidhinvarghese 14:f5c62d30c6fc 37
nidhinvarghese 8:adf2ba7e6412 38 uint8_t uch_dummy;
nidhinvarghese 8:adf2ba7e6412 39 uint32_t ppgdata;
nidhinvarghese 8:adf2ba7e6412 40
nidhinvarghese 8:adf2ba7e6412 41 //int dummyconcat = 700000;
nidhinvarghese 8:adf2ba7e6412 42 int concatenate_value2 = 0;
nidhinvarghese 8:adf2ba7e6412 43 unsigned char chk = 1;
nidhinvarghese 8:adf2ba7e6412 44
nidhinvarghese 14:f5c62d30c6fc 45 uint32_t drum11[64];
nidhinvarghese 8:adf2ba7e6412 46 uint32_t drum21[64];
nidhinvarghese 14:f5c62d30c6fc 47
nidhinvarghese 14:f5c62d30c6fc 48
nidhinvarghese 14:f5c62d30c6fc 49 //Declaration of Master structure
nidhinvarghese 14:f5c62d30c6fc 50
nidhinvarghese 14:f5c62d30c6fc 51 BLEMsg_info *ptr_BLEMsg_info_bp, BLEMsg_info_bp; // A copy of master strcuture [ "BLEMsg_info" ] by name "BLEMsg_info_bp" is created
nidhinvarghese 14:f5c62d30c6fc 52 ptr_BLEMsg_info_bp = &BLEMsg_info_bp; // *ptr_BLEMsg_info_bp is the pointer to local copy;
nidhinvarghese 14:f5c62d30c6fc 53
nidhinvarghese 14:f5c62d30c6fc 54 // Declaration of Date Structure
nidhinvarghese 14:f5c62d30c6fc 55 DateTime_info *ptr_DateTime_info_bp, DateTime_info_bp; // A copy of Master Structure "DateTime_info" created,
nidhinvarghese 14:f5c62d30c6fc 56 ptr_DateTime_info_bp = &DateTime_info_bp; // Structure pointer points to that copy.
nidhinvarghese 14:f5c62d30c6fc 57
nidhinvarghese 14:f5c62d30c6fc 58
nidhinvarghese 14:f5c62d30c6fc 59 // RTC operations
nidhinvarghese 14:f5c62d30c6fc 60 time_t epoch_time_bp; //A copy of time_t by name epoch_time_bp is created
nidhinvarghese 14:f5c62d30c6fc 61 epoch_time_bp = rtc_read(); // time is got from get epoch function.
nidhinvarghese 14:f5c62d30c6fc 62
nidhinvarghese 14:f5c62d30c6fc 63 struct tm * ptr_time_info_bp, time_info_bp; // Sturucture copy of tm is created
nidhinvarghese 14:f5c62d30c6fc 64 ptr_time_info_bp = localtime(&epoch_time_bp); // Structure accepts the time in local format from "time_t" type.
nidhinvarghese 14:f5c62d30c6fc 65
nidhinvarghese 14:f5c62d30c6fc 66 //BELOW LINE IS TO CHECK Date and TIME
nidhinvarghese 14:f5c62d30c6fc 67 bpr.printf("Time is %d: %d: %d\n", (*ptr_time_info_bp).tm_hour, (*ptr_time_info_bp).tm_min, (*ptr_time_info_bp).tm_sec);
nidhinvarghese 14:f5c62d30c6fc 68 bpr.printf("Date is %d:%d:%d\n", (*ptr_time_info_bp).tm_mday, (*ptr_time_info_bp).tm_mon+1, (*ptr_time_info_bp).tm_year-100);
nidhinvarghese 14:f5c62d30c6fc 69
nidhinvarghese 14:f5c62d30c6fc 70 //Copying from one structure to the other using variables
nidhinvarghese 14:f5c62d30c6fc 71 DateTime_info_bp.hour = (uint8_t)(*ptr_time_info_bp).tm_hour;
nidhinvarghese 14:f5c62d30c6fc 72 DateTime_info_bp.mins = (uint8_t)(*ptr_time_info_bp).tm_min;
nidhinvarghese 14:f5c62d30c6fc 73 DateTime_info_bp.sec = (uint8_t)(*ptr_time_info_bp).tm_sec;
nidhinvarghese 14:f5c62d30c6fc 74
nidhinvarghese 14:f5c62d30c6fc 75 DateTime_info_bp.date = (uint8_t) (*ptr_time_info_bp).tm_mday;
nidhinvarghese 14:f5c62d30c6fc 76 DateTime_info_bp.month =(uint8_t)(*ptr_time_info_bp).tm_mon+1;
nidhinvarghese 14:f5c62d30c6fc 77 DateTime_info_bp.year = (uint8_t)(*ptr_time_info_bp).tm_year-100;
nidhinvarghese 14:f5c62d30c6fc 78
nidhinvarghese 14:f5c62d30c6fc 79 // Copying Time to Main structure
nidhinvarghese 14:f5c62d30c6fc 80 BLEMsg_info_bp.date_time.hour = DateTime_info_bp.hour;
nidhinvarghese 14:f5c62d30c6fc 81 BLEMsg_info_bp.date_time.mins = DateTime_info_bp.mins;
nidhinvarghese 14:f5c62d30c6fc 82 BLEMsg_info_bp.date_time.sec = DateTime_info_bp.sec;
nidhinvarghese 14:f5c62d30c6fc 83
nidhinvarghese 14:f5c62d30c6fc 84 BLEMsg_info_bp.date_time.date = DateTime_info_bp.date ;
nidhinvarghese 14:f5c62d30c6fc 85 BLEMsg_info_bp.date_time.month = DateTime_info_bp.month ;
nidhinvarghese 14:f5c62d30c6fc 86 BLEMsg_info_bp.date_time.year = DateTime_info_bp.year ;
nidhinvarghese 14:f5c62d30c6fc 87
nidhinvarghese 14:f5c62d30c6fc 88
nidhinvarghese 14:f5c62d30c6fc 89 //Checking if the structure has these values
nidhinvarghese 14:f5c62d30c6fc 90 bpr.printf("Time 2 is %d:%d:%d\n", DateTime_info_bp.hour, DateTime_info_bp.mins, DateTime_info_bp.sec);
nidhinvarghese 14:f5c62d30c6fc 91 bpr.printf("\t Date is %d:%d:%d\n",DateTime_info_bp.date, DateTime_info_bp.month, DateTime_info_bp.year);
nidhinvarghese 14:f5c62d30c6fc 92
nidhinvarghese 14:f5c62d30c6fc 93
nidhinvarghese 14:f5c62d30c6fc 94 //Loading values to of Test type
nidhinvarghese 14:f5c62d30c6fc 95 test_type_info test_type_info_bp; // copy of " test_type_info" created
nidhinvarghese 14:f5c62d30c6fc 96 test_type_info_bp = BP_Test; // Loaded value 00 to the test type
nidhinvarghese 14:f5c62d30c6fc 97
nidhinvarghese 14:f5c62d30c6fc 98 BLEMsg_info_bp.test_type = test_type_info_bp;
nidhinvarghese 14:f5c62d30c6fc 99 //Check if 00 is getting printed
nidhinvarghese 14:f5c62d30c6fc 100 bpr.printf("Test Type is : %d\n", test_type_info_bp);
nidhinvarghese 14:f5c62d30c6fc 101
nidhinvarghese 14:f5c62d30c6fc 102
nidhinvarghese 14:f5c62d30c6fc 103 // Loading values of Length , PID, DID, sampling frequency, number of samples, calculated data.
nidhinvarghese 14:f5c62d30c6fc 104 BLEMsg_info_bp.device_id = 01; // Device ID fixed
nidhinvarghese 14:f5c62d30c6fc 105 BLEMsg_info_bp.patient_id = (uint32_t)pid; // Patient ID
nidhinvarghese 14:f5c62d30c6fc 106 BLEMsg_info_bp.sampling_freq = 500; // sampling frrquency
nidhinvarghese 14:f5c62d30c6fc 107 BLEMsg_info_bp.length = 10774; //Total length of data in bytes 22 B+10752 B
nidhinvarghese 14:f5c62d30c6fc 108
nidhinvarghese 14:f5c62d30c6fc 109 /*
nidhinvarghese 14:f5c62d30c6fc 110 //Loading number of samples
nidhinvarghese 14:f5c62d30c6fc 111 NumSamples_info NumSamples_info_bp; //Copy of structure NumSamples_info
nidhinvarghese 14:f5c62d30c6fc 112 NumSamples_info_bp.num_ppg_sample = 1664; // PPG & ECG Sample number loaded in structure copy
nidhinvarghese 14:f5c62d30c6fc 113 NumSamples_info_bp.num_ecg_sample = 1024;
nidhinvarghese 14:f5c62d30c6fc 114 */
nidhinvarghese 14:f5c62d30c6fc 115
nidhinvarghese 14:f5c62d30c6fc 116 BLEMsg_info_bp.num_samples.num_sample_ppg_dummy = 1664 ;// PPG number of samples copied to master struct
nidhinvarghese 14:f5c62d30c6fc 117 BLEMsg_info_bp.num_samples.num_sample_ecg_OTtyp = 1024 ; // ECG number of samples copied to master struct
nidhinvarghese 14:f5c62d30c6fc 118
nidhinvarghese 14:f5c62d30c6fc 119
nidhinvarghese 14:f5c62d30c6fc 120
nidhinvarghese 14:f5c62d30c6fc 121
nidhinvarghese 8:adf2ba7e6412 122
nidhinvarghese 8:adf2ba7e6412 123 // Variables for file operations
nidhinvarghese 8:adf2ba7e6412 124 // uint32_t amax=0;
nidhinvarghese 8:adf2ba7e6412 125 //uint32_t bmax = 0;
nidhinvarghese 8:adf2ba7e6412 126 //uint32_t a;
nidhinvarghese 8:adf2ba7e6412 127 //uint32_t b;
nidhinvarghese 8:adf2ba7e6412 128
nidhinvarghese 8:adf2ba7e6412 129 //int aloc = 0;
nidhinvarghese 8:adf2ba7e6412 130 //int bloc = 0;
nidhinvarghese 8:adf2ba7e6412 131 //int samples = 0;
nidhinvarghese 8:adf2ba7e6412 132
nidhinvarghese 14:f5c62d30c6fc 133 bpr.baud(baud_rate);
nidhinvarghese 8:adf2ba7e6412 134
nidhinvarghese 8:adf2ba7e6412 135 freqset(); // setting the frequency
nidhinvarghese 8:adf2ba7e6412 136 setupfunc();
nidhinvarghese 8:adf2ba7e6412 137 ecgsetupfunc();
nidhinvarghese 8:adf2ba7e6412 138
nidhinvarghese 8:adf2ba7e6412 139 maxim_max30102_reset(); // PPG reset
nidhinvarghese 8:adf2ba7e6412 140 wait_ms(20);
nidhinvarghese 8:adf2ba7e6412 141 maxim_max30102_init();
nidhinvarghese 8:adf2ba7e6412 142
nidhinvarghese 8:adf2ba7e6412 143 chk = 1;
nidhinvarghese 8:adf2ba7e6412 144
nidhinvarghese 8:adf2ba7e6412 145 wait(2);
nidhinvarghese 8:adf2ba7e6412 146 //bc.printf("BP start\n");
nidhinvarghese 8:adf2ba7e6412 147
nidhinvarghese 8:adf2ba7e6412 148 for(int i=0; i<500; i++)
nidhinvarghese 8:adf2ba7e6412 149 {
nidhinvarghese 8:adf2ba7e6412 150
nidhinvarghese 8:adf2ba7e6412 151 concatenate_value2= readvalue();
nidhinvarghese 8:adf2ba7e6412 152
nidhinvarghese 8:adf2ba7e6412 153 maxim_max30102_read_reg(0,&uch_dummy);
nidhinvarghese 8:adf2ba7e6412 154 while(INTR.read()==1);
nidhinvarghese 8:adf2ba7e6412 155 maxim_max30102_read_fifo(&ppgdata);
nidhinvarghese 8:adf2ba7e6412 156
nidhinvarghese 8:adf2ba7e6412 157 }
nidhinvarghese 8:adf2ba7e6412 158
nidhinvarghese 14:f5c62d30c6fc 159 sd_open_BPfilee(pid);
nidhinvarghese 8:adf2ba7e6412 160 sd_open_BP_ECGfile(pid) ; // sd_open_BP_ECGfile(123) draft file for testing
nidhinvarghese 8:adf2ba7e6412 161 sd_open_BP_PPGfile(pid) ; // sd_open_BP_PPGfile(123) draft file for testing
nidhinvarghese 8:adf2ba7e6412 162
nidhinvarghese 8:adf2ba7e6412 163 FILE *ecg = NULL;
nidhinvarghese 8:adf2ba7e6412 164 FILE *ppg = NULL;
nidhinvarghese 14:f5c62d30c6fc 165 uint32_t *drum_ecg = NULL; // pointers to ecg buffer changed to uint32_t from int
nidhinvarghese 8:adf2ba7e6412 166 uint32_t *drum_ppg = NULL; // pointer to ppg buffer
nidhinvarghese 8:adf2ba7e6412 167
nidhinvarghese 8:adf2ba7e6412 168 drum_ecg = drum11;
nidhinvarghese 8:adf2ba7e6412 169 drum_ppg = drum21;
nidhinvarghese 8:adf2ba7e6412 170
nidhinvarghese 14:f5c62d30c6fc 171 for(int j =0 ; j<1; j++) // changed to 16 from 8 // Change by Nidhin 30/5/2017 Direct writing to SD card. no loop required
nidhinvarghese 8:adf2ba7e6412 172 {
nidhinvarghese 14:f5c62d30c6fc 173 for(int i=0; i<1024; i++)
nidhinvarghese 8:adf2ba7e6412 174 {
nidhinvarghese 8:adf2ba7e6412 175 concatenate_value2 = readvalue();
nidhinvarghese 14:f5c62d30c6fc 176 //drum11[i]=(uint32_t)concatenate_value2; //drum11[i]=concatenate_value2; drum11[i]=dummyconcat; typecasted to uint32_t
nidhinvarghese 14:f5c62d30c6fc 177 //Change by Nidhin 30/5/2017 Direct writing to SD card.
nidhinvarghese 15:208b146151ba 178 ecg = sd_BP_ecgwrite(&concatenate_value2);
nidhinvarghese 14:f5c62d30c6fc 179
nidhinvarghese 8:adf2ba7e6412 180 maxim_max30102_read_reg(0,&uch_dummy);
nidhinvarghese 8:adf2ba7e6412 181 while(INTR.read()==1);
nidhinvarghese 8:adf2ba7e6412 182 maxim_max30102_read_fifo(&ppgdata);
nidhinvarghese 14:f5c62d30c6fc 183 ppg = sd_BP_ppgwrite(&ppgdata); //Change by Nidhin 30/5/2017 Direct writing to SD card.
nidhinvarghese 14:f5c62d30c6fc 184 //drum21[i]=ppgdata; //Change by Nidhin 30/5/2017 Direct writing to SD card.
nidhinvarghese 8:adf2ba7e6412 185 }
nidhinvarghese 8:adf2ba7e6412 186
nidhinvarghese 14:f5c62d30c6fc 187 //ecg = sd_BP_ecgwrite(drum_ecg); // //Change by Nidhin 30/5/2017 Direct writing to SD card.
nidhinvarghese 14:f5c62d30c6fc 188 //ppg = sd_BP_ppgwrite(drum_ppg); // //Change by Nidhin 30/5/2017 Direct writing to SD card.
nidhinvarghese 8:adf2ba7e6412 189
nidhinvarghese 8:adf2ba7e6412 190
nidhinvarghese 8:adf2ba7e6412 191 }
nidhinvarghese 8:adf2ba7e6412 192
nidhinvarghese 14:f5c62d30c6fc 193 for(int j =0 ; j<1; j++) // changed to 16 from 8
nidhinvarghese 8:adf2ba7e6412 194 {
nidhinvarghese 14:f5c62d30c6fc 195 for(int i=0; i<640; i++)
nidhinvarghese 8:adf2ba7e6412 196 {
nidhinvarghese 8:adf2ba7e6412 197 maxim_max30102_read_reg(0,&uch_dummy);
nidhinvarghese 8:adf2ba7e6412 198 while(INTR.read()==1);
nidhinvarghese 8:adf2ba7e6412 199 maxim_max30102_read_fifo(&ppgdata);
nidhinvarghese 14:f5c62d30c6fc 200 //drum21[i]=ppgdata;
nidhinvarghese 14:f5c62d30c6fc 201 ppg = sd_BP_ppgwrite (&ppgdata); //Change by Nidhin 30/5/2017 Direct writing to SD card.
nidhinvarghese 8:adf2ba7e6412 202 }
nidhinvarghese 8:adf2ba7e6412 203
nidhinvarghese 14:f5c62d30c6fc 204
nidhinvarghese 8:adf2ba7e6412 205
nidhinvarghese 8:adf2ba7e6412 206 }
nidhinvarghese 8:adf2ba7e6412 207 fclose(ecg);
nidhinvarghese 8:adf2ba7e6412 208 fclose(ppg);
nidhinvarghese 8:adf2ba7e6412 209
nidhinvarghese 8:adf2ba7e6412 210 maxim_max30102_reset();
nidhinvarghese 8:adf2ba7e6412 211
nidhinvarghese 8:adf2ba7e6412 212 //-----Reading from file starts //
nidhinvarghese 8:adf2ba7e6412 213
nidhinvarghese 8:adf2ba7e6412 214 //std::ifstream ec1("/sd/123_BP_ECG.csv");
nidhinvarghese 8:adf2ba7e6412 215 //std::ifstream pp1("/sd/123_BP_PPG.csv");
nidhinvarghese 8:adf2ba7e6412 216
nidhinvarghese 8:adf2ba7e6412 217 int eloc = 0;
nidhinvarghese 8:adf2ba7e6412 218 int ploc = 0;
nidhinvarghese 8:adf2ba7e6412 219
nidhinvarghese 8:adf2ba7e6412 220
nidhinvarghese 8:adf2ba7e6412 221 eloc = sd_BP_ECGMAX(pid); // eloc = sd_BP_ECGMAX(123);
nidhinvarghese 8:adf2ba7e6412 222 ploc = sd_BP_PPGMAX(pid); // ploc = sd_BP_PPGMAX(123);
nidhinvarghese 8:adf2ba7e6412 223
nidhinvarghese 8:adf2ba7e6412 224 //bc.printf("The ECG max is at : %d", eloc);
nidhinvarghese 8:adf2ba7e6412 225 //bc.printf("The PPG Max is at : %d", ploc);
nidhinvarghese 8:adf2ba7e6412 226
nidhinvarghese 8:adf2ba7e6412 227 float PWV = 0;
nidhinvarghese 8:adf2ba7e6412 228 int d = 210000;
nidhinvarghese 8:adf2ba7e6412 229 int delta_t = 0;
nidhinvarghese 8:adf2ba7e6412 230 float SBP = 0;
nidhinvarghese 8:adf2ba7e6412 231 float DBP = 0;
nidhinvarghese 8:adf2ba7e6412 232
nidhinvarghese 8:adf2ba7e6412 233 delta_t = (2*(ploc - eloc));
nidhinvarghese 8:adf2ba7e6412 234 PWV = d/delta_t;
nidhinvarghese 8:adf2ba7e6412 235
nidhinvarghese 8:adf2ba7e6412 236 SBP = 0.0508955*PWV+62.559;
nidhinvarghese 8:adf2ba7e6412 237 DBP = (0.0494*PWV + 17.480)+10;
nidhinvarghese 8:adf2ba7e6412 238
nidhinvarghese 14:f5c62d30c6fc 239 /*
nidhinvarghese 14:f5c62d30c6fc 240 //Copying to Structure starts
nidhinvarghese 14:f5c62d30c6fc 241 CalData_info CalData_info_bp;
nidhinvarghese 14:f5c62d30c6fc 242 CalData_info_bp.cal_sbp = (uint16_t) SBP;
nidhinvarghese 14:f5c62d30c6fc 243 CalData_info_bp.cal_dbp = (uint16_t) DBP; NOT REQUIRED
nidhinvarghese 14:f5c62d30c6fc 244 */
nidhinvarghese 14:f5c62d30c6fc 245
nidhinvarghese 14:f5c62d30c6fc 246 BLEMsg_info_bp.cal_data.cal_sbp_dummy = (uint16_t) SBP;
nidhinvarghese 14:f5c62d30c6fc 247 BLEMsg_info_bp.cal_data.cal_dbp_OTtyp = (uint16_t) DBP;
nidhinvarghese 14:f5c62d30c6fc 248
nidhinvarghese 14:f5c62d30c6fc 249
nidhinvarghese 14:f5c62d30c6fc 250
nidhinvarghese 14:f5c62d30c6fc 251 create_single_BPfile(pid); // Copy ECG and PPG data to BP file
nidhinvarghese 14:f5c62d30c6fc 252 structure_file(ptr_BLEMsg_info_bp, pid); // Copy BP structure to main file
nidhinvarghese 14:f5c62d30c6fc 253 bpfile_mainfile(pid);
nidhinvarghese 14:f5c62d30c6fc 254
nidhinvarghese 8:adf2ba7e6412 255 screen_bp1(SBP,DBP);
nidhinvarghese 10:aeff3309002a 256
nidhinvarghese 14:f5c62d30c6fc 257 // Only for testing
nidhinvarghese 14:f5c62d30c6fc 258 }
nidhinvarghese 14:f5c62d30c6fc 259