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