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