Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Committer:
MAA
Date:
Thu Mar 09 15:26:22 2017 +0000
Revision:
6:6d1683c8b26b
Parent:
5:11782a2008c2
Child:
7:872984a67d5b
sps filename generation functionality in progress

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MAA 0:b3313c5ffca3 1 #include "SPS.h"
MAA 0:b3313c5ffca3 2
MAA 0:b3313c5ffca3 3 SPS::SPS(){
MAA 2:39c4a85dc2a4 4
MAA 6:6d1683c8b26b 5 fileNameString = "";
MAA 2:39c4a85dc2a4 6 currentString = "";
MAA 2:39c4a85dc2a4 7 currentErrString = "";
MAA 6:6d1683c8b26b 8 lastString = "";
MAA 4:c70ef089a3fd 9 headerString = "/tag identifier_id group_id date time zzz | s1 source_id interpreter_id data_line_version encoding source_firmware_version interpreter_firmware_version | latitude longitude gpsFixFlag mag_time mag_nt mag_sq checksum";
MAA 5:11782a2008c2 10 crc_tab16_init = 0;
MAA 5:11782a2008c2 11 errStatus = true;
MAA 2:39c4a85dc2a4 12
MAA 2:39c4a85dc2a4 13 };
MAA 2:39c4a85dc2a4 14
MAA 5:11782a2008c2 15 void SPS::UpdateCurrentString(string tag, string identifier_id, string group_id, string date, string time, string ZZZ,string s1, string source_id, string interpreter_id, string data_line_version, string source_firmware_version, string interpreter_firmware_version, string latitude, string longitude, bool gpsFixFlag, string mag_time, string mag_nt, string mag_sq, Serial * dbg){
MAA 2:39c4a85dc2a4 16
MAA 2:39c4a85dc2a4 17 currentString = "";
MAA 5:11782a2008c2 18 currentString.resize(256);
MAA 5:11782a2008c2 19 char checkSum[5];
MAA 5:11782a2008c2 20 memset(checkSum,'\0',5);
MAA 5:11782a2008c2 21 unsigned short chkSum = 0;
MAA 0:b3313c5ffca3 22
MAA 6:6d1683c8b26b 23 //dbg->printf("Commencing sps line generation\r\n");
MAA 2:39c4a85dc2a4 24 //create current string
MAA 5:11782a2008c2 25 addToCurrentString(LINESTART);
MAA 5:11782a2008c2 26 addToCurrentString(tag);
MAA 5:11782a2008c2 27 addToCurrentString(SPACE);
MAA 5:11782a2008c2 28 addToCurrentString(identifier_id);
MAA 5:11782a2008c2 29 addToCurrentString(SPACE);
MAA 5:11782a2008c2 30 addToCurrentString(group_id);
MAA 5:11782a2008c2 31 addToCurrentString(SPACE);
MAA 5:11782a2008c2 32 addToCurrentString(date);
MAA 5:11782a2008c2 33 addToCurrentString(SPACE);
MAA 5:11782a2008c2 34 addToCurrentString(time);
MAA 5:11782a2008c2 35 addToCurrentString(SPACE);
MAA 5:11782a2008c2 36 addToCurrentString("ZZZ");
MAA 5:11782a2008c2 37 addToCurrentString(SPACE);
MAA 5:11782a2008c2 38 addToCurrentString(HEADEREND);
MAA 5:11782a2008c2 39 addToCurrentString(SPACE);
MAA 5:11782a2008c2 40 addToCurrentString(s1);
MAA 5:11782a2008c2 41 addToCurrentString(SPACE);
MAA 5:11782a2008c2 42 addToCurrentString(source_id);
MAA 5:11782a2008c2 43 addToCurrentString(SPACE);
MAA 5:11782a2008c2 44 addToCurrentString(interpreter_id);
MAA 5:11782a2008c2 45 addToCurrentString(SPACE);
MAA 5:11782a2008c2 46 addToCurrentString(data_line_version);
MAA 5:11782a2008c2 47 addToCurrentString(SPACE);
MAA 5:11782a2008c2 48 addToCurrentString(source_firmware_version);
MAA 5:11782a2008c2 49 addToCurrentString(SPACE);
MAA 5:11782a2008c2 50 addToCurrentString(interpreter_firmware_version);
MAA 5:11782a2008c2 51 addToCurrentString(SPACE);
MAA 5:11782a2008c2 52 addToCurrentString(HEADEREND);
MAA 5:11782a2008c2 53 addToCurrentString(SPACE);
MAA 5:11782a2008c2 54 addToCurrentString(latitude);
MAA 5:11782a2008c2 55 addToCurrentString(SPACE);
MAA 5:11782a2008c2 56 addToCurrentString(longitude);
MAA 5:11782a2008c2 57 addToCurrentString(SPACE);
MAA 5:11782a2008c2 58
MAA 5:11782a2008c2 59 if(gpsFixFlag){
MAA 5:11782a2008c2 60 addToCurrentString('1');
MAA 5:11782a2008c2 61 }
MAA 5:11782a2008c2 62 if(!gpsFixFlag){
MAA 5:11782a2008c2 63 addToCurrentString('0');
MAA 5:11782a2008c2 64 }
MAA 5:11782a2008c2 65
MAA 5:11782a2008c2 66 addToCurrentString(SPACE);
MAA 5:11782a2008c2 67 addToCurrentString(mag_time);
MAA 5:11782a2008c2 68 addToCurrentString(SPACE);
MAA 5:11782a2008c2 69 addToCurrentString(mag_nt);
MAA 5:11782a2008c2 70 addToCurrentString(SPACE);
MAA 5:11782a2008c2 71 addToCurrentString(mag_sq);
MAA 5:11782a2008c2 72
MAA 6:6d1683c8b26b 73 //dbg->printf("CurrentStringBeforeChecksumCalc: ");
MAA 6:6d1683c8b26b 74 //dbg->printf(currentString.c_str());
MAA 6:6d1683c8b26b 75 //dbg->printf("\r\n");
MAA 2:39c4a85dc2a4 76
MAA 2:39c4a85dc2a4 77 //calculate checksum
MAA 5:11782a2008c2 78 for(int i = 0; i < strlen(currentString.c_str()); i++){
MAA 5:11782a2008c2 79 if(currentString[i] != '>' || currentString[i] != '<'){
MAA 5:11782a2008c2 80
MAA 5:11782a2008c2 81 chkSum = update_crc_16(chkSum, currentString[i]);
MAA 5:11782a2008c2 82
MAA 5:11782a2008c2 83 }
MAA 5:11782a2008c2 84 }
MAA 5:11782a2008c2 85
MAA 6:6d1683c8b26b 86 sprintf(checkSum, "%04X", chkSum);
MAA 2:39c4a85dc2a4 87
MAA 2:39c4a85dc2a4 88 //append rest of string
MAA 5:11782a2008c2 89 addToCurrentString(SPACE);
MAA 5:11782a2008c2 90 addToCurrentString(checkSum);
MAA 5:11782a2008c2 91 addToCurrentString(LINESTOP);
MAA 2:39c4a85dc2a4 92
MAA 2:39c4a85dc2a4 93 };
MAA 2:39c4a85dc2a4 94
MAA 4:c70ef089a3fd 95 //Fills the array for calculation of the CRC-16 with values
MAA 4:c70ef089a3fd 96 void SPS::init_crc16_tab()
MAA 4:c70ef089a3fd 97 {
MAA 4:c70ef089a3fd 98 int i, j;
MAA 4:c70ef089a3fd 99 unsigned short crc, c;
MAA 4:c70ef089a3fd 100
MAA 4:c70ef089a3fd 101 for (i = 0; i < 256; i++)
MAA 4:c70ef089a3fd 102 {
MAA 4:c70ef089a3fd 103 crc = 0;
MAA 4:c70ef089a3fd 104 c = (unsigned short) i;
MAA 4:c70ef089a3fd 105
MAA 4:c70ef089a3fd 106 for (j = 0; j < 8; j++)
MAA 4:c70ef089a3fd 107 {
MAA 4:c70ef089a3fd 108 if ((crc ^ c) & 0x0001)
MAA 4:c70ef089a3fd 109 crc = (crc >> 1) ^ P_16;
MAA 4:c70ef089a3fd 110 else
MAA 4:c70ef089a3fd 111 crc = crc >> 1;
MAA 4:c70ef089a3fd 112
MAA 4:c70ef089a3fd 113 c = c >> 1;
MAA 4:c70ef089a3fd 114 }
MAA 4:c70ef089a3fd 115 crc_tab16[i] = crc;
MAA 4:c70ef089a3fd 116 }
MAA 4:c70ef089a3fd 117 crc_tab16_init = 1;
MAA 4:c70ef089a3fd 118 };
MAA 4:c70ef089a3fd 119
MAA 4:c70ef089a3fd 120
MAA 4:c70ef089a3fd 121 // The function update_crc_16 calculates a new CRC-16 value based on the
MAA 4:c70ef089a3fd 122 // previous value of the CRC and the next byte of the data to be checked.
MAA 4:c70ef089a3fd 123 unsigned short SPS::update_crc_16(unsigned short crc, char c)
MAA 4:c70ef089a3fd 124 {
MAA 4:c70ef089a3fd 125 unsigned short tmp, short_c;
MAA 4:c70ef089a3fd 126
MAA 4:c70ef089a3fd 127 short_c = 0x00ff & (unsigned short) c;
MAA 4:c70ef089a3fd 128
MAA 4:c70ef089a3fd 129 if (!crc_tab16_init)
MAA 4:c70ef089a3fd 130 init_crc16_tab();
MAA 4:c70ef089a3fd 131
MAA 4:c70ef089a3fd 132 tmp = crc ^ short_c;
MAA 4:c70ef089a3fd 133 crc = (crc >> 8) ^ crc_tab16[tmp & 0xff];
MAA 4:c70ef089a3fd 134
MAA 4:c70ef089a3fd 135 return crc;
MAA 4:c70ef089a3fd 136 };
MAA 5:11782a2008c2 137
MAA 5:11782a2008c2 138
MAA 5:11782a2008c2 139 //Gets / returns current sps data string
MAA 5:11782a2008c2 140 string SPS::getCurrentString(void){
MAA 5:11782a2008c2 141 return this->currentString;
MAA 5:11782a2008c2 142 };
MAA 5:11782a2008c2 143
MAA 5:11782a2008c2 144
MAA 5:11782a2008c2 145 //Get error status
MAA 5:11782a2008c2 146 bool SPS::getErrStatus(void){
MAA 5:11782a2008c2 147 return this->errStatus;
MAA 5:11782a2008c2 148 };
MAA 5:11782a2008c2 149
MAA 5:11782a2008c2 150 //Set err status
MAA 5:11782a2008c2 151 void SPS::setErrStatus(bool status){
MAA 5:11782a2008c2 152 this->errStatus = status;
MAA 5:11782a2008c2 153 };
MAA 5:11782a2008c2 154
MAA 5:11782a2008c2 155 //get headerstring
MAA 5:11782a2008c2 156 string SPS::getHeaderString(void){
MAA 5:11782a2008c2 157 return this->headerString;
MAA 5:11782a2008c2 158 };
MAA 5:11782a2008c2 159
MAA 5:11782a2008c2 160 //add data to current string
MAA 5:11782a2008c2 161 void SPS::addToCurrentString(string data){
MAA 5:11782a2008c2 162 int currentStartIndex = strlen(currentString.c_str());
MAA 5:11782a2008c2 163 char dataStrLen = 0;
MAA 5:11782a2008c2 164 dataStrLen = strlen(data.c_str());
MAA 5:11782a2008c2 165
MAA 5:11782a2008c2 166 for(int i = 0; i < dataStrLen; i++){
MAA 5:11782a2008c2 167 currentString[currentStartIndex+i] = data[i];
MAA 5:11782a2008c2 168 }
MAA 5:11782a2008c2 169 }
MAA 5:11782a2008c2 170
MAA 5:11782a2008c2 171 void SPS::addToCurrentString(char data){
MAA 5:11782a2008c2 172 int currentStartIndex = strlen(currentString.c_str());
MAA 5:11782a2008c2 173 currentString[currentStartIndex] = data;
MAA 6:6d1683c8b26b 174 };
MAA 6:6d1683c8b26b 175
MAA 6:6d1683c8b26b 176
MAA 6:6d1683c8b26b 177 void SPS::generateSpsFilename(string formattedDate){
MAA 6:6d1683c8b26b 178
MAA 6:6d1683c8b26b 179 fileNameString = "";
MAA 6:6d1683c8b26b 180
MAA 6:6d1683c8b26b 181 fileNameString.append("/usb/BMAG_");
MAA 6:6d1683c8b26b 182
MAA 6:6d1683c8b26b 183 fileNameString.append(formattedDate);
MAA 6:6d1683c8b26b 184
MAA 6:6d1683c8b26b 185 fileNameString.append(".sps");
MAA 6:6d1683c8b26b 186
MAA 6:6d1683c8b26b 187 };
MAA 6:6d1683c8b26b 188
MAA 6:6d1683c8b26b 189
MAA 6:6d1683c8b26b 190 string SPS::getSpsFileName(void){
MAA 6:6d1683c8b26b 191
MAA 6:6d1683c8b26b 192 return this->fileNameString;
MAA 6:6d1683c8b26b 193
MAA 5:11782a2008c2 194 };