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