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:
nikitateggi
Date:
Fri Sep 15 07:38:47 2017 +0000
Revision:
48:f90edb35a20e
Parent:
47:3716dd41dba6
Child:
52:7d9ff7781bdc
Merged code with battery monitoring, bluetooth connection timeout and ack timeout

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nikitateggi 19:1650bbc923cc 1 #include "mbed.h"
nikitateggi 19:1650bbc923cc 2 #include "sdcard.h"
nikitateggi 19:1650bbc923cc 3 #include "struct.h"
nikitateggi 44:05dbea558c9a 4 #include "eeprom_pgm.h"
nikitateggi 44:05dbea558c9a 5 #include "main.h"
nikitateggi 44:05dbea558c9a 6
nikitateggi 45:067673dae242 7 #define ACK 0x00 /// changed //16/06 #define all the values removing structure
nikitateggi 45:067673dae242 8 #define START_OF_FILE 0x01
nikitateggi 45:067673dae242 9 #define END_OF_FILE 0x02
nikitateggi 45:067673dae242 10 #define DATA_TRFR 0x03
nikitateggi 45:067673dae242 11 #define NACK 0xFF
nikitateggi 45:067673dae242 12 #define SOS 0xc0
nikitateggi 45:067673dae242 13 #define EOS 0xc0
nikitateggi 45:067673dae242 14 #define NO_OF_SAMPLES 600 //changed the buffer size from 150- 22/7/17- suhasini
nikitateggi 45:067673dae242 15 #define CHKSUM 0x80
nikitateggi 45:067673dae242 16 #define STRUCTURE_LENGTH 26
nikitateggi 19:1650bbc923cc 17
nikitateggi 22:ffa88619551d 18 BLEMsg_info *bt_file;
nikitateggi 22:ffa88619551d 19 Point *myBleMsg;
nikitateggi 27:907f8ce0025e 20
nikitateggi 27:907f8ce0025e 21 Serial mc(USBTX,USBRX); // serial terminal
nikitateggi 27:907f8ce0025e 22 Serial blue(PTC4,PTC3); // communicating bluetooth through serial
nikitateggi 27:907f8ce0025e 23
nikitateggi 28:65f2d80b7eb7 24 static uint32_t total_samples=0; // variable to hold total number of samples
nikitateggi 19:1650bbc923cc 25 Point b; // structure copy
nikitateggi 19:1650bbc923cc 26 BLEMsg_info bt_file1; // structure copy
nikitateggi 28:65f2d80b7eb7 27 static uint32_t total_file_size=0; // to determine the size of the file
nikitateggi 44:05dbea558c9a 28 static uint32_t total_file_size_sent=0;
nikitateggi 28:65f2d80b7eb7 29 static uint32_t file_start=0; // variable for 'for loop'
nikitateggi 44:05dbea558c9a 30 //static uint32_t j=0;
nikitateggi 27:907f8ce0025e 31 uint8_t count_send=0;
nikitateggi 28:65f2d80b7eb7 32 static uint32_t file_pointer_position=0;
nikitateggi 28:65f2d80b7eb7 33 static uint32_t counter=0;
nikitateggi 44:05dbea558c9a 34 static uint32_t v1[NO_OF_SAMPLES];
nikitateggi 27:907f8ce0025e 35 uint8_t file=0;
nikitateggi 44:05dbea558c9a 36
nikitateggi 19:1650bbc923cc 37
nikitateggi 19:1650bbc923cc 38 uint8_t bt_send(uint8_t state_tx)
nikitateggi 19:1650bbc923cc 39 {
nikitateggi 32:76892fdf5e4c 40
nikitateggi 44:05dbea558c9a 41 blue.baud(115200); // baud rate of bluetooth
nikitateggi 44:05dbea558c9a 42 bt_file=&bt_file1;
nikitateggi 44:05dbea558c9a 43 myBleMsg=&b;
nikitateggi 44:05dbea558c9a 44 int8_t check_sum=0;
nikitateggi 44:05dbea558c9a 45 uint8_t state_rx =0;
nikitateggi 27:907f8ce0025e 46
nikitateggi 45:067673dae242 47 switch(state_tx)
nikitateggi 19:1650bbc923cc 48 {
nikitateggi 19:1650bbc923cc 49
nikitateggi 27:907f8ce0025e 50 case 1:
nikitateggi 44:05dbea558c9a 51 state_tx =0;
nikitateggi 27:907f8ce0025e 52 counter=0;
nikitateggi 44:05dbea558c9a 53 total_file_size_sent=0;
nikitateggi 45:067673dae242 54 file_start=0;
nikitateggi 45:067673dae242 55 count_send=0; total_samples=0;
nikitateggi 27:907f8ce0025e 56 file_pointer_position=0;
nikitateggi 48:f90edb35a20e 57 mc.printf("bt_pid = %d\n",get_btpid());
nikitateggi 45:067673dae242 58 if(get_filepid()>get_btpid()) //check for pid and bluetooth pid by reading from file
nikitateggi 44:05dbea558c9a 59 {
nikitateggi 44:05dbea558c9a 60 send_initial_string();
nikitateggi 44:05dbea558c9a 61 blue.printf("\n");
nikitateggi 48:f90edb35a20e 62 file=sd_open_read(get_btpid()); // open the file for reading
nikitateggi 48:f90edb35a20e 63 total_file_size=file_size(); // determined the file size
nikitateggi 47:3716dd41dba6 64 state_rx = 1;
nikitateggi 44:05dbea558c9a 65 }
nikitateggi 44:05dbea558c9a 66
nikitateggi 44:05dbea558c9a 67 else
nikitateggi 44:05dbea558c9a 68 {
nikitateggi 45:067673dae242 69 state_rx =0; //no files to send
nikitateggi 44:05dbea558c9a 70 mc.printf("no file to send\n");
nikitateggi 44:05dbea558c9a 71 }
nikitateggi 44:05dbea558c9a 72 break;
nikitateggi 44:05dbea558c9a 73
nikitateggi 19:1650bbc923cc 74
nikitateggi 44:05dbea558c9a 75 case 2: total_samples=0;
nikitateggi 47:3716dd41dba6 76 //file=sd_open_read(get_btpid());
nikitateggi 48:f90edb35a20e 77 // total_file_size=file_size() ; // determined the file size
nikitateggi 45:067673dae242 78 sd_read1(bt_file1); // read the sd card file
avp2417 36:00d96aa14658 79 mc.printf("sd file opened to read data in it\n");
nikitateggi 45:067673dae242 80 myBleMsg->bt_msg=bt_file1; //copy the contents to the structure
nikitateggi 45:067673dae242 81 send_structure(); // send the structure string
avp2417 36:00d96aa14658 82 blue.printf("\n");
avp2417 36:00d96aa14658 83 mc.printf("sent initial string\n");
nikitateggi 27:907f8ce0025e 84 total_samples= myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp;
nikitateggi 19:1650bbc923cc 85 state_tx =0;
nikitateggi 19:1650bbc923cc 86 state_rx =2;
nikitateggi 27:907f8ce0025e 87 count_send=0;
nikitateggi 44:05dbea558c9a 88 total_file_size_sent=file_size1();
nikitateggi 19:1650bbc923cc 89 break;
nikitateggi 19:1650bbc923cc 90
nikitateggi 19:1650bbc923cc 91
nikitateggi 44:05dbea558c9a 92 case 3:
nikitateggi 44:05dbea558c9a 93 counter=0;
nikitateggi 44:05dbea558c9a 94 if(count_send<(total_samples/NO_OF_SAMPLES))
nikitateggi 44:05dbea558c9a 95 {
nikitateggi 44:05dbea558c9a 96 sd_read(v1,NO_OF_SAMPLES);
nikitateggi 44:05dbea558c9a 97 counter=NO_OF_SAMPLES; //read 1500 samples
nikitateggi 44:05dbea558c9a 98 send_data_str1();
nikitateggi 44:05dbea558c9a 99 for(file_start=0;file_start<(NO_OF_SAMPLES);file_start++)
nikitateggi 44:05dbea558c9a 100 {
nikitateggi 19:1650bbc923cc 101
nikitateggi 44:05dbea558c9a 102 blue.printf("%08x", v1[file_start]);
nikitateggi 27:907f8ce0025e 103
nikitateggi 44:05dbea558c9a 104 }
avp2417 36:00d96aa14658 105
nikitateggi 44:05dbea558c9a 106 check_sum=checksum_rawdata((uint8_t *)v1,counter); // calculating checksum // calculating checksum
nikitateggi 44:05dbea558c9a 107 state_tx =0;
nikitateggi 44:05dbea558c9a 108 state_rx =3;
nikitateggi 44:05dbea558c9a 109 send_data_str2(check_sum); // send the remaining part of the string
nikitateggi 19:1650bbc923cc 110
nikitateggi 44:05dbea558c9a 111 }
nikitateggi 19:1650bbc923cc 112
nikitateggi 44:05dbea558c9a 113 else
nikitateggi 44:05dbea558c9a 114 {
nikitateggi 44:05dbea558c9a 115 for(uint16_t l=0;l<NO_OF_SAMPLES;l++)
nikitateggi 44:05dbea558c9a 116 {
nikitateggi 44:05dbea558c9a 117 v1[l]=NULL;
nikitateggi 44:05dbea558c9a 118 }
nikitateggi 44:05dbea558c9a 119 counter=(total_samples-( NO_OF_SAMPLES*count_send));
nikitateggi 27:907f8ce0025e 120 send_data_str1();
nikitateggi 44:05dbea558c9a 121 sd_read(v1,(total_samples-( NO_OF_SAMPLES*count_send)));
nikitateggi 44:05dbea558c9a 122
nikitateggi 44:05dbea558c9a 123 for(file_start=0;file_start<(total_samples-( NO_OF_SAMPLES*count_send));file_start++)
nikitateggi 19:1650bbc923cc 124 {
nikitateggi 24:d992ee8369f4 125 blue.printf("%08x", v1[file_start]);
nikitateggi 27:907f8ce0025e 126
nikitateggi 19:1650bbc923cc 127 }
nikitateggi 27:907f8ce0025e 128
nikitateggi 19:1650bbc923cc 129 state_tx=0;
nikitateggi 19:1650bbc923cc 130 state_rx=4;
nikitateggi 44:05dbea558c9a 131 check_sum=checksum_rawdata((uint8_t *)v1,counter); // calculating checksum
nikitateggi 22:ffa88619551d 132 send_data_str2(check_sum);
nikitateggi 19:1650bbc923cc 133 }
nikitateggi 19:1650bbc923cc 134
nikitateggi 45:067673dae242 135 total_file_size_sent=file_size1();
nikitateggi 27:907f8ce0025e 136 blue.printf("\n");
nikitateggi 27:907f8ce0025e 137 break;
nikitateggi 19:1650bbc923cc 138
nikitateggi 19:1650bbc923cc 139
nikitateggi 48:f90edb35a20e 140 case 4: // total_file_size_sent=file_size1();
nikitateggi 44:05dbea558c9a 141 if(total_file_size_sent==total_file_size) // if the total file size is same as total file size
nikitateggi 27:907f8ce0025e 142 { // then there is no more data to send in that particular file
nikitateggi 19:1650bbc923cc 143
nikitateggi 44:05dbea558c9a 144 state_rx=5;
nikitateggi 44:05dbea558c9a 145 send_last_string();
nikitateggi 24:d992ee8369f4 146 blue.printf("\n");
nikitateggi 44:05dbea558c9a 147
nikitateggi 19:1650bbc923cc 148 }
nikitateggi 19:1650bbc923cc 149 else
nikitateggi 19:1650bbc923cc 150 {
nikitateggi 44:05dbea558c9a 151
nikitateggi 44:05dbea558c9a 152 total_samples=0;
nikitateggi 44:05dbea558c9a 153 sd_read1(bt_file1); // read the sd card file
nikitateggi 46:162ed3f051b2 154 // mc.printf("sd file opened to read data in it\n");
nikitateggi 44:05dbea558c9a 155 myBleMsg->bt_msg=bt_file1; //copy the contents to the structure
nikitateggi 44:05dbea558c9a 156 send_structure(); // send the initial string
nikitateggi 44:05dbea558c9a 157 blue.printf("\n");
nikitateggi 46:162ed3f051b2 158 // mc.printf("sent initial string\n");
nikitateggi 44:05dbea558c9a 159 total_samples= myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp;
nikitateggi 44:05dbea558c9a 160 state_tx =0;
nikitateggi 44:05dbea558c9a 161 state_rx =2;
nikitateggi 44:05dbea558c9a 162 count_send=0;
nikitateggi 44:05dbea558c9a 163 total_file_size_sent=file_size1();
nikitateggi 44:05dbea558c9a 164
nikitateggi 19:1650bbc923cc 165 }
nikitateggi 19:1650bbc923cc 166
nikitateggi 19:1650bbc923cc 167
nikitateggi 27:907f8ce0025e 168
nikitateggi 19:1650bbc923cc 169
nikitateggi 19:1650bbc923cc 170 }
nikitateggi 27:907f8ce0025e 171
nikitateggi 19:1650bbc923cc 172 return state_rx;
nikitateggi 19:1650bbc923cc 173
nikitateggi 19:1650bbc923cc 174 }
nikitateggi 19:1650bbc923cc 175
nikitateggi 19:1650bbc923cc 176
nikitateggi 19:1650bbc923cc 177
nikitateggi 19:1650bbc923cc 178
nikitateggi 19:1650bbc923cc 179 uint8_t bt_receive (uint8_t state_rx)
nikitateggi 19:1650bbc923cc 180 {
nikitateggi 19:1650bbc923cc 181
nikitateggi 45:067673dae242 182 unsigned char state_tx =1; //state for transmitting
nikitateggi 44:05dbea558c9a 183 uint8_t ack_rx=0; // varibale to receive ack from the bluetooth
nikitateggi 27:907f8ce0025e 184 uint8_t sos_rx=0; // sos received from app
nikitateggi 27:907f8ce0025e 185 uint8_t eos_rx=0; // eos received from app
nikitateggi 44:05dbea558c9a 186 uint32_t length_rx=0;
nikitateggi 44:05dbea558c9a 187 uint8_t chksum_rx=0;
nikitateggi 44:05dbea558c9a 188
nikitateggi 45:067673dae242 189 while (blue.readable()) // reading from bluetooth app
nikitateggi 44:05dbea558c9a 190 {
nikitateggi 44:05dbea558c9a 191 blue.scanf("%02x", &sos_rx);
nikitateggi 44:05dbea558c9a 192 blue.scanf("%02x", &ack_rx);
nikitateggi 44:05dbea558c9a 193 blue.scanf("%08x", &length_rx);
nikitateggi 44:05dbea558c9a 194 blue.scanf("%02x", &eos_rx);
nikitateggi 44:05dbea558c9a 195 blue.scanf("%02x", &chksum_rx);
nikitateggi 44:05dbea558c9a 196 break;
nikitateggi 44:05dbea558c9a 197 }
nikitateggi 46:162ed3f051b2 198 // mc.printf("%02x%02x%08x%02x%02x", sos_rx,ack_rx,length_rx,eos_rx,chksum_rx);
nikitateggi 44:05dbea558c9a 199
nikitateggi 19:1650bbc923cc 200 switch(state_rx)
nikitateggi 19:1650bbc923cc 201 {
nikitateggi 19:1650bbc923cc 202
nikitateggi 19:1650bbc923cc 203 case 1: // wait for the ack
nikitateggi 22:ffa88619551d 204 state_rx=0;
nikitateggi 19:1650bbc923cc 205 myBleMsg->proto=ACK;
nikitateggi 45:067673dae242 206 if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) // check for ack and checksum
nikitateggi 44:05dbea558c9a 207
nikitateggi 19:1650bbc923cc 208 {
nikitateggi 19:1650bbc923cc 209
nikitateggi 19:1650bbc923cc 210 state_tx=2;
nikitateggi 19:1650bbc923cc 211 }
nikitateggi 19:1650bbc923cc 212
nikitateggi 22:ffa88619551d 213 else
nikitateggi 19:1650bbc923cc 214
nikitateggi 19:1650bbc923cc 215 {
nikitateggi 19:1650bbc923cc 216
nikitateggi 19:1650bbc923cc 217 state_tx=1;
nikitateggi 48:f90edb35a20e 218 sd_close();
nikitateggi 19:1650bbc923cc 219 }
nikitateggi 19:1650bbc923cc 220 break;
nikitateggi 19:1650bbc923cc 221
nikitateggi 19:1650bbc923cc 222 case 2: state_rx =0; //wait for the ack
nikitateggi 44:05dbea558c9a 223
nikitateggi 45:067673dae242 224 if ((ack_rx == ACK) && (chksum_rx == CHKSUM))
nikitateggi 19:1650bbc923cc 225 {
nikitateggi 19:1650bbc923cc 226
nikitateggi 19:1650bbc923cc 227 state_tx=3;
nikitateggi 44:05dbea558c9a 228 file_pointer_position=total_file_size_sent;
nikitateggi 19:1650bbc923cc 229 }
nikitateggi 19:1650bbc923cc 230
nikitateggi 19:1650bbc923cc 231 else
nikitateggi 19:1650bbc923cc 232
nikitateggi 19:1650bbc923cc 233 {
nikitateggi 19:1650bbc923cc 234
nikitateggi 19:1650bbc923cc 235 state_tx=2;
nikitateggi 22:ffa88619551d 236 setpos1(file_pointer_position);
nikitateggi 47:3716dd41dba6 237 //sd_close(); //file closed which was opened earlier because it opens again in bt_send case 1
nikitateggi 27:907f8ce0025e 238
nikitateggi 19:1650bbc923cc 239 }
nikitateggi 27:907f8ce0025e 240 //blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
nikitateggi 19:1650bbc923cc 241 break;
nikitateggi 19:1650bbc923cc 242
nikitateggi 19:1650bbc923cc 243
nikitateggi 19:1650bbc923cc 244 case 3:
nikitateggi 19:1650bbc923cc 245 state_rx =0;
nikitateggi 45:067673dae242 246 if ((ack_rx == ACK) && (chksum_rx == CHKSUM))
nikitateggi 19:1650bbc923cc 247 {
nikitateggi 19:1650bbc923cc 248
nikitateggi 45:067673dae242 249 state_tx=3;
nikitateggi 27:907f8ce0025e 250 count_send=count_send+1;
nikitateggi 45:067673dae242 251 file_pointer_position=total_file_size_sent;
nikitateggi 19:1650bbc923cc 252 }
nikitateggi 19:1650bbc923cc 253
nikitateggi 19:1650bbc923cc 254 else
nikitateggi 19:1650bbc923cc 255
nikitateggi 19:1650bbc923cc 256 {
nikitateggi 27:907f8ce0025e 257
nikitateggi 19:1650bbc923cc 258 state_tx=3;
nikitateggi 44:05dbea558c9a 259 file_start=file_start- NO_OF_SAMPLES;
nikitateggi 44:05dbea558c9a 260 setpos1(file_pointer_position);
nikitateggi 44:05dbea558c9a 261 }
nikitateggi 44:05dbea558c9a 262
nikitateggi 44:05dbea558c9a 263 break;
nikitateggi 44:05dbea558c9a 264
nikitateggi 44:05dbea558c9a 265 case 4:
nikitateggi 44:05dbea558c9a 266 state_rx =0;
nikitateggi 45:067673dae242 267 if ((ack_rx == ACK) && (chksum_rx == CHKSUM))
nikitateggi 44:05dbea558c9a 268 {
nikitateggi 45:067673dae242 269 state_tx=4;
nikitateggi 44:05dbea558c9a 270 count_send=count_send+1;
nikitateggi 45:067673dae242 271 file_pointer_position=total_file_size_sent;
nikitateggi 44:05dbea558c9a 272 }
nikitateggi 44:05dbea558c9a 273
nikitateggi 44:05dbea558c9a 274 else
nikitateggi 44:05dbea558c9a 275
nikitateggi 44:05dbea558c9a 276 {
nikitateggi 44:05dbea558c9a 277
nikitateggi 44:05dbea558c9a 278 state_tx=3;
nikitateggi 44:05dbea558c9a 279 file_start=file_start- NO_OF_SAMPLES;
nikitateggi 19:1650bbc923cc 280 setpos1(file_pointer_position);
nikitateggi 19:1650bbc923cc 281 }
nikitateggi 19:1650bbc923cc 282 break;
nikitateggi 19:1650bbc923cc 283
nikitateggi 44:05dbea558c9a 284
nikitateggi 44:05dbea558c9a 285 case 5: state_rx = 0;
nikitateggi 45:067673dae242 286 if ((ack_rx == ACK) && (chksum_rx == CHKSUM))
nikitateggi 44:05dbea558c9a 287 {
nikitateggi 44:05dbea558c9a 288 sd_close();
nikitateggi 45:067673dae242 289 increment_btpid(); // increment the bluetooth pid and write in the file
nikitateggi 47:3716dd41dba6 290 mc.printf("next file");
nikitateggi 47:3716dd41dba6 291 mc.printf("bt_pid = %d\n",get_btpid());
nikitateggi 44:05dbea558c9a 292 state_tx=1;
nikitateggi 44:05dbea558c9a 293
nikitateggi 44:05dbea558c9a 294 }
nikitateggi 27:907f8ce0025e 295
nikitateggi 44:05dbea558c9a 296 else
nikitateggi 44:05dbea558c9a 297 {
nikitateggi 44:05dbea558c9a 298 state_tx = 4;
nikitateggi 27:907f8ce0025e 299
nikitateggi 44:05dbea558c9a 300 }
nikitateggi 44:05dbea558c9a 301 break;
nikitateggi 19:1650bbc923cc 302
nikitateggi 19:1650bbc923cc 303 }
nikitateggi 27:907f8ce0025e 304
nikitateggi 19:1650bbc923cc 305 return state_tx;
nikitateggi 19:1650bbc923cc 306 }
nikitateggi 19:1650bbc923cc 307
nikitateggi 19:1650bbc923cc 308
nikitateggi 19:1650bbc923cc 309
nikitateggi 19:1650bbc923cc 310
nikitateggi 19:1650bbc923cc 311
nikitateggi 19:1650bbc923cc 312
nikitateggi 19:1650bbc923cc 313
nikitateggi 44:05dbea558c9a 314 void send_structure() // function to send the structure to bluetooth
nikitateggi 19:1650bbc923cc 315 {
nikitateggi 45:067673dae242 316 myBleMsg->start_of_string=SOS;
nikitateggi 45:067673dae242 317 myBleMsg->length_file = STRUCTURE_LENGTH;
nikitateggi 45:067673dae242 318 myBleMsg->proto=DATA_TRFR; // data transfer mode
nikitateggi 45:067673dae242 319 blue.printf("%02x",myBleMsg->start_of_string);
nikitateggi 45:067673dae242 320 blue.printf("%02x",myBleMsg->proto);
nikitateggi 45:067673dae242 321 blue.printf("%08x",myBleMsg->length_file); //changed the sequence
nikitateggi 45:067673dae242 322 blue.printf("%08x",myBleMsg->bt_msg.length);
nikitateggi 45:067673dae242 323 blue.printf("%02x",myBleMsg->bt_msg.device_id);
nikitateggi 45:067673dae242 324 blue.printf("%08x",myBleMsg->bt_msg.patient_id);
nikitateggi 45:067673dae242 325 blue.printf("%02x",myBleMsg->bt_msg.date_time.date);
nikitateggi 45:067673dae242 326 blue.printf("%02x",myBleMsg->bt_msg.date_time.month);
nikitateggi 45:067673dae242 327 blue.printf("%02x",myBleMsg->bt_msg.date_time.year);
nikitateggi 45:067673dae242 328 blue.printf("%02x",myBleMsg->bt_msg.date_time.hour);
nikitateggi 45:067673dae242 329 blue.printf("%02x",myBleMsg->bt_msg.date_time.mins);
nikitateggi 45:067673dae242 330 blue.printf("%02x",myBleMsg->bt_msg.date_time.sec);
nikitateggi 45:067673dae242 331 blue.printf("%02x",myBleMsg->bt_msg.test_type);
nikitateggi 45:067673dae242 332 blue.printf("%04x",myBleMsg->bt_msg.sampling_freq);
nikitateggi 45:067673dae242 333 blue.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy);
nikitateggi 45:067673dae242 334 blue.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp);
nikitateggi 45:067673dae242 335 blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy);
nikitateggi 45:067673dae242 336 blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp);
nikitateggi 45:067673dae242 337 myBleMsg->end_of_string=EOS;
nikitateggi 45:067673dae242 338 myBleMsg->chk_sum = checksum_struct((uint8_t *)myBleMsg, sizeof(Point));
nikitateggi 45:067673dae242 339 blue.printf("%02x",myBleMsg->end_of_string);
nikitateggi 45:067673dae242 340 blue.printf("%02x",myBleMsg->chk_sum);
nikitateggi 44:05dbea558c9a 341
nikitateggi 19:1650bbc923cc 342
nikitateggi 19:1650bbc923cc 343 }
nikitateggi 19:1650bbc923cc 344
nikitateggi 44:05dbea558c9a 345 void send_initial_string() // function to send the initial string at the start f communication to bluetooth
nikitateggi 22:ffa88619551d 346 {
nikitateggi 22:ffa88619551d 347
nikitateggi 45:067673dae242 348 uint8_t sum_init=0 ;
nikitateggi 45:067673dae242 349 myBleMsg->start_of_string=SOS;
nikitateggi 45:067673dae242 350 myBleMsg->length_file=0;
nikitateggi 45:067673dae242 351 myBleMsg->proto=START_OF_FILE;
nikitateggi 45:067673dae242 352 sum_init=checksum_init();
nikitateggi 45:067673dae242 353 myBleMsg->chk_sum=sum_init;
nikitateggi 45:067673dae242 354 blue.printf("%02x",myBleMsg->start_of_string);
nikitateggi 45:067673dae242 355 blue.printf("%02x",myBleMsg->proto);
nikitateggi 45:067673dae242 356 blue.printf("%08x",myBleMsg->length_file); // chnged
nikitateggi 45:067673dae242 357 myBleMsg->end_of_string=EOS;
nikitateggi 45:067673dae242 358 blue.printf("%02x",myBleMsg->end_of_string);
nikitateggi 45:067673dae242 359 blue.printf("%02x",myBleMsg->chk_sum);
nikitateggi 19:1650bbc923cc 360
nikitateggi 19:1650bbc923cc 361 }
nikitateggi 22:ffa88619551d 362
nikitateggi 22:ffa88619551d 363
nikitateggi 27:907f8ce0025e 364 uint8_t checksum_init() // function to calculate the checksum for the initial string
nikitateggi 22:ffa88619551d 365 {
nikitateggi 22:ffa88619551d 366
nikitateggi 45:067673dae242 367 uint32_t sum = 0;
nikitateggi 45:067673dae242 368 uint8_t chksum_init = 0;
nikitateggi 44:05dbea558c9a 369 sum=SOS+EOS+START_OF_FILE;
nikitateggi 45:067673dae242 370 chksum_init = sum & 0x000000FF;
nikitateggi 22:ffa88619551d 371 return chksum_init;
nikitateggi 22:ffa88619551d 372 }
nikitateggi 22:ffa88619551d 373
nikitateggi 27:907f8ce0025e 374 uint8_t checksum_last() // function to calculate the checksum for the last string
nikitateggi 22:ffa88619551d 375 {
nikitateggi 22:ffa88619551d 376
nikitateggi 45:067673dae242 377 uint32_t sum = 0;
nikitateggi 45:067673dae242 378 uint8_t chksum_last = 0;
nikitateggi 44:05dbea558c9a 379 sum=SOS+EOS+END_OF_FILE; // changed //16/06 #define all the values removing structure
nikitateggi 22:ffa88619551d 380 chksum_last=sum & 0x000000FF;
nikitateggi 22:ffa88619551d 381 return chksum_last;
nikitateggi 22:ffa88619551d 382 }
nikitateggi 22:ffa88619551d 383
nikitateggi 45:067673dae242 384 void send_last_string() // function to send the last string to the bluetooth // which determines the end of file
nikitateggi 22:ffa88619551d 385 {
nikitateggi 45:067673dae242 386 uint8_t sum_last=0;
nikitateggi 45:067673dae242 387 myBleMsg->start_of_string = SOS;
nikitateggi 45:067673dae242 388 myBleMsg->length_file = 0;
nikitateggi 45:067673dae242 389 myBleMsg->proto=END_OF_FILE;
nikitateggi 45:067673dae242 390 sum_last=checksum_last();
nikitateggi 45:067673dae242 391 myBleMsg->chk_sum=sum_last;
nikitateggi 45:067673dae242 392 blue.printf("%02x",myBleMsg->start_of_string);
nikitateggi 45:067673dae242 393 blue.printf("%02x",myBleMsg->proto);
nikitateggi 45:067673dae242 394 blue.printf("%08x",myBleMsg->length_file); // chnged
nikitateggi 45:067673dae242 395 myBleMsg->end_of_string=EOS;
nikitateggi 45:067673dae242 396 blue.printf("%02x",myBleMsg->end_of_string);
nikitateggi 45:067673dae242 397 blue.printf("%02x",myBleMsg->chk_sum);
nikitateggi 45:067673dae242 398
nikitateggi 45:067673dae242 399 }
nikitateggi 22:ffa88619551d 400
nikitateggi 45:067673dae242 401 void send_data_str1() // function to send a part of the structure // after this raw data is sent//
nikitateggi 45:067673dae242 402 {
nikitateggi 45:067673dae242 403 myBleMsg->start_of_string=SOS;
nikitateggi 45:067673dae242 404 myBleMsg->length_file=(counter*4);
nikitateggi 45:067673dae242 405 myBleMsg->proto=DATA_TRFR;
nikitateggi 45:067673dae242 406 blue.printf("%02x",myBleMsg->start_of_string);
nikitateggi 45:067673dae242 407 blue.printf("%02x",myBleMsg->proto); // chnaged
nikitateggi 45:067673dae242 408 blue.printf("%08x",myBleMsg->length_file);
nikitateggi 45:067673dae242 409
nikitateggi 45:067673dae242 410 }
nikitateggi 19:1650bbc923cc 411
nikitateggi 19:1650bbc923cc 412
nikitateggi 19:1650bbc923cc 413
nikitateggi 45:067673dae242 414 void send_data_str2(uint8_t check_sum) // after raw data// this last part of structure is sent
nikitateggi 45:067673dae242 415 {
nikitateggi 45:067673dae242 416 myBleMsg->chk_sum=check_sum;
nikitateggi 45:067673dae242 417 myBleMsg->end_of_string=EOS;
nikitateggi 45:067673dae242 418 blue.printf("%02x",myBleMsg->end_of_string);
nikitateggi 45:067673dae242 419 blue.printf("%02x",myBleMsg->chk_sum);
nikitateggi 45:067673dae242 420
nikitateggi 45:067673dae242 421 }
nikitateggi 19:1650bbc923cc 422
nikitateggi 31:0f67acfc6236 423
nikitateggi 31:0f67acfc6236 424 uint8_t checksum_struct(const uint8_t data[], uint16_t numBytes) // calculating checksum for
nikitateggi 31:0f67acfc6236 425 {
nikitateggi 31:0f67acfc6236 426 uint32_t chksum_struct = 0;
nikitateggi 31:0f67acfc6236 427 uint16_t index = 0;
nikitateggi 31:0f67acfc6236 428
nikitateggi 31:0f67acfc6236 429 for (index=0U; index < (numBytes-1); index++)
nikitateggi 31:0f67acfc6236 430 {
nikitateggi 31:0f67acfc6236 431 chksum_struct += data[index];
nikitateggi 31:0f67acfc6236 432 //blue.printf("data=%02x\n",data[index]);
nikitateggi 31:0f67acfc6236 433
nikitateggi 31:0f67acfc6236 434 }
nikitateggi 31:0f67acfc6236 435 // blue.printf("%08x\n",chksum_struct);
nikitateggi 31:0f67acfc6236 436 return chksum_struct;
nikitateggi 22:ffa88619551d 437 }
nikitateggi 31:0f67acfc6236 438
nikitateggi 31:0f67acfc6236 439
nikitateggi 44:05dbea558c9a 440 uint8_t checksum_rawdata(const uint8_t data[], uint32_t numBytes) // this function to calculate the checksum for the raw data
nikitateggi 31:0f67acfc6236 441 {
nikitateggi 31:0f67acfc6236 442
nikitateggi 45:067673dae242 443 uint32_t sum=0;
nikitateggi 45:067673dae242 444 uint8_t chksum_datatrfr=0;
nikitateggi 45:067673dae242 445 uint32_t index = 0;
nikitateggi 45:067673dae242 446 uint32_t index1 = 0;
nikitateggi 44:05dbea558c9a 447 sum += SOS;
nikitateggi 44:05dbea558c9a 448 sum += DATA_TRFR;
nikitateggi 45:067673dae242 449 index1=(numBytes*4);
nikitateggi 45:067673dae242 450 sum +=(index1>>(8*0))&0xFF;
nikitateggi 31:0f67acfc6236 451 sum +=(index1>>(8*1))&0xFF;
nikitateggi 31:0f67acfc6236 452
nikitateggi 45:067673dae242 453 for (index=0U; index<(numBytes*4); index++)
nikitateggi 45:067673dae242 454 {
nikitateggi 31:0f67acfc6236 455 sum += data[index];
nikitateggi 45:067673dae242 456 }
nikitateggi 31:0f67acfc6236 457
nikitateggi 45:067673dae242 458 sum+=EOS;
nikitateggi 31:0f67acfc6236 459
nikitateggi 45:067673dae242 460 chksum_datatrfr = sum & 0x000000FF;
nikitateggi 44:05dbea558c9a 461 //mc.printf("chksum_datatrfr=%02x\n",chksum_datatrfr);
nikitateggi 45:067673dae242 462 return chksum_datatrfr;
nikitateggi 44:05dbea558c9a 463 }
nikitateggi 44:05dbea558c9a 464
nikitateggi 44:05dbea558c9a 465
nikitateggi 44:05dbea558c9a 466
nikitateggi 44:05dbea558c9a 467