Changes done in ECG and BT

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of merged_code2_20sept_2017_4th_oct_2017 by nikita teggi

Committer:
avp2417
Date:
Sun Aug 20 16:21:54 2017 +0000
Revision:
40:f7f351699288
Parent:
39:69b09bd87502
Child:
41:1141a75eacc4
Modification of BP, ECG and GLC operating ranges into 11th Aug Dharwad code. - By Amit on 12th Aug 2017

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nidhinvarghese 15:208b146151ba 1 // Code Modified with Bluetooth Structure Nidhin 2/6/17
nidhinvarghese 15:208b146151ba 2 ///
nidhinvarghese 15:208b146151ba 3 #include "mbed.h"
nidhinvarghese 15:208b146151ba 4 #include "glc.h"
nidhinvarghese 15:208b146151ba 5 #include "i2c_dec.h"
nidhinvarghese 15:208b146151ba 6 //#include "ili9325.h"
nidhinvarghese 15:208b146151ba 7 //#include "lcd_base.h"
nidhinvarghese 15:208b146151ba 8 #include "display_modules.h"
nidhinvarghese 15:208b146151ba 9 //#include "touch_modules.h"
nidhinvarghese 15:208b146151ba 10 #include "rtc.h"
nidhinvarghese 15:208b146151ba 11 #include "sdcard.h"
nikitateggi 20:7c64e6ecad76 12 #include "eeprom_pgm.h" // Nidhin 12/6/17
nidhinvarghese 15:208b146151ba 13
nidhinvarghese 15:208b146151ba 14 #include "struct.h" //Nidhin 31/5/2017
nikitateggi 19:1650bbc923cc 15 //#include "test_type.h" //Nidhin 31/5/2017
nidhinvarghese 15:208b146151ba 16
avp2417 40:f7f351699288 17 #define MAX_GLC_THRESHOLD 600
avp2417 40:f7f351699288 18 #define MIN_GLC_THRESHOLD 10
nidhinvarghese 15:208b146151ba 19
nidhinvarghese 15:208b146151ba 20 void sample();
nidhinvarghese 15:208b146151ba 21 void Compute();
nidhinvarghese 15:208b146151ba 22 void i2c_spec();
nidhinvarghese 15:208b146151ba 23 Ticker flipper;
nidhinvarghese 15:208b146151ba 24 Ticker flipper1;
nikitateggi 16:8fa1d7fd7953 25 unsigned char GLC_START = 0;
nidhinvarghese 15:208b146151ba 26 //osTimerDef (sample_data, sample); // define timers
nidhinvarghese 15:208b146151ba 27 //osTimerDef (Compute_GLC, Compute);
nidhinvarghese 15:208b146151ba 28 DigitalIn test_strip(PTA12);
nidhinvarghese 15:208b146151ba 29
nidhinvarghese 15:208b146151ba 30 AnalogIn Ain(PTC1);//Electrode one
nidhinvarghese 15:208b146151ba 31 AnalogIn Ain2(PTE29); // Electrode two
nidhinvarghese 15:208b146151ba 32 AnalogOut DAC_signal(PTE30);
nidhinvarghese 15:208b146151ba 33 Serial gtc(USBTX, USBRX);
nidhinvarghese 15:208b146151ba 34 //unsigned char c;
nidhinvarghese 15:208b146151ba 35 //unsigned char HOME = 0;
nidhinvarghese 15:208b146151ba 36
nidhinvarghese 15:208b146151ba 37 //signed char test_strip = 0;
nidhinvarghese 15:208b146151ba 38 static uint16_t mgdl = 0;
nikitateggi 16:8fa1d7fd7953 39 static uint16_t mgdll = 0;
nikitateggi 16:8fa1d7fd7953 40 uint16_t daata1= 0;
nikitateggi 16:8fa1d7fd7953 41 uint16_t data2 = 0;
nidhinvarghese 15:208b146151ba 42
nikitateggi 16:8fa1d7fd7953 43 static uint16_t data1n= 0;
nikitateggi 16:8fa1d7fd7953 44 static uint16_t data2n = 0;
nikitateggi 16:8fa1d7fd7953 45 uint16_t datafinal1 = 0;
nikitateggi 16:8fa1d7fd7953 46 uint16_t datafinal2 = 0;
nidhinvarghese 15:208b146151ba 47 //unsigned int data_diff = 0;
nidhinvarghese 15:208b146151ba 48 unsigned char err = 0;
nidhinvarghese 15:208b146151ba 49
nidhinvarghese 17:22a87e12fb3a 50 uint32_t glcraw[2]; // Nidhin 31/5/2017
nidhinvarghese 15:208b146151ba 51 uint8_t chk_glc_data = 0;
nikitateggi 20:7c64e6ecad76 52 uint16_t glc_testtype; // Nidhin 12/6/2017
nidhinvarghese 15:208b146151ba 53
nidhinvarghese 15:208b146151ba 54 Serial ble(PTC4,PTC3);
nidhinvarghese 15:208b146151ba 55
nidhinvarghese 15:208b146151ba 56
nidhinvarghese 15:208b146151ba 57
nidhinvarghese 15:208b146151ba 58 void sample()
nidhinvarghese 15:208b146151ba 59
nidhinvarghese 15:208b146151ba 60 {
nidhinvarghese 15:208b146151ba 61 data1n = Ain.read_u16();
nidhinvarghese 15:208b146151ba 62 data2n = Ain2.read_u16();
nidhinvarghese 15:208b146151ba 63 daata1 = beta*daata1+alpha*data1n;
nidhinvarghese 15:208b146151ba 64 data2 = beta*data2+alpha*data2n;
nidhinvarghese 15:208b146151ba 65
nidhinvarghese 15:208b146151ba 66 if( ((daata1-data2) >=(data2/10) && (daata1 > data2)) || ((data2-daata1) >=(data2/10) && (data2 > daata1)) )
nidhinvarghese 15:208b146151ba 67 {
nidhinvarghese 15:208b146151ba 68
nidhinvarghese 15:208b146151ba 69 err = 1;
nidhinvarghese 15:208b146151ba 70
nidhinvarghese 15:208b146151ba 71 }
nidhinvarghese 15:208b146151ba 72 else
nidhinvarghese 15:208b146151ba 73 {
nidhinvarghese 15:208b146151ba 74 err = 0;
nidhinvarghese 15:208b146151ba 75 //gtc.printf("\n", data1n, data2n); only for testing added nidhin 5Jun17
nidhinvarghese 15:208b146151ba 76 }
nidhinvarghese 15:208b146151ba 77
nidhinvarghese 15:208b146151ba 78 }
nidhinvarghese 15:208b146151ba 79
nidhinvarghese 15:208b146151ba 80
nidhinvarghese 15:208b146151ba 81
nidhinvarghese 15:208b146151ba 82 void Compute()
nidhinvarghese 15:208b146151ba 83 {
nidhinvarghese 15:208b146151ba 84
nikitateggi 22:ffa88619551d 85 flipper.detach(); // ticker for sampling is disabled
nikitateggi 16:8fa1d7fd7953 86 datafinal1 = (uint16_t ) NUMB1*(data2*VREF/(TOTAL))-NUMB2;
nikitateggi 16:8fa1d7fd7953 87 datafinal2 = (uint16_t ) NUMB1*(daata1*VREF/(TOTAL))-NUMB2;
nikitateggi 16:8fa1d7fd7953 88 mgdl = (uint16_t) NUMB1*(((daata1+data2)/2)*VREF/(TOTAL))-NUMB2; // Nidhin 5/6/17 mgdl = (uint16_t) NUMB1*(((daata1+data2)/2)*VREF/(TOTAL))-NUMB2;
nidhinvarghese 15:208b146151ba 89
nidhinvarghese 15:208b146151ba 90
nidhinvarghese 15:208b146151ba 91
nidhinvarghese 15:208b146151ba 92 if (err == 0)
nidhinvarghese 15:208b146151ba 93 {
nidhinvarghese 15:208b146151ba 94
nidhinvarghese 15:208b146151ba 95 glc_4(mgdl);
nikitateggi 22:ffa88619551d 96
nidhinvarghese 15:208b146151ba 97 }
nidhinvarghese 15:208b146151ba 98
nidhinvarghese 15:208b146151ba 99 else
nikitateggi 22:ffa88619551d 100 {
nidhinvarghese 17:22a87e12fb3a 101 // gtc.printf("err=1\n"); Commented Nidhin 8/6/2017
nikitateggi 22:ffa88619551d 102
nikitateggi 22:ffa88619551d 103 }
nikitateggi 16:8fa1d7fd7953 104 GLC_START = 0;
nikitateggi 22:ffa88619551d 105 flipper1.detach();// ticker for computation is disabled
nidhinvarghese 15:208b146151ba 106 }
nidhinvarghese 15:208b146151ba 107
nidhinvarghese 15:208b146151ba 108
nikitateggi 22:ffa88619551d 109 void glc(int32_t pid){
nikitateggi 16:8fa1d7fd7953 110
nidhinvarghese 15:208b146151ba 111 char t_s_d = 0;
nikitateggi 22:ffa88619551d 112 i2c_spec(); // programming the i2c expander
nidhinvarghese 15:208b146151ba 113
nikitateggi 22:ffa88619551d 114 sd_open_GLCfilee(pid); // Opens GLC file in APPEND mode
nidhinvarghese 15:208b146151ba 115
nidhinvarghese 15:208b146151ba 116 // Bluetooth Structure created globally
nidhinvarghese 15:208b146151ba 117 BLEMsg_info *ptr_BLEMsg_info_glc, BLEMsg_info_glc; // A copy of master strcuture [ "BLEMsg_info" ] by name "BLEMsg_info_bp" is created
nidhinvarghese 15:208b146151ba 118 ptr_BLEMsg_info_glc = &BLEMsg_info_glc; // *ptr_BLEMsg_info_bp is the pointer to local copy;
nidhinvarghese 15:208b146151ba 119 // Cant declare this statement outside main. Causing conflicts
nidhinvarghese 15:208b146151ba 120
nidhinvarghese 15:208b146151ba 121 // Declaration of Date Structure
nidhinvarghese 15:208b146151ba 122 DateTime_info *ptr_DateTime_info_glc, DateTime_info_glc; // A copy of Master Structure "DateTime_info" created,
nidhinvarghese 15:208b146151ba 123 ptr_DateTime_info_glc = &DateTime_info_glc; // Structure pointer points to that copy.
nidhinvarghese 15:208b146151ba 124
nidhinvarghese 15:208b146151ba 125
nidhinvarghese 15:208b146151ba 126
nidhinvarghese 15:208b146151ba 127 // RTC operations
nidhinvarghese 15:208b146151ba 128 time_t epoch_time_glc; //A copy of time_t by name epoch_time_bp is created
nidhinvarghese 15:208b146151ba 129 epoch_time_glc = rtc_read(); // time is got from get epoch function.
nidhinvarghese 15:208b146151ba 130
nidhinvarghese 15:208b146151ba 131 struct tm * ptr_time_info_glc, time_info_glc; // Sturucture copy of tm is created
nidhinvarghese 15:208b146151ba 132 ptr_time_info_glc = localtime(&epoch_time_glc); // Structure accepts the time in local format from "time_t" type.
nidhinvarghese 15:208b146151ba 133 /*
nidhinvarghese 15:208b146151ba 134 //BELOW LINE IS TO CHECK Date and TIME
nidhinvarghese 15:208b146151ba 135 gtc.printf("Time is - %d:%d:%d\n", (*ptr_time_info_glc).tm_hour, (*ptr_time_info_glc).tm_min, (*ptr_time_info_glc).tm_sec);
nidhinvarghese 15:208b146151ba 136 gtc.printf("Date is - %d:%d:%d\n", (*ptr_time_info_glc).tm_mday, (*ptr_time_info_glc).tm_mon+1, (*ptr_time_info_glc).tm_year-100);
nidhinvarghese 15:208b146151ba 137 */
nidhinvarghese 15:208b146151ba 138 //Copying from one structure to the other using variables
nidhinvarghese 15:208b146151ba 139 DateTime_info_glc.hour = (uint8_t)(*ptr_time_info_glc).tm_hour;
nidhinvarghese 15:208b146151ba 140 DateTime_info_glc.mins = (uint8_t)(*ptr_time_info_glc).tm_min;
nidhinvarghese 15:208b146151ba 141 DateTime_info_glc.sec = (uint8_t)(*ptr_time_info_glc).tm_sec;
nidhinvarghese 15:208b146151ba 142
nidhinvarghese 15:208b146151ba 143 DateTime_info_glc.date = (uint8_t) (*ptr_time_info_glc).tm_mday;
nidhinvarghese 15:208b146151ba 144 DateTime_info_glc.month =(uint8_t)(*ptr_time_info_glc).tm_mon+1;
nidhinvarghese 15:208b146151ba 145 DateTime_info_glc.year = (uint8_t)(*ptr_time_info_glc).tm_year-100;
nidhinvarghese 15:208b146151ba 146
nidhinvarghese 15:208b146151ba 147 // Copying Time to Main structure
nidhinvarghese 15:208b146151ba 148 BLEMsg_info_glc.date_time.hour = DateTime_info_glc.hour;
nidhinvarghese 15:208b146151ba 149 BLEMsg_info_glc.date_time.mins = DateTime_info_glc.mins;
nidhinvarghese 15:208b146151ba 150 BLEMsg_info_glc.date_time.sec = DateTime_info_glc.sec;
nidhinvarghese 15:208b146151ba 151
nidhinvarghese 15:208b146151ba 152 BLEMsg_info_glc.date_time.date = DateTime_info_glc.date ;
nidhinvarghese 15:208b146151ba 153 BLEMsg_info_glc.date_time.month = DateTime_info_glc.month ;
nidhinvarghese 15:208b146151ba 154 BLEMsg_info_glc.date_time.year = DateTime_info_glc.year ;
nidhinvarghese 15:208b146151ba 155
nidhinvarghese 15:208b146151ba 156 /*
nidhinvarghese 15:208b146151ba 157 //Checking if the structure has these values
nidhinvarghese 15:208b146151ba 158 gtc.printf("\nTime 2 is - %d:%d:%d\n", DateTime_info_glc.hour, DateTime_info_glc.mins, DateTime_info_glc.sec);
nidhinvarghese 15:208b146151ba 159 gtc.printf("\nDate 2 is - %d:%d:%d\n",DateTime_info_glc.date, DateTime_info_glc.month, DateTime_info_glc.year);
nidhinvarghese 15:208b146151ba 160 */
nidhinvarghese 15:208b146151ba 161
nidhinvarghese 15:208b146151ba 162 //Loading values to of Test type
nidhinvarghese 15:208b146151ba 163 test_type_info test_type_info_glc; // copy of " test_type_info" created
nidhinvarghese 15:208b146151ba 164 test_type_info_glc = BG_Test; // Loaded value 00 to the test type
nidhinvarghese 15:208b146151ba 165
nidhinvarghese 15:208b146151ba 166 BLEMsg_info_glc.test_type = test_type_info_glc;
nidhinvarghese 15:208b146151ba 167 //Check if 01 is getting printed
nidhinvarghese 15:208b146151ba 168 //gtc.printf("\nTest Type for BG is 01 = %d\n", test_type_info_glc);
nidhinvarghese 15:208b146151ba 169
nidhinvarghese 15:208b146151ba 170
nidhinvarghese 15:208b146151ba 171 // Loading values of Length , PID, DID, sampling frequency, number of samples, calculated data.
nikitateggi 39:69b09bd87502 172 BLEMsg_info_glc.device_id = eprom_read_8(12); // Device ID fixed // change on 11/8/17 nikita
nikitateggi 16:8fa1d7fd7953 173 BLEMsg_info_glc.patient_id = (uint16_t)pid; // Patient ID
nidhinvarghese 15:208b146151ba 174 BLEMsg_info_glc.sampling_freq = 0; // sampling frrquency
nidhinvarghese 15:208b146151ba 175 BLEMsg_info_glc.length = 30; //Total length of data in bytes 22 B+10752 B
nidhinvarghese 15:208b146151ba 176
nidhinvarghese 15:208b146151ba 177 BLEMsg_info_glc.num_samples.num_sample_ecg_OTtyp = 2;
nidhinvarghese 15:208b146151ba 178 BLEMsg_info_glc.num_samples.num_sample_ppg_dummy = 0;
nidhinvarghese 15:208b146151ba 179
nidhinvarghese 15:208b146151ba 180
nidhinvarghese 15:208b146151ba 181
nidhinvarghese 15:208b146151ba 182 GLC_START = 1;
nidhinvarghese 15:208b146151ba 183 // ble.baud(115200);
nidhinvarghese 15:208b146151ba 184 if(GLC_START == 1)
nidhinvarghese 15:208b146151ba 185 {
nidhinvarghese 15:208b146151ba 186 glc_1(); // displaying messages to insert the test strip
nidhinvarghese 15:208b146151ba 187 err = 0;
nidhinvarghese 15:208b146151ba 188 data1n = 0;
nidhinvarghese 15:208b146151ba 189 data2n = 0;
nidhinvarghese 15:208b146151ba 190 daata1 = 0;
nidhinvarghese 15:208b146151ba 191 data2 = 0;
nidhinvarghese 15:208b146151ba 192 //sd_open_GLCfile(pid); // opening the glc file Commented
nidhinvarghese 15:208b146151ba 193
nidhinvarghese 15:208b146151ba 194 while((test_strip));
nidhinvarghese 15:208b146151ba 195
nidhinvarghese 15:208b146151ba 196 if (!test_strip) // checks for the presence of test strip
nidhinvarghese 15:208b146151ba 197 {
nidhinvarghese 15:208b146151ba 198 flipper.attach(&sample, 0.8);
nidhinvarghese 15:208b146151ba 199 t_s_d = 0;
nidhinvarghese 15:208b146151ba 200 glc_2(); // displaying message to insert blood
nidhinvarghese 15:208b146151ba 201
nidhinvarghese 15:208b146151ba 202 while((!t_s_d) && (!test_strip))
nidhinvarghese 15:208b146151ba 203 {
nidhinvarghese 15:208b146151ba 204 //if(chk_glc_data == 0) //Nidhin 31/5/2017
nidhinvarghese 15:208b146151ba 205 //{
nidhinvarghese 15:208b146151ba 206 /*
nikitateggi 16:8fa1d7fd7953 207 glcraw[0] = (uint16_t) data1n; //Nidhin 31/5/2017
nikitateggi 16:8fa1d7fd7953 208 glcraw[1] = (uint16_t) data2n; // Nidhin 31/5/2017
nidhinvarghese 15:208b146151ba 209
nidhinvarghese 15:208b146151ba 210 gtc.printf("datain is =%d\n", data1n);
nidhinvarghese 15:208b146151ba 211 gtc.printf("data2n is = %d\n", data2n);
nidhinvarghese 15:208b146151ba 212
nidhinvarghese 15:208b146151ba 213 gtc.printf("datain is =%d\n", data1n);
nidhinvarghese 15:208b146151ba 214 gtc.printf("data2n is = %d\n", data2n);
nidhinvarghese 15:208b146151ba 215 */
nidhinvarghese 15:208b146151ba 216 //chk_glc_data = 1;
nidhinvarghese 15:208b146151ba 217 //}
nidhinvarghese 15:208b146151ba 218 //gtc.printf("Checking\n");
nidhinvarghese 15:208b146151ba 219
nidhinvarghese 15:208b146151ba 220 if ((daata1 >= THRESHOLD) || (data2 >= THRESHOLD))
nidhinvarghese 15:208b146151ba 221 {
nikitateggi 16:8fa1d7fd7953 222 //glcraw[0] = (uint16_t) data1n; //Nidhin 5/6/2017
nikitateggi 16:8fa1d7fd7953 223 //glcraw[1] = (uint16_t) data2n; // Nidhin 5/6/2017
nidhinvarghese 15:208b146151ba 224
nidhinvarghese 15:208b146151ba 225 //gtc.printf("data1n is =%d\n", data1n);
nidhinvarghese 15:208b146151ba 226 //gtc.printf("data2n is = %d\n", data2n);
nidhinvarghese 15:208b146151ba 227
nidhinvarghese 15:208b146151ba 228 // gtc.printf("glcraw[0] is =%d\n", glcraw[0]);
nidhinvarghese 15:208b146151ba 229 //gtc.printf("glcraw[1] is = %d\n", glcraw[1]);
nidhinvarghese 15:208b146151ba 230
nidhinvarghese 15:208b146151ba 231 t_s_d = 1;
nidhinvarghese 15:208b146151ba 232 flipper1.attach(&Compute, 2.25); //computing the glucose level
nidhinvarghese 15:208b146151ba 233 glc_3();
nidhinvarghese 15:208b146151ba 234 }
nidhinvarghese 15:208b146151ba 235 else
nidhinvarghese 15:208b146151ba 236 {
nidhinvarghese 15:208b146151ba 237 //wait for insertion of blood;
nidhinvarghese 15:208b146151ba 238 }
nidhinvarghese 15:208b146151ba 239 }
nikitateggi 39:69b09bd87502 240 if(test_strip) // added on 11/8/17 nikita
nikitateggi 39:69b09bd87502 241 glc_error_test_strip_removed();
nidhinvarghese 15:208b146151ba 242 wait_ms(800);
nidhinvarghese 15:208b146151ba 243 }
nikitateggi 16:8fa1d7fd7953 244
nidhinvarghese 17:22a87e12fb3a 245 //gtc.printf("mgdl is - %d\n", mgdll);
nidhinvarghese 15:208b146151ba 246 }
nikitateggi 16:8fa1d7fd7953 247 wait(2.25);
nikitateggi 22:ffa88619551d 248
nikitateggi 22:ffa88619551d 249 if (err==0)
nikitateggi 22:ffa88619551d 250 {
nidhinvarghese 15:208b146151ba 251 gtc.printf("DATA1 is - %d\n", data1n);
nidhinvarghese 15:208b146151ba 252 gtc.printf("DATA2 is - %d\n", data2n);
nikitateggi 24:d992ee8369f4 253 gtc.printf("mgdl is - %d\n", mgdl);
nidhinvarghese 15:208b146151ba 254
nidhinvarghese 17:22a87e12fb3a 255 glcraw[0] = (uint32_t)data1n;
nidhinvarghese 17:22a87e12fb3a 256 glcraw[1] = (uint32_t)data2n;
nidhinvarghese 17:22a87e12fb3a 257
nidhinvarghese 17:22a87e12fb3a 258 sd_glcwrite(glcraw, pid); //Writes the raw data into GLC file
nidhinvarghese 15:208b146151ba 259
nikitateggi 20:7c64e6ecad76 260 //Results Stored to Bluetooth structure
nikitateggi 24:d992ee8369f4 261 glc_testtype = eprom_read_16(20); // Reads the test type (before meal = 0 / after meal = 1 OR Random = 2 from location 2 of the EEPROM) Nidhin 12/6/17
nikitateggi 20:7c64e6ecad76 262 BLEMsg_info_glc.cal_data.cal_sbp_dummy = (uint16_t) glc_testtype; // Data stored into structure
nikitateggi 24:d992ee8369f4 263 BLEMsg_info_glc.cal_data.cal_dbp_OTtyp = (uint16_t) mgdl; //To be checked for reliable results
nidhinvarghese 15:208b146151ba 264
nidhinvarghese 15:208b146151ba 265 structure_file(ptr_BLEMsg_info_glc, pid); // Copy the structure into the GLC file
nidhinvarghese 15:208b146151ba 266 glcfile_mainfile(pid);
nikitateggi 22:ffa88619551d 267 }
avp2417 40:f7f351699288 268 else if(err == 1 || mgdl > MAX_GLC_THRESHOLD || mgdl < MIN_GLC_THRESHOLD) // added threshold to check value out of range
nikitateggi 22:ffa88619551d 269 {
nikitateggi 22:ffa88619551d 270 gtc.printf("replace teststip");
avp2417 34:8f6b0dc124e8 271 glc_error(); //disaply for showing error//10/7/17//nikita
nikitateggi 39:69b09bd87502 272 delete_subfiles(pid); // added on 11/8/17 nikita
nidhinvarghese 15:208b146151ba 273 }
nidhinvarghese 15:208b146151ba 274
nikitateggi 1:8316c23ec6b9 275
avp2417 34:8f6b0dc124e8 276 }