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

Files at this revision

API Documentation at this revision

Comitter:
nikitateggi
Date:
Fri Jun 09 17:12:21 2017 +0000
Parent:
18:20679eaa54d1
Child:
20:7c64e6ecad76
Commit message:
integrated code with bluetooth

Changed in this revision

bp.cpp Show annotated file Show diff for this revision Revisions of this file
bt.cpp Show annotated file Show diff for this revision Revisions of this file
ecgg.cpp Show annotated file Show diff for this revision Revisions of this file
glc.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
sdcard.cpp Show annotated file Show diff for this revision Revisions of this file
sdcard.h Show annotated file Show diff for this revision Revisions of this file
struct.h Show annotated file Show diff for this revision Revisions of this file
test_type.h Show diff for this revision Revisions of this file
--- a/bp.cpp	Fri Jun 09 09:22:32 2017 +0000
+++ b/bp.cpp	Fri Jun 09 17:12:21 2017 +0000
@@ -20,7 +20,7 @@
 
 
 #include "struct.h"
-#include "test_type.h"
+//#include "test_type.h"
 
 using namespace std;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bt.cpp	Fri Jun 09 17:12:21 2017 +0000
@@ -0,0 +1,341 @@
+#include "mbed.h"
+#include "sdcard.h"
+#include "struct.h"
+
+BLEMsg_info *bt_file;              //pointer to structure 
+Point *myBleMsg;                   // pointer to bluetooth structure
+//bt_st *as;
+Serial mc(USBTX,USBRX);            // serial terminal
+Serial blue(PTC4,PTC3);            // bluetooth serial terminal
+                  
+uint32_t total_samples=0;              // variable to hold total number of samples
+//Timer t;                               // timer 
+uint8_t ack_rx;                        // varibale to receive ack from the bluetooth 
+Point b;                               // structure copy
+BLEMsg_info bt_file1;                  // structure copy
+
+uint32_t total_file_size=0;             // to determine the size of the file 
+uint32_t total_file_read=0; 
+                  
+uint32_t file_start=0;                   // variable for 'for loop'
+uint32_t j=0;
+uint8_t k=0; 
+uint32_t file_pointer_position=0;
+
+
+
+
+uint8_t bt_send(uint8_t state_tx)
+{
+  uint32_t v1[150]; 
+  blue.baud(115200);                  // baud rate of bluetooth
+  bt_file=&bt_file1;                      
+  myBleMsg=&b; 
+  static  uint8_t state_rx =0;
+//t.start();                             // timer start
+  
+
+    switch(state_tx)
+          {
+ 
+       case 1:   sendinit();  
+                 sd_open_read(1);                       // open the file for read
+                 total_file_size=file_size() ;             // determined the file size  
+                // mc.printf("filesize=%d",total_file_size);              
+                 state_rx =1;
+                 state_tx =0;                
+                 break;
+               
+                   
+       case 2:   total_samples=0;   
+                 sd_read1(bt_file1);               // read the sd card file
+                 myBleMsg->bt_msg=bt_file1;         //copy the contents to the structure
+                 send();                             // send the structure
+                 total_samples= myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp;
+                 state_tx =0;
+                 state_rx =2;
+                 k=0;
+                 mc.printf("samples=%d", total_samples);
+                 total_file_read=file_size1();         // read the file size
+                 //mc.printf("fileread=%d",total_file_read);
+                 //mc.printf("pointer=%d\n",file_pointer_position);
+                 break;                 
+ 
+ 
+       case 3:      
+                 //mc.printf("kkkk=%d\n",k);     
+                 //mc.printf("samples=%d", total_samples);            // send the raw data
+                if(k<(total_samples/150))
+                  {
+                    sd_read(v1,150);                                  //read 1500 samples
+                    send_data_str1();                                 // send the initial structure
+                    //mc.printf("file_start,j=%d %d\n",file_start,j);                     
+                      for(file_start=j;file_start<(j+150);file_start++)
+                         {     
+                       
+                             blue.printf("%08x", v1[file_start]);
+                            
+                         }
+                            //  blue.printf("\n");
+                       // mc.printf("iiii,hjjjj=%d %d\n",file_start,j);   
+                    state_tx =0;
+                    state_rx =3;
+                    send_data_str2();
+                  
+                   }
+               
+               else 
+                     {
+                        for(int l=0;l<150;l++)
+                          {    
+                            v1[l]=NULL;
+                           }
+                        
+                           send_data_str1();
+                           sd_read(v1,(total_samples-(150*k)));
+                        for(file_start=j;file_start<(total_samples-(150*k));file_start++)
+                          {     
+                             blue.printf("%08x", v1[file_start]);
+                           }
+                           
+                          // blue.printf("\n");
+                          state_tx=0;
+                          state_rx=4;
+                          send_data_str2();
+                    }   
+                     
+                       total_file_read=file_size1();
+                   //   mc.printf("fileread=%d",total_file_read );
+                      //  mc.printf("pointer=%d\n",file_pointer_position);
+                break;        
+               
+         
+        case 4:   total_file_read=file_size1();
+                  mc.printf("fileread=%d",total_file_read);
+                 if(total_file_read==total_file_size)
+                     {
+                    
+                        state_rx=6;
+                        sd_close();
+                      //  mc.printf("exit");
+                     }
+                else   
+                       {
+                         state_rx=5;
+                         total_file_read=file_size1();
+                      //   mc.printf("fileread=%d",total_file_read );
+                        //mc.printf("pointer=%d\n",file_pointer_position);
+                         
+                        }   
+                       
+   
+//t.stop();
+
+         }
+//mc.printf("time=%d\n",t.read_ms());
+   return state_rx;     
+        
+  }           
+ 
+ 
+ 
+ 
+uint8_t bt_receive (uint8_t state_rx)
+{
+       
+static unsigned char state_tx =1;      //state for transmitting 
+            
+Timer t;                               // timer 
+uint8_t ack_rx;                        // varibale to receive ack from the bluetooth 
+//Point b;                               // structure copy                     
+//myBleMsg=&b;
+t.start();                             // timer start
+
+     switch(state_rx)
+         {
+                
+          case 1:                                       // wait for the ack
+                      state_rx=0; 
+                      myBleMsg->proto=ACK;
+                      scanf("%02x",&ack_rx);            // read the ack/nack from serial terminal
+                      mc.printf("%02x",ack_rx);         
+                   if (ack_rx==myBleMsg->proto)         // check for ack
+                     {
+                       
+                       state_tx=2;
+                      }
+                    
+                    else                                // its a nack
+                    
+                      { 
+                       
+                        state_tx=1;
+                        sd_close();
+                      }
+                   break;                 
+ 
+         case 2:      state_rx =0;                        //wait for the ack
+                      myBleMsg->proto=ACK;
+                
+                      scanf("%02x", &ack_rx);
+                     if (ack_rx==myBleMsg->proto)
+                        {
+                
+                          state_tx=3;
+                          file_pointer_position=total_file_read;
+                        }
+                    
+                    else 
+                    
+                        { 
+                      
+                        state_tx=2;
+                        setpos1(file_pointer_position);          // set the file pointer to send the previous data
+                     //   mc.printf("pointer=%d\n",file_pointer_position);
+                        }
+            
+                    break;
+
+                
+     case 3:       
+                   state_rx =0;
+                   myBleMsg->proto=ACK;
+                   scanf("%02x", &ack_rx);
+                 if (ack_rx==myBleMsg->proto)
+                      {
+                     
+                        state_tx=3;
+                         k=k+1;
+                       file_pointer_position=total_file_read;
+                      }
+                    
+                  else 
+                    
+                      { 
+                       
+                        state_tx=3;
+                      //  mc.printf("j=%d\n",j);
+                        file_start=file_start-150;
+                        //mc.printf("j=%d\n",j); 
+                       // mc.printf("file_pointer_position=%d\n",file_pointer_position);
+                        setpos1(file_pointer_position);
+                      }
+               
+                   break;    
+                                            
+     case 4:       
+                   state_rx =0;
+                   myBleMsg->proto=ACK;
+                   scanf("%02x", &ack_rx);
+                   if (ack_rx==myBleMsg->proto)
+                      {
+                       // bt_r.printf("sent");
+                        state_tx=4;
+                         k=k+1;
+                        file_pointer_position=total_file_read;
+                      }
+                    
+                  else 
+                    
+                      { 
+                        //bt_r.printf("no-ack");
+                        state_tx=3;
+                        file_start=file_start-150;
+                        mc.printf("file_pointer_position=%d\n",file_pointer_position);
+                        setpos1(file_pointer_position);
+                      } 
+                   break;                
+                 
+      case 5:      state_tx=2;
+                   break;
+                   
+      case 6:      state_tx=0;
+                   mc.printf("out");
+                   break;
+   
+   
+                
+//t.stop();
+
+  }
+//bt_r.printf("time=%d\n",t.read_ms());
+   return state_tx;          
+  }           
+   
+ 
+ 
+ 
+ 
+ 
+             
+void send()
+{
+                myBleMsg->start_end_of_string=sos;
+                myBleMsg->length_file=28;
+                myBleMsg->proto=start_of_file;
+                myBleMsg->chk_sum=32;
+                mc.printf("%02x",myBleMsg->start_end_of_string); 
+                mc.printf("%08x",myBleMsg->length_file);
+                mc.printf("%02x",myBleMsg->proto);
+                mc.printf("%08x",myBleMsg->bt_msg.length); 
+                mc.printf("%02x",myBleMsg->bt_msg.device_id); 
+                mc.printf("%08x",myBleMsg->bt_msg.patient_id);    
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.date);     
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.month);  
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.year);  
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.hour);      
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.mins);  
+                mc.printf("%02x",myBleMsg->bt_msg.date_time.sec);  
+                mc.printf("%02x",myBleMsg->bt_msg.test_type);   
+                mc.printf("%04x",myBleMsg->bt_msg.sampling_freq); 
+                mc.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy); 
+                mc.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); 
+                mc.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy);   
+                mc.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp);   
+                myBleMsg->start_end_of_string=eos;  
+                mc.printf("%02x",myBleMsg->start_end_of_string);
+                mc.printf("%02x",myBleMsg->chk_sum);
+    
+    }
+    
+void sendinit()
+{
+                myBleMsg->start_end_of_string=sos;
+                myBleMsg->length_file=3;
+                myBleMsg->proto=start_of_file;
+               //  myBleMsg->bt_msg=NULL;
+                myBleMsg->chk_sum=3;
+                mc.printf("%02x",myBleMsg->start_end_of_string); 
+                mc.printf("%08x",myBleMsg->length_file);
+                mc.printf("%02x",myBleMsg->proto);
+                myBleMsg->start_end_of_string=eos;  
+                mc.printf("%02x",myBleMsg->start_end_of_string);
+                mc.printf("%02x",myBleMsg->chk_sum);
+    
+    }
+    
+    
+    
+ void send_data_str1()
+   {
+                 myBleMsg->start_end_of_string=sos;
+                 myBleMsg->length_file=3;
+                 myBleMsg->proto=start_of_file;
+                 mc.printf("%02x",myBleMsg->start_end_of_string); 
+                 mc.printf("%08x",myBleMsg->length_file);
+                 mc.printf("%02x",myBleMsg->proto);
+    }
+    
+    
+    
+ void send_data_str2()
+    {          
+                myBleMsg->chk_sum=3;
+                myBleMsg->start_end_of_string=eos;  
+                mc.printf("%02x",myBleMsg->start_end_of_string);
+                mc.printf("%02x",myBleMsg->chk_sum);
+    }
+    
+
+
+
--- a/ecgg.cpp	Fri Jun 09 09:22:32 2017 +0000
+++ b/ecgg.cpp	Fri Jun 09 17:12:21 2017 +0000
@@ -14,7 +14,7 @@
 #include "ec_bp.h"
 
 #include "struct.h"  // Added on 31/5/17_Nidhin
