sqefqsdf

Dependencies:   C12832 EthernetInterface LM75B mbed-rtos mbed

Fork of app-board-LM75B by Chris Styles

Committer:
gimohd
Date:
Tue May 09 12:26:57 2017 +0000
Revision:
7:0618a1e407d0
Parent:
6:77a4c45f6416
dfdsqf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gimohd 6:77a4c45f6416 1 #include "Packet.h"
gimohd 6:77a4c45f6416 2
gimohd 6:77a4c45f6416 3 Packet::Packet()
gimohd 6:77a4c45f6416 4 {
gimohd 6:77a4c45f6416 5 this->NUM = 0;
gimohd 6:77a4c45f6416 6 this->IDD = 0;
gimohd 6:77a4c45f6416 7 this->PWM = 0;
gimohd 6:77a4c45f6416 8 }
gimohd 6:77a4c45f6416 9
gimohd 6:77a4c45f6416 10 void Packet::addValues(uint8_t num,uint8_t destID,uint8_t pwm, uint8_t ownID,int16_t temp)
gimohd 6:77a4c45f6416 11 {
gimohd 6:77a4c45f6416 12 this->NUM = num +1;
gimohd 6:77a4c45f6416 13 this->IDD = destID;
gimohd 6:77a4c45f6416 14 this->PWM = pwm;
gimohd 6:77a4c45f6416 15 this->IDN.push_back(ownID);
gimohd 6:77a4c45f6416 16 this->TMP.push_back(temp);
gimohd 6:77a4c45f6416 17 //this->CRC = crc;
gimohd 6:77a4c45f6416 18 printf("ADDED %5s, %5s, %5s, %5s, %5s\r\n","Numb","DesID","PWM","OwnID","Temp");
gimohd 6:77a4c45f6416 19 printf("ADDED %5d, %5d, %5d, %5d, %5d\r\n",num,destID,pwm,ownID,temp);
gimohd 6:77a4c45f6416 20 }
gimohd 6:77a4c45f6416 21
gimohd 6:77a4c45f6416 22 void Packet::dataInput(std::string stri)
gimohd 6:77a4c45f6416 23 {
gimohd 6:77a4c45f6416 24 std::string str = stri;
gimohd 6:77a4c45f6416 25 NUM = str[2];
gimohd 6:77a4c45f6416 26 printf("NO of mbeds visited= %d\r\n", NUM);
gimohd 6:77a4c45f6416 27 IDD = str[3];
gimohd 6:77a4c45f6416 28 printf("Destination ID= %d\r\n", IDD);
gimohd 6:77a4c45f6416 29 PWM = str[4];
gimohd 6:77a4c45f6416 30 printf("PWM for the LED= %d\r\n", PWM);
gimohd 6:77a4c45f6416 31
gimohd 6:77a4c45f6416 32 int frameNumber = 5;
gimohd 6:77a4c45f6416 33 IDN.clear();
gimohd 6:77a4c45f6416 34 printf("IDN=");
gimohd 6:77a4c45f6416 35 for( int i = 0; i < NUM; i++ ) {
gimohd 6:77a4c45f6416 36 IDN.push_back(str.at(frameNumber));
gimohd 6:77a4c45f6416 37 printf(" %d=%d ",i ,str.at(frameNumber));
gimohd 6:77a4c45f6416 38 frameNumber++;
gimohd 6:77a4c45f6416 39 }
gimohd 6:77a4c45f6416 40 printf("\r\n");
gimohd 6:77a4c45f6416 41 TMP.clear();
gimohd 6:77a4c45f6416 42 printf("TMP=");
gimohd 6:77a4c45f6416 43 for( int i = 0; i < NUM; i++ ) {
gimohd 6:77a4c45f6416 44 //for( int j = 0; j < 2; j++ ) {
gimohd 6:77a4c45f6416 45 printf("%c and %c) =",str.at(frameNumber+1),str.at(frameNumber));
gimohd 6:77a4c45f6416 46 int16_t data = (str.at(frameNumber) << 8) | str.at(frameNumber+1);
gimohd 6:77a4c45f6416 47 frameNumber = frameNumber +2;
gimohd 6:77a4c45f6416 48
gimohd 6:77a4c45f6416 49 printf("%d= %d ",i ,data);
gimohd 6:77a4c45f6416 50 TMP.push_back(data);
gimohd 6:77a4c45f6416 51 //}
gimohd 6:77a4c45f6416 52 }
gimohd 6:77a4c45f6416 53 printf("\r\n");
gimohd 6:77a4c45f6416 54 CRC = str[frameNumber];
gimohd 6:77a4c45f6416 55 }
gimohd 6:77a4c45f6416 56
gimohd 6:77a4c45f6416 57 std::string Packet::toDataString()
gimohd 6:77a4c45f6416 58 {
gimohd 6:77a4c45f6416 59 std::string test;
gimohd 6:77a4c45f6416 60 test.push_back(FRAME_BYTE_1);
gimohd 6:77a4c45f6416 61 test.push_back(FRAME_BYTE_2);
gimohd 6:77a4c45f6416 62 string crcString = Packet::crcString();
gimohd 6:77a4c45f6416 63 test.append(crcString);
gimohd 6:77a4c45f6416 64 test.push_back(Packet::crc(crcString,0));
gimohd 6:77a4c45f6416 65 test.push_back(FRAME_BYTE_2);
gimohd 6:77a4c45f6416 66 test.push_back(FRAME_BYTE_1);
gimohd 6:77a4c45f6416 67 printf("chars in stringmaker: ");
gimohd 6:77a4c45f6416 68 for (int a = 0; a<test.size(); a++) {
gimohd 6:77a4c45f6416 69 printf("%d\r\n",test.at(a));
gimohd 6:77a4c45f6416 70 }
gimohd 6:77a4c45f6416 71 return test;
gimohd 6:77a4c45f6416 72 }
gimohd 6:77a4c45f6416 73
gimohd 6:77a4c45f6416 74
gimohd 6:77a4c45f6416 75
gimohd 6:77a4c45f6416 76 std::string Packet::crcString()
gimohd 6:77a4c45f6416 77 {
gimohd 6:77a4c45f6416 78 std::string test;
gimohd 6:77a4c45f6416 79 test.push_back(NUM);
gimohd 6:77a4c45f6416 80 test.push_back(IDD);
gimohd 6:77a4c45f6416 81 test.push_back(PWM);
gimohd 6:77a4c45f6416 82 //printf("Sending - mbeds visited= %d\r\n", NUM);
gimohd 6:77a4c45f6416 83 //printf("Sending - destination ID= %d\r\n", IDD);
gimohd 6:77a4c45f6416 84 //printf("Sending - PWM= %d\r\n", PWM);
gimohd 6:77a4c45f6416 85 for( int i = 0; i < IDN.size(); i++ ) {
gimohd 6:77a4c45f6416 86 test.push_back(IDN.at(i));
gimohd 6:77a4c45f6416 87 //printf("Sending - IDN %d= %d\r\n",i ,test[5+i]);
gimohd 6:77a4c45f6416 88 }
gimohd 6:77a4c45f6416 89
gimohd 6:77a4c45f6416 90 for( int j = 0; j< TMP.size(); j++ ) {
gimohd 6:77a4c45f6416 91 int16_t data = TMP.at(j);
gimohd 6:77a4c45f6416 92 test.push_back((data >> 8) & 0xff);
gimohd 6:77a4c45f6416 93 test.push_back(data & 0xff);
gimohd 6:77a4c45f6416 94 }
gimohd 6:77a4c45f6416 95 return test;
gimohd 6:77a4c45f6416 96 }
gimohd 6:77a4c45f6416 97
gimohd 6:77a4c45f6416 98
gimohd 6:77a4c45f6416 99 bool Packet::crcCheck()
gimohd 6:77a4c45f6416 100 {
gimohd 6:77a4c45f6416 101 return 0 == Packet::crc(Packet::crcString(),CRC);
gimohd 6:77a4c45f6416 102 }
gimohd 6:77a4c45f6416 103
gimohd 6:77a4c45f6416 104 bool Packet::idCheck(int ID)
gimohd 6:77a4c45f6416 105 {
gimohd 6:77a4c45f6416 106 for( int i = 0; i < IDN.size(); i++ ) {
gimohd 6:77a4c45f6416 107 if(ID == IDN.at(i)){
gimohd 6:77a4c45f6416 108 return false;
gimohd 6:77a4c45f6416 109 }
gimohd 6:77a4c45f6416 110 }
gimohd 6:77a4c45f6416 111 return true;
gimohd 6:77a4c45f6416 112 }
gimohd 6:77a4c45f6416 113
gimohd 6:77a4c45f6416 114
gimohd 6:77a4c45f6416 115 /**
gimohd 6:77a4c45f6416 116 * EXAMPLE CRC CHECK : 0xAA00FF55 => checksum = 0x39
gimohd 6:77a4c45f6416 117 * 0xAA 0x00 0xFF 0x55
gimohd 6:77a4c45f6416 118 * 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 0 1 0 1 0 1 | 0 0 0 0 0 0
gimohd 6:77a4c45f6416 119 * 1 0 1 0 0 1 1 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 120 * 0 0 0 0 1 1 0 0 0 0 0 | | | | | | | | | | | | | | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 121 * 1 0 1 0 0 1 1 | | | | | | | | | | | | | | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 122 * 0 1 1 0 0 1 1 0 | | | | | | | | | | | | | | | | | | | | | | | | | | | //102
gimohd 6:77a4c45f6416 123 * 1 0 1 0 0 1 1 | | | | | | | | | | | | | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 124 * 0 1 1 0 1 0 1 0 | | | | | | | | | | | | | | | | | | | | | | | | | | //106
gimohd 6:77a4c45f6416 125 * 1 0 1 0 0 1 1 | | | | | | | | | | | | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 126 * 0 1 1 1 0 0 1 0 | | | | | | | | | | | | | | | | | | | | | | | | | //114
gimohd 6:77a4c45f6416 127 * 1 0 1 0 0 1 1 | | | | | | | | | | | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 128 * 0 1 0 0 0 0 1 0 | | | | | | | | | | | | | | | | | | | | | | | | //66
gimohd 6:77a4c45f6416 129 * 1 0 1 0 0 1 1 | | | | | | | | | | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 130 * 0 0 1 0 0 0 1 0 1 | | | | | | | | | | | | | | | | | | | | | | //69
gimohd 6:77a4c45f6416 131 * 1 0 1 0 0 1 1 | | | | | | | | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 132 * 0 0 1 0 1 1 0 1 1 | | | | | | | | | | | | | | | | | | | | //91
gimohd 6:77a4c45f6416 133 * 1 0 1 0 0 1 1 | | | | | | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 134 * 0 0 0 1 0 0 0 1 1 1 | | | | | | | | | | | | | | | | | //71
gimohd 6:77a4c45f6416 135 * 1 0 1 0 0 1 1 | | | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 136 * 0 0 1 0 1 0 0 1 1 | | | | | | | | | | | | | | | // 83
gimohd 6:77a4c45f6416 137 * 1 0 1 0 0 1 1 | | | | | | | | | | | | | | |
gimohd 6:77a4c45f6416 138 * 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 | | | | | | | //85
gimohd 6:77a4c45f6416 139 * 1 0 1 0 0 1 1 | | | | | | |
gimohd 6:77a4c45f6416 140 * 0 0 0 0 1 1 0 | 0 0 0 0 | | //96
gimohd 6:77a4c45f6416 141 * 1 0 1 | 0 0 1 1 | |
gimohd 6:77a4c45f6416 142 * 0 1 1 | 0 0 1 1 0 | //102
gimohd 6:77a4c45f6416 143 * 1 0 | 1 0 0 1 1 |
gimohd 6:77a4c45f6416 144 * 0 1 | 1 0 1 0 1 0 //106
gimohd 6:77a4c45f6416 145 * 1 | 0 1 0 0 1 1
gimohd 6:77a4c45f6416 146 * 0 | 1 1 1 0 0 1 //57 = 0x39
gimohd 6:77a4c45f6416 147 * using this algorithm gives the same values.
gimohd 6:77a4c45f6416 148 */
gimohd 6:77a4c45f6416 149 unsigned char Packet::crc(std::string message, char chk)
gimohd 6:77a4c45f6416 150 {
gimohd 6:77a4c45f6416 151 int nBytes = message.size();
gimohd 6:77a4c45f6416 152 char remainder = 0;
gimohd 6:77a4c45f6416 153 /**
gimohd 6:77a4c45f6416 154 * Shifts the checksum 2 bits to the right,
gimohd 6:77a4c45f6416 155 * the checksum is of the form 0b00xx xxxx,
gimohd 6:77a4c45f6416 156 * so this has to get shifted to 0bxxxx xx00 to do the check
gimohd 6:77a4c45f6416 157 */
gimohd 6:77a4c45f6416 158 char check = chk<<2;
gimohd 6:77a4c45f6416 159
gimohd 6:77a4c45f6416 160 /*
gimohd 6:77a4c45f6416 161 * Perform modulo-2 division, bit by bit, byte by byte
gimohd 6:77a4c45f6416 162 */
gimohd 6:77a4c45f6416 163
gimohd 6:77a4c45f6416 164 for (int byte = 0; byte < nBytes; byte++)
gimohd 6:77a4c45f6416 165 {
gimohd 6:77a4c45f6416 166 for (char bit = 8; bit > 0; --bit)
gimohd 6:77a4c45f6416 167 {
gimohd 6:77a4c45f6416 168 /*
gimohd 6:77a4c45f6416 169 * Try to divide the current data bit.
gimohd 6:77a4c45f6416 170 */
gimohd 6:77a4c45f6416 171 if (remainder & 0x40)
gimohd 6:77a4c45f6416 172 {
gimohd 6:77a4c45f6416 173 remainder = (remainder ^ 0x53) & 0x7F;
gimohd 6:77a4c45f6416 174 remainder = (remainder << 1) | (0x01 & (message.at(byte)>>(bit-1)) & 0x7F);
gimohd 6:77a4c45f6416 175 printf("Other remainder %d %d\r\n", remainder, byte);
gimohd 6:77a4c45f6416 176 }
gimohd 6:77a4c45f6416 177 else
gimohd 6:77a4c45f6416 178 {
gimohd 6:77a4c45f6416 179 remainder = ((remainder << 1) | (0x01 & (message.at(byte)>>(bit-1))) & 0x7F);
gimohd 6:77a4c45f6416 180 printf("Shifted remainder dqfs %d\r\n", message[byte]);
gimohd 6:77a4c45f6416 181 printf("Shifted remainder %d %d\r\n", remainder, byte);
gimohd 6:77a4c45f6416 182 }
gimohd 6:77a4c45f6416 183 }
gimohd 6:77a4c45f6416 184 }
gimohd 6:77a4c45f6416 185
gimohd 6:77a4c45f6416 186 /*
gimohd 6:77a4c45f6416 187 * Perform modulo-2 division, this for only the 6 bits of the chk value
gimohd 6:77a4c45f6416 188 * will be performed 7 times but the trailing 0 will be removed later
gimohd 6:77a4c45f6416 189 */
gimohd 6:77a4c45f6416 190 for (char bit = 7; bit > 0; --bit)
gimohd 6:77a4c45f6416 191 {
gimohd 6:77a4c45f6416 192 /*
gimohd 6:77a4c45f6416 193 * Try to divide the current data bit.
gimohd 6:77a4c45f6416 194 */
gimohd 6:77a4c45f6416 195 if (remainder & 0x40)
gimohd 6:77a4c45f6416 196 {
gimohd 6:77a4c45f6416 197 remainder = (remainder ^ 0x53) & 0x7F;
gimohd 6:77a4c45f6416 198 remainder = (remainder << 1) | (0x01 & (check >> bit)) & 0x7F;
gimohd 6:77a4c45f6416 199 printf("%d %d\r\n",bit,(0x01 & (check >> bit)));
gimohd 6:77a4c45f6416 200 printf("Other remainder %d\r\n", remainder);
gimohd 6:77a4c45f6416 201 }
gimohd 6:77a4c45f6416 202 else
gimohd 6:77a4c45f6416 203 {
gimohd 6:77a4c45f6416 204 remainder = ((remainder << 1) | (0x01 & (check >> bit))) & 0x7F;
gimohd 6:77a4c45f6416 205 printf("%d %d\r\n",bit,(0x01 & (check >> bit)));
gimohd 6:77a4c45f6416 206 printf("Shifted remainder %d\r\n", remainder);
gimohd 6:77a4c45f6416 207 }
gimohd 6:77a4c45f6416 208 }
gimohd 6:77a4c45f6416 209 /*
gimohd 6:77a4c45f6416 210 * Remove the trailing 0 that has been shifted and has to be removed
gimohd 6:77a4c45f6416 211 */
gimohd 6:77a4c45f6416 212 remainder = remainder >>1;
gimohd 6:77a4c45f6416 213 return (remainder);
gimohd 6:77a4c45f6416 214
gimohd 6:77a4c45f6416 215 }
gimohd 6:77a4c45f6416 216
gimohd 6:77a4c45f6416 217
gimohd 6:77a4c45f6416 218 void Packet::setNUM(uint8_t NUM)
gimohd 6:77a4c45f6416 219 {
gimohd 6:77a4c45f6416 220 this->NUM = NUM;
gimohd 6:77a4c45f6416 221 }
gimohd 6:77a4c45f6416 222
gimohd 6:77a4c45f6416 223 uint8_t Packet::getNUM()
gimohd 6:77a4c45f6416 224 {
gimohd 6:77a4c45f6416 225 return this->NUM;
gimohd 6:77a4c45f6416 226 }
gimohd 6:77a4c45f6416 227
gimohd 6:77a4c45f6416 228 void Packet::setIDD(uint8_t IDD)
gimohd 6:77a4c45f6416 229 {
gimohd 6:77a4c45f6416 230 this->IDD = IDD;
gimohd 6:77a4c45f6416 231 }
gimohd 6:77a4c45f6416 232
gimohd 6:77a4c45f6416 233 uint8_t Packet::getIDD()
gimohd 6:77a4c45f6416 234 {
gimohd 6:77a4c45f6416 235 return this->IDD;
gimohd 6:77a4c45f6416 236 }
gimohd 6:77a4c45f6416 237
gimohd 6:77a4c45f6416 238 void Packet::setCRC(uint8_t CRC)
gimohd 6:77a4c45f6416 239 {
gimohd 6:77a4c45f6416 240 this->CRC = CRC;
gimohd 6:77a4c45f6416 241 }
gimohd 6:77a4c45f6416 242
gimohd 6:77a4c45f6416 243 uint8_t Packet::getCRC()
gimohd 6:77a4c45f6416 244 {
gimohd 6:77a4c45f6416 245 return this->CRC;
gimohd 6:77a4c45f6416 246 }
gimohd 6:77a4c45f6416 247 void Packet::setPWM(uint8_t PWM)
gimohd 6:77a4c45f6416 248 {
gimohd 6:77a4c45f6416 249 this->PWM = PWM;
gimohd 6:77a4c45f6416 250 }
gimohd 6:77a4c45f6416 251 uint8_t Packet::getPWM()
gimohd 6:77a4c45f6416 252 {
gimohd 6:77a4c45f6416 253 return this->PWM;
gimohd 6:77a4c45f6416 254 }
gimohd 6:77a4c45f6416 255 void Packet::setIDN(vector<uint8_t> IDN)
gimohd 6:77a4c45f6416 256 {
gimohd 6:77a4c45f6416 257 this->IDN = IDN;
gimohd 6:77a4c45f6416 258 }
gimohd 6:77a4c45f6416 259 vector<uint8_t> Packet::getIDN()
gimohd 6:77a4c45f6416 260 {
gimohd 6:77a4c45f6416 261 return this->IDN;
gimohd 6:77a4c45f6416 262 }
gimohd 6:77a4c45f6416 263 void Packet::setTMP(vector<int16_t> TMP)
gimohd 6:77a4c45f6416 264 {
gimohd 6:77a4c45f6416 265 this->TMP = TMP;
gimohd 6:77a4c45f6416 266 }
gimohd 6:77a4c45f6416 267 vector<int16_t> Packet::getTMP()
gimohd 6:77a4c45f6416 268 {
gimohd 6:77a4c45f6416 269 return this->TMP;
gimohd 6:77a4c45f6416 270 }
gimohd 6:77a4c45f6416 271
gimohd 6:77a4c45f6416 272 /*
gimohd 6:77a4c45f6416 273 unsigned char Packet::crcCalculate()
gimohd 6:77a4c45f6416 274 {
gimohd 6:77a4c45f6416 275 std::string test;
gimohd 6:77a4c45f6416 276 test.append(Packet::crcString());
gimohd 6:77a4c45f6416 277 return Packet::calculateCrc(test);
gimohd 6:77a4c45f6416 278 }
gimohd 6:77a4c45f6416 279
gimohd 6:77a4c45f6416 280 unsigned char Packet::calculateCrc(std::string str)
gimohd 6:77a4c45f6416 281 {
gimohd 6:77a4c45f6416 282 int i = 0;
gimohd 6:77a4c45f6416 283 char poly = 0x53; //1010011
gimohd 6:77a4c45f6416 284 int bits = str.size()*8;
gimohd 6:77a4c45f6416 285 char *msg= new char[str.length() + 1];
gimohd 6:77a4c45f6416 286 strcpy(msg, str.c_str());
gimohd 6:77a4c45f6416 287 //unsigned char crc = 0xfc;
gimohd 6:77a4c45f6416 288 unsigned char crc = 0x00;
gimohd 6:77a4c45f6416 289
gimohd 6:77a4c45f6416 290 while(bits--) {
gimohd 6:77a4c45f6416 291 if(!i--) {
gimohd 6:77a4c45f6416 292 i = 7;
gimohd 6:77a4c45f6416 293 crc ^= *msg++;
gimohd 6:77a4c45f6416 294 }
gimohd 6:77a4c45f6416 295 crc = crc << 1 ^ (crc & 0x80 ? poly : 0);
gimohd 6:77a4c45f6416 296 }
gimohd 6:77a4c45f6416 297 return(crc >> 2 & 0x3f);
gimohd 6:77a4c45f6416 298
gimohd 6:77a4c45f6416 299 }
gimohd 6:77a4c45f6416 300 */