nikita teggi / Mbed 2 deprecated merged_code2_3rd_nov_2017

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of merged_code2_20sept_2017_4th_oct_2017 by nikita teggi

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 
00020 void sample();
00021 void Compute();
00022 void i2c_spec();
00023 Ticker flipper;
00024 Ticker flipper1;
00025  unsigned char GLC_START = 0;
00026 //osTimerDef (sample_data, sample);                      // define timers
00027 //osTimerDef (Compute_GLC, Compute);  
00028 DigitalIn test_strip(PTA12);
00029 
00030 AnalogIn Ain(PTC1);//Electrode one
00031 AnalogIn Ain2(PTE29); // Electrode two
00032 AnalogOut DAC_signal(PTE30);
00033 Serial gtc(USBTX, USBRX);
00034 //unsigned char  c;
00035 //unsigned char HOME = 0;
00036 
00037 //signed char test_strip = 0;
00038 static uint16_t mgdl = 0;
00039 static uint16_t mgdll = 0;
00040 uint16_t  daata1= 0;
00041 uint16_t  data2 = 0;
00042 
00043 static uint16_t  data1n= 0;
00044 static uint16_t  data2n = 0;
00045 uint16_t  datafinal1 = 0;
00046 uint16_t  datafinal2 = 0;
00047 //unsigned int data_diff = 0;
00048 unsigned char err = 0;
00049 
00050 uint32_t glcraw[2]; // Nidhin 31/5/2017
00051 uint8_t chk_glc_data = 0;
00052 uint16_t glc_testtype; // Nidhin 12/6/2017
00053 
00054 Serial ble(PTC4,PTC3);
00055 
00056 
00057 
00058 void sample()
00059 
00060 {
00061      data1n = Ain.read_u16();
00062      data2n = Ain2.read_u16();
00063     daata1 = beta*daata1+alpha*data1n;
00064     data2 = beta*data2+alpha*data2n;
00065       
00066      if( ((daata1-data2) >=(data2/10) && (daata1 > data2)) || ((data2-daata1) >=(data2/10) && (data2 > daata1)) )
00067          {
00068            
00069             err = 1;     
00070             
00071          }
00072          else
00073          {
00074              err = 0;
00075              //gtc.printf("\n", data1n, data2n); only for testing added nidhin 5Jun17
00076          }
00077      
00078 }
00079 
00080 
00081 
00082 void Compute()
00083 {
00084    
00085    flipper.detach();     // ticker for sampling is disabled
00086    datafinal1 = (uint16_t ) NUMB1*(data2*VREF/(TOTAL))-NUMB2;
00087    datafinal2 = (uint16_t ) NUMB1*(daata1*VREF/(TOTAL))-NUMB2;
00088    mgdl = (uint16_t) NUMB1*(((daata1+data2)/2)*VREF/(TOTAL))-NUMB2;  // Nidhin 5/6/17 mgdl = (uint16_t) NUMB1*(((daata1+data2)/2)*VREF/(TOTAL))-NUMB2;
00089    GLC_START = 0;
00090    flipper1.detach();// ticker for computation is disabled
00091 }
00092 
00093 
00094 void  glc(uint32_t pid){
00095    
00096     char t_s_d = 0;
00097      i2c_spec();         // programming the i2c expander
00098      uint8_t err_no_teststip = 0; 
00099      
00100      sd_open_GLCfilee(pid);                     // Opens GLC file in APPEND mode
00101    
00102     // Bluetooth Structure created globally
00103     BLEMsg_info *ptr_BLEMsg_info_glc, BLEMsg_info_glc; // A copy of master strcuture [ "BLEMsg_info" ] by name "BLEMsg_info_bp" is created
00104     ptr_BLEMsg_info_glc = &BLEMsg_info_glc;            // *ptr_BLEMsg_info_bp is the pointer to local copy;
00105                                                         // Cant declare this statement outside main. Causing conflicts
00106 
00107 // Declaration of Date Structure
00108     DateTime_info *ptr_DateTime_info_glc, DateTime_info_glc; // A copy of Master Structure "DateTime_info" created, 
00109     ptr_DateTime_info_glc = &DateTime_info_glc;             // Structure pointer points to that copy.
00110     
00111     
00112       
00113     // RTC operations
00114     time_t epoch_time_glc;           //A copy of time_t by name  epoch_time_bp is created 
00115     epoch_time_glc = rtc_read();    // time is got from get epoch function.  
00116     
00117     struct tm * ptr_time_info_glc, time_info_glc;   // Sturucture copy of tm is created
00118     ptr_time_info_glc = localtime(&epoch_time_glc); // Structure accepts the time in local format from "time_t" type.
00119     /*  
00120     //BELOW LINE IS TO CHECK Date and TIME 
00121     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);
00122     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);
00123     */
00124     //Copying from one structure to the other using variables
00125     DateTime_info_glc.hour = (uint8_t)(*ptr_time_info_glc).tm_hour;
00126     DateTime_info_glc.mins = (uint8_t)(*ptr_time_info_glc).tm_min;
00127     DateTime_info_glc.sec =  (uint8_t)(*ptr_time_info_glc).tm_sec;
00128     
00129     DateTime_info_glc.date = (uint8_t) (*ptr_time_info_glc).tm_mday;
00130     DateTime_info_glc.month =(uint8_t)(*ptr_time_info_glc).tm_mon+1;
00131     DateTime_info_glc.year = (uint8_t)(*ptr_time_info_glc).tm_year-100;
00132     
00133     // Copying Time to Main structure
00134     BLEMsg_info_glc.date_time.hour = DateTime_info_glc.hour;
00135     BLEMsg_info_glc.date_time.mins = DateTime_info_glc.mins;
00136     BLEMsg_info_glc.date_time.sec = DateTime_info_glc.sec;
00137     
00138     BLEMsg_info_glc.date_time.date = DateTime_info_glc.date ;
00139     BLEMsg_info_glc.date_time.month = DateTime_info_glc.month ;
00140     BLEMsg_info_glc.date_time.year =  DateTime_info_glc.year ;
00141     
00142     /*
00143     //Checking if the structure has these values    
00144     gtc.printf("\nTime 2 is - %d:%d:%d\n", DateTime_info_glc.hour, DateTime_info_glc.mins, DateTime_info_glc.sec);
00145     gtc.printf("\nDate 2 is - %d:%d:%d\n",DateTime_info_glc.date, DateTime_info_glc.month, DateTime_info_glc.year);
00146     */
00147     
00148     //Loading values to of Test type
00149     test_type_info test_type_info_glc;  // copy of " test_type_info" created  
00150     test_type_info_glc = BG_Test;       // Loaded value 00 to the test type 
00151     
00152     BLEMsg_info_glc.test_type = test_type_info_glc;
00153     //Check if 01 is getting printed
00154     //gtc.printf("\nTest Type for BG is 01 = %d\n", test_type_info_glc);
00155     
00156     
00157     // Loading values of Length ,  PID, DID, sampling frequency, number of samples, calculated data.
00158     BLEMsg_info_glc.device_id = eprom_read_8(12);                   // Device ID fixed  // change on 11/8/17 nikita
00159     BLEMsg_info_glc.patient_id = (uint16_t)pid;      // Patient ID
00160     BLEMsg_info_glc.sampling_freq = 0;              // sampling frrquency
00161     BLEMsg_info_glc.length = 30;                   //Total length of data in bytes  22 B+10752 B
00162     
00163     BLEMsg_info_glc.num_samples.num_sample_ecg_OTtyp =   2;
00164     BLEMsg_info_glc.num_samples.num_sample_ppg_dummy =  0;
00165        
00166      
00167             
00168      GLC_START = 1;
00169     // ble.baud(115200);
00170    if(GLC_START == 1)
00171         {
00172             glc_1();             // displaying messages to insert the test strip
00173             err = 0;
00174             data1n = 0;
00175             data2n = 0;
00176             daata1 = 0;
00177             data2 = 0;
00178             //sd_open_GLCfile(pid);      // opening the glc file Commented
00179 
00180             while((test_strip));
00181     
00182                 if (!test_strip)                  // checks for the presence of test strip
00183                     { 
00184                         flipper.attach(&sample, 0.8);    
00185                         t_s_d = 0;
00186                         glc_2();                  // displaying message to insert blood
00187              
00188                         while((!t_s_d) && (!test_strip))
00189                             {
00190                                 //if(chk_glc_data == 0)       //Nidhin 31/5/2017
00191                                 //{
00192                                 /*    
00193                                 glcraw[0] = (uint16_t) data1n; //Nidhin 31/5/2017
00194                                 glcraw[1] = (uint16_t) data2n; // Nidhin 31/5/2017
00195                                 
00196                                 gtc.printf("datain is  =%d\n", data1n);
00197                                 gtc.printf("data2n is = %d\n", data2n);
00198                                 
00199                                 gtc.printf("datain is  =%d\n", data1n);
00200                                 gtc.printf("data2n is = %d\n", data2n);
00201                                 */
00202                                 //chk_glc_data = 1;  
00203                                 //}
00204                                 //gtc.printf("Checking\n");
00205                                 
00206                                 if ((daata1 >= THRESHOLD) || (data2 >= THRESHOLD)) 
00207                                     { 
00208                                 //glcraw[0] = (uint16_t) data1n; //Nidhin 5/6/2017
00209                                 //glcraw[1] = (uint16_t) data2n; // Nidhin 5/6/2017
00210                                 
00211                                 //gtc.printf("data1n is  =%d\n", data1n);
00212                                 //gtc.printf("data2n is = %d\n", data2n);
00213                                 
00214                                // gtc.printf("glcraw[0] is  =%d\n", glcraw[0]);
00215                                 //gtc.printf("glcraw[1] is = %d\n", glcraw[1]); 
00216                                         
00217                                         t_s_d = 1;
00218                                         flipper1.attach(&Compute, 2.25);        //computing the glucose level
00219                                         glc_3();
00220                                     }
00221                                 else
00222                                     {
00223                                         //wait for insertion of blood;
00224                                     }
00225                             }
00226                              if(test_strip)     // added on 11/8/17 nikita
00227                                {
00228                                 err_no_teststip = 1;
00229                               
00230                                 
00231                                 }   
00232                         wait_ms(800);          
00233                     }
00234                     
00235                       //gtc.printf("mgdl is - %d\n", mgdll);
00236             } 
00237         wait(2.25);
00238      gtc.printf("error = %d\n", err);   
00239    if ((err==0) && (err_no_teststip == 0) && ( mgdl < MAX_GLC_THRESHOLD) && ( mgdl > MIN_GLC_THRESHOLD))
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     }
00264   else                                                                          // added threshold to check value out of range   
00265     {  
00266          if(err_no_teststip == 1)                                               // added on  22/09/2017 nikita // to separately detect what is the parameter failed while testing 
00267          {
00268                glc_error_test_strip_removed();
00269          }
00270          
00271          else if(err == 1 || mgdl > MAX_GLC_THRESHOLD || mgdl < MIN_GLC_THRESHOLD)
00272          {   
00273             gtc.printf("replace teststip"); 
00274             glc_error(); //disaply for showing error//10/7/17//nikita
00275            }
00276             
00277          delete_subfiles(pid); // added on 11/8/17 nikita
00278     }  
00279 
00280 
00281  }