created separate function for hex to char

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of SS_SensePOC2P0_11Dec2017_USERPID by rashmi v

Committer:
rashmivenkataramaiah
Date:
Wed Nov 15 16:10:05 2017 +0000
Revision:
61:ce5ad0838227
Parent:
56:89eb9a977641
Child:
68:41da0a3ba038
Moved ECG result display from main.cpp to ecg.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikitateggi 1:8316c23ec6b9 1 /** ECG ADS1291 Test program.
nikitateggi 1:8316c23ec6b9 2 ADS1291 is a single channel ECG chip
nikitateggi 1:8316c23ec6b9 3 with a 24 bit Sigma-Delta ADC
nikitateggi 1:8316c23ec6b9 4 */
nikitateggi 1:8316c23ec6b9 5
nikitateggi 1:8316c23ec6b9 6 #include "mbed.h"
nikitateggi 1:8316c23ec6b9 7 #include <string.h>
nikitateggi 1:8316c23ec6b9 8 #include <stdio.h>
nikitateggi 1:8316c23ec6b9 9 #include "ds3231.h"
nikitateggi 1:8316c23ec6b9 10 #include "SDFileSystem.h"
nikitateggi 1:8316c23ec6b9 11 #include "ecg_dec.h"
nikitateggi 1:8316c23ec6b9 12 #include "rtc.h"
nikitateggi 1:8316c23ec6b9 13 #include "sdcard.h"
nikitateggi 3:9a06c2bed650 14 #include "ec_bp.h"
nikitateggi 39:69b09bd87502 15 #include "eeprom_pgm.h"
nidhinvarghese 14:f5c62d30c6fc 16 #include "struct.h" // Added on 31/5/17_Nidhin
rashmivenkataramaiah 61:ce5ad0838227 17 #include "display_modules.h"
rashmivenkataramaiah 43:85a7f399cb9d 18 #include "main.h"
nikitateggi 19:1650bbc923cc 19 //#include "test_type.h" //Added on 31/5/17_Nidhin
avp2417 40:f7f351699288 20
nikitateggi 51:1a4693774b60 21 void low_pass_filter(uint32_t pid);
nikitateggi 51:1a4693774b60 22 void swap(uint32_t *ptr);
nikitateggi 51:1a4693774b60 23
avp2417 40:f7f351699288 24 #define MAX_HR_THRESHOLD 205
avp2417 40:f7f351699288 25 #define MIN_HR_THRESHOLD 25
avp2417 40:f7f351699288 26
avp2417 40:f7f351699288 27
suhasini 26:53ff13a164f3 28 FILE *fpeecg1; // FILE pointer to ECG file
nikitateggi 3:9a06c2bed650 29 Serial pc(USBTX,USBRX);
nikitateggi 1:8316c23ec6b9 30
nikitateggi 1:8316c23ec6b9 31
rashmivenkataramaiah 61:ce5ad0838227 32 void ecg(uint32_t pid) //changed uint16_t to void on 15Nov rashmi
nikitateggi 22:ffa88619551d 33 //float ecg(int pid)
nikitateggi 3:9a06c2bed650 34 {
nikitateggi 20:7c64e6ecad76 35
nikitateggi 20:7c64e6ecad76 36 uint8_t lead_reg=0; // added on 14/06 to check lead -off
nikitateggi 51:1a4693774b60 37 FILE *fpeecg1;
nikitateggi 51:1a4693774b60 38 pc.baud(baud_rate);
nidhinvarghese 14:f5c62d30c6fc 39 //----------------------- Structure for Bluetooth Added Nidhin 1/6/2017-------------------//
nidhinvarghese 14:f5c62d30c6fc 40
nidhinvarghese 14:f5c62d30c6fc 41 //BLUETOOTH STRUCTURE
nidhinvarghese 14:f5c62d30c6fc 42
nidhinvarghese 14:f5c62d30c6fc 43 BLEMsg_info *ptr_BLEMsg_info_ecg, BLEMsg_info_ecg; // A copy of master strcuture [ "BLEMsg_info" ] by name "BLEMsg_info_ecg" is created
nidhinvarghese 14:f5c62d30c6fc 44 ptr_BLEMsg_info_ecg = &BLEMsg_info_ecg; // *ptr_BLEMsg_info_bp is the pointer to local copy;
nidhinvarghese 14:f5c62d30c6fc 45
nidhinvarghese 14:f5c62d30c6fc 46 // Declaration of Date Structure
nidhinvarghese 14:f5c62d30c6fc 47 DateTime_info *ptr_DateTime_info_ecg, DateTime_info_ecg; // A copy of Master Structure "DateTime_info" created,
nidhinvarghese 14:f5c62d30c6fc 48 ptr_DateTime_info_ecg = &DateTime_info_ecg; // Structure pointer points to that copy.
nidhinvarghese 14:f5c62d30c6fc 49
nidhinvarghese 14:f5c62d30c6fc 50
nidhinvarghese 14:f5c62d30c6fc 51 // RTC operations
nidhinvarghese 14:f5c62d30c6fc 52 time_t epoch_time_ecg; //A copy of time_t by name epoch_time_bp is created
nidhinvarghese 14:f5c62d30c6fc 53 epoch_time_ecg = rtc_read(); // time is got from get epoch function.
nidhinvarghese 14:f5c62d30c6fc 54
nidhinvarghese 14:f5c62d30c6fc 55 struct tm * ptr_time_info_ecg, time_info_ecg; // Sturucture copy of tm is created
nidhinvarghese 14:f5c62d30c6fc 56 ptr_time_info_ecg = localtime(&epoch_time_ecg); // Structure accepts the time in local format from "time_t" type.
nidhinvarghese 14:f5c62d30c6fc 57
nidhinvarghese 14:f5c62d30c6fc 58 //BELOW LINE IS TO CHECK Date and TIME
nidhinvarghese 15:208b146151ba 59 //pc.printf("Time is %d: %d: %d\n", (*ptr_time_info_ecg).tm_hour, (*ptr_time_info_ecg).tm_min, (*ptr_time_info_ecg).tm_sec);
nidhinvarghese 15:208b146151ba 60 //pc.printf("Date is %d:%d:%d\n", (*ptr_time_info_ecg).tm_mday, (*ptr_time_info_ecg).tm_mon+1, (*ptr_time_info_ecg).tm_year-100);
nidhinvarghese 14:f5c62d30c6fc 61
nidhinvarghese 14:f5c62d30c6fc 62 //Copying from one structure to the other using variables
nidhinvarghese 14:f5c62d30c6fc 63 DateTime_info_ecg.hour = (uint8_t)(*ptr_time_info_ecg).tm_hour;
nidhinvarghese 14:f5c62d30c6fc 64 DateTime_info_ecg.mins = (uint8_t)(*ptr_time_info_ecg).tm_min;
nidhinvarghese 14:f5c62d30c6fc 65 DateTime_info_ecg.sec = (uint8_t)(*ptr_time_info_ecg).tm_sec;
nidhinvarghese 14:f5c62d30c6fc 66
nidhinvarghese 14:f5c62d30c6fc 67 DateTime_info_ecg.date = (uint8_t) (*ptr_time_info_ecg).tm_mday;
nidhinvarghese 14:f5c62d30c6fc 68 DateTime_info_ecg.month =(uint8_t)(*ptr_time_info_ecg).tm_mon+1;
nidhinvarghese 14:f5c62d30c6fc 69 DateTime_info_ecg.year = (uint8_t)(*ptr_time_info_ecg).tm_year-100;
nidhinvarghese 14:f5c62d30c6fc 70
nidhinvarghese 14:f5c62d30c6fc 71 // Copying Time to Main structure
nidhinvarghese 14:f5c62d30c6fc 72 BLEMsg_info_ecg.date_time.hour = DateTime_info_ecg.hour;
nidhinvarghese 14:f5c62d30c6fc 73 BLEMsg_info_ecg.date_time.mins = DateTime_info_ecg.mins;
nidhinvarghese 14:f5c62d30c6fc 74 BLEMsg_info_ecg.date_time.sec = DateTime_info_ecg.sec;
nidhinvarghese 14:f5c62d30c6fc 75
nidhinvarghese 14:f5c62d30c6fc 76 BLEMsg_info_ecg.date_time.date = DateTime_info_ecg.date ;
nidhinvarghese 14:f5c62d30c6fc 77 BLEMsg_info_ecg.date_time.month = DateTime_info_ecg.month ;
nidhinvarghese 14:f5c62d30c6fc 78 BLEMsg_info_ecg.date_time.year = DateTime_info_ecg.year ;
nidhinvarghese 14:f5c62d30c6fc 79
nidhinvarghese 14:f5c62d30c6fc 80
nidhinvarghese 14:f5c62d30c6fc 81 //Checking if the structure has these values
nidhinvarghese 15:208b146151ba 82 //pc.printf("Time 2 is %d:%d:%d\n", DateTime_info_ecg.hour, DateTime_info_ecg.mins, DateTime_info_ecg.sec);
nidhinvarghese 15:208b146151ba 83 //pc.printf("\t Date is %d:%d:%d\n",DateTime_info_ecg.date, DateTime_info_ecg.month, DateTime_info_ecg.year);
nidhinvarghese 14:f5c62d30c6fc 84
nidhinvarghese 14:f5c62d30c6fc 85
nidhinvarghese 14:f5c62d30c6fc 86 //Loading values to of Test type
nidhinvarghese 14:f5c62d30c6fc 87 test_type_info test_type_info_ecg; // copy of " test_type_info" created
nidhinvarghese 14:f5c62d30c6fc 88 test_type_info_ecg = ECG_Test; // Loaded value 00 to the test type
nidhinvarghese 14:f5c62d30c6fc 89
nidhinvarghese 14:f5c62d30c6fc 90 BLEMsg_info_ecg.test_type = test_type_info_ecg;
nidhinvarghese 14:f5c62d30c6fc 91 //Check if 00 is getting printed
nidhinvarghese 15:208b146151ba 92 //pc.printf("Test Type is : %d\n", test_type_info_ecg);
nidhinvarghese 14:f5c62d30c6fc 93
nidhinvarghese 14:f5c62d30c6fc 94
nidhinvarghese 14:f5c62d30c6fc 95 // Loading values of Length , PID, DID, sampling frequency, number of samples, calculated data.
nikitateggi 39:69b09bd87502 96 BLEMsg_info_ecg.device_id = eprom_read_8(12); // Device ID fixed
nidhinvarghese 14:f5c62d30c6fc 97 BLEMsg_info_ecg.patient_id = (uint32_t)pid; // Patient ID
nidhinvarghese 14:f5c62d30c6fc 98 BLEMsg_info_ecg.sampling_freq = 500; // sampling frrquency
nikitateggi 32:76892fdf5e4c 99 BLEMsg_info_ecg.length = 8022; //Total length of data in bytes 22 B+ 4000 data
nidhinvarghese 14:f5c62d30c6fc 100
nidhinvarghese 14:f5c62d30c6fc 101 /*
nidhinvarghese 14:f5c62d30c6fc 102 //Loading number of samples
nidhinvarghese 14:f5c62d30c6fc 103 NumSamples_info NumSamples_info_bp; //Copy of structure NumSamples_info
nidhinvarghese 14:f5c62d30c6fc 104 NumSamples_info_bp.num_ppg_sample = 1664; // PPG & ECG Sample number loaded in structure copy
nidhinvarghese 14:f5c62d30c6fc 105 NumSamples_info_bp.num_ecg_sample = 1024;
nidhinvarghese 14:f5c62d30c6fc 106 */
nidhinvarghese 14:f5c62d30c6fc 107
nidhinvarghese 14:f5c62d30c6fc 108 BLEMsg_info_ecg.num_samples.num_sample_ppg_dummy = 0 ;// PPG number of samples copied to master struct
nikitateggi 32:76892fdf5e4c 109 BLEMsg_info_ecg.num_samples.num_sample_ecg_OTtyp = 2000 ; // ECG number of samples copied to master struct
nidhinvarghese 14:f5c62d30c6fc 110
nidhinvarghese 14:f5c62d30c6fc 111 //----------------------------------------END Structure for Bluetooth - Added Nidhin 1/6/2017-------
nidhinvarghese 14:f5c62d30c6fc 112
suhasini 26:53ff13a164f3 113 uint32_t concatenate_value2 = 0; // ORG. "int concatenate_value2 = 0;" Nidhin 1/6/17
suhasini 26:53ff13a164f3 114 uint32_t *ecg_ptr; // Added 1/6/2017 Nidhin
nidhinvarghese 14:f5c62d30c6fc 115 ecg_ptr = &concatenate_value2; // Pointer to pass for ECG write into SD card Nidhin 1/6/2017
nidhinvarghese 14:f5c62d30c6fc 116
suhasini 26:53ff13a164f3 117 uint16_t count = 0;
suhasini 26:53ff13a164f3 118 uint16_t fs = 500;
suhasini 26:53ff13a164f3 119 //uint32_t ecg_buf[N_ECG];
suhasini 26:53ff13a164f3 120 Timer t;
suhasini 13:5d3b478ea9c7 121
nikitateggi 51:1a4693774b60 122 /********** Declaration for Peak value detection ***************/
nikitateggi 51:1a4693774b60 123
nikitateggi 51:1a4693774b60 124 int32_t ecg_samp1[1] ; //changed from uint32_t to static double, because LPF output is of static double type - by Suhasini- 5-8-17
nikitateggi 51:1a4693774b60 125 int32_t ecg_samp2[1]; //changed from uint32_t to static double- by Suhasini- 5-8-17
nikitateggi 51:1a4693774b60 126 int32_t ecg_samp3[1]; // to Stores sample 1, 2 & 10th sample //changed from uint32_t to static double- by Suhasini- 5-8-17
nikitateggi 51:1a4693774b60 127
nikitateggi 51:1a4693774b60 128
nikitateggi 51:1a4693774b60 129 uint32_t samp_10 = 28;
nikitateggi 55:057f4af9d581 130 int32_t hi_val; //changed from uint32_t to static double- by Suhasini- 5-8-17 //4thoct nikita non static
nikitateggi 51:1a4693774b60 131 int32_t pk_val[20]; //changed from uint32_t to double - Suhasini - 5-8-17
nikitateggi 51:1a4693774b60 132 int32_t hi_dif = 0; //diff between high value and it's consecutive value //changed from uint32_t to static double- by Suhasini- 5-8-17
nikitateggi 51:1a4693774b60 133 uint16_t pk_pos[20]= {0}; // Variable to hold pointer positio
nikitateggi 51:1a4693774b60 134 uint8_t j=0; // int count1 = N_ECG/fs, a_dif=0, fs1 = fs ,h=0;
nikitateggi 51:1a4693774b60 135 uint32_t m = 400;
suhasini 26:53ff13a164f3 136
suhasini 13:5d3b478ea9c7 137 // ------------------------- Declaration for Heart Rate calculation --------------------------
nikitateggi 51:1a4693774b60 138 uint8_t n=0;
nikitateggi 54:f2a413d5dffd 139 uint16_t pos_dif; // changed from float to uint16_t- Suhasini- 8-8-17, changed back to float- 17th aug,2017
nikitateggi 51:1a4693774b60 140 float HR[10],HR1,t_pos_dif;
nikitateggi 51:1a4693774b60 141 uint8_t t_sec = 60;
nikitateggi 51:1a4693774b60 142 float HR_sum = 0,HR_avg;
nikitateggi 51:1a4693774b60 143 char buffer4[32] = {0};
nikitateggi 51:1a4693774b60 144
nikitateggi 51:1a4693774b60 145
nikitateggi 51:1a4693774b60 146 freqset(); // setting the frequency
nikitateggi 51:1a4693774b60 147 setupfunc();
nikitateggi 51:1a4693774b60 148 lead_reg = ecgsetupfunc();
nikitateggi 22:ffa88619551d 149
nikitateggi 51:1a4693774b60 150
nikitateggi 51:1a4693774b60 151 if (lead_reg==0)
nikitateggi 51:1a4693774b60 152 { // checking for proper lead contact
nikitateggi 51:1a4693774b60 153
nikitateggi 51:1a4693774b60 154 sd_open_ECGfilee(pid); //open temporary ecg.csv file to write 2000 samples of data
nikitateggi 51:1a4693774b60 155 pc.printf( "Raw data is = \n");
nikitateggi 51:1a4693774b60 156
nikitateggi 51:1a4693774b60 157 for(int i=0; i<N_ECG; i++) {
nikitateggi 51:1a4693774b60 158 concatenate_value2= readvalue();
nikitateggi 51:1a4693774b60 159 pc.printf( "%d\n", concatenate_value2);
nikitateggi 51:1a4693774b60 160
nikitateggi 51:1a4693774b60 161 sd_ecgwrite(ecg_ptr); //writing data to temporary file ECG.csv
nikitateggi 51:1a4693774b60 162 }
rashmivenkataramaiah 61:ce5ad0838227 163
nikitateggi 51:1a4693774b60 164 sd_close_ecg(); // closing the ECG file REPLACED Nidhin 1/6/2017 Nidhin
nikitateggi 51:1a4693774b60 165
nikitateggi 51:1a4693774b60 166 low_pass_filter(pid); // low pass filter calculation function
nikitateggi 51:1a4693774b60 167
nikitateggi 22:ffa88619551d 168
suhasini 13:5d3b478ea9c7 169 //----------------------------- PEAK DETECTION AND HEART RATE CALCULATION ---------------------------------------------------
suhasini 26:53ff13a164f3 170
suhasini 13:5d3b478ea9c7 171 // -------------------------------------- PEAK DETECTION -------------------------------------------------------------
suhasini 26:53ff13a164f3 172 // ----------------------------------------- Main loop --------------------------------------------------
suhasini 26:53ff13a164f3 173
nikitateggi 51:1a4693774b60 174 sprintf(buffer4, "/sd/%d_ECG_lpf.csv", pid); // For opening a specific file
nikitateggi 51:1a4693774b60 175 fpeecg1 = fopen(buffer4, "r");
suhasini 13:5d3b478ea9c7 176
nikitateggi 56:89eb9a977641 177 for(uint16_t i=101;i<=((N_ECG-4)-10);i++) //loop started from 100 //4thoct nikita// loop started from 101//3rd nov skipping 100 samples, therefore starting from 101
nikitateggi 56:89eb9a977641 178 {
suhasini 26:53ff13a164f3 179 count++;
suhasini 26:53ff13a164f3 180 rewind(fpeecg1); // Go to start of file each time
suhasini 26:53ff13a164f3 181 fseek(fpeecg1, m, SEEK_CUR); // Update the count value according to move pointer //// after every calc. the pointer moves to 0th position, as we have used fseek, hence to make it jump to the respective position by "m" bytes this command is used
nikitateggi 51:1a4693774b60 182 fread(ecg_samp1, sizeof(int32_t), 1, fpeecg1); // Read sample 1
nikitateggi 51:1a4693774b60 183 fread(ecg_samp2, sizeof(int32_t), 1, fpeecg1); // Read Sample 2
suhasini 26:53ff13a164f3 184 fseek(fpeecg1, samp_10, SEEK_CUR); // Moving to tenth sample
nikitateggi 51:1a4693774b60 185 fread(ecg_samp3, sizeof(int32_t), 1, fpeecg1); // Read 3rd sample
suhasini 26:53ff13a164f3 186
suhasini 26:53ff13a164f3 187 if(ecg_samp1[0]>ecg_samp2[0])
suhasini 13:5d3b478ea9c7 188 {
suhasini 26:53ff13a164f3 189 hi_val = ecg_samp1[0]; //To find the high value
suhasini 26:53ff13a164f3 190 hi_dif = hi_val-ecg_samp3[0];
suhasini 13:5d3b478ea9c7 191
suhasini 26:53ff13a164f3 192 // ---------------------------- If hi_val is greater than next ten input values, then compare the hi_val with the tenth input value.
nikitateggi 51:1a4693774b60 193 // If the diff is greater than 4000, then it is a valid peak (pls chk the below condition)-------------------------------------
nikitateggi 51:1a4693774b60 194 if(hi_dif > 4000)
suhasini 13:5d3b478ea9c7 195 {
suhasini 13:5d3b478ea9c7 196 pk_val[j] = hi_val; //if condition satisfied, put the "pk" value into "pk_val" buffer
nikitateggi 22:ffa88619551d 197 pc.printf("peak value= %d\n",pk_val[j]);
suhasini 26:53ff13a164f3 198 pk_pos[j]=i; // also save the peak's position
nikitateggi 22:ffa88619551d 199 pc.printf("peak position is = %d\n",pk_pos[j]);
nikitateggi 56:89eb9a977641 200 i = i+120; // once confirmed that this is the necessary peak, skip the next 120 input values
nikitateggi 56:89eb9a977641 201 n = j; // where n is the number of peaks detected
suhasini 13:5d3b478ea9c7 202 j = j+1;
nikitateggi 56:89eb9a977641 203 m = m + 484; //similar reason to considering 28, but to skip 120 samples. this cond. is satisfied only when we hit a peak - suhasini_26thjune17
nikitateggi 56:89eb9a977641 204 // as we are skipping 120 samples, the pointer should point to 121st sample // 3rd nov//nikita
avp2417 36:00d96aa14658 205 }
suhasini 26:53ff13a164f3 206
suhasini 13:5d3b478ea9c7 207 else
suhasini 13:5d3b478ea9c7 208 {
suhasini 26:53ff13a164f3 209 m = m+4; // this is when we do not hit a peak and have to continue searching thru, hence move to the next sample and not skip 120 samples- - suhasini_26thjune17
suhasini 13:5d3b478ea9c7 210 }
avp2417 36:00d96aa14658 211 }
suhasini 26:53ff13a164f3 212 else
suhasini 26:53ff13a164f3 213 {
suhasini 26:53ff13a164f3 214 m = m+4;
suhasini 13:5d3b478ea9c7 215 }
suhasini 26:53ff13a164f3 216 //pc.printf("i=%d",i);
suhasini 13:5d3b478ea9c7 217 }
suhasini 26:53ff13a164f3 218 n=n+1;
suhasini 26:53ff13a164f3 219 pc.printf("n=%d\n",n);
suhasini 26:53ff13a164f3 220
nikitateggi 51:1a4693774b60 221 pc.printf("to enter HR loop\n");
suhasini 13:5d3b478ea9c7 222 // ----------------- HEART RATE LOGIC ---------------------------
nikitateggi 3:9a06c2bed650 223
suhasini 26:53ff13a164f3 224 for(uint16_t i = 0;i < n-1;i++)
suhasini 13:5d3b478ea9c7 225 {
suhasini 13:5d3b478ea9c7 226 pos_dif = pk_pos[i+1] - pk_pos[i]; // difference between two consequtive peaks
nikitateggi 51:1a4693774b60 227 pc.printf("peak position diff is = %d\n",pos_dif);
nikitateggi 54:f2a413d5dffd 228 t_pos_dif = (float)pos_dif/fs; // sample difference between peak positions divided by sampling frequency gives the difference value in terms of actual time
suhasini 13:5d3b478ea9c7 229 pc.printf("time in seconds is = %f\n",t_pos_dif);
suhasini 13:5d3b478ea9c7 230 HR[i] = t_sec/t_pos_dif; //HR calculation
suhasini 13:5d3b478ea9c7 231 pc.printf("Heart Rate is = %f\n",HR[i]);
suhasini 13:5d3b478ea9c7 232 HR1 = HR[0];
suhasini 26:53ff13a164f3 233 }
suhasini 13:5d3b478ea9c7 234
suhasini 26:53ff13a164f3 235
suhasini 13:5d3b478ea9c7 236 // ---------------------- To average individual HRs for higher number of samples -----------------------
suhasini 26:53ff13a164f3 237 for(uint16_t i = 0;i < n-1;i++)
suhasini 13:5d3b478ea9c7 238 {
avp2417 40:f7f351699288 239 HR_sum = HR[i] + HR_sum;
suhasini 13:5d3b478ea9c7 240 }
avp2417 40:f7f351699288 241 HR_avg = (HR_sum / (n-1)); // To find average of all the individual HRs calculated
suhasini 13:5d3b478ea9c7 242 printf("Heart Rate sum is = %f\n",HR_sum);
suhasini 26:53ff13a164f3 243 printf("Heart Rate avg is = %f\n",HR_avg);
nidhinvarghese 14:f5c62d30c6fc 244
suhasini 26:53ff13a164f3 245 fclose(fpeecg1);
suhasini 26:53ff13a164f3 246 pc.printf("temporary file closed\n");
nikitateggi 31:0f67acfc6236 247
avp2417 40:f7f351699288 248 if(HR_avg > MAX_HR_THRESHOLD || HR_avg < MIN_HR_THRESHOLD)
nikitateggi 31:0f67acfc6236 249 {
rashmivenkataramaiah 61:ce5ad0838227 250 poc2p0_send_device_msg_res(ECG_RESULT_IMPROPER_RES); //added on 15Nov rashmi
rashmivenkataramaiah 61:ce5ad0838227 251 screen_ecg_error(); //added on 15Nov rashmi
nikitateggi 39:69b09bd87502 252 delete_subfiles(pid); // added on 11/8/17 nikita
rashmivenkataramaiah 61:ce5ad0838227 253 //return 1; // out of range condition returning 1 //nikita//10/7
nikitateggi 31:0f67acfc6236 254 }
nikitateggi 31:0f67acfc6236 255 else
nikitateggi 31:0f67acfc6236 256 {
rashmivenkataramaiah 61:ce5ad0838227 257 poc2p0_send_device_test_result_res (ECG_TEST_SUCCSS_RES, (uint32_t)HR_avg); //added on 15Nov rashmi
rashmivenkataramaiah 61:ce5ad0838227 258 screen_ecg_2(HR_avg); //added on 15Nov rashmi
nikitateggi 31:0f67acfc6236 259
nikitateggi 31:0f67acfc6236 260 BLEMsg_info_ecg.cal_data.cal_sbp_dummy = 0;
rashmivenkataramaiah 41:1141a75eacc4 261 BLEMsg_info_ecg.cal_data.cal_dbp_OTtyp = HR_avg; //To be modified after HR code is added.
nidhinvarghese 14:f5c62d30c6fc 262
rashmivenkataramaiah 41:1141a75eacc4 263 structure_file(ptr_BLEMsg_info_ecg, pid); //copy the ECG structure to Main file //COMMENTED Nidhin 10/6/2017
rashmivenkataramaiah 41:1141a75eacc4 264 ecgfile_mainfile(pid); // copy raw data to the main file and ECG file is cleared. //COMMENTED Nidhin 10/6/2017
rashmivenkataramaiah 43:85a7f399cb9d 265 if(get_filecreated_status() == false) //if file is in write mode
rashmivenkataramaiah 43:85a7f399cb9d 266 {
rashmivenkataramaiah 43:85a7f399cb9d 267 set_filecreated_status();
rashmivenkataramaiah 43:85a7f399cb9d 268 increment_filepid ();
rashmivenkataramaiah 43:85a7f399cb9d 269 }
nikitateggi 31:0f67acfc6236 270 pc.printf("Closed the main file\n");
rashmivenkataramaiah 61:ce5ad0838227 271 //return HR_avg;
nikitateggi 31:0f67acfc6236 272 }
nikitateggi 22:ffa88619551d 273 }
nikitateggi 20:7c64e6ecad76 274 else
nikitateggi 20:7c64e6ecad76 275 {
rashmivenkataramaiah 61:ce5ad0838227 276 poc2p0_send_device_msg_res(ECG_LEADOFF_RES); //added on 15Nov rashmi
rashmivenkataramaiah 61:ce5ad0838227 277 screen_ecg_lead(); //added on 15Nov rashmi
nikitateggi 31:0f67acfc6236 278 pc.printf("improper lead connection");
rashmivenkataramaiah 61:ce5ad0838227 279 //return 0;
nikitateggi 20:7c64e6ecad76 280 }
suhasini 26:53ff13a164f3 281 pc.printf("closing temporary file\n");
nikitateggi 51:1a4693774b60 282 } // End of main function
nikitateggi 51:1a4693774b60 283 /************************* LOW PASS FILTER FUNCTION **************************/
nikitateggi 51:1a4693774b60 284
nikitateggi 51:1a4693774b60 285 void low_pass_filter(uint32_t pid)
nikitateggi 51:1a4693774b60 286 {
nikitateggi 51:1a4693774b60 287 FILE *fpecg1; // FILE pointer to read raw data ECG file added by Suhasini 5-8-17
nikitateggi 51:1a4693774b60 288 FILE *fpeecg_lpf; // FILE pointer to read and write into LPF output file added by Suhasini 5-8-17
nikitateggi 55:057f4af9d581 289 // uint32_t ecg_raw_temp[5]; // changed by rohit on 13 sep
nikitateggi 54:f2a413d5dffd 290 uint32_t ecg_raw[5]; // Buff of 5 samples created to Stores input samples for LPF added by Suhasini - 5-8-17 added by rohit
nikitateggi 54:f2a413d5dffd 291 uint32_t lpf_op[5]= {0.0}; // for LPF output added by Suhasini - 5-8-17 // changed from double to int32_t- 16-8-17 added by rohit
nikitateggi 51:1a4693774b60 292
nikitateggi 51:1a4693774b60 293 uint32_t lpf_4samp = 0; // Variable to move 16 samples back from current in input file - changed by Suhasini- 5-8-17
nikitateggi 51:1a4693774b60 294 char buffer3[32];
nikitateggi 51:1a4693774b60 295 char buffer4[32]; // Added by Suhasini - 5-8-17
nikitateggi 51:1a4693774b60 296 const float b[5]= {0.0000624,0.0002495,0.0003743,0.0002495,0.0000624}; //Numerator co-efficients of low-pass butter-worth filter of order=4
nikitateggi 51:1a4693774b60 297 const float a[5]= {1.0000,-3.5078,4.6409,-2.7427,0.6105}; //Denominator co-efficients of low-pass butter-worth filter of order=4
nikitateggi 51:1a4693774b60 298
nikitateggi 51:1a4693774b60 299
nikitateggi 51:1a4693774b60 300
nikitateggi 51:1a4693774b60 301 sprintf(buffer3, "/sd/%d_ECG.csv", pid); // opening raw data file to read
nikitateggi 51:1a4693774b60 302 fpecg1 = fopen(buffer3, "r"); // to read data from the above opened temp file
nikitateggi 51:1a4693774b60 303 pc.printf("entered raw data file\n");
nikitateggi 51:1a4693774b60 304 sprintf(buffer4, "/sd/%d_ECG_lpf.csv", pid); // opening empty file to write lpf data
nikitateggi 51:1a4693774b60 305 fpeecg_lpf = fopen(buffer4, "a"); // to read data from and write data into the above opened file // changed from "a+" to "a"
nikitateggi 51:1a4693774b60 306 pc.printf("entered LPF data file\n");
nikitateggi 51:1a4693774b60 307
nikitateggi 51:1a4693774b60 308 fwrite(&lpf_op, sizeof(int32_t), 4, fpeecg_lpf);
nikitateggi 51:1a4693774b60 309 pc.printf("first 4 LPF data %d\n %d\n %d\n %d\n", lpf_op[0],lpf_op[1],lpf_op[2],lpf_op[3]);
nikitateggi 51:1a4693774b60 310
rashmivenkataramaiah 61:ce5ad0838227 311 //pc.printf("low pass filter output is= %d\n",lpf_op[4]); //message to display LPF output
nikitateggi 51:1a4693774b60 312
nikitateggi 51:1a4693774b60 313 fread(&ecg_raw, sizeof(uint32_t), 5, fpecg1);
nikitateggi 51:1a4693774b60 314 // pc.printf("first 4 RAW data %d\n %d\n %d\n %d\n %d\n", ecg_raw[0],ecg_raw[1],ecg_raw[2],ecg_raw[3],ecg_raw[4]);
nikitateggi 51:1a4693774b60 315
nikitateggi 55:057f4af9d581 316 for (uint16_t i = 1; i <= (N_ECG-4); i++) { // reduced loop length to check i/p and o/p timing //4thoct nikita<=
nikitateggi 51:1a4693774b60 317 //fseek(fpecg1, lpf_4samp, SEEK_CUR); // Update the count value according to move pointer
nikitateggi 51:1a4693774b60 318 // fread(&ecg_raw, sizeof(uint32_t), 5, fpecg1); // Read sample 1 of i/p
nikitateggi 51:1a4693774b60 319
nikitateggi 51:1a4693774b60 320 /**********rohit***************/
nikitateggi 51:1a4693774b60 321 //fread(&ecg_raw_temp, sizeof(uint32_t), 5, fpecg1);
nikitateggi 51:1a4693774b60 322 //memcpy(ecg_raw, ecg_raw_temp,sizeof(ecg_raw));
nikitateggi 51:1a4693774b60 323
nikitateggi 51:1a4693774b60 324 // fread(ecg_raw, sizeof(uint32_t), 5, fpecg1);
nikitateggi 51:1a4693774b60 325
nikitateggi 51:1a4693774b60 326 /*********** filter output for the ECG data for a low-pass butter-worth filter of order=4 is as follows******************/
nikitateggi 51:1a4693774b60 327
nikitateggi 51:1a4693774b60 328
nikitateggi 51:1a4693774b60 329
nikitateggi 51:1a4693774b60 330 (lpf_op[4]) = (int32_t)(b[0]*((ecg_raw[4]))) + (int32_t)(b[1]*((ecg_raw[3]))) + (int32_t)(b[2]*((ecg_raw[2]))) + (int32_t)(b[3]*((ecg_raw[1]))) + (int32_t)(b[4]*((ecg_raw[0]))) - (int32_t)(a[1]*lpf_op[3]) - (int32_t)(a[2]*lpf_op[2]) - (int32_t)(a[3]*lpf_op[1]) - (int32_t)(a[4]*lpf_op[0]);
nikitateggi 51:1a4693774b60 331
nikitateggi 51:1a4693774b60 332
nikitateggi 51:1a4693774b60 333 fwrite(&lpf_op[4], sizeof(int32_t), 1, fpeecg_lpf);
nikitateggi 51:1a4693774b60 334
nikitateggi 51:1a4693774b60 335 //pc.printf("pointer to output value is= %ld\n",ftell(fpeecg_lpf));
nikitateggi 51:1a4693774b60 336 pc.printf("%d\n",lpf_op[4]); // to print LPF output on hyperterminal
nikitateggi 51:1a4693774b60 337 swap(lpf_op);
nikitateggi 51:1a4693774b60 338 /*
nikitateggi 51:1a4693774b60 339 lpf_op[0] = lpf_op[1];
nikitateggi 51:1a4693774b60 340 lpf_op[1] = lpf_op[2];
nikitateggi 51:1a4693774b60 341 lpf_op[2] = lpf_op[3];
nikitateggi 51:1a4693774b60 342 lpf_op[3] = lpf_op[4];
nikitateggi 51:1a4693774b60 343 */
nikitateggi 51:1a4693774b60 344 // rewind(fpecg1); commented by rohit
nikitateggi 51:1a4693774b60 345 // rewind(fpeecg_lpf); // commented by nikita 5/09/17 // no need to rewind as single array member result is stored in the file
nikitateggi 51:1a4693774b60 346 // lpf_4samp = (i*4); // commented by rohit
nikitateggi 51:1a4693774b60 347
nikitateggi 51:1a4693774b60 348 // lpfop_4samp = (i*4);
nikitateggi 51:1a4693774b60 349 swap(ecg_raw); // 8 changed to 4, because datatype has been changed from double to int32_t- suhasini- 16/8/17 /// commented by nikita 5/09/17 // not used anywhere//
nikitateggi 51:1a4693774b60 350 fread(&ecg_raw[4], sizeof(uint32_t), 1, fpecg1);
rashmivenkataramaiah 61:ce5ad0838227 351 //pc.printf("5th samples: %d\n",ecg_raw[4]);
nikitateggi 51:1a4693774b60 352
nikitateggi 51:1a4693774b60 353 //fseek(fpecg1, -16 , SEEK_CUR);
nikitateggi 51:1a4693774b60 354 // memset(ecg_raw,0,sizeof(ecg_raw));
nikitateggi 51:1a4693774b60 355 }
nikitateggi 51:1a4693774b60 356
nikitateggi 51:1a4693774b60 357
nikitateggi 51:1a4693774b60 358 fclose(fpecg1);
nikitateggi 51:1a4693774b60 359 fclose(fpeecg_lpf);
nikitateggi 51:1a4693774b60 360 pc.printf("temporary file closed after LPF\n");
nikitateggi 51:1a4693774b60 361
nikitateggi 51:1a4693774b60 362 }
nikitateggi 51:1a4693774b60 363
nikitateggi 51:1a4693774b60 364 void swap(uint32_t *ptr)
nikitateggi 51:1a4693774b60 365 {
nikitateggi 51:1a4693774b60 366 int i;
nikitateggi 51:1a4693774b60 367 for(i=0;i<4;i++)
nikitateggi 51:1a4693774b60 368 ptr[i] = ptr[i+1];
nikitateggi 51:1a4693774b60 369 }