rashmi v / Mbed 2 deprecated SS_SensePOC2P0_12Dec2017_USERPID

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of SS_SensePOC2P0_11Dec2017_USERPID by rashmi v

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers glc.cpp Source File

glc.cpp

00001 // Code Modified with Bluetooth Structure Nidhin 2/6/17
00002 ///
00003 #include "mbed.h"
00004 #include "glc.h"
00005 #include "i2c_dec.h"
00006 //#include "ili9325.h"
00007 //#include "lcd_base.h"
00008 //#include "display_modules.h"
00009 //#include "touch_modules.h"
00010 #include "rtc.h"
00011 #include "sdcard.h"
00012 #include "eeprom_pgm.h"   // Nidhin 12/6/17
00013 #include "main.h"
00014 #include "struct.h" //Nidhin 31/5/2017
00015 //#include "test_type.h" //Nidhin 31/5/2017
00016 
00017 #define MAX_GLC_THRESHOLD       600
00018 #define MIN_GLC_THRESHOLD       10
00019 #define TIMEOUT_BG              20000  
00020 
00021 void sample();
00022 void Compute();
00023 void i2c_spec();
00024 Ticker flipper;
00025 Ticker flipper1;
00026  unsigned char GLC_START = 0;
00027 //osTimerDef (sample_data, sample);                      // define timers
00028 //osTimerDef (Compute_GLC, Compute);  
00029 DigitalIn test_strip(PTA12);
00030 
00031 AnalogIn Ain(PTC1);//Electrode one
00032 AnalogIn Ain2(PTE29); // Electrode two
00033 AnalogOut DAC_signal(PTE30);
00034 Serial gtc(USBTX, USBRX);
00035 Timer timer_bg;
00036 
00037 
00038 //signed char test_strip = 0;
00039 static uint16_t mgdl = 0;
00040 static uint16_t mgdll = 0;
00041 uint16_t  daata1= 0;
00042 uint16_t  data2 = 0;
00043 
00044 static uint16_t  data1n= 0;
00045 static uint16_t  data2n = 0;
00046 uint16_t  datafinal1 = 0;
00047 uint16_t  datafinal2 = 0;
00048 //unsigned int data_diff = 0;
00049 unsigned char err = 0;
00050 
00051 uint32_t glcraw[2]; // Nidhin 31/5/2017
00052 uint8_t chk_glc_data = 0;
00053 uint16_t glc_testtype; // Nidhin 12/6/2017
00054 
00055 void sample()
00056 {
00057     data1n = Ain.read_u16();
00058     data2n = Ain2.read_u16();
00059     daata1 = beta*daata1+alpha*data1n;
00060     data2 = beta*data2+alpha*data2n;
00061       
00062     if( ((daata1-data2) >=(data2/10) && (daata1 > data2)) || ((data2-daata1) >=(data2/10) && (data2 > daata1)) )
00063     { 
00064         err = 1;       
00065     }
00066     else
00067     {
00068         err = 0;
00069         //gtc.printf("\n", data1n, data2n); only for testing added nidhin 5Jun17
00070     }
00071      
00072 }
00073 
00074 
00075 
00076 void Compute()
00077 {
00078    flipper.detach();     // ticker for sampling is disabled
00079    datafinal1 = (uint16_t ) NUMB1*(data2*VREF/(TOTAL))-NUMB2;
00080    datafinal2 = (uint16_t ) NUMB1*(daata1*VREF/(TOTAL))-NUMB2;
00081    mgdl = (uint16_t) NUMB1*(((daata1+data2)/2)*VREF/(TOTAL))-NUMB2;  // Nidhin 5/6/17 mgdl = (uint16_t) NUMB1*(((daata1+data2)/2)*VREF/(TOTAL))-NUMB2;
00082    GLC_START = 0;
00083    flipper1.detach();// ticker for computation is disabled
00084 }
00085 
00086 
00087 void  glc(uint32_t pid)
00088 {
00089    
00090     char t_s_d = 0;
00091     uint8_t err_no_teststip = 0; 
00092     bool timeout_flag = false; 
00093     
00094     i2c_spec();                                                                 // programming the i2c expander
00095     sd_open_GLCfilee(pid);                                                      // Opens GLC file in APPEND mode
00096    
00097     // Bluetooth Structure created globally
00098     BLEMsg_info *ptr_BLEMsg_info_glc, BLEMsg_info_glc;                          // A copy of master strcuture [ "BLEMsg_info" ] by name "BLEMsg_info_bp" is created
00099 ptr_BLEMsg_info_glc = &BLEMsg_info_glc;                                         // *ptr_BLEMsg_info_bp is the pointer to local copy;
00100                                                                                 // Cant declare this statement outside main. Causing conflicts
00101 
00102     // Declaration of Date Structure
00103     DateTime_info *ptr_DateTime_info_glc, DateTime_info_glc;                    // A copy of Master Structure "DateTime_info" created, 
00104     ptr_DateTime_info_glc = &DateTime_info_glc;                                 // Structure pointer points to that copy.
00105     
00106     // RTC operations
00107     time_t epoch_time_glc;                                                      //A copy of time_t by name  epoch_time_bp is created 
00108     epoch_time_glc = rtc_read();                                                // time is got from get epoch function.  
00109     
00110     struct tm * ptr_time_info_glc, time_info_glc;   // Sturucture copy of tm is created
00111     ptr_time_info_glc = localtime(&epoch_time_glc); // Structure accepts the time in local format from "time_t" type.
00112     /*  
00113     //BELOW LINE IS TO CHECK Date and TIME 
00114     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);
00115     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);
00116     */
00117     //Copying from one structure to the other using variables
00118     DateTime_info_glc.hour = (uint8_t)(*ptr_time_info_glc).tm_hour;
00119     DateTime_info_glc.mins = (uint8_t)(*ptr_time_info_glc).tm_min;
00120     DateTime_info_glc.sec =  (uint8_t)(*ptr_time_info_glc).tm_sec;
00121     
00122     DateTime_info_glc.date = (uint8_t) (*ptr_time_info_glc).tm_mday;
00123     DateTime_info_glc.month =(uint8_t)(*ptr_time_info_glc).tm_mon+1;
00124     DateTime_info_glc.year = (uint8_t)(*ptr_time_info_glc).tm_year-100;
00125     
00126     // Copying Time to Main structure
00127     BLEMsg_info_glc.date_time.hour = DateTime_info_glc.hour;
00128     BLEMsg_info_glc.date_time.mins = DateTime_info_glc.mins;
00129     BLEMsg_info_glc.date_time.sec = DateTime_info_glc.sec;
00130     
00131     BLEMsg_info_glc.date_time.date = DateTime_info_glc.date ;
00132     BLEMsg_info_glc.date_time.month = DateTime_info_glc.month ;
00133     BLEMsg_info_glc.date_time.year =  DateTime_info_glc.year ;
00134     
00135     /*
00136     //Checking if the structure has these values    
00137     gtc.printf("\nTime 2 is - %d:%d:%d\n", DateTime_info_glc.hour, DateTime_info_glc.mins, DateTime_info_glc.sec);
00138     gtc.printf("\nDate 2 is - %d:%d:%d\n",DateTime_info_glc.date, DateTime_info_glc.month, DateTime_info_glc.year);
00139     */
00140     
00141     //Loading values to of Test type
00142     test_type_info test_type_info_glc;  // copy of " test_type_info" created  
00143     test_type_info_glc = BG_Test;       // Loaded value 00 to the test type 
00144     
00145     BLEMsg_info_glc.test_type = test_type_info_glc;
00146     //Check if 01 is getting printed
00147     //gtc.printf("\nTest Type for BG is 01 = %d\n", test_type_info_glc);
00148     
00149     
00150     // Loading values of Length ,  PID, DID, sampling frequency, number of samples, calculated data.
00151     BLEMsg_info_glc.device_id = get_did();                   // Device ID fixed  // change on 11/8/17 nikita
00152     //BLEMsg_info_glc.patient_id = (uint16_t)pid;                               // Patient ID
00153     
00154     memcpy(BLEMsg_info_glc.patient_id,get_userpid(),sizeof(BLEMsg_info_glc.patient_id));
00155     
00156     BLEMsg_info_glc.sampling_freq = 0;              // sampling frrquency
00157     BLEMsg_info_glc.length = 30;                   //Total length of data in bytes  22 B+10752 B
00158     
00159     BLEMsg_info_glc.num_samples.num_sample_ecg_OTtyp =   2;
00160     BLEMsg_info_glc.num_samples.num_sample_ppg_dummy =  0;
00161        
00162      
00163             
00164     GLC_START = 1;
00165     // ble.baud(115200);
00166     if(GLC_START == 1)
00167     {
00168         timer_bg.reset();                           
00169         timer_bg.start();
00170         
00171         // send BG Insert Strip messages to mobile application
00172         poc2p0_send_device_msg_res(BG_INSERT_STRIP_RES);
00173         
00174         //glc_1();             // displaying messages to insert the test strip
00175         err = 0;
00176         data1n = 0;
00177         data2n = 0;
00178         daata1 = 0;
00179         data2 = 0;
00180         //sd_open_GLCfile(pid);      // opening the glc file Commented
00181 
00182         while((test_strip))
00183         { 
00184             if(timer_bg.read_ms() > TIMEOUT_BG)
00185             {
00186                 timer_bg.stop();
00187                 timeout_flag = true;
00188                 break;
00189             }                                                                   
00190         }                                                                       
00191         gtc.printf("T_O: %d\n",timer_bg.read_ms());
00192         timer_bg.reset();
00193             
00194         if (!test_strip)                  // checks for the presence of test strip
00195         { 
00196             flipper.attach(&sample, 0.8);    
00197             t_s_d = 0;
00198             timer_bg.start();
00199             // send BG Drop Blood messages to mobile application
00200             poc2p0_send_device_msg_res(BG_DROP_BLOOD_RES);
00201     
00202             //glc_2();                  // displaying message to insert blood
00203     
00204             while((!t_s_d) && (!test_strip))
00205             {
00206                                 
00207                 if ((daata1 >= THRESHOLD) || (data2 >= THRESHOLD)) 
00208                 { 
00209                         t_s_d = 1;
00210                         flipper1.attach(&Compute, 2.25);        //computing the glucose level
00211     
00212                         // send BG Test in progress messages to mobile application
00213                         poc2p0_send_device_msg_res(BG_TEST_IN_PROGRESS_RES);
00214     
00215                         //glc_3();
00216                         timer_bg.stop();
00217                 }                                                               
00218                 else if((timer_bg.read_ms() > TIMEOUT_BG))
00219                 {
00220                     timer_bg.stop();
00221                     timeout_flag = true;
00222                     break;
00223                 }                                                                
00224             }                                                                   //end of while(2)
00225             if(test_strip)     // added on 11/8/17 nikita
00226             {
00227                 err_no_teststip = 1;
00228                 timer_bg.stop();
00229                 
00230             }   
00231             wait_ms(800);          
00232         }                                                                       //end of if(!test_strip)
00233     }                                                                           //end of if(GLC_start==1) 
00234     wait(2.25);
00235     gtc.printf("error = %d\n", err);   
00236     if ((err==0) && (err_no_teststip == 0) && ( mgdl < MAX_GLC_THRESHOLD) && ( mgdl > MIN_GLC_THRESHOLD) && (timeout_flag == false))
00237     {     
00238         // send Final BG result messages to mobile application
00239         poc2p0_send_device_test_result_res(BG_TEST_SUCCSS_RES, (uint32_t)mgdl);
00240         
00241         //glc_4(mgdl);
00242         gtc.printf("DATA1 is - %d\n", data1n); 
00243         gtc.printf("DATA2 is - %d\n", data2n);  
00244         gtc.printf("mgdl is - %d\n", mgdl);   
00245         
00246         glcraw[0] = (uint32_t)data1n;
00247         glcraw[1] = (uint32_t)data2n;
00248         
00249         sd_glcwrite(glcraw, pid);  //Writes the raw data into GLC file 
00250         
00251         //Results Stored to Bluetooth structure
00252         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
00253         BLEMsg_info_glc.cal_data.cal_sbp_dummy = (uint16_t) glc_testtype;  // Data stored into structure
00254         BLEMsg_info_glc.cal_data.cal_dbp_OTtyp = (uint16_t) mgdl;  //To be checked for reliable results           
00255         
00256         structure_file(ptr_BLEMsg_info_glc, pid);                                   // Copy the structure into the GLC file 
00257         glcfile_mainfile(pid);
00258         if(get_filecreated_status() == false)                                          //if file is in write mode 
00259         {   
00260             set_filecreated_status();                    
00261             increment_filepid (); 
00262         } 
00263         //screen_glc_2();   
00264     }
00265     else if(timeout_flag == false)                                                                           // added threshold to check value out of range   
00266     {  
00267         if(err_no_teststip == 1)                                               // added on  22/09/2017 nikita // to separately detect what is the parameter failed while testing 
00268         {
00269          // send BG remove test strip messages to mobile application
00270          poc2p0_send_device_msg_res(BG_TEST_STRIP_REMOVE_RES);
00271         
00272          //glc_error_test_strip_removed();
00273         }
00274          
00275         else if(err == 1 || mgdl > MAX_GLC_THRESHOLD || mgdl < MIN_GLC_THRESHOLD)
00276         {   
00277             // send BG Replace test strip messages to mobile application
00278             poc2p0_send_device_msg_res(BG_REPLACE_TEST_STRIP_RES);
00279             
00280             gtc.printf("replace teststip");
00281             //glc_error(); //disaply for showing error//10/7/17//nikita
00282         }
00283             
00284         delete_subfiles(pid); // added on 11/8/17 nikita
00285         //screen_glc_2();
00286     } 
00287     else
00288     {
00289         poc2p0_send_device_msg_res(BG_TIMEOUT_RES);
00290     }  
00291     
00292 }