-#include "test_type.h" //Added on  31/5/17_Nidhin
+//#include "test_type.h" //Added on  31/5/17_Nidhin
 Serial pc(USBTX,USBRX);  
 
 
--- a/glc.cpp	Fri Jun 09 09:22:32 2017 +0000
+++ b/glc.cpp	Fri Jun 09 17:12:21 2017 +0000
@@ -182,7 +182,7 @@
 #include "sdcard.h"
 
 #include "struct.h" //Nidhin 31/5/2017
-#include "test_type.h" //Nidhin 31/5/2017
+//#include "test_type.h" //Nidhin 31/5/2017
 
 
 void sample();
--- a/main.cpp	Fri Jun 09 09:22:32 2017 +0000
+++ b/main.cpp	Fri Jun 09 17:12:21 2017 +0000
@@ -1,6 +1,7 @@
+// changed the pid in all to int32_t;
+//added bt
+
 #include "mbed.h"
-//#include "ili9325.h"
-//#include "lcd_base.h"
 #include "display_modules.h"
 #include "touch_modules.h"
 #include "ecg_dec.h"
@@ -9,42 +10,99 @@
 #include "bp.h"
 #include "sdcard.h"
 #include "eeprom_pgm.h"
-
+#include "struct.h"
  
 Serial gc(USBTX, USBRX);
 DigitalIn q1(PTB11);
 DigitalIn q(PTB8);
