APP3 / Mbed 2 deprecated APP4

Dependencies:   mbed mbed-rtos

Committer:
ShaolinPoutine
Date:
Tue Mar 07 04:29:14 2017 +0000
Revision:
3:7b1110501ef9
Parent:
2:1665cd4c922c
Child:
4:4277898e55d8
Assemblage de trames et d?sassemblage

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ShaolinPoutine 0:529210499c6d 1 #include "mbed.h"
ShaolinPoutine 2:1665cd4c922c 2 #include "rtos.h"
ShaolinPoutine 2:1665cd4c922c 3 #define CLOCK_MAX 96000000
ShaolinPoutine 3:7b1110501ef9 4 #define TRAME_MAX 1024
ShaolinPoutine 3:7b1110501ef9 5 #define DATA_LEN 80
ShaolinPoutine 2:1665cd4c922c 6
ShaolinPoutine 3:7b1110501ef9 7 bool PREAMBULEBOOL[] = {0,1,0,1,0,1,0,1};
ShaolinPoutine 3:7b1110501ef9 8 bool STARTENDBOOL[] = {0,1,1,1,1,1,1,0};
ShaolinPoutine 3:7b1110501ef9 9 char PREAMBULE = 0b01010101;
ShaolinPoutine 3:7b1110501ef9 10 char STARTEND = 0b01111110;
ShaolinPoutine 0:529210499c6d 11
ShaolinPoutine 0:529210499c6d 12 DigitalOut myled(LED1);
ShaolinPoutine 3:7b1110501ef9 13 DigitalOut led2(LED2);
ShaolinPoutine 3:7b1110501ef9 14 Serial pc(USBTX, USBRX);
ShaolinPoutine 0:529210499c6d 15
ShaolinPoutine 2:1665cd4c922c 16 //Mail<char (*) [256], 16> messageBox;
ShaolinPoutine 2:1665cd4c922c 17 Mail<char, 92 * 4> trameBox;
ShaolinPoutine 2:1665cd4c922c 18
ShaolinPoutine 3:7b1110501ef9 19 void printBin(uint16_t data)
ShaolinPoutine 3:7b1110501ef9 20 {
ShaolinPoutine 3:7b1110501ef9 21 uint16_t mask = 1 << 15;
ShaolinPoutine 3:7b1110501ef9 22
ShaolinPoutine 3:7b1110501ef9 23 while (mask != 0)
ShaolinPoutine 3:7b1110501ef9 24 {
ShaolinPoutine 3:7b1110501ef9 25 if ((data & mask) > 0)
ShaolinPoutine 3:7b1110501ef9 26 {
ShaolinPoutine 3:7b1110501ef9 27 pc.printf("1");
ShaolinPoutine 3:7b1110501ef9 28 }
ShaolinPoutine 3:7b1110501ef9 29 else
ShaolinPoutine 3:7b1110501ef9 30 {
ShaolinPoutine 3:7b1110501ef9 31 pc.printf("0");
ShaolinPoutine 3:7b1110501ef9 32 }
ShaolinPoutine 3:7b1110501ef9 33 mask = mask >> 1;
ShaolinPoutine 3:7b1110501ef9 34 }
ShaolinPoutine 3:7b1110501ef9 35 }
ShaolinPoutine 3:7b1110501ef9 36
ShaolinPoutine 3:7b1110501ef9 37 uint16_t GetBit(char *payload, int bitNumber)
ShaolinPoutine 3:7b1110501ef9 38 {
ShaolinPoutine 3:7b1110501ef9 39 int index = bitNumber / 8;
ShaolinPoutine 3:7b1110501ef9 40 int bitNo = bitNumber % 8;
ShaolinPoutine 3:7b1110501ef9 41
ShaolinPoutine 3:7b1110501ef9 42 return (payload[index] & (0x80 >> bitNo)) > 0;
ShaolinPoutine 3:7b1110501ef9 43 }
ShaolinPoutine 3:7b1110501ef9 44
ShaolinPoutine 3:7b1110501ef9 45 void CRC16(char *payload, int length)
ShaolinPoutine 3:7b1110501ef9 46 {
ShaolinPoutine 3:7b1110501ef9 47 payload[length - 1] = 0;
ShaolinPoutine 3:7b1110501ef9 48 payload[length - 2] = 0;
ShaolinPoutine 3:7b1110501ef9 49
ShaolinPoutine 3:7b1110501ef9 50 uint16_t polynome = 0x8005; // Les 16 bits du polynome (17ieme bit a 1 est implicitement)
ShaolinPoutine 3:7b1110501ef9 51 uint16_t workingSet;
ShaolinPoutine 3:7b1110501ef9 52 uint16_t lastBit;
ShaolinPoutine 3:7b1110501ef9 53
ShaolinPoutine 3:7b1110501ef9 54 int noBit;
ShaolinPoutine 3:7b1110501ef9 55 int nbBit = length * 8;
ShaolinPoutine 3:7b1110501ef9 56
ShaolinPoutine 3:7b1110501ef9 57 // Initialiser les données
ShaolinPoutine 3:7b1110501ef9 58 lastBit = (payload[0] & 0x80) >> 7;
ShaolinPoutine 3:7b1110501ef9 59 workingSet = (((payload[0] << 8) | payload[1]) << 1) | ((payload[2] & 0x80) >> 7);
ShaolinPoutine 3:7b1110501ef9 60 noBit = 16;
ShaolinPoutine 3:7b1110501ef9 61
ShaolinPoutine 3:7b1110501ef9 62 // CRC
ShaolinPoutine 3:7b1110501ef9 63 while (noBit < (nbBit - 1))
ShaolinPoutine 3:7b1110501ef9 64 {
ShaolinPoutine 3:7b1110501ef9 65 if (lastBit > 0)
ShaolinPoutine 3:7b1110501ef9 66 {
ShaolinPoutine 3:7b1110501ef9 67 workingSet = workingSet xor polynome;
ShaolinPoutine 3:7b1110501ef9 68 }
ShaolinPoutine 3:7b1110501ef9 69 else
ShaolinPoutine 3:7b1110501ef9 70 {
ShaolinPoutine 3:7b1110501ef9 71 workingSet = workingSet xor 0;
ShaolinPoutine 3:7b1110501ef9 72 }
ShaolinPoutine 3:7b1110501ef9 73
ShaolinPoutine 3:7b1110501ef9 74 // Ajouter le prochain bit des valeurs
ShaolinPoutine 3:7b1110501ef9 75 lastBit = (workingSet & 0x8000) >> 15;
ShaolinPoutine 3:7b1110501ef9 76 workingSet = workingSet << 1;
ShaolinPoutine 3:7b1110501ef9 77 workingSet = workingSet | GetBit(payload, noBit);
ShaolinPoutine 3:7b1110501ef9 78
ShaolinPoutine 3:7b1110501ef9 79 noBit++;
ShaolinPoutine 3:7b1110501ef9 80 }
ShaolinPoutine 3:7b1110501ef9 81
ShaolinPoutine 3:7b1110501ef9 82 // Final XOR
ShaolinPoutine 3:7b1110501ef9 83 if (lastBit > 0)
ShaolinPoutine 3:7b1110501ef9 84 {
ShaolinPoutine 3:7b1110501ef9 85 workingSet = workingSet xor polynome;
ShaolinPoutine 3:7b1110501ef9 86 }
ShaolinPoutine 3:7b1110501ef9 87 else
ShaolinPoutine 3:7b1110501ef9 88 {
ShaolinPoutine 3:7b1110501ef9 89 workingSet = workingSet xor 0;
ShaolinPoutine 3:7b1110501ef9 90 }
ShaolinPoutine 3:7b1110501ef9 91
ShaolinPoutine 3:7b1110501ef9 92 // Ecrire la valeur dans le payload
ShaolinPoutine 3:7b1110501ef9 93 payload[length - 1] = workingSet & 0x00FF;
ShaolinPoutine 3:7b1110501ef9 94 payload[length - 2] = workingSet >> 8;
ShaolinPoutine 3:7b1110501ef9 95 }
ShaolinPoutine 3:7b1110501ef9 96
ShaolinPoutine 3:7b1110501ef9 97 bool compareBoolArray(bool* a, bool* b)
ShaolinPoutine 3:7b1110501ef9 98 {
ShaolinPoutine 3:7b1110501ef9 99 int i = 0;
ShaolinPoutine 3:7b1110501ef9 100 while (i < 8)
ShaolinPoutine 3:7b1110501ef9 101 {
ShaolinPoutine 3:7b1110501ef9 102 if (a[i] != b[i])
ShaolinPoutine 3:7b1110501ef9 103 {
ShaolinPoutine 3:7b1110501ef9 104 return false;
ShaolinPoutine 3:7b1110501ef9 105 }
ShaolinPoutine 3:7b1110501ef9 106 i++;
ShaolinPoutine 3:7b1110501ef9 107 }
ShaolinPoutine 3:7b1110501ef9 108 return true;
ShaolinPoutine 3:7b1110501ef9 109 }
ShaolinPoutine 3:7b1110501ef9 110
ShaolinPoutine 3:7b1110501ef9 111 char msbBooltoChar(bool data[8])
ShaolinPoutine 3:7b1110501ef9 112 {
ShaolinPoutine 3:7b1110501ef9 113 char c = 0;
ShaolinPoutine 3:7b1110501ef9 114 for (int i=0; i < 8 ; i++)
ShaolinPoutine 3:7b1110501ef9 115 if (data[i])
ShaolinPoutine 3:7b1110501ef9 116 c |= 1 << (7 - i);
ShaolinPoutine 3:7b1110501ef9 117 return c;
ShaolinPoutine 3:7b1110501ef9 118 }
ShaolinPoutine 3:7b1110501ef9 119
ShaolinPoutine 3:7b1110501ef9 120 void charToMsbBool(char data, bool out[8])
ShaolinPoutine 3:7b1110501ef9 121 {
ShaolinPoutine 3:7b1110501ef9 122 for (int i=0; i < 8; ++i)
ShaolinPoutine 3:7b1110501ef9 123 out[i] = (data & (1<<(7 - i))) != 0;
ShaolinPoutine 3:7b1110501ef9 124 }
ShaolinPoutine 3:7b1110501ef9 125
ShaolinPoutine 3:7b1110501ef9 126 void PrintBoolArray(bool* data, int len)
ShaolinPoutine 3:7b1110501ef9 127 {
ShaolinPoutine 3:7b1110501ef9 128 pc.printf("[");
ShaolinPoutine 3:7b1110501ef9 129 for (int i = 0; i < len - 1; i++)
ShaolinPoutine 3:7b1110501ef9 130 {
ShaolinPoutine 3:7b1110501ef9 131 pc.printf("%d, ", data[i]);
ShaolinPoutine 3:7b1110501ef9 132 }
ShaolinPoutine 3:7b1110501ef9 133 pc.printf("%d]", data[len-1]);
ShaolinPoutine 3:7b1110501ef9 134 }
ShaolinPoutine 3:7b1110501ef9 135
ShaolinPoutine 3:7b1110501ef9 136 bool ManchesterDecodeBit(bool* data, bool* out) // Tested
ShaolinPoutine 2:1665cd4c922c 137 {
ShaolinPoutine 2:1665cd4c922c 138 if (data[0] == 0 && data[1] == 1)
ShaolinPoutine 2:1665cd4c922c 139 {
ShaolinPoutine 3:7b1110501ef9 140 out[0] = 1;
ShaolinPoutine 2:1665cd4c922c 141 }
ShaolinPoutine 2:1665cd4c922c 142 else if (data[0] == 1 && data[1] == 0)
ShaolinPoutine 2:1665cd4c922c 143 {
ShaolinPoutine 3:7b1110501ef9 144 out[0] = 0;
ShaolinPoutine 2:1665cd4c922c 145 }
ShaolinPoutine 2:1665cd4c922c 146 else
ShaolinPoutine 2:1665cd4c922c 147 {
ShaolinPoutine 2:1665cd4c922c 148 return false;
ShaolinPoutine 2:1665cd4c922c 149 }
ShaolinPoutine 2:1665cd4c922c 150 return true;
ShaolinPoutine 2:1665cd4c922c 151 }
ShaolinPoutine 2:1665cd4c922c 152
ShaolinPoutine 3:7b1110501ef9 153 bool ManchesterDecodeByte(bool* data, char* out) // Tested
ShaolinPoutine 2:1665cd4c922c 154 {
ShaolinPoutine 2:1665cd4c922c 155 bool Valid = true;
ShaolinPoutine 2:1665cd4c922c 156 bool tmp = true;
ShaolinPoutine 2:1665cd4c922c 157 for (int i = 0; i < 8; i++)
ShaolinPoutine 2:1665cd4c922c 158 {
ShaolinPoutine 2:1665cd4c922c 159 Valid = ManchesterDecodeBit(data, &tmp);
ShaolinPoutine 2:1665cd4c922c 160 data += 2;
ShaolinPoutine 3:7b1110501ef9 161 if (!tmp)
ShaolinPoutine 2:1665cd4c922c 162 {
ShaolinPoutine 3:7b1110501ef9 163 *out = *out & ~(0x1 << (7-i));
ShaolinPoutine 2:1665cd4c922c 164 }
ShaolinPoutine 2:1665cd4c922c 165 else
ShaolinPoutine 2:1665cd4c922c 166 {
ShaolinPoutine 3:7b1110501ef9 167 *out = *out | (0x1 << (7-i));
ShaolinPoutine 2:1665cd4c922c 168 }
ShaolinPoutine 2:1665cd4c922c 169 if (!Valid)
ShaolinPoutine 2:1665cd4c922c 170 {
ShaolinPoutine 2:1665cd4c922c 171 return false;
ShaolinPoutine 2:1665cd4c922c 172 }
ShaolinPoutine 2:1665cd4c922c 173 }
ShaolinPoutine 2:1665cd4c922c 174 return true;
ShaolinPoutine 2:1665cd4c922c 175 }
ShaolinPoutine 2:1665cd4c922c 176
ShaolinPoutine 3:7b1110501ef9 177 void ManchesterEncodeBit(bool data, bool* out) // Tested
ShaolinPoutine 2:1665cd4c922c 178 {
ShaolinPoutine 2:1665cd4c922c 179 if (data == 0)
ShaolinPoutine 2:1665cd4c922c 180 {
ShaolinPoutine 2:1665cd4c922c 181 out[0] = 1;
ShaolinPoutine 2:1665cd4c922c 182 out[1] = 0;
ShaolinPoutine 2:1665cd4c922c 183 }
ShaolinPoutine 3:7b1110501ef9 184 else
ShaolinPoutine 3:7b1110501ef9 185 {
ShaolinPoutine 3:7b1110501ef9 186 out[0] = 0;
ShaolinPoutine 3:7b1110501ef9 187 out[1] = 1;
ShaolinPoutine 3:7b1110501ef9 188 }
ShaolinPoutine 3:7b1110501ef9 189 }
ShaolinPoutine 3:7b1110501ef9 190
ShaolinPoutine 3:7b1110501ef9 191 void ManchesterEncodeByte(char data, bool* out) // Tested
ShaolinPoutine 3:7b1110501ef9 192 {
ShaolinPoutine 3:7b1110501ef9 193 for (int i = 0; i < 8; i++)
ShaolinPoutine 3:7b1110501ef9 194 {
ShaolinPoutine 3:7b1110501ef9 195 ManchesterEncodeBit((bool) ((data >> (7 - i)) & 1), &(out[2*i]));
ShaolinPoutine 3:7b1110501ef9 196 }
ShaolinPoutine 2:1665cd4c922c 197 }
ShaolinPoutine 2:1665cd4c922c 198
ShaolinPoutine 3:7b1110501ef9 199 int CountData(char* data) // Tested
ShaolinPoutine 3:7b1110501ef9 200 {
ShaolinPoutine 3:7b1110501ef9 201 int i = 0;
ShaolinPoutine 3:7b1110501ef9 202 while(*data != 0)
ShaolinPoutine 3:7b1110501ef9 203 {
ShaolinPoutine 3:7b1110501ef9 204 data++;
ShaolinPoutine 3:7b1110501ef9 205 i++;
ShaolinPoutine 3:7b1110501ef9 206 }
ShaolinPoutine 3:7b1110501ef9 207 return i;
ShaolinPoutine 3:7b1110501ef9 208 }
ShaolinPoutine 3:7b1110501ef9 209
ShaolinPoutine 3:7b1110501ef9 210 void AddPreambule(bool* data)
ShaolinPoutine 3:7b1110501ef9 211 {
ShaolinPoutine 3:7b1110501ef9 212 for (int i = 0; i < 8; i++)
ShaolinPoutine 3:7b1110501ef9 213 {
ShaolinPoutine 3:7b1110501ef9 214 *data = PREAMBULEBOOL[i];
ShaolinPoutine 3:7b1110501ef9 215 data++;
ShaolinPoutine 3:7b1110501ef9 216 }
ShaolinPoutine 3:7b1110501ef9 217 }
ShaolinPoutine 3:7b1110501ef9 218
ShaolinPoutine 3:7b1110501ef9 219 void AddStartEnd(bool* data)
ShaolinPoutine 2:1665cd4c922c 220 {
ShaolinPoutine 2:1665cd4c922c 221 for (int i = 0; i < 8; i++)
ShaolinPoutine 2:1665cd4c922c 222 {
ShaolinPoutine 3:7b1110501ef9 223 *data = STARTENDBOOL[i];
ShaolinPoutine 3:7b1110501ef9 224 data++;
ShaolinPoutine 2:1665cd4c922c 225 }
ShaolinPoutine 2:1665cd4c922c 226 }
ShaolinPoutine 2:1665cd4c922c 227
ShaolinPoutine 3:7b1110501ef9 228 void AnalyseHeading(char* data, int* id, int* frame, int* frame_count, int* len)
ShaolinPoutine 3:7b1110501ef9 229 {
ShaolinPoutine 3:7b1110501ef9 230 *id = data[0] >> 6;
ShaolinPoutine 3:7b1110501ef9 231 *frame = (data[0] >> 3) & 7;
ShaolinPoutine 3:7b1110501ef9 232 *frame_count = data[0] & 7;
ShaolinPoutine 3:7b1110501ef9 233
ShaolinPoutine 3:7b1110501ef9 234 *len = data[1];
ShaolinPoutine 3:7b1110501ef9 235 }
ShaolinPoutine 3:7b1110501ef9 236
ShaolinPoutine 3:7b1110501ef9 237 bool AddHeading(char* out, int id, int frame, int frame_count, char len) // Tested
ShaolinPoutine 3:7b1110501ef9 238 {
ShaolinPoutine 3:7b1110501ef9 239 char tmp = 0;
ShaolinPoutine 3:7b1110501ef9 240 if (frame > frame_count || frame > 7 || frame_count > 7 || len - 2 > DATA_LEN)
ShaolinPoutine 3:7b1110501ef9 241 {
ShaolinPoutine 3:7b1110501ef9 242 return false;
ShaolinPoutine 3:7b1110501ef9 243 }
ShaolinPoutine 3:7b1110501ef9 244 // Two first bits -> id
ShaolinPoutine 3:7b1110501ef9 245 id = id & 3;
ShaolinPoutine 3:7b1110501ef9 246 tmp = tmp | (id << 6);
ShaolinPoutine 3:7b1110501ef9 247
ShaolinPoutine 3:7b1110501ef9 248 // Three next -> frame number
ShaolinPoutine 3:7b1110501ef9 249 frame = frame & 7;
ShaolinPoutine 3:7b1110501ef9 250 tmp = tmp | (frame << 3);
ShaolinPoutine 3:7b1110501ef9 251
ShaolinPoutine 3:7b1110501ef9 252 // Three last -> number of frames
ShaolinPoutine 3:7b1110501ef9 253 frame_count = frame_count & 7;
ShaolinPoutine 3:7b1110501ef9 254 tmp = tmp | frame_count;
ShaolinPoutine 3:7b1110501ef9 255 *out = tmp;
ShaolinPoutine 3:7b1110501ef9 256
ShaolinPoutine 3:7b1110501ef9 257 // Last char is lenght
ShaolinPoutine 3:7b1110501ef9 258 out++;
ShaolinPoutine 3:7b1110501ef9 259 *out = len;
ShaolinPoutine 3:7b1110501ef9 260 return true;
ShaolinPoutine 3:7b1110501ef9 261 }
ShaolinPoutine 3:7b1110501ef9 262
ShaolinPoutine 3:7b1110501ef9 263 void AddCRC(char* data, int len)
ShaolinPoutine 2:1665cd4c922c 264 {
ShaolinPoutine 2:1665cd4c922c 265
ShaolinPoutine 2:1665cd4c922c 266 }
ShaolinPoutine 3:7b1110501ef9 267 void ReceiveBits(bool* data, int len);
ShaolinPoutine 3:7b1110501ef9 268 void SendBits(bool* bits, int len)
ShaolinPoutine 3:7b1110501ef9 269 {
ShaolinPoutine 3:7b1110501ef9 270 // Synchronize with wire
ShaolinPoutine 3:7b1110501ef9 271 ReceiveBits(bits, len);
ShaolinPoutine 3:7b1110501ef9 272 }
ShaolinPoutine 3:7b1110501ef9 273
ShaolinPoutine 3:7b1110501ef9 274 void SendFormated(bool* data, int len)
ShaolinPoutine 3:7b1110501ef9 275 {
ShaolinPoutine 3:7b1110501ef9 276 pc.printf("SendFormated - len %d\r\n", len);
ShaolinPoutine 3:7b1110501ef9 277 bool tmp[len + 3 * 8];
ShaolinPoutine 3:7b1110501ef9 278 AddPreambule(tmp);
ShaolinPoutine 3:7b1110501ef9 279 AddStartEnd(tmp + 8);
ShaolinPoutine 3:7b1110501ef9 280 for (int i = 0; i < len; i++)
ShaolinPoutine 3:7b1110501ef9 281 {
ShaolinPoutine 3:7b1110501ef9 282 tmp[i + 2 * 8] = data[i];
ShaolinPoutine 3:7b1110501ef9 283 }
ShaolinPoutine 3:7b1110501ef9 284 AddStartEnd(tmp + (len + 3 - 1) * 8);
ShaolinPoutine 3:7b1110501ef9 285 SendBits(tmp, len + 3 * 8);
ShaolinPoutine 3:7b1110501ef9 286 }
ShaolinPoutine 3:7b1110501ef9 287
ShaolinPoutine 3:7b1110501ef9 288 void SendManchester(char* data, int len)
ShaolinPoutine 3:7b1110501ef9 289 {
ShaolinPoutine 3:7b1110501ef9 290 pc.printf("SendManchester - len %d\r\n", len);
ShaolinPoutine 3:7b1110501ef9 291 bool bits[2*len*8];
ShaolinPoutine 3:7b1110501ef9 292 for (int i = 0; i < len; i++)
ShaolinPoutine 3:7b1110501ef9 293 {
ShaolinPoutine 3:7b1110501ef9 294 ManchesterEncodeByte(data[i], bits + 2*i*8);
ShaolinPoutine 3:7b1110501ef9 295 }
ShaolinPoutine 3:7b1110501ef9 296 SendFormated(bits, 2*len*8);
ShaolinPoutine 3:7b1110501ef9 297 }
ShaolinPoutine 3:7b1110501ef9 298
ShaolinPoutine 3:7b1110501ef9 299 int lastIdUsed = 0;
ShaolinPoutine 3:7b1110501ef9 300
ShaolinPoutine 3:7b1110501ef9 301 void SendText(char* data, int len)
ShaolinPoutine 3:7b1110501ef9 302 {
ShaolinPoutine 3:7b1110501ef9 303 pc.printf("SendText - len %d\r\n", len);
ShaolinPoutine 3:7b1110501ef9 304 char tmp[len + 4];
ShaolinPoutine 3:7b1110501ef9 305 int frame, frames;
ShaolinPoutine 3:7b1110501ef9 306 // Gestion des ID
ShaolinPoutine 3:7b1110501ef9 307 if (len > DATA_LEN)
ShaolinPoutine 3:7b1110501ef9 308 {
ShaolinPoutine 3:7b1110501ef9 309 pc.printf("Plz implement multiple trames messages.\r\n");
ShaolinPoutine 3:7b1110501ef9 310 return;
ShaolinPoutine 3:7b1110501ef9 311 }
ShaolinPoutine 3:7b1110501ef9 312 else
ShaolinPoutine 3:7b1110501ef9 313 {
ShaolinPoutine 3:7b1110501ef9 314 frame = 1;
ShaolinPoutine 3:7b1110501ef9 315 frames = 1;
ShaolinPoutine 3:7b1110501ef9 316 // Gestion exception
ShaolinPoutine 3:7b1110501ef9 317 AddHeading(tmp, lastIdUsed, frame, frames, len + 2);
ShaolinPoutine 3:7b1110501ef9 318 for (int i = 0; i < len; i++)
ShaolinPoutine 3:7b1110501ef9 319 {
ShaolinPoutine 3:7b1110501ef9 320 tmp[i + 2] = data[i];
ShaolinPoutine 3:7b1110501ef9 321 }
ShaolinPoutine 3:7b1110501ef9 322 tmp[len + 2] = 0;
ShaolinPoutine 3:7b1110501ef9 323 tmp[len + 3] = 0;
ShaolinPoutine 3:7b1110501ef9 324 CRC16(tmp + 2, len + 2);
ShaolinPoutine 3:7b1110501ef9 325 SendManchester(tmp, len + 4);
ShaolinPoutine 3:7b1110501ef9 326 }
ShaolinPoutine 3:7b1110501ef9 327 }
ShaolinPoutine 3:7b1110501ef9 328
ShaolinPoutine 3:7b1110501ef9 329 void ReceiveText(char* data)
ShaolinPoutine 3:7b1110501ef9 330 {
ShaolinPoutine 3:7b1110501ef9 331 pc.printf("Received: %s\r\n", data);
ShaolinPoutine 3:7b1110501ef9 332 }
ShaolinPoutine 3:7b1110501ef9 333
ShaolinPoutine 3:7b1110501ef9 334 void ReceiveFormated(char* data, int len)
ShaolinPoutine 3:7b1110501ef9 335 {
ShaolinPoutine 3:7b1110501ef9 336 pc.printf("ReceiveFormated - len %d\r\n", len);
ShaolinPoutine 3:7b1110501ef9 337 char tmp[len - 2];
ShaolinPoutine 3:7b1110501ef9 338 int id, frame, frames, predicted_len;
ShaolinPoutine 3:7b1110501ef9 339 AnalyseHeading(data, &id, &frame, &frames, &predicted_len);
ShaolinPoutine 3:7b1110501ef9 340 // Gestion des id et frames et frame number
ShaolinPoutine 3:7b1110501ef9 341
ShaolinPoutine 3:7b1110501ef9 342 if (predicted_len != len - 2)
ShaolinPoutine 3:7b1110501ef9 343 {
ShaolinPoutine 3:7b1110501ef9 344 pc.printf("DEBUG -- Received payload with wrong lenght. %d %d\r\n", len - 2, predicted_len);
ShaolinPoutine 3:7b1110501ef9 345 return;
ShaolinPoutine 3:7b1110501ef9 346 }
ShaolinPoutine 3:7b1110501ef9 347 // Copy data
ShaolinPoutine 3:7b1110501ef9 348 for (int i = 0; i < len - 2; i++)
ShaolinPoutine 3:7b1110501ef9 349 {
ShaolinPoutine 3:7b1110501ef9 350 tmp[i] = data[i + 2];
ShaolinPoutine 3:7b1110501ef9 351 }
ShaolinPoutine 3:7b1110501ef9 352 for (int i = len - 4; i < len - 2; i++)
ShaolinPoutine 3:7b1110501ef9 353 {
ShaolinPoutine 3:7b1110501ef9 354 tmp[i] = 0;
ShaolinPoutine 3:7b1110501ef9 355 }
ShaolinPoutine 3:7b1110501ef9 356
ShaolinPoutine 3:7b1110501ef9 357 //CRC calculate
ShaolinPoutine 3:7b1110501ef9 358 CRC16(tmp, len - 2);
ShaolinPoutine 3:7b1110501ef9 359 if (tmp[len - 4] == data[len - 2] && tmp[len - 3] == data[len - 1])
ShaolinPoutine 3:7b1110501ef9 360 {
ShaolinPoutine 3:7b1110501ef9 361 tmp[len - 4] = '\0';
ShaolinPoutine 3:7b1110501ef9 362 led2= 1;
ShaolinPoutine 3:7b1110501ef9 363 ReceiveText(tmp);
ShaolinPoutine 3:7b1110501ef9 364 }
ShaolinPoutine 3:7b1110501ef9 365 else
ShaolinPoutine 3:7b1110501ef9 366 {
ShaolinPoutine 3:7b1110501ef9 367 pc.printf("DEBUG -- Received payload with wrong CRC.\r\n");
ShaolinPoutine 3:7b1110501ef9 368 return;
ShaolinPoutine 3:7b1110501ef9 369 }
ShaolinPoutine 3:7b1110501ef9 370 }
ShaolinPoutine 3:7b1110501ef9 371
ShaolinPoutine 3:7b1110501ef9 372 void ReceiveManchester(bool* data, int len)
ShaolinPoutine 3:7b1110501ef9 373 {
ShaolinPoutine 3:7b1110501ef9 374 pc.printf("ReceiveManchester - len %d\r\n", len);
ShaolinPoutine 3:7b1110501ef9 375 char tmp[len / 16];
ShaolinPoutine 3:7b1110501ef9 376 for (int i = 0; i < len / 16; i++)
ShaolinPoutine 3:7b1110501ef9 377 {
ShaolinPoutine 3:7b1110501ef9 378 ManchesterDecodeByte(data, tmp + i);
ShaolinPoutine 3:7b1110501ef9 379 data += 16;
ShaolinPoutine 3:7b1110501ef9 380 }
ShaolinPoutine 3:7b1110501ef9 381 ReceiveFormated(tmp, len / 16);
ShaolinPoutine 3:7b1110501ef9 382 }
ShaolinPoutine 3:7b1110501ef9 383
ShaolinPoutine 3:7b1110501ef9 384 void ReceiveBits(bool* data, int len)
ShaolinPoutine 3:7b1110501ef9 385 {
ShaolinPoutine 3:7b1110501ef9 386 if (compareBoolArray(data, PREAMBULEBOOL))
ShaolinPoutine 3:7b1110501ef9 387 {
ShaolinPoutine 3:7b1110501ef9 388 data += 8;
ShaolinPoutine 3:7b1110501ef9 389 len -= 8;
ShaolinPoutine 3:7b1110501ef9 390 }
ShaolinPoutine 3:7b1110501ef9 391 else
ShaolinPoutine 3:7b1110501ef9 392 {
ShaolinPoutine 3:7b1110501ef9 393 pc.printf("DEBUG -- Received payload with wrong Preambule.");PrintBoolArray(data , 8);pc.printf("\r\n");
ShaolinPoutine 3:7b1110501ef9 394 return;
ShaolinPoutine 3:7b1110501ef9 395 }
ShaolinPoutine 3:7b1110501ef9 396
ShaolinPoutine 3:7b1110501ef9 397 // Ensure Start and end are present
ShaolinPoutine 3:7b1110501ef9 398 if (compareBoolArray(data, STARTENDBOOL) || compareBoolArray(data + (len - 8), STARTENDBOOL))
ShaolinPoutine 3:7b1110501ef9 399 {
ShaolinPoutine 3:7b1110501ef9 400 ReceiveManchester(data + 8, len - 2 * 8);
ShaolinPoutine 3:7b1110501ef9 401 }
ShaolinPoutine 3:7b1110501ef9 402 else
ShaolinPoutine 3:7b1110501ef9 403 {
ShaolinPoutine 3:7b1110501ef9 404 pc.printf("DEBUG -- Received payload with wrong Start or End.\r\n");
ShaolinPoutine 3:7b1110501ef9 405 return;
ShaolinPoutine 3:7b1110501ef9 406 }
ShaolinPoutine 3:7b1110501ef9 407 }
ShaolinPoutine 2:1665cd4c922c 408
ShaolinPoutine 1:8b93b2102ac5 409 extern "C" void TIMER2_IRQHandler (void)
ShaolinPoutine 1:8b93b2102ac5 410 {
ShaolinPoutine 1:8b93b2102ac5 411 if((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
ShaolinPoutine 1:8b93b2102ac5 412 {
ShaolinPoutine 1:8b93b2102ac5 413 LPC_TIM2->IR |= 1 << 0; // Clear MR0 interrupt flag
ShaolinPoutine 0:529210499c6d 414 }
ShaolinPoutine 0:529210499c6d 415 }
ShaolinPoutine 1:8b93b2102ac5 416
ShaolinPoutine 1:8b93b2102ac5 417 void init_clk()
ShaolinPoutine 1:8b93b2102ac5 418 {
ShaolinPoutine 1:8b93b2102ac5 419 LPC_PINCON->PINSEL0 |=3<<12; //P0.6 = MAT2.0 // p8
ShaolinPoutine 1:8b93b2102ac5 420 LPC_SC->PCLKSEL1 |=1<<12; //pclk timer2 = cclk
ShaolinPoutine 1:8b93b2102ac5 421 LPC_SC->PCONP |=1<<22; //timer2 power on
ShaolinPoutine 2:1665cd4c922c 422 LPC_TIM2->MR0 = CLOCK_MAX / 2; // 1 sec period
ShaolinPoutine 1:8b93b2102ac5 423 LPC_TIM2->MCR = 3; //interrupt and reset control
ShaolinPoutine 1:8b93b2102ac5 424 //3 = Interrupt & reset timer2 on match
ShaolinPoutine 1:8b93b2102ac5 425 //1 = Interrupt only, no reset of timer0
ShaolinPoutine 1:8b93b2102ac5 426 LPC_TIM2->EMR =3<<4; //EMC0 = 11 (Toogle)
ShaolinPoutine 1:8b93b2102ac5 427 NVIC_EnableIRQ(TIMER2_IRQn); //enable timer2 interrupt
ShaolinPoutine 1:8b93b2102ac5 428 LPC_TIM2->TCR = 1; //enable Timer2
ShaolinPoutine 1:8b93b2102ac5 429 }
ShaolinPoutine 3:7b1110501ef9 430
ShaolinPoutine 2:1665cd4c922c 431 void TestManchester();
ShaolinPoutine 3:7b1110501ef9 432 void TestUtility();
ShaolinPoutine 2:1665cd4c922c 433 void mainRaph()
ShaolinPoutine 1:8b93b2102ac5 434 {
ShaolinPoutine 1:8b93b2102ac5 435 init_clk();
ShaolinPoutine 3:7b1110501ef9 436 TestUtility();
ShaolinPoutine 2:1665cd4c922c 437 TestManchester();
ShaolinPoutine 3:7b1110501ef9 438 SendText("OK", 2);
ShaolinPoutine 3:7b1110501ef9 439 SendText("A more complex sentence to prove our algorithm is robust!", 57);
ShaolinPoutine 1:8b93b2102ac5 440 }
ShaolinPoutine 1:8b93b2102ac5 441
ShaolinPoutine 1:8b93b2102ac5 442 void tick()
ShaolinPoutine 1:8b93b2102ac5 443 {
ShaolinPoutine 3:7b1110501ef9 444 myled = !myled;
ShaolinPoutine 1:8b93b2102ac5 445 }
ShaolinPoutine 1:8b93b2102ac5 446 int main() {
ShaolinPoutine 1:8b93b2102ac5 447 Ticker ticker;
ShaolinPoutine 1:8b93b2102ac5 448 ticker.attach(&tick, 0.5);
ShaolinPoutine 2:1665cd4c922c 449 mainRaph();
ShaolinPoutine 3:7b1110501ef9 450 while (1){}
ShaolinPoutine 1:8b93b2102ac5 451 }
ShaolinPoutine 2:1665cd4c922c 452
ShaolinPoutine 2:1665cd4c922c 453 //--------------------------------TEST---------------------------------------------------//
ShaolinPoutine 2:1665cd4c922c 454
ShaolinPoutine 3:7b1110501ef9 455 char ToByte(bool b[8])
ShaolinPoutine 3:7b1110501ef9 456 {
ShaolinPoutine 3:7b1110501ef9 457 char c = 0;
ShaolinPoutine 3:7b1110501ef9 458 for (int i=0; i < 8; ++i)
ShaolinPoutine 3:7b1110501ef9 459 if (b[i])
ShaolinPoutine 3:7b1110501ef9 460 c |= 1 << i;
ShaolinPoutine 3:7b1110501ef9 461 return c;
ShaolinPoutine 3:7b1110501ef9 462 }
ShaolinPoutine 3:7b1110501ef9 463
ShaolinPoutine 3:7b1110501ef9 464 void FromByte(char c, bool b[8])
ShaolinPoutine 3:7b1110501ef9 465 {
ShaolinPoutine 3:7b1110501ef9 466 for (int i=0; i < 8; ++i)
ShaolinPoutine 3:7b1110501ef9 467 b[i] = (c & (1<<i)) != 0;
ShaolinPoutine 3:7b1110501ef9 468 }
ShaolinPoutine 3:7b1110501ef9 469
ShaolinPoutine 3:7b1110501ef9 470 void TestUtility()
ShaolinPoutine 3:7b1110501ef9 471 {
ShaolinPoutine 3:7b1110501ef9 472 pc.printf("TestUtility -- Begin\r\n");
ShaolinPoutine 3:7b1110501ef9 473 char charValue = 1;
ShaolinPoutine 3:7b1110501ef9 474 bool boolValue[8] = {0,0,0,0,0,0,0,1};
ShaolinPoutine 3:7b1110501ef9 475 bool boolAns[8] = {0,0,0,0,0,0,0,1};
ShaolinPoutine 3:7b1110501ef9 476 char charAns;
ShaolinPoutine 3:7b1110501ef9 477
ShaolinPoutine 3:7b1110501ef9 478 if (!compareBoolArray(boolValue, boolAns))
ShaolinPoutine 3:7b1110501ef9 479 {
ShaolinPoutine 3:7b1110501ef9 480 pc.printf("Should be gud");
ShaolinPoutine 3:7b1110501ef9 481 return;
ShaolinPoutine 3:7b1110501ef9 482 }
ShaolinPoutine 3:7b1110501ef9 483 boolAns[7] = 0;
ShaolinPoutine 3:7b1110501ef9 484 if (compareBoolArray(boolValue, boolAns))
ShaolinPoutine 3:7b1110501ef9 485 {
ShaolinPoutine 3:7b1110501ef9 486 pc.printf("Should not be gud");
ShaolinPoutine 3:7b1110501ef9 487 return;
ShaolinPoutine 3:7b1110501ef9 488 }
ShaolinPoutine 3:7b1110501ef9 489
ShaolinPoutine 3:7b1110501ef9 490 charAns = msbBooltoChar(boolValue);
ShaolinPoutine 3:7b1110501ef9 491
ShaolinPoutine 3:7b1110501ef9 492 if (charValue != charAns)
ShaolinPoutine 3:7b1110501ef9 493 {
ShaolinPoutine 3:7b1110501ef9 494 pc.printf("msbBooltoChar error. %d %d\r\n", charValue, charAns);
ShaolinPoutine 3:7b1110501ef9 495 return;
ShaolinPoutine 3:7b1110501ef9 496 }
ShaolinPoutine 3:7b1110501ef9 497
ShaolinPoutine 3:7b1110501ef9 498 charToMsbBool(charValue, boolAns);
ShaolinPoutine 3:7b1110501ef9 499
ShaolinPoutine 3:7b1110501ef9 500 if(!compareBoolArray(boolValue, boolAns))
ShaolinPoutine 3:7b1110501ef9 501 {
ShaolinPoutine 3:7b1110501ef9 502 pc.printf("charToMsbBool error ");PrintBoolArray(boolValue,8);pc.printf(" ");PrintBoolArray(boolAns,8);pc.printf("\r\n");
ShaolinPoutine 3:7b1110501ef9 503 return;
ShaolinPoutine 3:7b1110501ef9 504 }
ShaolinPoutine 3:7b1110501ef9 505 pc.printf("TestUtility -- End\r\n");
ShaolinPoutine 3:7b1110501ef9 506 }
ShaolinPoutine 3:7b1110501ef9 507
ShaolinPoutine 2:1665cd4c922c 508 void TestManchester()
ShaolinPoutine 2:1665cd4c922c 509 {
ShaolinPoutine 3:7b1110501ef9 510 pc.printf("TestManchester - Begin\r\n");
ShaolinPoutine 2:1665cd4c922c 511
ShaolinPoutine 2:1665cd4c922c 512 // ----------------------------ENCODE-----------------------------------------------//
ShaolinPoutine 2:1665cd4c922c 513 bool Valid = true;
ShaolinPoutine 2:1665cd4c922c 514 bool boolData0 = (char) 0b0;
ShaolinPoutine 3:7b1110501ef9 515 bool boolExpected0[] = {1,0};
ShaolinPoutine 2:1665cd4c922c 516 bool boolAns0[2];
ShaolinPoutine 2:1665cd4c922c 517
ShaolinPoutine 3:7b1110501ef9 518 ManchesterEncodeBit(boolData0, (bool *) boolAns0);
ShaolinPoutine 2:1665cd4c922c 519
ShaolinPoutine 2:1665cd4c922c 520 int i = 0;
ShaolinPoutine 2:1665cd4c922c 521 Valid = true;
ShaolinPoutine 2:1665cd4c922c 522 while (Valid && i < 2)
ShaolinPoutine 2:1665cd4c922c 523 {
ShaolinPoutine 2:1665cd4c922c 524 Valid = !(boolExpected0[i] ^ boolAns0[i]);
ShaolinPoutine 2:1665cd4c922c 525 i++;
ShaolinPoutine 2:1665cd4c922c 526 }
ShaolinPoutine 2:1665cd4c922c 527
ShaolinPoutine 2:1665cd4c922c 528 if (!Valid)
ShaolinPoutine 2:1665cd4c922c 529 {
ShaolinPoutine 3:7b1110501ef9 530 pc.printf("TestManchester - encode - bool0 - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 531 pc.printf("Expected: [%d, %d]. Received: [%d, %d].\r\n\n", boolExpected0[0], boolExpected0[1], boolAns0[0], boolAns0[1]);
ShaolinPoutine 2:1665cd4c922c 532 }
ShaolinPoutine 2:1665cd4c922c 533
ShaolinPoutine 2:1665cd4c922c 534 bool boolData1 = (char) 0b1;
ShaolinPoutine 3:7b1110501ef9 535 bool boolExpected1[] = {0,1};
ShaolinPoutine 2:1665cd4c922c 536 bool boolAns1[2];
ShaolinPoutine 3:7b1110501ef9 537 ManchesterEncodeBit(boolData1, (bool*) boolAns1);
ShaolinPoutine 2:1665cd4c922c 538
ShaolinPoutine 2:1665cd4c922c 539 i = 0;
ShaolinPoutine 2:1665cd4c922c 540 Valid = true;
ShaolinPoutine 2:1665cd4c922c 541 while (Valid && i < 2)
ShaolinPoutine 2:1665cd4c922c 542 {
ShaolinPoutine 2:1665cd4c922c 543 Valid = !(boolExpected1[i] ^ boolAns1[i]);
ShaolinPoutine 2:1665cd4c922c 544 i++;
ShaolinPoutine 2:1665cd4c922c 545 }
ShaolinPoutine 2:1665cd4c922c 546 if (!Valid)
ShaolinPoutine 2:1665cd4c922c 547 {
ShaolinPoutine 3:7b1110501ef9 548 pc.printf("TestManchester - encode - boo11 - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 549 pc.printf("Expected: [%d, %d]. Received: [%d, %d].\r\n\n", boolExpected1[0], boolExpected1[1], boolAns1[0], boolAns1[1]);
ShaolinPoutine 2:1665cd4c922c 550 }
ShaolinPoutine 2:1665cd4c922c 551
ShaolinPoutine 2:1665cd4c922c 552 char charData = (char) 0b01010101;
ShaolinPoutine 3:7b1110501ef9 553 bool charExpected[] = {1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1};
ShaolinPoutine 2:1665cd4c922c 554 bool charAns[16];
ShaolinPoutine 3:7b1110501ef9 555 ManchesterEncodeByte(charData, (bool*) charAns);
ShaolinPoutine 2:1665cd4c922c 556
ShaolinPoutine 2:1665cd4c922c 557 i = 0;
ShaolinPoutine 2:1665cd4c922c 558 Valid = true;
ShaolinPoutine 2:1665cd4c922c 559 while (Valid && i < 16)
ShaolinPoutine 2:1665cd4c922c 560 {
ShaolinPoutine 2:1665cd4c922c 561 Valid = !(charExpected[i] ^ charAns[i]);
ShaolinPoutine 2:1665cd4c922c 562 i++;
ShaolinPoutine 2:1665cd4c922c 563 }
ShaolinPoutine 2:1665cd4c922c 564 if (!Valid)
ShaolinPoutine 2:1665cd4c922c 565 {
ShaolinPoutine 3:7b1110501ef9 566 pc.printf("TestManchester - encode - char - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 567 pc.printf("Expected: [%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d]\r\nReceived: [%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d].\r\n\n",
ShaolinPoutine 2:1665cd4c922c 568 charExpected[0], charExpected[1], charExpected[2], charExpected[3],
ShaolinPoutine 2:1665cd4c922c 569 charExpected[4], charExpected[5], charExpected[6], charExpected[7],
ShaolinPoutine 2:1665cd4c922c 570 charExpected[8], charExpected[9], charExpected[10], charExpected[11],
ShaolinPoutine 2:1665cd4c922c 571 charExpected[12], charExpected[13], charExpected[14], charExpected[15],
ShaolinPoutine 2:1665cd4c922c 572 charAns[0], charAns[1], charAns[2], charAns[3],
ShaolinPoutine 2:1665cd4c922c 573 charAns[4], charAns[5], charAns[6], charAns[7],
ShaolinPoutine 2:1665cd4c922c 574 charAns[8], charAns[9], charAns[10], charAns[11],
ShaolinPoutine 2:1665cd4c922c 575 charAns[12], charAns[13], charAns[14], charAns[15]);
ShaolinPoutine 2:1665cd4c922c 576 }
ShaolinPoutine 2:1665cd4c922c 577
ShaolinPoutine 2:1665cd4c922c 578 // ----------------------------DECODE-----------------------------------------------//
ShaolinPoutine 2:1665cd4c922c 579 Valid = true;
ShaolinPoutine 3:7b1110501ef9 580 bool boolDecodeData0[] = {1,0};
ShaolinPoutine 2:1665cd4c922c 581 bool boolDecodeExpected0 = 0b0;
ShaolinPoutine 2:1665cd4c922c 582 bool boolDecodeAns0;
ShaolinPoutine 2:1665cd4c922c 583 bool Success = true;
ShaolinPoutine 2:1665cd4c922c 584 Success = ManchesterDecodeBit((bool *)boolDecodeData0, &boolDecodeAns0);
ShaolinPoutine 2:1665cd4c922c 585
ShaolinPoutine 2:1665cd4c922c 586 if (!Success)
ShaolinPoutine 2:1665cd4c922c 587 {
ShaolinPoutine 3:7b1110501ef9 588 pc.printf("TestManchester - decode - bool0 - Invalid format\r\n");
ShaolinPoutine 2:1665cd4c922c 589 }
ShaolinPoutine 2:1665cd4c922c 590
ShaolinPoutine 2:1665cd4c922c 591 if (boolDecodeExpected0 != boolDecodeAns0)
ShaolinPoutine 2:1665cd4c922c 592 {
ShaolinPoutine 3:7b1110501ef9 593 pc.printf("TestManchester - decode - bool0 - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 594 pc.printf("Expected: %d. Received: %d.\r\n\n", boolDecodeExpected0, boolDecodeAns0);
ShaolinPoutine 2:1665cd4c922c 595 }
ShaolinPoutine 2:1665cd4c922c 596
ShaolinPoutine 3:7b1110501ef9 597 bool boolDecodeData1[] = {0,1};
ShaolinPoutine 2:1665cd4c922c 598 bool boolDecodeExpected1 = (char) 0b1;
ShaolinPoutine 2:1665cd4c922c 599 bool boolDecodeAns1;
ShaolinPoutine 2:1665cd4c922c 600 Success = ManchesterDecodeBit((bool *)boolDecodeData1, &boolDecodeAns1);
ShaolinPoutine 2:1665cd4c922c 601
ShaolinPoutine 2:1665cd4c922c 602 if (!Success)
ShaolinPoutine 2:1665cd4c922c 603 {
ShaolinPoutine 3:7b1110501ef9 604 pc.printf("TestManchester - decode - bool1 - Invalid format\r\n");
ShaolinPoutine 2:1665cd4c922c 605 }
ShaolinPoutine 2:1665cd4c922c 606 if (boolDecodeExpected1 != boolDecodeAns1)
ShaolinPoutine 2:1665cd4c922c 607 {
ShaolinPoutine 3:7b1110501ef9 608 pc.printf("TestManchester - decode - boo11 - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 609 pc.printf("Expected: %d. Received: %d\r\n\n", boolDecodeExpected1, boolDecodeAns1);
ShaolinPoutine 2:1665cd4c922c 610 }
ShaolinPoutine 2:1665cd4c922c 611
ShaolinPoutine 2:1665cd4c922c 612 bool boolDataInv0[] = {0,0};
ShaolinPoutine 3:7b1110501ef9 613 bool boolDataInv1[] = {1,1};
ShaolinPoutine 2:1665cd4c922c 614 bool boolAnsInv;
ShaolinPoutine 2:1665cd4c922c 615 Success = ManchesterDecodeBit((bool *)boolDataInv0, (bool*) boolAnsInv);
ShaolinPoutine 2:1665cd4c922c 616 if (Success)
ShaolinPoutine 2:1665cd4c922c 617 {
ShaolinPoutine 3:7b1110501ef9 618 pc.printf("TestManchester - decode - boolInv0 - Invalid format not caught\r\n");
ShaolinPoutine 2:1665cd4c922c 619 }
ShaolinPoutine 2:1665cd4c922c 620 Success = ManchesterDecodeBit((bool *)boolDataInv1, (bool*) boolAnsInv);
ShaolinPoutine 2:1665cd4c922c 621 if (Success)
ShaolinPoutine 2:1665cd4c922c 622 {
ShaolinPoutine 3:7b1110501ef9 623 pc.printf("TestManchester - decode - boolInv1 - Invalid format not caught\r\n");
ShaolinPoutine 2:1665cd4c922c 624 }
ShaolinPoutine 2:1665cd4c922c 625
ShaolinPoutine 2:1665cd4c922c 626
ShaolinPoutine 3:7b1110501ef9 627 bool charEncodeData[] = {1,0,1,0,0,1,1,0,0,1,1,0,0,1,1,0};
ShaolinPoutine 2:1665cd4c922c 628 char charEncodeExpected = 42;
ShaolinPoutine 2:1665cd4c922c 629 char charEncodeAns;
ShaolinPoutine 2:1665cd4c922c 630 Valid = ManchesterDecodeByte(charEncodeData, &charEncodeAns);
ShaolinPoutine 2:1665cd4c922c 631
ShaolinPoutine 2:1665cd4c922c 632 if (!Valid)
ShaolinPoutine 2:1665cd4c922c 633 {
ShaolinPoutine 3:7b1110501ef9 634 pc.printf("Function detected error in format\r\n");
ShaolinPoutine 2:1665cd4c922c 635 }
ShaolinPoutine 2:1665cd4c922c 636 else if (charEncodeExpected != charEncodeAns)
ShaolinPoutine 2:1665cd4c922c 637 {
ShaolinPoutine 3:7b1110501ef9 638 pc.printf("TestManchester - decode - char - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 639 pc.printf("Expected: %d\t\tReceived: %d\r\n",
ShaolinPoutine 2:1665cd4c922c 640 charEncodeExpected, charEncodeAns);
ShaolinPoutine 2:1665cd4c922c 641 }
ShaolinPoutine 2:1665cd4c922c 642
ShaolinPoutine 3:7b1110501ef9 643 //--------------------TEST COMPLET -------------------------------//
ShaolinPoutine 3:7b1110501ef9 644
ShaolinPoutine 3:7b1110501ef9 645 char charComplet = 158;
ShaolinPoutine 3:7b1110501ef9 646 bool tmpAns[16];
ShaolinPoutine 3:7b1110501ef9 647 char charCompletAns;
ShaolinPoutine 3:7b1110501ef9 648
ShaolinPoutine 3:7b1110501ef9 649 ManchesterEncodeByte(charComplet, (bool *) tmpAns);
ShaolinPoutine 3:7b1110501ef9 650 Valid = ManchesterDecodeByte((bool *) tmpAns, &charCompletAns);
ShaolinPoutine 3:7b1110501ef9 651
ShaolinPoutine 3:7b1110501ef9 652 if (!Valid)
ShaolinPoutine 3:7b1110501ef9 653 {
ShaolinPoutine 3:7b1110501ef9 654 pc.printf("Function detected error in format\r\n");
ShaolinPoutine 3:7b1110501ef9 655 }
ShaolinPoutine 3:7b1110501ef9 656 if (charComplet != charCompletAns)
ShaolinPoutine 3:7b1110501ef9 657 {
ShaolinPoutine 3:7b1110501ef9 658 pc.printf("TestManchester - decode - complete - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 659 pc.printf("Expected: %d\t\tReceived: %d\r\n",
ShaolinPoutine 3:7b1110501ef9 660 charComplet, charCompletAns);
ShaolinPoutine 3:7b1110501ef9 661 }
ShaolinPoutine 3:7b1110501ef9 662
ShaolinPoutine 3:7b1110501ef9 663 pc.printf("TestManchester - End\r\n");
ShaolinPoutine 2:1665cd4c922c 664 }