Projectlab Elektronica-ICT KULeuven
Dependencies: EthernetInterface TMP102 TextLCD mbed-rtos mbed
Diff: Frame.cpp
- Revision:
- 1:635e76c52151
- Parent:
- 0:ae3af7d18c4a
- Child:
- 2:1243006bb879
--- a/Frame.cpp Fri Mar 14 19:41:14 2014 +0000 +++ b/Frame.cpp Sun Mar 16 15:08:10 2014 +0000 @@ -11,61 +11,68 @@ //ID0–IDn(n x 8bit): lijst van reeds bezochte mbeds //CRC (16bit): CRC16 checksum(x16+ x15+ x2+ 1) checksumop “LEN IDD TMP PWM ID0…IDn” //EOF (16bit): end of frame (0xCC 0xDD) - -//char buf[]={0xAA,0xBB,0x0F,0x0B,0x0F,0xA0,0x02,0x05,0x08,0x09,0x0A,0xF2,0xE4,0xCC,0xDD}; //BB 0E A0 0F 32 02 05 08 09 F2 E4 CCDD"; -// SOF SOF LEN IDD TMP TMP PWM TUN ID0 ID1 ID2 CRC CRC EOF EOF - -void decode(char* frame) +Frame::Frame() { - int length; - int IdDestinationMbed; - int temperature; - int pWMDutycycle; - int tune; - char* IDs; - char* CRC; - int numberOfFrames; + IdDestinationMbed=0; + temperature=0; + pWMDutycycle=0; + tune=0; + lengthIDs=0; + IDs= new char[0]; +} +Frame::Frame( int IdDestinationMbed, + int temperature, + int pWMDutycycle, + int tune, + int lengthIDs, + char* IDs) +{ + this->IdDestinationMbed=IdDestinationMbed; + this->temperature=temperature; + this->pWMDutycycle=pWMDutycycle; + this->tune=tune; + this->lengthIDs=lengthIDs; + this->IDs=IDs; +} - length = (int)frame[2]; - numberOfFrames = length- 12; - IDs = new char[numberOfFrames]; +int Frame::Decode(char* frame) +{ + int length = (int)frame[2]; + lengthIDs = length- 12; + IDs = new char[lengthIDs]; if ( frame[0]==0xAA && frame[1]==0xBB && frame[length-2]==0xCC && frame[length-1]==0xDD) { IdDestinationMbed = (int) frame[3]; - temperature = (int) ((frame[4]<<8) | frame[5]); + //temperature = (int) (0.026862*((frame[4]<<8) | frame[5])-25) ; + temperature = (int) ((frame[4]<<8) | frame[5]) ; pWMDutycycle = (int)frame[6]; tune = (int) frame [7]; + crc= (int) ((frame[length-4]<<8) | frame[length-3]); + for (int i=0; i<lengthIDs; i++) { + IDs[i]=frame[i+8]; + } - for (int i=0; i<numberOfFrames; i++) { - IDs[i]=frame[i+numberOfFrames]; - } printf("Decode length: %d IdDestinationMbed: %d temperature: %d \n\r",length,IdDestinationMbed,temperature); printf("pWMDutycycle: %d tune: %d \n\r",pWMDutycycle,tune); - + for(int i=0; i<lengthIDs; i++) { + printf("IDs %d \n\r",IDs[i]); + } } + return CheckCRC(frame); } -int encode( - int IdDestinationMbed, - int temperature, - int pWMDutycycle, - int tune, - int lengthIDs, - char* IDs, - char* encode -) +int Frame::Encode(char* encode) { + memset(encode, 0, 255); int length = 12+lengthIDs; - //encode = new char[length]; encode[0]=0xAA; encode[1]=0xBB; - encode[2]= (char) length; encode[3]= (char) IdDestinationMbed; - encode[4]= (char) (temperature>>8); - encode[5]= (char) temperature; + encode[4]= (char) (((int)(37.227*temperature+930.68))>>8); + encode[5]= (int) (37.227*temperature+930.68); encode[6]= (char) pWMDutycycle; encode[7]= (char) tune; @@ -73,12 +80,6 @@ encode[8+i]=IDs[i]; } - //copy over static fields - /* for (int i=2; i<7; i++) { - CRC[i-2]= encode[i]; - }*/ - - // Calculate CRC int CRCint = MakeCRC(encode[2],encode[3],encode[4],encode[5],encode[6],IDs,lengthIDs); @@ -90,7 +91,7 @@ return length; } -int MakeCRC( +int Frame::MakeCRC( char LEN, //lenght of frame char IDD, //Destination ID char TMP0,//first tmp char @@ -114,4 +115,83 @@ crcData[i+5]= data[i]; } return calculate_crc16(crcData, lenghtData+5); +} + +void Frame::testEncode(char* frame) +{ + printf("testEncode\n\r"); + char controle[]= {0xAA,0xBB,0x0F,0x0B,0x0F,0xA0,0x02,0x05,0x08,0x09,0x0A,0x4B,0x7C,0xCC,0xDD}; + // SOF SOF LEN IDD TMP TMP PWM TUN ID0 ID1 ID2 CRC CRC EOF EOF + for (int i=0; i<15; i++) { + // loop all chars and check if there right + if(controle[i]!=frame[i]) { + printf("ERROR in frame %d : %X != %X\n\r",i,(unsigned char) controle[i],(unsigned char)frame[i] ); + } + } + printf("End testEncode\n\r"); +} +int Frame::CheckCRC(char* frame) +{ + if(crc == MakeCRC((char)(12+lengthIDs),(char)IdDestinationMbed,(char)(temperature>>8),(char)temperature,(char)pWMDutycycle,(char* )IDs,lengthIDs)) + return 1; + else return 0; +} +void Frame::AddID(int id) +{ + lengthIDs++; + char* IDs2 = new char[lengthIDs]; + for(int i=0; i<lengthIDs; i++) { + IDs2[i] = IDs[i]; + } + IDs2[lengthIDs-1]=id; + + char* tempIDs=IDs; + IDs=IDs2; + delete tempIDs; +} + +int Frame::CheckMyID(int id) +{ + for(int i=0; i<lengthIDs; i++) { + if(id == IDs[i]) + return 1; + } + return 0; +} + +void Frame::setIdDestinationMbed(int IdDestinationMbed) +{ + this->IdDestinationMbed=IdDestinationMbed; +} +int Frame::getIdDestinationMbed(void) +{ + return IdDestinationMbed; +} +void Frame::setTemperature(int temperature) +{ + this->temperature=temperature; +} +int Frame::getTemperature(void) +{ + return temperature; +} +void Frame::setPWMDutycycle(int pWMDutycycle) +{ + this->pWMDutycycle=pWMDutycycle; +} +void Frame::setTune(int tune) +{ + this->tune=tune; +} +int Frame::getTune(void) +{ + return tune; +} +void Frame::setLengthIDs(int lengthIDs) +{ + this->lengthIDs=lengthIDs; +} +void Frame::setIDs(char* IDs) +{ + this->IDs=IDs; } \ No newline at end of file