-//Serial ble(PTC4,PTC3);
+DigitalIn BT(PTA5);                     //bluetooth status pin
 
+uint8_t touch(uint8_t state);           // touch state
+uint8_t display(uint8_t state1);       //  display state
+int pid=0;                // patient ID
+static uint8_t nstate=0;
+static uint8_t nstate1=0;
+static uint8_t flag=0;             //flag to indicate not to update pid as it is already done
+static uint8_t ecg_flag=0;         // ecg_flag to indicate the ecg test is already done for the same pid
+static uint8_t glc_flag=0;         // glc_flag to indicate the glc test is already done for the same pid
+uint8_t state_r=0;                 // state to receive from bluetooth
+uint8_t state_t=1;                 // state to transmit to bluetooth
+float heart_rate=0;          
 
 
 int main(){
 
 
- unsigned char state;
- unsigned char state1;
- unsigned char nstate;
- unsigned char nstate1;
- int pid=0;                // patient ID
- unsigned char flag=0;
- unsigned char ecg_flag=0;
- unsigned char glc_flag=0;
-float heart_rate=0;
+
   
    DisableTouch();
    gc.baud(115200);
   
    init_screen();    //initialize lcd
    screen_main(); // display of main screen 
-   state=1;
+   //state=10;
    flag=0;
-   nstate=state;
+   //nstate=state;
    EnableTouch();   // enable touch function
+static uint8_t main_state=1;
+static uint8_t state_display=0;
+static uint8_t state_touch=1;
+static uint8_t state_t=1;
+static uint8_t state_r=0;
 
    while(1)
    {
-      
-    detect_touch();     // determine the touch points
+     switch(main_state)                          // main state machine
+     {
+         case 1:  state_display=touch(state_touch);           // touch state
+                
+                  
+                if(state_display==10)
+                    main_state=3;
+                    
+                else 
+                    main_state=2;
+                  break;
+    
+         case 2:  state_touch=display(state_display);          //display state
+                  main_state=1;
+                 break;
+                
+                
+         case 3: DisableTouch();                             // bluetooth send 
+                 if(BT==1)
+                 {
+                    state_r=bt_send(state_t);
+                    main_state=4;
+                 }
+               else   
+                 gc.printf("timeout");
+                 break;
+                 
+         case 4:  
+                 state_t=bt_receive(state_r);               // bluetooth receive
+                 main_state=3;
+                // gc.printf("statet=%d",state_t);
+                 if(state_t ==0)
+                 {
+                    EnableTouch();
+                    main_state=1;
+                   } 
+                 break;    
+                
+                
+    }
+
+
+}
+
+}
+
+
+uint8_t touch(uint8_t state)
+{
+   static  uint8_t state1=0;
+   detect_touch();     // determine the touch points
     
     switch(state)    // state for touch
   {
@@ -121,9 +179,13 @@
                           
     }         
      
-
+return state1;
+  }
   
   
+uint8_t display(uint8_t state1)
+{
+  static uint8_t state=0;
   
  switch(state1)    // state for display screens
   {
@@ -141,6 +203,7 @@
                 break;
       
       case 2: DisableTouch();
+                 // bt();
                 screen_main_1();     // System configration screen 
                 state1=0;
                 state=2;
@@ -153,15 +216,12 @@
               {
                 //pid=pid+1;
                 pid=eprom_read(1);         // reading the patient id from eeprom
+                gc.printf("pidinit=%d",pid);
                 pid=pid+1;
+                gc.printf("pid+1=%d",pid);
                eprom_write(1,pid);         // writing the patient id to eeprom
                 screen_main_2(pid);
-                
-                sd_open_mainfile(pid);    //ADDED on 1/6/2017 - NIDHIN
-                sd_close();
-                
-                
-                /*sd_open_GLCfilee(pid);     // only opening the glc,bp and ecg file // COMMENTED Nidhin 1/6/17
+              /*  sd_open_GLCfilee(pid);     // only opening the glc,bp and ecg file
                  sd_close();
                  sd_open_ECGfilee(pid);
                  sd_close();
@@ -189,7 +249,8 @@
                  nstate=state;
                  }
             else
-                { screen_glc();       // glc screen
+                { 
+                 screen_glc();       // glc screen
                  state1=0;
                  state=4;
                  nstate=state;
@@ -233,6 +294,7 @@
      
      case 7:    DisableTouch();     
                 glc(pid);        // glc measurement
+             //  bt();
                 glc_flag=1;
                 wait(5); 
                 state1=0;
@@ -265,7 +327,7 @@
                 EnableTouch();
                  break;*/
         
-     case 9: 
+    case 9: 
                 DisableTouch();
                 screen_bp2();      // bp main screen
                 bp_countdown();    // countdown for bp 
@@ -277,8 +339,8 @@
                 EnableTouch();
                  break;
         
-        
-        
+               
+                 
         default :  
                   
                   state1=nstate1;
@@ -289,16 +351,9 @@
     
            
                 
-                
+         return state;       
                 
-  }              
-                
-  
 }
 
 
 
-
-
-
-
--- a/sdcard.cpp	Fri Jun 09 09:22:32 2017 +0000
+++ b/sdcard.cpp	Fri Jun 09 17:12:21 2017 +0000
@@ -126,6 +126,82 @@
     fprintf(fp,"%d\n", value);
  }
  
