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@52:7d9ff7781bdc, 2017-09-20 (annotated)
- Committer:
- nikitateggi
- Date:
- Wed Sep 20 10:33:17 2017 +0000
- Revision:
- 52:7d9ff7781bdc
- Parent:
- 48:f90edb35a20e
- Child:
- 54:f2a413d5dffd
Modified the bluetooth part. checking both ack and checksum and all the data are visible on the app.
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 | 52:7d9ff7781bdc | 36 | |
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 | 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 | 52:7d9ff7781bdc | 168 | |
nikitateggi | 52:7d9ff7781bdc | 169 | |
nikitateggi | 19:1650bbc923cc | 170 | } |
nikitateggi | 52:7d9ff7781bdc | 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 | 52:7d9ff7781bdc | 182 | unsigned char state_tx =1; //state for transmitting |
nikitateggi | 52:7d9ff7781bdc | 183 | uint8_t string_length_rx=0; |
nikitateggi | 52:7d9ff7781bdc | 184 | char ack_rx[2]; // varibale to receive ack from the bluetooth |
nikitateggi | 52:7d9ff7781bdc | 185 | char chksum_rx[3]; |
nikitateggi | 52:7d9ff7781bdc | 186 | char btdata_received[17]; |
nikitateggi | 52:7d9ff7781bdc | 187 | |
nikitateggi | 44:05dbea558c9a | 188 | |
nikitateggi | 52:7d9ff7781bdc | 189 | while (blue.readable()!= NULL) // reading string from bluetooth app |
nikitateggi | 52:7d9ff7781bdc | 190 | { |
nikitateggi | 52:7d9ff7781bdc | 191 | blue.scanf("%17s", btdata_received); |
nikitateggi | 52:7d9ff7781bdc | 192 | mc.printf("%s\r\n", btdata_received); |
nikitateggi | 52:7d9ff7781bdc | 193 | |
nikitateggi | 52:7d9ff7781bdc | 194 | } |
nikitateggi | 52:7d9ff7781bdc | 195 | |
nikitateggi | 52:7d9ff7781bdc | 196 | string_length_rx = strlen(btdata_received); // storing the length of the string received |
nikitateggi | 52:7d9ff7781bdc | 197 | memcpy(ack_rx, btdata_received+1, 2); // reading the ack received |
nikitateggi | 52:7d9ff7781bdc | 198 | memcpy(chksum_rx, btdata_received+(string_length_rx-2), 3); // reading the checksum received |
nikitateggi | 52:7d9ff7781bdc | 199 | mc.printf("%s\r\n",ack_rx); |
nikitateggi | 52:7d9ff7781bdc | 200 | mc.printf("%s\r\n",chksum_rx); |
nikitateggi | 44:05dbea558c9a | 201 | |
nikitateggi | 19:1650bbc923cc | 202 | switch(state_rx) |
nikitateggi | 19:1650bbc923cc | 203 | { |
nikitateggi | 19:1650bbc923cc | 204 | |
nikitateggi | 52:7d9ff7781bdc | 205 | case 1: // wait for the ack |
nikitateggi | 22:ffa88619551d | 206 | state_rx=0; |
nikitateggi | 19:1650bbc923cc | 207 | myBleMsg->proto=ACK; |
nikitateggi | 52:7d9ff7781bdc | 208 | if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL)) // check for ack and checksum |
nikitateggi | 44:05dbea558c9a | 209 | |
nikitateggi | 19:1650bbc923cc | 210 | { |
nikitateggi | 19:1650bbc923cc | 211 | |
nikitateggi | 19:1650bbc923cc | 212 | state_tx=2; |
nikitateggi | 19:1650bbc923cc | 213 | } |
nikitateggi | 19:1650bbc923cc | 214 | |
nikitateggi | 22:ffa88619551d | 215 | else |
nikitateggi | 19:1650bbc923cc | 216 | |
nikitateggi | 19:1650bbc923cc | 217 | { |
nikitateggi | 19:1650bbc923cc | 218 | |
nikitateggi | 19:1650bbc923cc | 219 | state_tx=1; |
nikitateggi | 48:f90edb35a20e | 220 | sd_close(); |
nikitateggi | 19:1650bbc923cc | 221 | } |
nikitateggi | 19:1650bbc923cc | 222 | break; |
nikitateggi | 19:1650bbc923cc | 223 | |
nikitateggi | 19:1650bbc923cc | 224 | case 2: state_rx =0; //wait for the ack |
nikitateggi | 44:05dbea558c9a | 225 | |
nikitateggi | 52:7d9ff7781bdc | 226 | if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL)) |
nikitateggi | 19:1650bbc923cc | 227 | { |
nikitateggi | 19:1650bbc923cc | 228 | |
nikitateggi | 19:1650bbc923cc | 229 | state_tx=3; |
nikitateggi | 44:05dbea558c9a | 230 | file_pointer_position=total_file_size_sent; |
nikitateggi | 19:1650bbc923cc | 231 | } |
nikitateggi | 19:1650bbc923cc | 232 | |
nikitateggi | 19:1650bbc923cc | 233 | else |
nikitateggi | 19:1650bbc923cc | 234 | |
nikitateggi | 19:1650bbc923cc | 235 | { |
nikitateggi | 19:1650bbc923cc | 236 | |
nikitateggi | 19:1650bbc923cc | 237 | state_tx=2; |
nikitateggi | 22:ffa88619551d | 238 | setpos1(file_pointer_position); |
nikitateggi | 47:3716dd41dba6 | 239 | //sd_close(); //file closed which was opened earlier because it opens again in bt_send case 1 |
nikitateggi | 27:907f8ce0025e | 240 | |
nikitateggi | 19:1650bbc923cc | 241 | } |
nikitateggi | 27:907f8ce0025e | 242 | //blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx); |
nikitateggi | 19:1650bbc923cc | 243 | break; |
nikitateggi | 52:7d9ff7781bdc | 244 | |
nikitateggi | 19:1650bbc923cc | 245 | |
nikitateggi | 19:1650bbc923cc | 246 | case 3: |
nikitateggi | 19:1650bbc923cc | 247 | state_rx =0; |
nikitateggi | 52:7d9ff7781bdc | 248 | if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL)) |
nikitateggi | 19:1650bbc923cc | 249 | { |
nikitateggi | 19:1650bbc923cc | 250 | |
nikitateggi | 45:067673dae242 | 251 | state_tx=3; |
nikitateggi | 27:907f8ce0025e | 252 | count_send=count_send+1; |
nikitateggi | 45:067673dae242 | 253 | file_pointer_position=total_file_size_sent; |
nikitateggi | 19:1650bbc923cc | 254 | } |
nikitateggi | 19:1650bbc923cc | 255 | |
nikitateggi | 19:1650bbc923cc | 256 | else |
nikitateggi | 19:1650bbc923cc | 257 | |
nikitateggi | 19:1650bbc923cc | 258 | { |
nikitateggi | 27:907f8ce0025e | 259 | |
nikitateggi | 19:1650bbc923cc | 260 | state_tx=3; |
nikitateggi | 44:05dbea558c9a | 261 | file_start=file_start- NO_OF_SAMPLES; |
nikitateggi | 44:05dbea558c9a | 262 | setpos1(file_pointer_position); |
nikitateggi | 44:05dbea558c9a | 263 | } |
nikitateggi | 44:05dbea558c9a | 264 | |
nikitateggi | 44:05dbea558c9a | 265 | break; |
nikitateggi | 44:05dbea558c9a | 266 | |
nikitateggi | 44:05dbea558c9a | 267 | case 4: |
nikitateggi | 44:05dbea558c9a | 268 | state_rx =0; |
nikitateggi | 52:7d9ff7781bdc | 269 | if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL)) |
nikitateggi | 44:05dbea558c9a | 270 | { |
nikitateggi | 45:067673dae242 | 271 | state_tx=4; |
nikitateggi | 44:05dbea558c9a | 272 | count_send=count_send+1; |
nikitateggi | 45:067673dae242 | 273 | file_pointer_position=total_file_size_sent; |
nikitateggi | 44:05dbea558c9a | 274 | } |
nikitateggi | 44:05dbea558c9a | 275 | |
nikitateggi | 44:05dbea558c9a | 276 | else |
nikitateggi | 44:05dbea558c9a | 277 | |
nikitateggi | 44:05dbea558c9a | 278 | { |
nikitateggi | 44:05dbea558c9a | 279 | |
nikitateggi | 44:05dbea558c9a | 280 | state_tx=3; |
nikitateggi | 44:05dbea558c9a | 281 | file_start=file_start- NO_OF_SAMPLES; |
nikitateggi | 19:1650bbc923cc | 282 | setpos1(file_pointer_position); |
nikitateggi | 19:1650bbc923cc | 283 | } |
nikitateggi | 19:1650bbc923cc | 284 | break; |
nikitateggi | 19:1650bbc923cc | 285 | |
nikitateggi | 52:7d9ff7781bdc | 286 | |
nikitateggi | 44:05dbea558c9a | 287 | case 5: state_rx = 0; |
nikitateggi | 52:7d9ff7781bdc | 288 | if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL)) |
nikitateggi | 44:05dbea558c9a | 289 | { |
nikitateggi | 44:05dbea558c9a | 290 | sd_close(); |
nikitateggi | 45:067673dae242 | 291 | increment_btpid(); // increment the bluetooth pid and write in the file |
nikitateggi | 47:3716dd41dba6 | 292 | mc.printf("next file"); |
nikitateggi | 47:3716dd41dba6 | 293 | mc.printf("bt_pid = %d\n",get_btpid()); |
nikitateggi | 44:05dbea558c9a | 294 | state_tx=1; |
nikitateggi | 44:05dbea558c9a | 295 | |
nikitateggi | 44:05dbea558c9a | 296 | } |
nikitateggi | 27:907f8ce0025e | 297 | |
nikitateggi | 44:05dbea558c9a | 298 | else |
nikitateggi | 44:05dbea558c9a | 299 | { |
nikitateggi | 44:05dbea558c9a | 300 | state_tx = 4; |
nikitateggi | 27:907f8ce0025e | 301 | |
nikitateggi | 44:05dbea558c9a | 302 | } |
nikitateggi | 44:05dbea558c9a | 303 | break; |
nikitateggi | 52:7d9ff7781bdc | 304 | |
nikitateggi | 19:1650bbc923cc | 305 | } |
nikitateggi | 52:7d9ff7781bdc | 306 | |
nikitateggi | 19:1650bbc923cc | 307 | return state_tx; |
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 | 19:1650bbc923cc | 314 | |
nikitateggi | 19:1650bbc923cc | 315 | |
nikitateggi | 44:05dbea558c9a | 316 | void send_structure() // function to send the structure to bluetooth |
nikitateggi | 19:1650bbc923cc | 317 | { |
nikitateggi | 45:067673dae242 | 318 | myBleMsg->start_of_string=SOS; |
nikitateggi | 45:067673dae242 | 319 | myBleMsg->length_file = STRUCTURE_LENGTH; |
nikitateggi | 45:067673dae242 | 320 | myBleMsg->proto=DATA_TRFR; // data transfer mode |
nikitateggi | 45:067673dae242 | 321 | blue.printf("%02x",myBleMsg->start_of_string); |
nikitateggi | 45:067673dae242 | 322 | blue.printf("%02x",myBleMsg->proto); |
nikitateggi | 45:067673dae242 | 323 | blue.printf("%08x",myBleMsg->length_file); //changed the sequence |
nikitateggi | 45:067673dae242 | 324 | blue.printf("%08x",myBleMsg->bt_msg.length); |
nikitateggi | 45:067673dae242 | 325 | blue.printf("%02x",myBleMsg->bt_msg.device_id); |
nikitateggi | 45:067673dae242 | 326 | blue.printf("%08x",myBleMsg->bt_msg.patient_id); |
nikitateggi | 45:067673dae242 | 327 | blue.printf("%02x",myBleMsg->bt_msg.date_time.date); |
nikitateggi | 45:067673dae242 | 328 | blue.printf("%02x",myBleMsg->bt_msg.date_time.month); |
nikitateggi | 45:067673dae242 | 329 | blue.printf("%02x",myBleMsg->bt_msg.date_time.year); |
nikitateggi | 45:067673dae242 | 330 | blue.printf("%02x",myBleMsg->bt_msg.date_time.hour); |
nikitateggi | 45:067673dae242 | 331 | blue.printf("%02x",myBleMsg->bt_msg.date_time.mins); |
nikitateggi | 45:067673dae242 | 332 | blue.printf("%02x",myBleMsg->bt_msg.date_time.sec); |
nikitateggi | 45:067673dae242 | 333 | blue.printf("%02x",myBleMsg->bt_msg.test_type); |
nikitateggi | 45:067673dae242 | 334 | blue.printf("%04x",myBleMsg->bt_msg.sampling_freq); |
nikitateggi | 45:067673dae242 | 335 | blue.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy); |
nikitateggi | 45:067673dae242 | 336 | blue.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); |
nikitateggi | 45:067673dae242 | 337 | blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy); |
nikitateggi | 45:067673dae242 | 338 | blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp); |
nikitateggi | 45:067673dae242 | 339 | myBleMsg->end_of_string=EOS; |
nikitateggi | 45:067673dae242 | 340 | myBleMsg->chk_sum = checksum_struct((uint8_t *)myBleMsg, sizeof(Point)); |
nikitateggi | 45:067673dae242 | 341 | blue.printf("%02x",myBleMsg->end_of_string); |
nikitateggi | 45:067673dae242 | 342 | blue.printf("%02x",myBleMsg->chk_sum); |
nikitateggi | 44:05dbea558c9a | 343 | |
nikitateggi | 19:1650bbc923cc | 344 | |
nikitateggi | 19:1650bbc923cc | 345 | } |
nikitateggi | 19:1650bbc923cc | 346 | |
nikitateggi | 44:05dbea558c9a | 347 | void send_initial_string() // function to send the initial string at the start f communication to bluetooth |
nikitateggi | 22:ffa88619551d | 348 | { |
nikitateggi | 52:7d9ff7781bdc | 349 | |
nikitateggi | 45:067673dae242 | 350 | uint8_t sum_init=0 ; |
nikitateggi | 45:067673dae242 | 351 | myBleMsg->start_of_string=SOS; |
nikitateggi | 45:067673dae242 | 352 | myBleMsg->length_file=0; |
nikitateggi | 45:067673dae242 | 353 | myBleMsg->proto=START_OF_FILE; |
nikitateggi | 45:067673dae242 | 354 | sum_init=checksum_init(); |
nikitateggi | 45:067673dae242 | 355 | myBleMsg->chk_sum=sum_init; |
nikitateggi | 45:067673dae242 | 356 | blue.printf("%02x",myBleMsg->start_of_string); |
nikitateggi | 45:067673dae242 | 357 | blue.printf("%02x",myBleMsg->proto); |
nikitateggi | 45:067673dae242 | 358 | blue.printf("%08x",myBleMsg->length_file); // chnged |
nikitateggi | 45:067673dae242 | 359 | myBleMsg->end_of_string=EOS; |
nikitateggi | 45:067673dae242 | 360 | blue.printf("%02x",myBleMsg->end_of_string); |
nikitateggi | 45:067673dae242 | 361 | blue.printf("%02x",myBleMsg->chk_sum); |
nikitateggi | 19:1650bbc923cc | 362 | |
nikitateggi | 19:1650bbc923cc | 363 | } |
nikitateggi | 22:ffa88619551d | 364 | |
nikitateggi | 22:ffa88619551d | 365 | |
nikitateggi | 27:907f8ce0025e | 366 | uint8_t checksum_init() // function to calculate the checksum for the initial string |
nikitateggi | 22:ffa88619551d | 367 | { |
nikitateggi | 22:ffa88619551d | 368 | |
nikitateggi | 45:067673dae242 | 369 | uint32_t sum = 0; |
nikitateggi | 45:067673dae242 | 370 | uint8_t chksum_init = 0; |
nikitateggi | 44:05dbea558c9a | 371 | sum=SOS+EOS+START_OF_FILE; |
nikitateggi | 45:067673dae242 | 372 | chksum_init = sum & 0x000000FF; |
nikitateggi | 22:ffa88619551d | 373 | return chksum_init; |
nikitateggi | 22:ffa88619551d | 374 | } |
nikitateggi | 52:7d9ff7781bdc | 375 | |
nikitateggi | 27:907f8ce0025e | 376 | uint8_t checksum_last() // function to calculate the checksum for the last string |
nikitateggi | 22:ffa88619551d | 377 | { |
nikitateggi | 22:ffa88619551d | 378 | |
nikitateggi | 45:067673dae242 | 379 | uint32_t sum = 0; |
nikitateggi | 45:067673dae242 | 380 | uint8_t chksum_last = 0; |
nikitateggi | 44:05dbea558c9a | 381 | sum=SOS+EOS+END_OF_FILE; // changed //16/06 #define all the values removing structure |
nikitateggi | 22:ffa88619551d | 382 | chksum_last=sum & 0x000000FF; |
nikitateggi | 22:ffa88619551d | 383 | return chksum_last; |
nikitateggi | 22:ffa88619551d | 384 | } |
nikitateggi | 22:ffa88619551d | 385 | |
nikitateggi | 45:067673dae242 | 386 | void send_last_string() // function to send the last string to the bluetooth // which determines the end of file |
nikitateggi | 22:ffa88619551d | 387 | { |
nikitateggi | 45:067673dae242 | 388 | uint8_t sum_last=0; |
nikitateggi | 45:067673dae242 | 389 | myBleMsg->start_of_string = SOS; |
nikitateggi | 45:067673dae242 | 390 | myBleMsg->length_file = 0; |
nikitateggi | 45:067673dae242 | 391 | myBleMsg->proto=END_OF_FILE; |
nikitateggi | 45:067673dae242 | 392 | sum_last=checksum_last(); |
nikitateggi | 45:067673dae242 | 393 | myBleMsg->chk_sum=sum_last; |
nikitateggi | 45:067673dae242 | 394 | blue.printf("%02x",myBleMsg->start_of_string); |
nikitateggi | 45:067673dae242 | 395 | blue.printf("%02x",myBleMsg->proto); |
nikitateggi | 45:067673dae242 | 396 | blue.printf("%08x",myBleMsg->length_file); // chnged |
nikitateggi | 45:067673dae242 | 397 | myBleMsg->end_of_string=EOS; |
nikitateggi | 45:067673dae242 | 398 | blue.printf("%02x",myBleMsg->end_of_string); |
nikitateggi | 45:067673dae242 | 399 | blue.printf("%02x",myBleMsg->chk_sum); |
nikitateggi | 45:067673dae242 | 400 | |
nikitateggi | 45:067673dae242 | 401 | } |
nikitateggi | 22:ffa88619551d | 402 | |
nikitateggi | 45:067673dae242 | 403 | void send_data_str1() // function to send a part of the structure // after this raw data is sent// |
nikitateggi | 45:067673dae242 | 404 | { |
nikitateggi | 45:067673dae242 | 405 | myBleMsg->start_of_string=SOS; |
nikitateggi | 45:067673dae242 | 406 | myBleMsg->length_file=(counter*4); |
nikitateggi | 45:067673dae242 | 407 | myBleMsg->proto=DATA_TRFR; |
nikitateggi | 45:067673dae242 | 408 | blue.printf("%02x",myBleMsg->start_of_string); |
nikitateggi | 45:067673dae242 | 409 | blue.printf("%02x",myBleMsg->proto); // chnaged |
nikitateggi | 45:067673dae242 | 410 | blue.printf("%08x",myBleMsg->length_file); |
nikitateggi | 52:7d9ff7781bdc | 411 | |
nikitateggi | 45:067673dae242 | 412 | } |
nikitateggi | 19:1650bbc923cc | 413 | |
nikitateggi | 19:1650bbc923cc | 414 | |
nikitateggi | 19:1650bbc923cc | 415 | |
nikitateggi | 45:067673dae242 | 416 | void send_data_str2(uint8_t check_sum) // after raw data// this last part of structure is sent |
nikitateggi | 45:067673dae242 | 417 | { |
nikitateggi | 45:067673dae242 | 418 | myBleMsg->chk_sum=check_sum; |
nikitateggi | 45:067673dae242 | 419 | myBleMsg->end_of_string=EOS; |
nikitateggi | 45:067673dae242 | 420 | blue.printf("%02x",myBleMsg->end_of_string); |
nikitateggi | 45:067673dae242 | 421 | blue.printf("%02x",myBleMsg->chk_sum); |
nikitateggi | 45:067673dae242 | 422 | |
nikitateggi | 45:067673dae242 | 423 | } |
nikitateggi | 19:1650bbc923cc | 424 | |
nikitateggi | 52:7d9ff7781bdc | 425 | |
nikitateggi | 31:0f67acfc6236 | 426 | uint8_t checksum_struct(const uint8_t data[], uint16_t numBytes) // calculating checksum for |
nikitateggi | 31:0f67acfc6236 | 427 | { |
nikitateggi | 31:0f67acfc6236 | 428 | uint32_t chksum_struct = 0; |
nikitateggi | 31:0f67acfc6236 | 429 | uint16_t index = 0; |
nikitateggi | 31:0f67acfc6236 | 430 | |
nikitateggi | 31:0f67acfc6236 | 431 | for (index=0U; index < (numBytes-1); index++) |
nikitateggi | 31:0f67acfc6236 | 432 | { |
nikitateggi | 31:0f67acfc6236 | 433 | chksum_struct += data[index]; |
nikitateggi | 31:0f67acfc6236 | 434 | //blue.printf("data=%02x\n",data[index]); |
nikitateggi | 31:0f67acfc6236 | 435 | |
nikitateggi | 31:0f67acfc6236 | 436 | } |
nikitateggi | 31:0f67acfc6236 | 437 | // blue.printf("%08x\n",chksum_struct); |
nikitateggi | 31:0f67acfc6236 | 438 | return chksum_struct; |
nikitateggi | 22:ffa88619551d | 439 | } |
nikitateggi | 52:7d9ff7781bdc | 440 | |
nikitateggi | 52:7d9ff7781bdc | 441 | |
nikitateggi | 44:05dbea558c9a | 442 | uint8_t checksum_rawdata(const uint8_t data[], uint32_t numBytes) // this function to calculate the checksum for the raw data |
nikitateggi | 31:0f67acfc6236 | 443 | { |
nikitateggi | 52:7d9ff7781bdc | 444 | |
nikitateggi | 45:067673dae242 | 445 | uint32_t sum=0; |
nikitateggi | 45:067673dae242 | 446 | uint8_t chksum_datatrfr=0; |
nikitateggi | 45:067673dae242 | 447 | uint32_t index = 0; |
nikitateggi | 45:067673dae242 | 448 | uint32_t index1 = 0; |
nikitateggi | 44:05dbea558c9a | 449 | sum += SOS; |
nikitateggi | 44:05dbea558c9a | 450 | sum += DATA_TRFR; |
nikitateggi | 45:067673dae242 | 451 | index1=(numBytes*4); |
nikitateggi | 45:067673dae242 | 452 | sum +=(index1>>(8*0))&0xFF; |
nikitateggi | 31:0f67acfc6236 | 453 | sum +=(index1>>(8*1))&0xFF; |
nikitateggi | 52:7d9ff7781bdc | 454 | |
nikitateggi | 45:067673dae242 | 455 | for (index=0U; index<(numBytes*4); index++) |
nikitateggi | 45:067673dae242 | 456 | { |
nikitateggi | 31:0f67acfc6236 | 457 | sum += data[index]; |
nikitateggi | 45:067673dae242 | 458 | } |
nikitateggi | 52:7d9ff7781bdc | 459 | |
nikitateggi | 45:067673dae242 | 460 | sum+=EOS; |
nikitateggi | 52:7d9ff7781bdc | 461 | |
nikitateggi | 45:067673dae242 | 462 | chksum_datatrfr = sum & 0x000000FF; |
nikitateggi | 44:05dbea558c9a | 463 | //mc.printf("chksum_datatrfr=%02x\n",chksum_datatrfr); |
nikitateggi | 45:067673dae242 | 464 | return chksum_datatrfr; |
nikitateggi | 44:05dbea558c9a | 465 | } |
nikitateggi | 52:7d9ff7781bdc | 466 | |
nikitateggi | 52:7d9ff7781bdc | 467 |