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