+ 
+ void sd_open_read(int32_t pid)          // opening the file for read
+ {
+     
+  char buffer1[32];
+   sprintf(buffer1,"/sd/%d.csv",pid);
+  fp = fopen(buffer1,"r"); 
+  if (fp==NULL)
+  {
+      snd.printf("no");
+      }
+      
+      else 
+      {
+          snd.printf("yes");
+          }
+ }
+ 
+ void sd_read(uint32_t  *v1, int size)          //reading the data from the openend file
+ {
+     fread(v1,sizeof(v1),size,fp);
+ }
+
+void setpos()
+{
+     //fseek ( fp , 1 , SEEK_SET );
+     rewind (fp);
+}
+
+void setpos1(uint32_t position)
+{
+     fseek ( fp , position, SEEK_SET );
+     //rewind (fp);
+}
+
+
+int file_size()    
+   {
+     int file_length;
+     fseek (fp, 0, SEEK_END);
+     file_length=ftell(fp);
+     file_length=sizeof(char)*file_length;
+        rewind(fp);
+     return   file_length;
+     }  
+     
+     
+     int file_size1()    
+   {
+     int file_length;
+    // fseek (fp, 0, SEEK_END);
+
+     file_length=ftell(fp);
+      // file_length=sizeof(char)*file_length;
+      //  rewind(fp);
+     return   file_length;
+     }  
+  
+
+
+
+ 
+     void sd_read1(BLEMsg_info &f)
+{
+     fread(&f,sizeof(BLEMsg_info),1,fp);
+      //fscanf(fp,"%08x",&v1);
+}
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
  //------------------------------------------------------
   
 //FILE *sd_BP_ecgwrite (int value)      // writing data into the file NIDHIN
