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