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
00001 #include "Receiver.h" 00002 #include "CRC.h" 00003 00004 InterruptIn DO(p21); 00005 00006 extern Serial pc; 00007 00008 Receiver::Receiver():currentData(0),endData(0),lastTime(0),timedOut(true) 00009 { 00010 time.start(); 00011 DO.rise(this,&Receiver::rise); 00012 DO.fall(this,&Receiver::fall); 00013 } 00014 00015 char Receiver::getNext() 00016 { 00017 if(dataReady.wait(2)==0) 00018 { 00019 timedOut = true; 00020 return BitData::timeout; 00021 } 00022 00023 char val = data[currentData]; 00024 currentData++; 00025 if(currentData==size) 00026 currentData = 0 ; 00027 return val; 00028 } 00029 00030 00031 void Receiver::pushData(char bitdata) 00032 { 00033 data[endData] = bitdata; 00034 endData++; 00035 if(endData==size) 00036 endData = 0; 00037 dataReady.release(); 00038 } 00039 00040 void Receiver::edgeFunction(char mode) 00041 { 00042 int curTime = time.read_us(); 00043 int dif = curTime - lastTime; 00044 // timeout 00045 if( dif > us_timeout) 00046 { 00047 pushData(BitData::timeout); 00048 if(mode == BitData::zero) 00049 { 00050 lastTime = curTime; 00051 pushData(BitData::zero); 00052 } 00053 return; 00054 } 00055 // data 00056 else if(dif > us_prepare) 00057 { 00058 lastTime = curTime; 00059 pushData(mode); 00060 } 00061 // else prepare for data 00062 return; 00063 } 00064 00065 void Receiver::rise() 00066 { 00067 if(timedOut) 00068 { 00069 timedOut = false; 00070 lastTime = time.read_us(); 00071 pushData(BitData::zero); 00072 return; 00073 } 00074 edgeFunction(BitData::zero); 00075 } 00076 void Receiver::fall() 00077 { 00078 if(timedOut) 00079 {// ignore it, preparation for start 00080 return; 00081 } 00082 edgeFunction(BitData::one); 00083 } 00084 00085 00086 00087 bool ManchesterReceiver::getByte(unsigned char &val) 00088 { 00089 val =0 ; 00090 for(int i = 0; i <8; i++) 00091 { 00092 char bitData = r.getNext(); 00093 switch(bitData) 00094 { 00095 case BitData::zero: 00096 break; 00097 case BitData::one: 00098 val = val|(1 << 7-i); 00099 break; 00100 case BitData::timeout: 00101 val = 0; 00102 return false; 00103 } 00104 } 00105 //pc.printf(" byte:%i\n",val); 00106 return true; 00107 } 00108 00109 bool ManchesterReceiver::getMessage(vector<unsigned char> &message, int& crc ) 00110 { 00111 unsigned char byte = 0; 00112 // for crc; 00113 vector<unsigned char> frame; 00114 00115 // wait till a valid byte start the message 00116 while(byte!=preamble_byte) 00117 { 00118 while(!getByte(byte)) 00119 { } 00120 } 00121 frame.push_back(byte); 00122 00123 if(!getByte(byte)){// timed out 00124 return false; 00125 } 00126 if(byte!=STARTBYTE) 00127 return false; 00128 frame.push_back(byte); 00129 00130 //flags 00131 if(!getByte( byte)) 00132 return false; 00133 frame.push_back(byte); 00134 00135 // payload length 00136 if(!getByte(byte)) 00137 return false; 00138 00139 frame.push_back(byte); 00140 00141 int length = byte; 00142 //data 00143 for(int i = 0; i < length; i++) 00144 { 00145 if(!getByte(byte)){ 00146 return false; 00147 } 00148 message.push_back(byte); 00149 frame.push_back(byte); 00150 } 00151 00152 int calccrc = crc16Remainder(frame); 00153 00154 // crc byte1 00155 if(!getByte(byte)) 00156 return false; 00157 00158 crc = byte << 8; 00159 // crc byte2 00160 if(!getByte(byte)) 00161 return false; 00162 crc = crc | byte; 00163 00164 if(calccrc!=crc) 00165 { 00166 pc.printf("\ncrc error\n"); 00167 return false; 00168 } 00169 00170 // endbyte 00171 if(!getByte(byte)) 00172 return false; 00173 if( byte != ENDBYTE ) 00174 return false; 00175 00176 return true; 00177 } 00178 00179 void ManchesterReceiver::getMessages() 00180 { 00181 while(true) 00182 { 00183 vector<unsigned char> message; 00184 int crc; 00185 if(getMessage(message,crc)) 00186 { 00187 for (int i = 0; i < message.size();i++) 00188 { 00189 pc.printf("%c", message[i] ); 00190 } 00191 pc.printf("\nCrc16 : %i\n\n",crc); 00192 } 00193 else 00194 { 00195 printf("\n\n **********************************\n Echec de reception du message\n\n\n"); 00196 } 00197 00198 } 00199 } 00200
Generated on Tue Jul 12 2022 18:11:54 by
1.7.2