APP3 / Mbed 2 deprecated APP4

Dependencies:   mbed mbed-rtos

Committer:
EmileArseneault
Date:
Wed Mar 08 02:59:25 2017 +0000
Revision:
6:3d0735a28b98
Parent:
5:25101883c27a
Merge dans un ?tat l?thargique

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);
EmileArseneault 6:3d0735a28b98 14 DigitalOut led3(LED3);
EmileArseneault 6:3d0735a28b98 15 DigitalOut led4(LED4);
EmileArseneault 5:25101883c27a 16 InterruptIn rX(p27);
EmileArseneault 5:25101883c27a 17 DigitalOut tX(p28);
ShaolinPoutine 3:7b1110501ef9 18 Serial pc(USBTX, USBRX);
ShaolinPoutine 0:529210499c6d 19
EmileArseneault 6:3d0735a28b98 20 Mail<char [83], 4> textToPrint;
ShaolinPoutine 4:4277898e55d8 21 Queue<int, 1500> envoi;
EmileArseneault 6:3d0735a28b98 22 Queue<int, 1500> recoit;
EmileArseneault 6:3d0735a28b98 23 Queue<int, 200> rise_n_fall_queue;
ShaolinPoutine 4:4277898e55d8 24 Thread* listeningThread;
EmileArseneault 5:25101883c27a 25 Thread* Push_data;
EmileArseneault 6:3d0735a28b98 26 osThreadId mainThread;
EmileArseneault 6:3d0735a28b98 27 Semaphore ecriture(0);
ShaolinPoutine 4:4277898e55d8 28
ShaolinPoutine 2:1665cd4c922c 29
ShaolinPoutine 3:7b1110501ef9 30 void printBin(uint16_t data)
ShaolinPoutine 3:7b1110501ef9 31 {
ShaolinPoutine 3:7b1110501ef9 32 uint16_t mask = 1 << 15;
ShaolinPoutine 3:7b1110501ef9 33
ShaolinPoutine 3:7b1110501ef9 34 while (mask != 0)
ShaolinPoutine 3:7b1110501ef9 35 {
ShaolinPoutine 3:7b1110501ef9 36 if ((data & mask) > 0)
ShaolinPoutine 3:7b1110501ef9 37 {
ShaolinPoutine 3:7b1110501ef9 38 pc.printf("1");
ShaolinPoutine 3:7b1110501ef9 39 }
ShaolinPoutine 3:7b1110501ef9 40 else
ShaolinPoutine 3:7b1110501ef9 41 {
ShaolinPoutine 3:7b1110501ef9 42 pc.printf("0");
ShaolinPoutine 3:7b1110501ef9 43 }
ShaolinPoutine 3:7b1110501ef9 44 mask = mask >> 1;
ShaolinPoutine 3:7b1110501ef9 45 }
ShaolinPoutine 3:7b1110501ef9 46 }
ShaolinPoutine 3:7b1110501ef9 47
ShaolinPoutine 3:7b1110501ef9 48 uint16_t GetBit(char *payload, int bitNumber)
ShaolinPoutine 3:7b1110501ef9 49 {
ShaolinPoutine 3:7b1110501ef9 50 int index = bitNumber / 8;
ShaolinPoutine 3:7b1110501ef9 51 int bitNo = bitNumber % 8;
ShaolinPoutine 3:7b1110501ef9 52
ShaolinPoutine 3:7b1110501ef9 53 return (payload[index] & (0x80 >> bitNo)) > 0;
ShaolinPoutine 3:7b1110501ef9 54 }
ShaolinPoutine 3:7b1110501ef9 55
ShaolinPoutine 3:7b1110501ef9 56 void CRC16(char *payload, int length)
ShaolinPoutine 3:7b1110501ef9 57 {
ShaolinPoutine 3:7b1110501ef9 58 payload[length - 1] = 0;
ShaolinPoutine 3:7b1110501ef9 59 payload[length - 2] = 0;
ShaolinPoutine 3:7b1110501ef9 60
ShaolinPoutine 3:7b1110501ef9 61 uint16_t polynome = 0x8005; // Les 16 bits du polynome (17ieme bit a 1 est implicitement)
ShaolinPoutine 3:7b1110501ef9 62 uint16_t workingSet;
ShaolinPoutine 3:7b1110501ef9 63 uint16_t lastBit;
ShaolinPoutine 3:7b1110501ef9 64
ShaolinPoutine 3:7b1110501ef9 65 int noBit;
ShaolinPoutine 3:7b1110501ef9 66 int nbBit = length * 8;
ShaolinPoutine 3:7b1110501ef9 67
ShaolinPoutine 3:7b1110501ef9 68 // Initialiser les données
ShaolinPoutine 3:7b1110501ef9 69 lastBit = (payload[0] & 0x80) >> 7;
ShaolinPoutine 3:7b1110501ef9 70 workingSet = (((payload[0] << 8) | payload[1]) << 1) | ((payload[2] & 0x80) >> 7);
ShaolinPoutine 3:7b1110501ef9 71 noBit = 16;
ShaolinPoutine 3:7b1110501ef9 72
ShaolinPoutine 3:7b1110501ef9 73 // CRC
ShaolinPoutine 3:7b1110501ef9 74 while (noBit < (nbBit - 1))
ShaolinPoutine 3:7b1110501ef9 75 {
ShaolinPoutine 3:7b1110501ef9 76 if (lastBit > 0)
ShaolinPoutine 3:7b1110501ef9 77 {
ShaolinPoutine 3:7b1110501ef9 78 workingSet = workingSet xor polynome;
ShaolinPoutine 3:7b1110501ef9 79 }
ShaolinPoutine 3:7b1110501ef9 80 else
ShaolinPoutine 3:7b1110501ef9 81 {
ShaolinPoutine 3:7b1110501ef9 82 workingSet = workingSet xor 0;
ShaolinPoutine 3:7b1110501ef9 83 }
ShaolinPoutine 3:7b1110501ef9 84
ShaolinPoutine 3:7b1110501ef9 85 // Ajouter le prochain bit des valeurs
ShaolinPoutine 3:7b1110501ef9 86 lastBit = (workingSet & 0x8000) >> 15;
ShaolinPoutine 3:7b1110501ef9 87 workingSet = workingSet << 1;
ShaolinPoutine 3:7b1110501ef9 88 workingSet = workingSet | GetBit(payload, noBit);
ShaolinPoutine 3:7b1110501ef9 89
ShaolinPoutine 3:7b1110501ef9 90 noBit++;
ShaolinPoutine 3:7b1110501ef9 91 }
ShaolinPoutine 3:7b1110501ef9 92
ShaolinPoutine 3:7b1110501ef9 93 // Final XOR
ShaolinPoutine 3:7b1110501ef9 94 if (lastBit > 0)
ShaolinPoutine 3:7b1110501ef9 95 {
ShaolinPoutine 3:7b1110501ef9 96 workingSet = workingSet xor polynome;
ShaolinPoutine 3:7b1110501ef9 97 }
ShaolinPoutine 3:7b1110501ef9 98 else
ShaolinPoutine 3:7b1110501ef9 99 {
ShaolinPoutine 3:7b1110501ef9 100 workingSet = workingSet xor 0;
ShaolinPoutine 3:7b1110501ef9 101 }
ShaolinPoutine 3:7b1110501ef9 102
ShaolinPoutine 3:7b1110501ef9 103 // Ecrire la valeur dans le payload
ShaolinPoutine 3:7b1110501ef9 104 payload[length - 1] = workingSet & 0x00FF;
ShaolinPoutine 3:7b1110501ef9 105 payload[length - 2] = workingSet >> 8;
ShaolinPoutine 3:7b1110501ef9 106 }
ShaolinPoutine 3:7b1110501ef9 107
ShaolinPoutine 3:7b1110501ef9 108 bool compareBoolArray(bool* a, bool* b)
ShaolinPoutine 3:7b1110501ef9 109 {
ShaolinPoutine 3:7b1110501ef9 110 int i = 0;
ShaolinPoutine 3:7b1110501ef9 111 while (i < 8)
ShaolinPoutine 3:7b1110501ef9 112 {
ShaolinPoutine 3:7b1110501ef9 113 if (a[i] != b[i])
ShaolinPoutine 3:7b1110501ef9 114 {
ShaolinPoutine 3:7b1110501ef9 115 return false;
ShaolinPoutine 3:7b1110501ef9 116 }
ShaolinPoutine 3:7b1110501ef9 117 i++;
ShaolinPoutine 3:7b1110501ef9 118 }
ShaolinPoutine 3:7b1110501ef9 119 return true;
ShaolinPoutine 3:7b1110501ef9 120 }
ShaolinPoutine 3:7b1110501ef9 121
ShaolinPoutine 3:7b1110501ef9 122 char msbBooltoChar(bool data[8])
ShaolinPoutine 3:7b1110501ef9 123 {
ShaolinPoutine 3:7b1110501ef9 124 char c = 0;
ShaolinPoutine 3:7b1110501ef9 125 for (int i=0; i < 8 ; i++)
ShaolinPoutine 3:7b1110501ef9 126 if (data[i])
ShaolinPoutine 3:7b1110501ef9 127 c |= 1 << (7 - i);
ShaolinPoutine 3:7b1110501ef9 128 return c;
ShaolinPoutine 3:7b1110501ef9 129 }
ShaolinPoutine 3:7b1110501ef9 130
ShaolinPoutine 3:7b1110501ef9 131 void charToMsbBool(char data, bool out[8])
ShaolinPoutine 3:7b1110501ef9 132 {
ShaolinPoutine 3:7b1110501ef9 133 for (int i=0; i < 8; ++i)
ShaolinPoutine 3:7b1110501ef9 134 out[i] = (data & (1<<(7 - i))) != 0;
ShaolinPoutine 3:7b1110501ef9 135 }
ShaolinPoutine 3:7b1110501ef9 136
ShaolinPoutine 3:7b1110501ef9 137 void PrintBoolArray(bool* data, int len)
ShaolinPoutine 3:7b1110501ef9 138 {
ShaolinPoutine 3:7b1110501ef9 139 pc.printf("[");
ShaolinPoutine 3:7b1110501ef9 140 for (int i = 0; i < len - 1; i++)
ShaolinPoutine 3:7b1110501ef9 141 {
ShaolinPoutine 3:7b1110501ef9 142 pc.printf("%d, ", data[i]);
ShaolinPoutine 3:7b1110501ef9 143 }
ShaolinPoutine 3:7b1110501ef9 144 pc.printf("%d]", data[len-1]);
ShaolinPoutine 3:7b1110501ef9 145 }
ShaolinPoutine 3:7b1110501ef9 146
ShaolinPoutine 3:7b1110501ef9 147 bool ManchesterDecodeBit(bool* data, bool* out) // Tested
ShaolinPoutine 2:1665cd4c922c 148 {
ShaolinPoutine 2:1665cd4c922c 149 if (data[0] == 0 && data[1] == 1)
ShaolinPoutine 2:1665cd4c922c 150 {
ShaolinPoutine 3:7b1110501ef9 151 out[0] = 1;
ShaolinPoutine 2:1665cd4c922c 152 }
ShaolinPoutine 2:1665cd4c922c 153 else if (data[0] == 1 && data[1] == 0)
ShaolinPoutine 2:1665cd4c922c 154 {
ShaolinPoutine 3:7b1110501ef9 155 out[0] = 0;
ShaolinPoutine 2:1665cd4c922c 156 }
ShaolinPoutine 2:1665cd4c922c 157 else
ShaolinPoutine 2:1665cd4c922c 158 {
ShaolinPoutine 2:1665cd4c922c 159 return false;
ShaolinPoutine 2:1665cd4c922c 160 }
ShaolinPoutine 2:1665cd4c922c 161 return true;
ShaolinPoutine 2:1665cd4c922c 162 }
ShaolinPoutine 2:1665cd4c922c 163
ShaolinPoutine 3:7b1110501ef9 164 bool ManchesterDecodeByte(bool* data, char* out) // Tested
ShaolinPoutine 2:1665cd4c922c 165 {
ShaolinPoutine 2:1665cd4c922c 166 bool Valid = true;
ShaolinPoutine 2:1665cd4c922c 167 bool tmp = true;
ShaolinPoutine 2:1665cd4c922c 168 for (int i = 0; i < 8; i++)
ShaolinPoutine 2:1665cd4c922c 169 {
ShaolinPoutine 2:1665cd4c922c 170 Valid = ManchesterDecodeBit(data, &tmp);
ShaolinPoutine 2:1665cd4c922c 171 data += 2;
ShaolinPoutine 3:7b1110501ef9 172 if (!tmp)
ShaolinPoutine 2:1665cd4c922c 173 {
ShaolinPoutine 3:7b1110501ef9 174 *out = *out & ~(0x1 << (7-i));
ShaolinPoutine 2:1665cd4c922c 175 }
ShaolinPoutine 2:1665cd4c922c 176 else
ShaolinPoutine 2:1665cd4c922c 177 {
ShaolinPoutine 3:7b1110501ef9 178 *out = *out | (0x1 << (7-i));
ShaolinPoutine 2:1665cd4c922c 179 }
ShaolinPoutine 2:1665cd4c922c 180 if (!Valid)
ShaolinPoutine 2:1665cd4c922c 181 {
ShaolinPoutine 2:1665cd4c922c 182 return false;
ShaolinPoutine 2:1665cd4c922c 183 }
ShaolinPoutine 2:1665cd4c922c 184 }
ShaolinPoutine 2:1665cd4c922c 185 return true;
ShaolinPoutine 2:1665cd4c922c 186 }
ShaolinPoutine 2:1665cd4c922c 187
ShaolinPoutine 3:7b1110501ef9 188 void ManchesterEncodeBit(bool data, bool* out) // Tested
ShaolinPoutine 2:1665cd4c922c 189 {
ShaolinPoutine 2:1665cd4c922c 190 if (data == 0)
ShaolinPoutine 2:1665cd4c922c 191 {
ShaolinPoutine 2:1665cd4c922c 192 out[0] = 1;
ShaolinPoutine 2:1665cd4c922c 193 out[1] = 0;
ShaolinPoutine 2:1665cd4c922c 194 }
ShaolinPoutine 3:7b1110501ef9 195 else
ShaolinPoutine 3:7b1110501ef9 196 {
ShaolinPoutine 3:7b1110501ef9 197 out[0] = 0;
ShaolinPoutine 3:7b1110501ef9 198 out[1] = 1;
ShaolinPoutine 3:7b1110501ef9 199 }
ShaolinPoutine 3:7b1110501ef9 200 }
ShaolinPoutine 3:7b1110501ef9 201
ShaolinPoutine 3:7b1110501ef9 202 void ManchesterEncodeByte(char data, bool* out) // Tested
ShaolinPoutine 3:7b1110501ef9 203 {
ShaolinPoutine 3:7b1110501ef9 204 for (int i = 0; i < 8; i++)
ShaolinPoutine 3:7b1110501ef9 205 {
ShaolinPoutine 3:7b1110501ef9 206 ManchesterEncodeBit((bool) ((data >> (7 - i)) & 1), &(out[2*i]));
ShaolinPoutine 3:7b1110501ef9 207 }
ShaolinPoutine 2:1665cd4c922c 208 }
ShaolinPoutine 2:1665cd4c922c 209
ShaolinPoutine 3:7b1110501ef9 210 int CountData(char* data) // Tested
ShaolinPoutine 3:7b1110501ef9 211 {
ShaolinPoutine 3:7b1110501ef9 212 int i = 0;
ShaolinPoutine 3:7b1110501ef9 213 while(*data != 0)
ShaolinPoutine 3:7b1110501ef9 214 {
ShaolinPoutine 3:7b1110501ef9 215 data++;
ShaolinPoutine 3:7b1110501ef9 216 i++;
ShaolinPoutine 3:7b1110501ef9 217 }
ShaolinPoutine 3:7b1110501ef9 218 return i;
ShaolinPoutine 3:7b1110501ef9 219 }
ShaolinPoutine 3:7b1110501ef9 220
ShaolinPoutine 3:7b1110501ef9 221 void AddPreambule(bool* data)
ShaolinPoutine 3:7b1110501ef9 222 {
ShaolinPoutine 3:7b1110501ef9 223 for (int i = 0; i < 8; i++)
ShaolinPoutine 3:7b1110501ef9 224 {
ShaolinPoutine 3:7b1110501ef9 225 *data = PREAMBULEBOOL[i];
ShaolinPoutine 3:7b1110501ef9 226 data++;
ShaolinPoutine 3:7b1110501ef9 227 }
ShaolinPoutine 3:7b1110501ef9 228 }
ShaolinPoutine 3:7b1110501ef9 229
ShaolinPoutine 3:7b1110501ef9 230 void AddStartEnd(bool* data)
ShaolinPoutine 2:1665cd4c922c 231 {
ShaolinPoutine 2:1665cd4c922c 232 for (int i = 0; i < 8; i++)
ShaolinPoutine 2:1665cd4c922c 233 {
ShaolinPoutine 3:7b1110501ef9 234 *data = STARTENDBOOL[i];
ShaolinPoutine 3:7b1110501ef9 235 data++;
ShaolinPoutine 2:1665cd4c922c 236 }
ShaolinPoutine 2:1665cd4c922c 237 }
ShaolinPoutine 2:1665cd4c922c 238
ShaolinPoutine 3:7b1110501ef9 239 void AnalyseHeading(char* data, int* id, int* frame, int* frame_count, int* len)
ShaolinPoutine 3:7b1110501ef9 240 {
ShaolinPoutine 3:7b1110501ef9 241 *id = data[0] >> 6;
ShaolinPoutine 3:7b1110501ef9 242 *frame = (data[0] >> 3) & 7;
ShaolinPoutine 3:7b1110501ef9 243 *frame_count = data[0] & 7;
ShaolinPoutine 3:7b1110501ef9 244
ShaolinPoutine 3:7b1110501ef9 245 *len = data[1];
ShaolinPoutine 3:7b1110501ef9 246 }
ShaolinPoutine 3:7b1110501ef9 247
ShaolinPoutine 3:7b1110501ef9 248 bool AddHeading(char* out, int id, int frame, int frame_count, char len) // Tested
ShaolinPoutine 3:7b1110501ef9 249 {
ShaolinPoutine 3:7b1110501ef9 250 char tmp = 0;
ShaolinPoutine 3:7b1110501ef9 251 if (frame > frame_count || frame > 7 || frame_count > 7 || len - 2 > DATA_LEN)
ShaolinPoutine 3:7b1110501ef9 252 {
ShaolinPoutine 3:7b1110501ef9 253 return false;
ShaolinPoutine 3:7b1110501ef9 254 }
ShaolinPoutine 3:7b1110501ef9 255 // Two first bits -> id
ShaolinPoutine 3:7b1110501ef9 256 id = id & 3;
ShaolinPoutine 3:7b1110501ef9 257 tmp = tmp | (id << 6);
ShaolinPoutine 3:7b1110501ef9 258
ShaolinPoutine 3:7b1110501ef9 259 // Three next -> frame number
ShaolinPoutine 3:7b1110501ef9 260 frame = frame & 7;
ShaolinPoutine 3:7b1110501ef9 261 tmp = tmp | (frame << 3);
ShaolinPoutine 3:7b1110501ef9 262
ShaolinPoutine 3:7b1110501ef9 263 // Three last -> number of frames
ShaolinPoutine 3:7b1110501ef9 264 frame_count = frame_count & 7;
ShaolinPoutine 3:7b1110501ef9 265 tmp = tmp | frame_count;
ShaolinPoutine 3:7b1110501ef9 266 *out = tmp;
ShaolinPoutine 3:7b1110501ef9 267
ShaolinPoutine 3:7b1110501ef9 268 // Last char is lenght
ShaolinPoutine 3:7b1110501ef9 269 out++;
ShaolinPoutine 3:7b1110501ef9 270 *out = len;
ShaolinPoutine 3:7b1110501ef9 271 return true;
ShaolinPoutine 3:7b1110501ef9 272 }
ShaolinPoutine 3:7b1110501ef9 273
ShaolinPoutine 3:7b1110501ef9 274 void AddCRC(char* data, int len)
ShaolinPoutine 2:1665cd4c922c 275 {
ShaolinPoutine 2:1665cd4c922c 276
ShaolinPoutine 2:1665cd4c922c 277 }
ShaolinPoutine 3:7b1110501ef9 278 void ReceiveBits(bool* data, int len);
ShaolinPoutine 3:7b1110501ef9 279 void SendBits(bool* bits, int len)
ShaolinPoutine 3:7b1110501ef9 280 {
ShaolinPoutine 4:4277898e55d8 281 // Emile
ShaolinPoutine 4:4277898e55d8 282 // thread.signal_set(0x1);
ShaolinPoutine 3:7b1110501ef9 283 // Synchronize with wire
EmileArseneault 6:3d0735a28b98 284 pc.printf("SendBits - len %d\r\n", len);
EmileArseneault 6:3d0735a28b98 285 pc.printf("Waiting\r\n");
EmileArseneault 6:3d0735a28b98 286 //Thread::signal_wait(1);
EmileArseneault 6:3d0735a28b98 287 ecriture.wait();
ShaolinPoutine 4:4277898e55d8 288 for (int i = 0; i < len; i++)
ShaolinPoutine 4:4277898e55d8 289 {
ShaolinPoutine 4:4277898e55d8 290 envoi.put((int *) bits[i]);
ShaolinPoutine 4:4277898e55d8 291 }
EmileArseneault 5:25101883c27a 292 Push_data->signal_set(0x1);
ShaolinPoutine 4:4277898e55d8 293 //ReceiveBits(bits, len);
ShaolinPoutine 3:7b1110501ef9 294 }
ShaolinPoutine 3:7b1110501ef9 295
ShaolinPoutine 3:7b1110501ef9 296 void SendFormated(bool* data, int len)
ShaolinPoutine 3:7b1110501ef9 297 {
ShaolinPoutine 3:7b1110501ef9 298 pc.printf("SendFormated - len %d\r\n", len);
ShaolinPoutine 3:7b1110501ef9 299 bool tmp[len + 3 * 8];
ShaolinPoutine 3:7b1110501ef9 300 AddPreambule(tmp);
ShaolinPoutine 3:7b1110501ef9 301 AddStartEnd(tmp + 8);
ShaolinPoutine 3:7b1110501ef9 302 for (int i = 0; i < len; i++)
ShaolinPoutine 3:7b1110501ef9 303 {
ShaolinPoutine 3:7b1110501ef9 304 tmp[i + 2 * 8] = data[i];
ShaolinPoutine 3:7b1110501ef9 305 }
EmileArseneault 6:3d0735a28b98 306 AddStartEnd(tmp + len + (2 * 8));
EmileArseneault 6:3d0735a28b98 307 SendBits(tmp, len + (3 * 8));
ShaolinPoutine 3:7b1110501ef9 308 }
ShaolinPoutine 3:7b1110501ef9 309
ShaolinPoutine 3:7b1110501ef9 310 void SendManchester(char* data, int len)
ShaolinPoutine 3:7b1110501ef9 311 {
ShaolinPoutine 3:7b1110501ef9 312 pc.printf("SendManchester - len %d\r\n", len);
ShaolinPoutine 3:7b1110501ef9 313 bool bits[2*len*8];
ShaolinPoutine 3:7b1110501ef9 314 for (int i = 0; i < len; i++)
ShaolinPoutine 3:7b1110501ef9 315 {
ShaolinPoutine 3:7b1110501ef9 316 ManchesterEncodeByte(data[i], bits + 2*i*8);
ShaolinPoutine 3:7b1110501ef9 317 }
ShaolinPoutine 3:7b1110501ef9 318 SendFormated(bits, 2*len*8);
ShaolinPoutine 3:7b1110501ef9 319 }
ShaolinPoutine 3:7b1110501ef9 320
ShaolinPoutine 3:7b1110501ef9 321 int lastIdUsed = 0;
ShaolinPoutine 3:7b1110501ef9 322
ShaolinPoutine 3:7b1110501ef9 323 void SendText(char* data, int len)
ShaolinPoutine 3:7b1110501ef9 324 {
ShaolinPoutine 3:7b1110501ef9 325 pc.printf("SendText - len %d\r\n", len);
ShaolinPoutine 3:7b1110501ef9 326 char tmp[len + 4];
ShaolinPoutine 3:7b1110501ef9 327 int frame, frames;
ShaolinPoutine 3:7b1110501ef9 328 // Gestion des ID
ShaolinPoutine 3:7b1110501ef9 329 if (len > DATA_LEN)
ShaolinPoutine 3:7b1110501ef9 330 {
ShaolinPoutine 3:7b1110501ef9 331 pc.printf("Plz implement multiple trames messages.\r\n");
ShaolinPoutine 3:7b1110501ef9 332 return;
ShaolinPoutine 3:7b1110501ef9 333 }
ShaolinPoutine 3:7b1110501ef9 334 else
ShaolinPoutine 3:7b1110501ef9 335 {
ShaolinPoutine 3:7b1110501ef9 336 frame = 1;
ShaolinPoutine 3:7b1110501ef9 337 frames = 1;
ShaolinPoutine 3:7b1110501ef9 338 // Gestion exception
ShaolinPoutine 3:7b1110501ef9 339 AddHeading(tmp, lastIdUsed, frame, frames, len + 2);
ShaolinPoutine 3:7b1110501ef9 340 for (int i = 0; i < len; i++)
ShaolinPoutine 3:7b1110501ef9 341 {
ShaolinPoutine 3:7b1110501ef9 342 tmp[i + 2] = data[i];
ShaolinPoutine 3:7b1110501ef9 343 }
ShaolinPoutine 3:7b1110501ef9 344 tmp[len + 2] = 0;
ShaolinPoutine 3:7b1110501ef9 345 tmp[len + 3] = 0;
ShaolinPoutine 3:7b1110501ef9 346 CRC16(tmp + 2, len + 2);
ShaolinPoutine 3:7b1110501ef9 347 SendManchester(tmp, len + 4);
ShaolinPoutine 3:7b1110501ef9 348 }
ShaolinPoutine 3:7b1110501ef9 349 }
ShaolinPoutine 3:7b1110501ef9 350
EmileArseneault 6:3d0735a28b98 351 void ReceiveText()
ShaolinPoutine 3:7b1110501ef9 352 {
EmileArseneault 6:3d0735a28b98 353 while (1)
EmileArseneault 6:3d0735a28b98 354 {
EmileArseneault 6:3d0735a28b98 355 osEvent evt = textToPrint.get();
EmileArseneault 6:3d0735a28b98 356 if (evt.status == osEventMail){
EmileArseneault 6:3d0735a28b98 357 led3 = 1;
EmileArseneault 6:3d0735a28b98 358 char * message = (char *) evt.value.p;
EmileArseneault 6:3d0735a28b98 359 pc.printf("Received: %s\r\n", message);
EmileArseneault 6:3d0735a28b98 360 //ReceiveBits((bool *) message->bits, message->len);
EmileArseneault 6:3d0735a28b98 361 textToPrint.free((char (*)[83]) message);
EmileArseneault 6:3d0735a28b98 362 }
EmileArseneault 6:3d0735a28b98 363 }
EmileArseneault 6:3d0735a28b98 364
ShaolinPoutine 3:7b1110501ef9 365 }
ShaolinPoutine 3:7b1110501ef9 366
ShaolinPoutine 3:7b1110501ef9 367 void ReceiveFormated(char* data, int len)
ShaolinPoutine 3:7b1110501ef9 368 {
ShaolinPoutine 3:7b1110501ef9 369 pc.printf("ReceiveFormated - len %d\r\n", len);
EmileArseneault 6:3d0735a28b98 370 //pc.printf("%c%c\r\n",data[2], data[3]);
ShaolinPoutine 3:7b1110501ef9 371 char tmp[len - 2];
ShaolinPoutine 3:7b1110501ef9 372 int id, frame, frames, predicted_len;
ShaolinPoutine 3:7b1110501ef9 373 AnalyseHeading(data, &id, &frame, &frames, &predicted_len);
ShaolinPoutine 3:7b1110501ef9 374 // Gestion des id et frames et frame number
ShaolinPoutine 3:7b1110501ef9 375
ShaolinPoutine 3:7b1110501ef9 376 if (predicted_len != len - 2)
ShaolinPoutine 3:7b1110501ef9 377 {
ShaolinPoutine 3:7b1110501ef9 378 pc.printf("DEBUG -- Received payload with wrong lenght. %d %d\r\n", len - 2, predicted_len);
ShaolinPoutine 3:7b1110501ef9 379 return;
ShaolinPoutine 3:7b1110501ef9 380 }
ShaolinPoutine 3:7b1110501ef9 381 // Copy data
ShaolinPoutine 3:7b1110501ef9 382 for (int i = 0; i < len - 2; i++)
ShaolinPoutine 3:7b1110501ef9 383 {
ShaolinPoutine 3:7b1110501ef9 384 tmp[i] = data[i + 2];
ShaolinPoutine 3:7b1110501ef9 385 }
ShaolinPoutine 3:7b1110501ef9 386 for (int i = len - 4; i < len - 2; i++)
ShaolinPoutine 3:7b1110501ef9 387 {
ShaolinPoutine 3:7b1110501ef9 388 tmp[i] = 0;
ShaolinPoutine 3:7b1110501ef9 389 }
ShaolinPoutine 3:7b1110501ef9 390
ShaolinPoutine 3:7b1110501ef9 391 //CRC calculate
ShaolinPoutine 3:7b1110501ef9 392 CRC16(tmp, len - 2);
ShaolinPoutine 3:7b1110501ef9 393 if (tmp[len - 4] == data[len - 2] && tmp[len - 3] == data[len - 1])
ShaolinPoutine 3:7b1110501ef9 394 {
ShaolinPoutine 3:7b1110501ef9 395 tmp[len - 4] = '\0';
EmileArseneault 6:3d0735a28b98 396 char* toPrint = (char *)textToPrint.alloc();
EmileArseneault 6:3d0735a28b98 397 int i = 0;
EmileArseneault 6:3d0735a28b98 398 for (; i < len - 3; i++)
EmileArseneault 6:3d0735a28b98 399 {
EmileArseneault 6:3d0735a28b98 400 toPrint[i] = tmp[i];
EmileArseneault 6:3d0735a28b98 401 }
EmileArseneault 6:3d0735a28b98 402 toPrint[i] = '\r';
EmileArseneault 6:3d0735a28b98 403 toPrint[i+1] = '\n';
EmileArseneault 6:3d0735a28b98 404 toPrint[i + 2] = '\0';
EmileArseneault 6:3d0735a28b98 405
EmileArseneault 6:3d0735a28b98 406 textToPrint.put((char (*)[83]) toPrint);
ShaolinPoutine 3:7b1110501ef9 407 }
ShaolinPoutine 3:7b1110501ef9 408 else
ShaolinPoutine 3:7b1110501ef9 409 {
ShaolinPoutine 3:7b1110501ef9 410 pc.printf("DEBUG -- Received payload with wrong CRC.\r\n");
ShaolinPoutine 3:7b1110501ef9 411 return;
ShaolinPoutine 3:7b1110501ef9 412 }
ShaolinPoutine 3:7b1110501ef9 413 }
ShaolinPoutine 3:7b1110501ef9 414
ShaolinPoutine 3:7b1110501ef9 415 void ReceiveManchester(bool* data, int len)
ShaolinPoutine 3:7b1110501ef9 416 {
ShaolinPoutine 3:7b1110501ef9 417 pc.printf("ReceiveManchester - len %d\r\n", len);
ShaolinPoutine 3:7b1110501ef9 418 char tmp[len / 16];
ShaolinPoutine 3:7b1110501ef9 419 for (int i = 0; i < len / 16; i++)
ShaolinPoutine 3:7b1110501ef9 420 {
ShaolinPoutine 3:7b1110501ef9 421 ManchesterDecodeByte(data, tmp + i);
ShaolinPoutine 3:7b1110501ef9 422 data += 16;
ShaolinPoutine 3:7b1110501ef9 423 }
ShaolinPoutine 3:7b1110501ef9 424 ReceiveFormated(tmp, len / 16);
ShaolinPoutine 3:7b1110501ef9 425 }
ShaolinPoutine 3:7b1110501ef9 426
ShaolinPoutine 3:7b1110501ef9 427 void ReceiveBits(bool* data, int len)
ShaolinPoutine 4:4277898e55d8 428 {
EmileArseneault 6:3d0735a28b98 429 /*
ShaolinPoutine 3:7b1110501ef9 430 if (compareBoolArray(data, PREAMBULEBOOL))
ShaolinPoutine 3:7b1110501ef9 431 {
ShaolinPoutine 3:7b1110501ef9 432 data += 8;
ShaolinPoutine 3:7b1110501ef9 433 len -= 8;
ShaolinPoutine 3:7b1110501ef9 434 }
ShaolinPoutine 3:7b1110501ef9 435 else
ShaolinPoutine 3:7b1110501ef9 436 {
ShaolinPoutine 3:7b1110501ef9 437 pc.printf("DEBUG -- Received payload with wrong Preambule.");PrintBoolArray(data , 8);pc.printf("\r\n");
ShaolinPoutine 3:7b1110501ef9 438 return;
EmileArseneault 6:3d0735a28b98 439 }*/
ShaolinPoutine 3:7b1110501ef9 440
ShaolinPoutine 3:7b1110501ef9 441 // Ensure Start and end are present
ShaolinPoutine 3:7b1110501ef9 442 if (compareBoolArray(data, STARTENDBOOL) || compareBoolArray(data + (len - 8), STARTENDBOOL))
ShaolinPoutine 3:7b1110501ef9 443 {
ShaolinPoutine 3:7b1110501ef9 444 ReceiveManchester(data + 8, len - 2 * 8);
ShaolinPoutine 3:7b1110501ef9 445 }
ShaolinPoutine 3:7b1110501ef9 446 else
ShaolinPoutine 3:7b1110501ef9 447 {
ShaolinPoutine 3:7b1110501ef9 448 pc.printf("DEBUG -- Received payload with wrong Start or End.\r\n");
ShaolinPoutine 3:7b1110501ef9 449 return;
ShaolinPoutine 3:7b1110501ef9 450 }
ShaolinPoutine 3:7b1110501ef9 451 }
ShaolinPoutine 2:1665cd4c922c 452
ShaolinPoutine 4:4277898e55d8 453 void Listening()
ShaolinPoutine 4:4277898e55d8 454 {
ShaolinPoutine 4:4277898e55d8 455 while (1)
ShaolinPoutine 4:4277898e55d8 456 {
ShaolinPoutine 4:4277898e55d8 457 Thread::signal_wait(0x1);
ShaolinPoutine 4:4277898e55d8 458 osEvent evt = envoi.get();
ShaolinPoutine 4:4277898e55d8 459 if (evt.status == osEventMessage){
ShaolinPoutine 4:4277898e55d8 460 /*bool message = (message_t*) evt.value.p;
ShaolinPoutine 4:4277898e55d8 461 //ReceiveBits((bool *) message->bits, message->len);
ShaolinPoutine 4:4277898e55d8 462 envoi.free(message);*/
ShaolinPoutine 4:4277898e55d8 463 }
ShaolinPoutine 4:4277898e55d8 464 }
ShaolinPoutine 4:4277898e55d8 465
ShaolinPoutine 4:4277898e55d8 466 }
ShaolinPoutine 4:4277898e55d8 467
ShaolinPoutine 1:8b93b2102ac5 468 extern "C" void TIMER2_IRQHandler (void)
ShaolinPoutine 1:8b93b2102ac5 469 {
EmileArseneault 5:25101883c27a 470 if((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
ShaolinPoutine 1:8b93b2102ac5 471 {
ShaolinPoutine 1:8b93b2102ac5 472 LPC_TIM2->IR |= 1 << 0; // Clear MR0 interrupt flag
EmileArseneault 5:25101883c27a 473 //myled = !myled; // Toggle LED1
EmileArseneault 5:25101883c27a 474 Push_data->signal_set(2); // Envoyer signal au coup de clock pour l'écriture de bit
ShaolinPoutine 0:529210499c6d 475 }
ShaolinPoutine 0:529210499c6d 476 }
EmileArseneault 5:25101883c27a 477
EmileArseneault 5:25101883c27a 478 void timer2_init(void)
EmileArseneault 5:25101883c27a 479 {
EmileArseneault 5:25101883c27a 480 LPC_SC->PCLKSEL1 |=1<<12; //pclk = cclk timer2
EmileArseneault 5:25101883c27a 481 LPC_SC->PCONP |=1<<22; //timer2 power on
EmileArseneault 6:3d0735a28b98 482 LPC_TIM2->MR0 = 960000; //10 msec
EmileArseneault 5:25101883c27a 483 LPC_TIM2->MCR = 3; //interrupt and reset control
EmileArseneault 5:25101883c27a 484 //3 = Interrupt & reset timer2 on match
EmileArseneault 5:25101883c27a 485 //1 = Interrupt only, no reset of timer0
EmileArseneault 5:25101883c27a 486 LPC_TIM2->EMR =3<<4; //EMC0 = 11 (Toogle)
EmileArseneault 5:25101883c27a 487 NVIC_EnableIRQ(TIMER2_IRQn); //enable timer2 interrupt
EmileArseneault 5:25101883c27a 488 LPC_TIM2->TCR = 1; //enable Timer2
EmileArseneault 5:25101883c27a 489 }
ShaolinPoutine 1:8b93b2102ac5 490
EmileArseneault 5:25101883c27a 491 void SendData()
ShaolinPoutine 1:8b93b2102ac5 492 {
EmileArseneault 6:3d0735a28b98 493 bool releaseSent = false;
EmileArseneault 5:25101883c27a 494 bool fin = false;
EmileArseneault 5:25101883c27a 495 int valeur = 0;
EmileArseneault 5:25101883c27a 496
EmileArseneault 5:25101883c27a 497 while(true)
EmileArseneault 5:25101883c27a 498 {
EmileArseneault 6:3d0735a28b98 499 //osSignalSet(mainThread, 1);
EmileArseneault 6:3d0735a28b98 500 if (!releaseSent)
EmileArseneault 6:3d0735a28b98 501 {
EmileArseneault 6:3d0735a28b98 502 pc.printf("Release sent\r\n");
EmileArseneault 6:3d0735a28b98 503 ecriture.release();
EmileArseneault 6:3d0735a28b98 504 releaseSent = true;
EmileArseneault 6:3d0735a28b98 505 }
EmileArseneault 6:3d0735a28b98 506 //pc.printf("SendData Waiting\r\n");
EmileArseneault 6:3d0735a28b98 507
EmileArseneault 6:3d0735a28b98 508 //Thread::signal_wait(1);
EmileArseneault 5:25101883c27a 509
EmileArseneault 5:25101883c27a 510 while(!fin)
EmileArseneault 5:25101883c27a 511 {
EmileArseneault 6:3d0735a28b98 512 osEvent evt = envoi.get(1000);
EmileArseneault 5:25101883c27a 513 if (evt.status == osEventMessage)
EmileArseneault 5:25101883c27a 514 {
EmileArseneault 5:25101883c27a 515 valeur = (int)evt.value.p;
EmileArseneault 5:25101883c27a 516 Thread::signal_wait(2);
EmileArseneault 5:25101883c27a 517 tX = valeur;
EmileArseneault 6:3d0735a28b98 518 releaseSent = false;
EmileArseneault 5:25101883c27a 519 }
EmileArseneault 5:25101883c27a 520 else
EmileArseneault 5:25101883c27a 521 {
EmileArseneault 5:25101883c27a 522 fin = true;
EmileArseneault 6:3d0735a28b98 523 tX = 1;
EmileArseneault 5:25101883c27a 524 }
EmileArseneault 5:25101883c27a 525
EmileArseneault 5:25101883c27a 526 }
EmileArseneault 5:25101883c27a 527 fin = false;
EmileArseneault 5:25101883c27a 528 }
ShaolinPoutine 1:8b93b2102ac5 529 }
ShaolinPoutine 3:7b1110501ef9 530
EmileArseneault 6:3d0735a28b98 531 void StartEnd(int intervale)
EmileArseneault 6:3d0735a28b98 532 {
EmileArseneault 6:3d0735a28b98 533 bool End = false;
EmileArseneault 6:3d0735a28b98 534 bool Start = false;
EmileArseneault 6:3d0735a28b98 535 bool data[1500] = {0};
EmileArseneault 6:3d0735a28b98 536 int i = 0;
EmileArseneault 6:3d0735a28b98 537 bool ones[8] = {1, 1, 1, 1, 1, 1, 1, 1};
EmileArseneault 6:3d0735a28b98 538
EmileArseneault 6:3d0735a28b98 539 while(!End)
EmileArseneault 6:3d0735a28b98 540 {
EmileArseneault 6:3d0735a28b98 541 wait_us(intervale);
EmileArseneault 6:3d0735a28b98 542 data[i] = rX.read();
EmileArseneault 6:3d0735a28b98 543 led2 = data[i];
EmileArseneault 6:3d0735a28b98 544 // Regarde si 3 valeurs consécutives sont 0
EmileArseneault 6:3d0735a28b98 545 if (i >= 2)
EmileArseneault 6:3d0735a28b98 546 {
EmileArseneault 6:3d0735a28b98 547 if ((data[i-2] == data[i-1]) && (data[i] == data[i-1]) && (data[i] == 0))
EmileArseneault 6:3d0735a28b98 548 {
EmileArseneault 6:3d0735a28b98 549 printf("Ligne coupee\r\n");
EmileArseneault 6:3d0735a28b98 550 return;
EmileArseneault 6:3d0735a28b98 551 }
EmileArseneault 6:3d0735a28b98 552 }
EmileArseneault 6:3d0735a28b98 553
EmileArseneault 6:3d0735a28b98 554 // Comparer pour les séquence de bits de fin et start
EmileArseneault 6:3d0735a28b98 555 if (i >= 7)
EmileArseneault 6:3d0735a28b98 556 {
EmileArseneault 6:3d0735a28b98 557 if (compareBoolArray(ones, &(data[i-7])))
EmileArseneault 6:3d0735a28b98 558 {
EmileArseneault 6:3d0735a28b98 559 printf("Court_circuit\r\n");
EmileArseneault 6:3d0735a28b98 560 return;
EmileArseneault 6:3d0735a28b98 561 }
EmileArseneault 6:3d0735a28b98 562 if (compareBoolArray(STARTENDBOOL, &(data[i-7])))
EmileArseneault 6:3d0735a28b98 563 {
EmileArseneault 6:3d0735a28b98 564 if (!Start)
EmileArseneault 6:3d0735a28b98 565 {
EmileArseneault 6:3d0735a28b98 566 Start = true;
EmileArseneault 6:3d0735a28b98 567 }
EmileArseneault 6:3d0735a28b98 568 else
EmileArseneault 6:3d0735a28b98 569 {
EmileArseneault 6:3d0735a28b98 570 ReceiveBits(data, i+1);
EmileArseneault 6:3d0735a28b98 571 End = true;
EmileArseneault 6:3d0735a28b98 572 }
EmileArseneault 6:3d0735a28b98 573 }
EmileArseneault 6:3d0735a28b98 574 }
EmileArseneault 6:3d0735a28b98 575 i++;
EmileArseneault 6:3d0735a28b98 576 }
EmileArseneault 6:3d0735a28b98 577 }
EmileArseneault 6:3d0735a28b98 578
EmileArseneault 6:3d0735a28b98 579 void Preambule_Check(void const *args)
EmileArseneault 6:3d0735a28b98 580 {
EmileArseneault 6:3d0735a28b98 581 int position = 1;
EmileArseneault 6:3d0735a28b98 582 int valeur = 0;
EmileArseneault 6:3d0735a28b98 583 int preambule[8] = {0, 1, 0, 1, 0, 1, 0, 1};
EmileArseneault 6:3d0735a28b98 584 int timeBetween[6] = {0};
EmileArseneault 6:3d0735a28b98 585 int sum;
EmileArseneault 6:3d0735a28b98 586 int intervale = 0;
EmileArseneault 6:3d0735a28b98 587 Timer t;
EmileArseneault 6:3d0735a28b98 588
EmileArseneault 6:3d0735a28b98 589 while(true)
EmileArseneault 6:3d0735a28b98 590 {
EmileArseneault 6:3d0735a28b98 591 osEvent evt = rise_n_fall_queue.get();
EmileArseneault 6:3d0735a28b98 592 if (evt.status == osEventMessage)
EmileArseneault 6:3d0735a28b98 593 {
EmileArseneault 6:3d0735a28b98 594 valeur = (int)evt.value.p;
EmileArseneault 6:3d0735a28b98 595 if (valeur == preambule[position])
EmileArseneault 6:3d0735a28b98 596 {
EmileArseneault 6:3d0735a28b98 597 if (position > 1)
EmileArseneault 6:3d0735a28b98 598 {
EmileArseneault 6:3d0735a28b98 599 t.stop();
EmileArseneault 6:3d0735a28b98 600 timeBetween[position - 2] = t.read_us();
EmileArseneault 6:3d0735a28b98 601 t.reset();
EmileArseneault 6:3d0735a28b98 602 t.start();
EmileArseneault 6:3d0735a28b98 603 }
EmileArseneault 6:3d0735a28b98 604 else
EmileArseneault 6:3d0735a28b98 605 {
EmileArseneault 6:3d0735a28b98 606 t.stop();
EmileArseneault 6:3d0735a28b98 607 t.reset();
EmileArseneault 6:3d0735a28b98 608 t.start();
EmileArseneault 6:3d0735a28b98 609 }
EmileArseneault 6:3d0735a28b98 610
EmileArseneault 6:3d0735a28b98 611 if (position < 7)
EmileArseneault 6:3d0735a28b98 612 {
EmileArseneault 6:3d0735a28b98 613 position++;
EmileArseneault 6:3d0735a28b98 614 }
EmileArseneault 6:3d0735a28b98 615 else
EmileArseneault 6:3d0735a28b98 616 {
EmileArseneault 6:3d0735a28b98 617 // Calculer la moyenne (pt oter les valeurs extrèmes s'il y en a)
EmileArseneault 6:3d0735a28b98 618 sum = 0;
EmileArseneault 6:3d0735a28b98 619 for (int i =0; i < 6; i++)
EmileArseneault 6:3d0735a28b98 620 {
EmileArseneault 6:3d0735a28b98 621 sum += timeBetween[i];
EmileArseneault 6:3d0735a28b98 622 }
EmileArseneault 6:3d0735a28b98 623 intervale = sum/6;
EmileArseneault 6:3d0735a28b98 624 StartEnd(intervale);
EmileArseneault 6:3d0735a28b98 625 wait(0.5);
EmileArseneault 6:3d0735a28b98 626 position = 1;
EmileArseneault 6:3d0735a28b98 627 led4 = !led4;
EmileArseneault 6:3d0735a28b98 628 }
EmileArseneault 6:3d0735a28b98 629 }
EmileArseneault 6:3d0735a28b98 630 else
EmileArseneault 6:3d0735a28b98 631 {
EmileArseneault 6:3d0735a28b98 632 position = 1;
EmileArseneault 6:3d0735a28b98 633 }
EmileArseneault 6:3d0735a28b98 634 }
EmileArseneault 6:3d0735a28b98 635 }
EmileArseneault 6:3d0735a28b98 636 }
EmileArseneault 6:3d0735a28b98 637
EmileArseneault 6:3d0735a28b98 638 void Call_Preambule_Rise()
EmileArseneault 6:3d0735a28b98 639 {
EmileArseneault 6:3d0735a28b98 640 rise_n_fall_queue.put((int *)1, 0);
EmileArseneault 6:3d0735a28b98 641 }
EmileArseneault 6:3d0735a28b98 642
EmileArseneault 6:3d0735a28b98 643 void Call_Preambule_Fall()
EmileArseneault 6:3d0735a28b98 644 {
EmileArseneault 6:3d0735a28b98 645 rise_n_fall_queue.put((int *)0, 0);
EmileArseneault 6:3d0735a28b98 646 }
EmileArseneault 6:3d0735a28b98 647
ShaolinPoutine 2:1665cd4c922c 648 void TestManchester();
ShaolinPoutine 3:7b1110501ef9 649 void TestUtility();
ShaolinPoutine 2:1665cd4c922c 650 void mainRaph()
ShaolinPoutine 1:8b93b2102ac5 651 {
ShaolinPoutine 4:4277898e55d8 652 //TestUtility();
ShaolinPoutine 4:4277898e55d8 653 // TestManchester();
EmileArseneault 6:3d0735a28b98 654 //SendText("A more complex sentence to prove our algorithm is robust!", 57);
EmileArseneault 6:3d0735a28b98 655 //wait(10);
EmileArseneault 6:3d0735a28b98 656 tX = 0;
EmileArseneault 6:3d0735a28b98 657 wait_us(100);
EmileArseneault 6:3d0735a28b98 658 tX= 1;
EmileArseneault 6:3d0735a28b98 659 wait_us(100);
EmileArseneault 6:3d0735a28b98 660 tX = 0;
EmileArseneault 6:3d0735a28b98 661 //SendText("OK", 2);
EmileArseneault 6:3d0735a28b98 662 //wait(4);
EmileArseneault 6:3d0735a28b98 663 //SendText("OK", 2);
EmileArseneault 6:3d0735a28b98 664 //wait(1);
EmileArseneault 6:3d0735a28b98 665 //SendText("A more complex sentence to prove our algorithm is robust!", 57);
ShaolinPoutine 1:8b93b2102ac5 666 }
ShaolinPoutine 1:8b93b2102ac5 667
ShaolinPoutine 1:8b93b2102ac5 668 void tick()
ShaolinPoutine 1:8b93b2102ac5 669 {
ShaolinPoutine 3:7b1110501ef9 670 myled = !myled;
ShaolinPoutine 1:8b93b2102ac5 671 }
ShaolinPoutine 1:8b93b2102ac5 672 int main() {
ShaolinPoutine 1:8b93b2102ac5 673 Ticker ticker;
ShaolinPoutine 1:8b93b2102ac5 674 ticker.attach(&tick, 0.5);
EmileArseneault 5:25101883c27a 675 timer2_init();
EmileArseneault 6:3d0735a28b98 676 mainThread = osThreadGetId();
EmileArseneault 5:25101883c27a 677
ShaolinPoutine 4:4277898e55d8 678 listeningThread = new Thread();
ShaolinPoutine 4:4277898e55d8 679 listeningThread->start(Listening);
ShaolinPoutine 4:4277898e55d8 680
EmileArseneault 6:3d0735a28b98 681 Thread get_data_thread(Preambule_Check);
EmileArseneault 5:25101883c27a 682 Thread push_thread(SendData);
EmileArseneault 6:3d0735a28b98 683 Thread printing(ReceiveText);
EmileArseneault 5:25101883c27a 684 Push_data = &push_thread;
EmileArseneault 5:25101883c27a 685
EmileArseneault 6:3d0735a28b98 686 rX.rise(Call_Preambule_Rise);
EmileArseneault 6:3d0735a28b98 687 rX.fall(Call_Preambule_Fall);
EmileArseneault 6:3d0735a28b98 688
ShaolinPoutine 2:1665cd4c922c 689 mainRaph();
EmileArseneault 6:3d0735a28b98 690 led3 = 1;
EmileArseneault 6:3d0735a28b98 691 while (1){
EmileArseneault 6:3d0735a28b98 692 }
ShaolinPoutine 1:8b93b2102ac5 693 }
ShaolinPoutine 2:1665cd4c922c 694
ShaolinPoutine 2:1665cd4c922c 695 //--------------------------------TEST---------------------------------------------------//
ShaolinPoutine 2:1665cd4c922c 696
ShaolinPoutine 3:7b1110501ef9 697 char ToByte(bool b[8])
ShaolinPoutine 3:7b1110501ef9 698 {
ShaolinPoutine 3:7b1110501ef9 699 char c = 0;
ShaolinPoutine 3:7b1110501ef9 700 for (int i=0; i < 8; ++i)
ShaolinPoutine 3:7b1110501ef9 701 if (b[i])
ShaolinPoutine 3:7b1110501ef9 702 c |= 1 << i;
ShaolinPoutine 3:7b1110501ef9 703 return c;
ShaolinPoutine 3:7b1110501ef9 704 }
ShaolinPoutine 3:7b1110501ef9 705
ShaolinPoutine 3:7b1110501ef9 706 void FromByte(char c, bool b[8])
ShaolinPoutine 3:7b1110501ef9 707 {
ShaolinPoutine 3:7b1110501ef9 708 for (int i=0; i < 8; ++i)
ShaolinPoutine 3:7b1110501ef9 709 b[i] = (c & (1<<i)) != 0;
ShaolinPoutine 3:7b1110501ef9 710 }
ShaolinPoutine 3:7b1110501ef9 711
ShaolinPoutine 3:7b1110501ef9 712 void TestUtility()
ShaolinPoutine 3:7b1110501ef9 713 {
ShaolinPoutine 3:7b1110501ef9 714 pc.printf("TestUtility -- Begin\r\n");
ShaolinPoutine 3:7b1110501ef9 715 char charValue = 1;
ShaolinPoutine 3:7b1110501ef9 716 bool boolValue[8] = {0,0,0,0,0,0,0,1};
ShaolinPoutine 3:7b1110501ef9 717 bool boolAns[8] = {0,0,0,0,0,0,0,1};
ShaolinPoutine 3:7b1110501ef9 718 char charAns;
ShaolinPoutine 3:7b1110501ef9 719
ShaolinPoutine 3:7b1110501ef9 720 if (!compareBoolArray(boolValue, boolAns))
ShaolinPoutine 3:7b1110501ef9 721 {
ShaolinPoutine 3:7b1110501ef9 722 pc.printf("Should be gud");
ShaolinPoutine 3:7b1110501ef9 723 return;
ShaolinPoutine 3:7b1110501ef9 724 }
ShaolinPoutine 3:7b1110501ef9 725 boolAns[7] = 0;
ShaolinPoutine 3:7b1110501ef9 726 if (compareBoolArray(boolValue, boolAns))
ShaolinPoutine 3:7b1110501ef9 727 {
ShaolinPoutine 3:7b1110501ef9 728 pc.printf("Should not be gud");
ShaolinPoutine 3:7b1110501ef9 729 return;
ShaolinPoutine 3:7b1110501ef9 730 }
ShaolinPoutine 3:7b1110501ef9 731
ShaolinPoutine 3:7b1110501ef9 732 charAns = msbBooltoChar(boolValue);
ShaolinPoutine 3:7b1110501ef9 733
ShaolinPoutine 3:7b1110501ef9 734 if (charValue != charAns)
ShaolinPoutine 3:7b1110501ef9 735 {
ShaolinPoutine 3:7b1110501ef9 736 pc.printf("msbBooltoChar error. %d %d\r\n", charValue, charAns);
ShaolinPoutine 3:7b1110501ef9 737 return;
ShaolinPoutine 3:7b1110501ef9 738 }
ShaolinPoutine 3:7b1110501ef9 739
ShaolinPoutine 3:7b1110501ef9 740 charToMsbBool(charValue, boolAns);
ShaolinPoutine 3:7b1110501ef9 741
ShaolinPoutine 3:7b1110501ef9 742 if(!compareBoolArray(boolValue, boolAns))
ShaolinPoutine 3:7b1110501ef9 743 {
ShaolinPoutine 3:7b1110501ef9 744 pc.printf("charToMsbBool error ");PrintBoolArray(boolValue,8);pc.printf(" ");PrintBoolArray(boolAns,8);pc.printf("\r\n");
ShaolinPoutine 3:7b1110501ef9 745 return;
ShaolinPoutine 3:7b1110501ef9 746 }
ShaolinPoutine 3:7b1110501ef9 747 pc.printf("TestUtility -- End\r\n");
ShaolinPoutine 3:7b1110501ef9 748 }
ShaolinPoutine 3:7b1110501ef9 749
ShaolinPoutine 2:1665cd4c922c 750 void TestManchester()
ShaolinPoutine 2:1665cd4c922c 751 {
ShaolinPoutine 3:7b1110501ef9 752 pc.printf("TestManchester - Begin\r\n");
ShaolinPoutine 2:1665cd4c922c 753
ShaolinPoutine 2:1665cd4c922c 754 // ----------------------------ENCODE-----------------------------------------------//
ShaolinPoutine 2:1665cd4c922c 755 bool Valid = true;
ShaolinPoutine 2:1665cd4c922c 756 bool boolData0 = (char) 0b0;
ShaolinPoutine 3:7b1110501ef9 757 bool boolExpected0[] = {1,0};
ShaolinPoutine 2:1665cd4c922c 758 bool boolAns0[2];
ShaolinPoutine 2:1665cd4c922c 759
ShaolinPoutine 3:7b1110501ef9 760 ManchesterEncodeBit(boolData0, (bool *) boolAns0);
ShaolinPoutine 2:1665cd4c922c 761
ShaolinPoutine 2:1665cd4c922c 762 int i = 0;
ShaolinPoutine 2:1665cd4c922c 763 Valid = true;
ShaolinPoutine 2:1665cd4c922c 764 while (Valid && i < 2)
ShaolinPoutine 2:1665cd4c922c 765 {
ShaolinPoutine 2:1665cd4c922c 766 Valid = !(boolExpected0[i] ^ boolAns0[i]);
ShaolinPoutine 2:1665cd4c922c 767 i++;
ShaolinPoutine 2:1665cd4c922c 768 }
ShaolinPoutine 2:1665cd4c922c 769
ShaolinPoutine 2:1665cd4c922c 770 if (!Valid)
ShaolinPoutine 2:1665cd4c922c 771 {
ShaolinPoutine 3:7b1110501ef9 772 pc.printf("TestManchester - encode - bool0 - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 773 pc.printf("Expected: [%d, %d]. Received: [%d, %d].\r\n\n", boolExpected0[0], boolExpected0[1], boolAns0[0], boolAns0[1]);
ShaolinPoutine 2:1665cd4c922c 774 }
ShaolinPoutine 2:1665cd4c922c 775
ShaolinPoutine 2:1665cd4c922c 776 bool boolData1 = (char) 0b1;
ShaolinPoutine 3:7b1110501ef9 777 bool boolExpected1[] = {0,1};
ShaolinPoutine 2:1665cd4c922c 778 bool boolAns1[2];
ShaolinPoutine 3:7b1110501ef9 779 ManchesterEncodeBit(boolData1, (bool*) boolAns1);
ShaolinPoutine 2:1665cd4c922c 780
ShaolinPoutine 2:1665cd4c922c 781 i = 0;
ShaolinPoutine 2:1665cd4c922c 782 Valid = true;
ShaolinPoutine 2:1665cd4c922c 783 while (Valid && i < 2)
ShaolinPoutine 2:1665cd4c922c 784 {
ShaolinPoutine 2:1665cd4c922c 785 Valid = !(boolExpected1[i] ^ boolAns1[i]);
ShaolinPoutine 2:1665cd4c922c 786 i++;
ShaolinPoutine 2:1665cd4c922c 787 }
ShaolinPoutine 2:1665cd4c922c 788 if (!Valid)
ShaolinPoutine 2:1665cd4c922c 789 {
ShaolinPoutine 3:7b1110501ef9 790 pc.printf("TestManchester - encode - boo11 - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 791 pc.printf("Expected: [%d, %d]. Received: [%d, %d].\r\n\n", boolExpected1[0], boolExpected1[1], boolAns1[0], boolAns1[1]);
ShaolinPoutine 2:1665cd4c922c 792 }
ShaolinPoutine 2:1665cd4c922c 793
ShaolinPoutine 2:1665cd4c922c 794 char charData = (char) 0b01010101;
ShaolinPoutine 3:7b1110501ef9 795 bool charExpected[] = {1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1};
ShaolinPoutine 2:1665cd4c922c 796 bool charAns[16];
ShaolinPoutine 3:7b1110501ef9 797 ManchesterEncodeByte(charData, (bool*) charAns);
ShaolinPoutine 2:1665cd4c922c 798
ShaolinPoutine 2:1665cd4c922c 799 i = 0;
ShaolinPoutine 2:1665cd4c922c 800 Valid = true;
ShaolinPoutine 2:1665cd4c922c 801 while (Valid && i < 16)
ShaolinPoutine 2:1665cd4c922c 802 {
ShaolinPoutine 2:1665cd4c922c 803 Valid = !(charExpected[i] ^ charAns[i]);
ShaolinPoutine 2:1665cd4c922c 804 i++;
ShaolinPoutine 2:1665cd4c922c 805 }
ShaolinPoutine 2:1665cd4c922c 806 if (!Valid)
ShaolinPoutine 2:1665cd4c922c 807 {
ShaolinPoutine 3:7b1110501ef9 808 pc.printf("TestManchester - encode - char - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 809 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 810 charExpected[0], charExpected[1], charExpected[2], charExpected[3],
ShaolinPoutine 2:1665cd4c922c 811 charExpected[4], charExpected[5], charExpected[6], charExpected[7],
ShaolinPoutine 2:1665cd4c922c 812 charExpected[8], charExpected[9], charExpected[10], charExpected[11],
ShaolinPoutine 2:1665cd4c922c 813 charExpected[12], charExpected[13], charExpected[14], charExpected[15],
ShaolinPoutine 2:1665cd4c922c 814 charAns[0], charAns[1], charAns[2], charAns[3],
ShaolinPoutine 2:1665cd4c922c 815 charAns[4], charAns[5], charAns[6], charAns[7],
ShaolinPoutine 2:1665cd4c922c 816 charAns[8], charAns[9], charAns[10], charAns[11],
ShaolinPoutine 2:1665cd4c922c 817 charAns[12], charAns[13], charAns[14], charAns[15]);
ShaolinPoutine 2:1665cd4c922c 818 }
ShaolinPoutine 2:1665cd4c922c 819
ShaolinPoutine 2:1665cd4c922c 820 // ----------------------------DECODE-----------------------------------------------//
ShaolinPoutine 2:1665cd4c922c 821 Valid = true;
ShaolinPoutine 3:7b1110501ef9 822 bool boolDecodeData0[] = {1,0};
ShaolinPoutine 2:1665cd4c922c 823 bool boolDecodeExpected0 = 0b0;
ShaolinPoutine 2:1665cd4c922c 824 bool boolDecodeAns0;
ShaolinPoutine 2:1665cd4c922c 825 bool Success = true;
ShaolinPoutine 2:1665cd4c922c 826 Success = ManchesterDecodeBit((bool *)boolDecodeData0, &boolDecodeAns0);
ShaolinPoutine 2:1665cd4c922c 827
ShaolinPoutine 2:1665cd4c922c 828 if (!Success)
ShaolinPoutine 2:1665cd4c922c 829 {
ShaolinPoutine 3:7b1110501ef9 830 pc.printf("TestManchester - decode - bool0 - Invalid format\r\n");
ShaolinPoutine 2:1665cd4c922c 831 }
ShaolinPoutine 2:1665cd4c922c 832
ShaolinPoutine 2:1665cd4c922c 833 if (boolDecodeExpected0 != boolDecodeAns0)
ShaolinPoutine 2:1665cd4c922c 834 {
ShaolinPoutine 3:7b1110501ef9 835 pc.printf("TestManchester - decode - bool0 - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 836 pc.printf("Expected: %d. Received: %d.\r\n\n", boolDecodeExpected0, boolDecodeAns0);
ShaolinPoutine 2:1665cd4c922c 837 }
ShaolinPoutine 2:1665cd4c922c 838
ShaolinPoutine 3:7b1110501ef9 839 bool boolDecodeData1[] = {0,1};
ShaolinPoutine 2:1665cd4c922c 840 bool boolDecodeExpected1 = (char) 0b1;
ShaolinPoutine 2:1665cd4c922c 841 bool boolDecodeAns1;
ShaolinPoutine 2:1665cd4c922c 842 Success = ManchesterDecodeBit((bool *)boolDecodeData1, &boolDecodeAns1);
ShaolinPoutine 2:1665cd4c922c 843
ShaolinPoutine 2:1665cd4c922c 844 if (!Success)
ShaolinPoutine 2:1665cd4c922c 845 {
ShaolinPoutine 3:7b1110501ef9 846 pc.printf("TestManchester - decode - bool1 - Invalid format\r\n");
ShaolinPoutine 2:1665cd4c922c 847 }
ShaolinPoutine 2:1665cd4c922c 848 if (boolDecodeExpected1 != boolDecodeAns1)
ShaolinPoutine 2:1665cd4c922c 849 {
ShaolinPoutine 3:7b1110501ef9 850 pc.printf("TestManchester - decode - boo11 - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 851 pc.printf("Expected: %d. Received: %d\r\n\n", boolDecodeExpected1, boolDecodeAns1);
ShaolinPoutine 2:1665cd4c922c 852 }
ShaolinPoutine 2:1665cd4c922c 853
ShaolinPoutine 2:1665cd4c922c 854 bool boolDataInv0[] = {0,0};
ShaolinPoutine 3:7b1110501ef9 855 bool boolDataInv1[] = {1,1};
ShaolinPoutine 2:1665cd4c922c 856 bool boolAnsInv;
ShaolinPoutine 2:1665cd4c922c 857 Success = ManchesterDecodeBit((bool *)boolDataInv0, (bool*) boolAnsInv);
ShaolinPoutine 2:1665cd4c922c 858 if (Success)
ShaolinPoutine 2:1665cd4c922c 859 {
ShaolinPoutine 3:7b1110501ef9 860 pc.printf("TestManchester - decode - boolInv0 - Invalid format not caught\r\n");
ShaolinPoutine 2:1665cd4c922c 861 }
ShaolinPoutine 2:1665cd4c922c 862 Success = ManchesterDecodeBit((bool *)boolDataInv1, (bool*) boolAnsInv);
ShaolinPoutine 2:1665cd4c922c 863 if (Success)
ShaolinPoutine 2:1665cd4c922c 864 {
ShaolinPoutine 3:7b1110501ef9 865 pc.printf("TestManchester - decode - boolInv1 - Invalid format not caught\r\n");
ShaolinPoutine 2:1665cd4c922c 866 }
ShaolinPoutine 2:1665cd4c922c 867
ShaolinPoutine 2:1665cd4c922c 868
ShaolinPoutine 3:7b1110501ef9 869 bool charEncodeData[] = {1,0,1,0,0,1,1,0,0,1,1,0,0,1,1,0};
ShaolinPoutine 2:1665cd4c922c 870 char charEncodeExpected = 42;
ShaolinPoutine 2:1665cd4c922c 871 char charEncodeAns;
ShaolinPoutine 2:1665cd4c922c 872 Valid = ManchesterDecodeByte(charEncodeData, &charEncodeAns);
ShaolinPoutine 2:1665cd4c922c 873
ShaolinPoutine 2:1665cd4c922c 874 if (!Valid)
ShaolinPoutine 2:1665cd4c922c 875 {
ShaolinPoutine 3:7b1110501ef9 876 pc.printf("Function detected error in format\r\n");
ShaolinPoutine 2:1665cd4c922c 877 }
ShaolinPoutine 2:1665cd4c922c 878 else if (charEncodeExpected != charEncodeAns)
ShaolinPoutine 2:1665cd4c922c 879 {
ShaolinPoutine 3:7b1110501ef9 880 pc.printf("TestManchester - decode - char - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 881 pc.printf("Expected: %d\t\tReceived: %d\r\n",
ShaolinPoutine 2:1665cd4c922c 882 charEncodeExpected, charEncodeAns);
ShaolinPoutine 2:1665cd4c922c 883 }
ShaolinPoutine 2:1665cd4c922c 884
ShaolinPoutine 3:7b1110501ef9 885 //--------------------TEST COMPLET -------------------------------//
ShaolinPoutine 3:7b1110501ef9 886
ShaolinPoutine 3:7b1110501ef9 887 char charComplet = 158;
ShaolinPoutine 3:7b1110501ef9 888 bool tmpAns[16];
ShaolinPoutine 3:7b1110501ef9 889 char charCompletAns;
ShaolinPoutine 3:7b1110501ef9 890
ShaolinPoutine 3:7b1110501ef9 891 ManchesterEncodeByte(charComplet, (bool *) tmpAns);
ShaolinPoutine 3:7b1110501ef9 892 Valid = ManchesterDecodeByte((bool *) tmpAns, &charCompletAns);
ShaolinPoutine 3:7b1110501ef9 893
ShaolinPoutine 3:7b1110501ef9 894 if (!Valid)
ShaolinPoutine 3:7b1110501ef9 895 {
ShaolinPoutine 3:7b1110501ef9 896 pc.printf("Function detected error in format\r\n");
ShaolinPoutine 3:7b1110501ef9 897 }
ShaolinPoutine 3:7b1110501ef9 898 if (charComplet != charCompletAns)
ShaolinPoutine 3:7b1110501ef9 899 {
ShaolinPoutine 3:7b1110501ef9 900 pc.printf("TestManchester - decode - complete - Failed\r\n");
ShaolinPoutine 3:7b1110501ef9 901 pc.printf("Expected: %d\t\tReceived: %d\r\n",
ShaolinPoutine 3:7b1110501ef9 902 charComplet, charCompletAns);
ShaolinPoutine 3:7b1110501ef9 903 }
ShaolinPoutine 3:7b1110501ef9 904
ShaolinPoutine 3:7b1110501ef9 905 pc.printf("TestManchester - End\r\n");
ShaolinPoutine 2:1665cd4c922c 906 }