--- a/sdcard.h	Fri Jun 09 09:22:32 2017 +0000
+++ b/sdcard.h	Fri Jun 09 17:12:21 2017 +0000
@@ -13,7 +13,13 @@
 void sd_open_BPfilee(int pid);
 void sd_open_BPfile(int pid);
 void sd_write (int value);
-
+int file_size() ;
+int file_size1() ;
+void sd_read(uint32_t  *v1, int size);
+void sd_open_read(int32_t pid);
+void sd_read1(BLEMsg_info &f);
+void setpos();
+void setpos1(uint32_t position);
 //void sd_BP_ppgwrite (uint32_t value);
 //void sd_BP_ecgwrite (int value);
 
--- a/struct.h	Fri Jun 09 09:22:32 2017 +0000
+++ b/struct.h	Fri Jun 09 17:12:21 2017 +0000
@@ -4,7 +4,7 @@
 #include "bp.h"
 #include "glc.h"
 #include "ecg_dec.h"
-#include "test_type.h"
+//#include "test_type.h"
 
 
 typedef struct __attribute__((__packed__))
@@ -30,6 +30,21 @@
     uint16_t             cal_dbp_OTtyp;
 }CalData_info;
 
+typedef enum 
+{ 
+     ACK = (uint8_t)0x00,
+    start_of_file,
+    end_of_file,
+    data_trfr,
+    NACK=0xff,
+}bt_st;
+
+typedef enum
+{ 
+    BP_Test =   (uint8_t )0x00,   // removed Semi Colon and replaced with comma
+    BG_Test ,
+    ECG_Test, 
+}test_type_info;
 
 
 typedef struct __attribute__((__packed__))
@@ -44,4 +59,31 @@
   CalData_info          cal_data;
 }BLEMsg_info;
 
+typedef enum 
+{ 
+     sos= (uint8_t)0xc0,
+     eos= (uint8_t)0xc0,
+}file_st;
+
+
+
+typedef struct __attribute__((__packed__))
+{
+
+ file_st start_end_of_string;
+ uint32_t length_file;
+ bt_st proto;
+ BLEMsg_info  bt_msg;
+ uint8_t chk_sum;
+
+}Point;
+
+
+uint8_t bt_send( uint8_t send_tx);
+uint8_t bt_receive(uint8_t send_rx);
+void send();
+void sendinit();
+void send_data_str1();
+void send_data_str2();
+
 #endif
\ No newline at end of file
--- a/test_type.h	Fri Jun 09 09:22:32 2017 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-#ifndef TEST_TYPE_H_
-#define TEST_TYPE_H_
-typedef enum
-{ 
-    BP_Test =   (uint8_t )0x00,   // removed Semi Colon and replaced with comma
-    BG_Test ,
-    ECG_Test, 
-}test_type_info;
-
-#endif
\ No newline at end of file