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.
Receiver.cpp@8:7c56fb1ed8c0, 2017-10-25 (annotated)
- Committer:
- ThierryLeonard
- Date:
- Wed Oct 25 04:17:42 2017 +0000
- Revision:
- 8:7c56fb1ed8c0
- Parent:
- 7:332766fb3114
- Child:
- 9:8f479f7c1b54
No guarantees
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThierryLeonard | 6:ac7c0ccf9b5d | 1 | #include "Receiver.h" |
ThierryLeonard | 6:ac7c0ccf9b5d | 2 | #include "CRC.h" |
ThierryLeonard | 4:a3c4a43f94f8 | 3 | |
ThierryLeonard | 4:a3c4a43f94f8 | 4 | InterruptIn DO(p21); |
ThierryLeonard | 4:a3c4a43f94f8 | 5 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 6 | extern Serial pc; |
ThierryLeonard | 6:ac7c0ccf9b5d | 7 | |
ThierryLeonard | 7:332766fb3114 | 8 | Receiver::Receiver():currentData(0),endData(0),lastTime(0),timedOut(true) |
ThierryLeonard | 6:ac7c0ccf9b5d | 9 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 10 | time.start(); |
ThierryLeonard | 6:ac7c0ccf9b5d | 11 | DO.rise(this,&Receiver::rise); |
ThierryLeonard | 6:ac7c0ccf9b5d | 12 | DO.fall(this,&Receiver::fall); |
ThierryLeonard | 4:a3c4a43f94f8 | 13 | } |
ThierryLeonard | 4:a3c4a43f94f8 | 14 | |
ThierryLeonard | 7:332766fb3114 | 15 | char Receiver::getNext() |
ThierryLeonard | 4:a3c4a43f94f8 | 16 | { |
ThierryLeonard | 8:7c56fb1ed8c0 | 17 | if(dataReady.wait(2)==0) |
ThierryLeonard | 8:7c56fb1ed8c0 | 18 | { |
ThierryLeonard | 8:7c56fb1ed8c0 | 19 | return BitData::timeout; |
ThierryLeonard | 8:7c56fb1ed8c0 | 20 | } |
ThierryLeonard | 7:332766fb3114 | 21 | char val = data[currentData]; |
ThierryLeonard | 6:ac7c0ccf9b5d | 22 | currentData++; |
ThierryLeonard | 6:ac7c0ccf9b5d | 23 | if(currentData==size) |
ThierryLeonard | 6:ac7c0ccf9b5d | 24 | currentData = 0 ; |
ThierryLeonard | 6:ac7c0ccf9b5d | 25 | return val; |
ThierryLeonard | 4:a3c4a43f94f8 | 26 | } |
ThierryLeonard | 4:a3c4a43f94f8 | 27 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 28 | |
ThierryLeonard | 7:332766fb3114 | 29 | void Receiver::pushData(char bitdata) |
ThierryLeonard | 4:a3c4a43f94f8 | 30 | { |
ThierryLeonard | 7:332766fb3114 | 31 | data[endData] = bitdata; |
ThierryLeonard | 6:ac7c0ccf9b5d | 32 | endData++; |
ThierryLeonard | 6:ac7c0ccf9b5d | 33 | if(endData==size) |
ThierryLeonard | 6:ac7c0ccf9b5d | 34 | endData = 0; |
ThierryLeonard | 6:ac7c0ccf9b5d | 35 | dataReady.release(); |
ThierryLeonard | 6:ac7c0ccf9b5d | 36 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 37 | |
ThierryLeonard | 7:332766fb3114 | 38 | void Receiver::edgeFunction(char mode) |
ThierryLeonard | 7:332766fb3114 | 39 | { |
ThierryLeonard | 7:332766fb3114 | 40 | int curTime = time.read_us(); |
ThierryLeonard | 7:332766fb3114 | 41 | int dif = curTime - lastTime; |
ThierryLeonard | 7:332766fb3114 | 42 | // timeout |
ThierryLeonard | 7:332766fb3114 | 43 | if( dif > us_timeout) |
ThierryLeonard | 7:332766fb3114 | 44 | { |
ThierryLeonard | 7:332766fb3114 | 45 | pushData(BitData::timeout); |
ThierryLeonard | 7:332766fb3114 | 46 | if(mode == BitData::zero) |
ThierryLeonard | 7:332766fb3114 | 47 | { |
ThierryLeonard | 7:332766fb3114 | 48 | lastTime = curTime; |
ThierryLeonard | 7:332766fb3114 | 49 | pushData(BitData::zero); |
ThierryLeonard | 7:332766fb3114 | 50 | return; |
ThierryLeonard | 7:332766fb3114 | 51 | } |
ThierryLeonard | 7:332766fb3114 | 52 | } |
ThierryLeonard | 7:332766fb3114 | 53 | // data |
ThierryLeonard | 7:332766fb3114 | 54 | if(dif > us_prepare) |
ThierryLeonard | 7:332766fb3114 | 55 | { |
ThierryLeonard | 7:332766fb3114 | 56 | lastTime = curTime; |
ThierryLeonard | 7:332766fb3114 | 57 | pushData(mode); |
ThierryLeonard | 7:332766fb3114 | 58 | } |
ThierryLeonard | 7:332766fb3114 | 59 | // else prepare for data |
ThierryLeonard | 7:332766fb3114 | 60 | return; |
ThierryLeonard | 7:332766fb3114 | 61 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 62 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 63 | void Receiver::rise() |
ThierryLeonard | 6:ac7c0ccf9b5d | 64 | { |
ThierryLeonard | 7:332766fb3114 | 65 | if(timedOut) |
ThierryLeonard | 7:332766fb3114 | 66 | { |
ThierryLeonard | 7:332766fb3114 | 67 | timedOut = false; |
ThierryLeonard | 7:332766fb3114 | 68 | lastTime = time.read_us(); |
ThierryLeonard | 7:332766fb3114 | 69 | pushData(BitData::zero); |
ThierryLeonard | 7:332766fb3114 | 70 | return; |
ThierryLeonard | 7:332766fb3114 | 71 | } |
ThierryLeonard | 7:332766fb3114 | 72 | edgeFunction(BitData::zero); |
ThierryLeonard | 6:ac7c0ccf9b5d | 73 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 74 | void Receiver::fall() |
ThierryLeonard | 6:ac7c0ccf9b5d | 75 | { |
ThierryLeonard | 7:332766fb3114 | 76 | if(timedOut) |
ThierryLeonard | 7:332766fb3114 | 77 | {// ignore it, preparation for start |
ThierryLeonard | 7:332766fb3114 | 78 | return; |
ThierryLeonard | 7:332766fb3114 | 79 | } |
ThierryLeonard | 7:332766fb3114 | 80 | edgeFunction(BitData::one); |
ThierryLeonard | 4:a3c4a43f94f8 | 81 | } |
ThierryLeonard | 4:a3c4a43f94f8 | 82 | |
ThierryLeonard | 4:a3c4a43f94f8 | 83 | |
ThierryLeonard | 7:332766fb3114 | 84 | |
ThierryLeonard | 7:332766fb3114 | 85 | bool ManchesterReceiver::getByte(unsigned char &val) |
ThierryLeonard | 6:ac7c0ccf9b5d | 86 | { |
ThierryLeonard | 7:332766fb3114 | 87 | val =0 ; |
ThierryLeonard | 7:332766fb3114 | 88 | for(int i = 0; i <8; i++) |
ThierryLeonard | 7:332766fb3114 | 89 | { |
ThierryLeonard | 7:332766fb3114 | 90 | char bitData = r.getNext(); |
ThierryLeonard | 7:332766fb3114 | 91 | switch(bitData) |
ThierryLeonard | 7:332766fb3114 | 92 | { |
ThierryLeonard | 7:332766fb3114 | 93 | case BitData::zero: |
ThierryLeonard | 7:332766fb3114 | 94 | break; |
ThierryLeonard | 7:332766fb3114 | 95 | case BitData::one: |
ThierryLeonard | 7:332766fb3114 | 96 | val = val|(1 << 7-i); |
ThierryLeonard | 7:332766fb3114 | 97 | break; |
ThierryLeonard | 7:332766fb3114 | 98 | case BitData::timeout: |
ThierryLeonard | 7:332766fb3114 | 99 | return false; |
ThierryLeonard | 7:332766fb3114 | 100 | } |
ThierryLeonard | 7:332766fb3114 | 101 | } |
ThierryLeonard | 7:332766fb3114 | 102 | pc.printf(" byte:%i\n",val); |
ThierryLeonard | 7:332766fb3114 | 103 | return true; |
ThierryLeonard | 6:ac7c0ccf9b5d | 104 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 105 | |
ThierryLeonard | 7:332766fb3114 | 106 | bool ManchesterReceiver::getMessage(vector<unsigned char> &message, int& crc ) |
ThierryLeonard | 4:a3c4a43f94f8 | 107 | { |
ThierryLeonard | 7:332766fb3114 | 108 | unsigned char byte = 0; |
ThierryLeonard | 7:332766fb3114 | 109 | // for crc; |
ThierryLeonard | 7:332766fb3114 | 110 | vector<unsigned char> frame; |
ThierryLeonard | 7:332766fb3114 | 111 | |
ThierryLeonard | 7:332766fb3114 | 112 | // wait till a valid byte start the message |
ThierryLeonard | 7:332766fb3114 | 113 | while(byte!=preamble_byte) |
ThierryLeonard | 6:ac7c0ccf9b5d | 114 | { |
ThierryLeonard | 7:332766fb3114 | 115 | while(!getByte(byte)) |
ThierryLeonard | 7:332766fb3114 | 116 | { } |
ThierryLeonard | 7:332766fb3114 | 117 | } |
ThierryLeonard | 7:332766fb3114 | 118 | frame.push_back(byte); |
ThierryLeonard | 7:332766fb3114 | 119 | |
ThierryLeonard | 7:332766fb3114 | 120 | if(!getByte(byte)){// timed out |
ThierryLeonard | 7:332766fb3114 | 121 | return false; |
ThierryLeonard | 4:a3c4a43f94f8 | 122 | } |
ThierryLeonard | 7:332766fb3114 | 123 | if(byte!=STARTBYTE) |
ThierryLeonard | 7:332766fb3114 | 124 | return false; |
ThierryLeonard | 7:332766fb3114 | 125 | frame.push_back(byte); |
ThierryLeonard | 7:332766fb3114 | 126 | |
ThierryLeonard | 7:332766fb3114 | 127 | //flags |
ThierryLeonard | 7:332766fb3114 | 128 | if(!getByte( byte)) |
ThierryLeonard | 7:332766fb3114 | 129 | return false; |
ThierryLeonard | 7:332766fb3114 | 130 | frame.push_back(byte); |
ThierryLeonard | 7:332766fb3114 | 131 | |
ThierryLeonard | 7:332766fb3114 | 132 | // payload length |
ThierryLeonard | 7:332766fb3114 | 133 | if(!getByte(byte)) |
ThierryLeonard | 7:332766fb3114 | 134 | return false; |
ThierryLeonard | 7:332766fb3114 | 135 | |
ThierryLeonard | 7:332766fb3114 | 136 | frame.push_back(byte); |
ThierryLeonard | 7:332766fb3114 | 137 | |
ThierryLeonard | 7:332766fb3114 | 138 | int length = byte; |
ThierryLeonard | 7:332766fb3114 | 139 | //data |
ThierryLeonard | 7:332766fb3114 | 140 | for(int i = 0; i < length; i++) |
ThierryLeonard | 6:ac7c0ccf9b5d | 141 | { |
ThierryLeonard | 8:7c56fb1ed8c0 | 142 | if(!getByte(byte)){ |
ThierryLeonard | 8:7c56fb1ed8c0 | 143 | return false; |
ThierryLeonard | 8:7c56fb1ed8c0 | 144 | } |
ThierryLeonard | 7:332766fb3114 | 145 | message.push_back(byte); |
ThierryLeonard | 7:332766fb3114 | 146 | frame.push_back(byte); |
ThierryLeonard | 4:a3c4a43f94f8 | 147 | } |
ThierryLeonard | 7:332766fb3114 | 148 | |
ThierryLeonard | 7:332766fb3114 | 149 | int calccrc = crc16Remainder(frame); |
ThierryLeonard | 7:332766fb3114 | 150 | |
ThierryLeonard | 7:332766fb3114 | 151 | // crc byte1 |
ThierryLeonard | 7:332766fb3114 | 152 | if(!getByte(byte)) |
ThierryLeonard | 7:332766fb3114 | 153 | return false; |
ThierryLeonard | 6:ac7c0ccf9b5d | 154 | |
ThierryLeonard | 7:332766fb3114 | 155 | crc = byte << 8; |
ThierryLeonard | 7:332766fb3114 | 156 | // crc byte2 |
ThierryLeonard | 7:332766fb3114 | 157 | if(!getByte(byte)) |
ThierryLeonard | 7:332766fb3114 | 158 | return false; |
ThierryLeonard | 7:332766fb3114 | 159 | crc = crc | byte; |
ThierryLeonard | 7:332766fb3114 | 160 | |
ThierryLeonard | 7:332766fb3114 | 161 | if(calccrc!=crc) |
ThierryLeonard | 7:332766fb3114 | 162 | { |
ThierryLeonard | 7:332766fb3114 | 163 | pc.printf("\ncrc error\n"); |
ThierryLeonard | 6:ac7c0ccf9b5d | 164 | return false; |
ThierryLeonard | 6:ac7c0ccf9b5d | 165 | } |
ThierryLeonard | 6:ac7c0ccf9b5d | 166 | |
ThierryLeonard | 7:332766fb3114 | 167 | // endbyte |
ThierryLeonard | 7:332766fb3114 | 168 | if(!getByte(byte)) |
ThierryLeonard | 6:ac7c0ccf9b5d | 169 | return false; |
ThierryLeonard | 6:ac7c0ccf9b5d | 170 | if( byte != ENDBYTE ) |
ThierryLeonard | 6:ac7c0ccf9b5d | 171 | return false; |
ThierryLeonard | 7:332766fb3114 | 172 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 173 | return true; |
ThierryLeonard | 4:a3c4a43f94f8 | 174 | } |
ThierryLeonard | 4:a3c4a43f94f8 | 175 | |
ThierryLeonard | 6:ac7c0ccf9b5d | 176 | void ManchesterReceiver::getMessages() |
ThierryLeonard | 4:a3c4a43f94f8 | 177 | { |
ThierryLeonard | 6:ac7c0ccf9b5d | 178 | while(true) |
ThierryLeonard | 6:ac7c0ccf9b5d | 179 | { |
ThierryLeonard | 7:332766fb3114 | 180 | vector<unsigned char> message; |
ThierryLeonard | 7:332766fb3114 | 181 | int crc; |
ThierryLeonard | 7:332766fb3114 | 182 | if(getMessage(message,crc)) |
ThierryLeonard | 6:ac7c0ccf9b5d | 183 | { |
ThierryLeonard | 7:332766fb3114 | 184 | for (int i = 0; i < message.size();i++) |
ThierryLeonard | 6:ac7c0ccf9b5d | 185 | { |
ThierryLeonard | 7:332766fb3114 | 186 | pc.printf("%c", message[i] ); |
ThierryLeonard | 6:ac7c0ccf9b5d | 187 | } |
ThierryLeonard | 7:332766fb3114 | 188 | pc.printf("\nCrc16 : %i\n\n",crc); |
ThierryLeonard | 6:ac7c0ccf9b5d | 189 | } |
ThierryLeonard | 7:332766fb3114 | 190 | else |
ThierryLeonard | 7:332766fb3114 | 191 | { |
ThierryLeonard | 7:332766fb3114 | 192 | printf("\n\n **********************************\n Echec de reception du message\n\n\n"); |
ThierryLeonard | 7:332766fb3114 | 193 | } |
ThierryLeonard | 7:332766fb3114 | 194 | |
ThierryLeonard | 4:a3c4a43f94f8 | 195 | } |
ThierryLeonard | 4:a3c4a43f94f8 | 196 | } |
ThierryLeonard | 4:a3c4a43f94f8 | 197 |