Projectlab Elektronica-ICT KULeuven

Dependencies:   EthernetInterface TMP102 TextLCD mbed-rtos mbed

werking.pdf

Frame.cpp

Committer:
seppeduwe
Date:
2014-03-14
Revision:
0:ae3af7d18c4a
Child:
1:635e76c52151

File content as of revision 0:ae3af7d18c4a:

#include "mbed.h"
#include "Frame.h"
#include "lib_crc.h"
//SOF LEN IDD TMP PWM ID0 ID1 … IDn CRC EOF
//SOF (16bit): start of frame (0xAA 0xBB)
//LEN (8bit): lengte van frame in bytes (volledige frame!)
//IDD (8bit): ID destinationmbed(1-15)
//TMP (16bit): 0b0000XXXX 0bXXXXXXXX Temperature(0 –4095) (-25°tot 85°)
//PWM (8bit): PWM dutycycle(0 –255) (0% –100%)
//TUN (8bit): Select tune & delay (1 –15) zie tabel 1
//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)
{
    int length;
    int IdDestinationMbed;
    int temperature;
    int pWMDutycycle;
    int tune;
    char* IDs;
    char* CRC;
    int numberOfFrames;

    length = (int)frame[2];
    numberOfFrames = length- 12;
    IDs = new char[numberOfFrames];

    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]);
        pWMDutycycle = (int)frame[6];
        tune = (int) frame [7];

        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);

    }
}

int encode(
    int IdDestinationMbed,
    int temperature,
    int pWMDutycycle,
    int tune,
    int lengthIDs,
    char* IDs,
    char* encode
)
{
    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[6]= (char) pWMDutycycle;
    encode[7]= (char) tune;

    for (int i=0; i<lengthIDs; i++) {
        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);

    encode[length-4]= ( char)(CRCint>>8);
    encode[length-3]=( char)(CRCint);

    encode[length-2]=0xCC;
    encode[length-1]=0xDD;
    return length;
}

int MakeCRC(
    char LEN, //lenght of frame
    char IDD, //Destination ID
    char TMP0,//first tmp char
    char TMP1,//second tmp char
    char PWM, //pwm tune
    char* data, //data frame
    int lenghtData //lenght of the data
)
{
    //char* crcData = new char[5+lenghtData] ;
    char crcData[5+lenghtData] ;

    crcData[0] = LEN; //lenght of frame
    crcData[1] = IDD; //Destination ID
    crcData[2] = TMP0;//first tmp char
    crcData[3] = TMP1;//second tmp char
    crcData[4] = PWM; //pwm tune

    // add the data to the crcData
    for (int i=0; i<lenghtData; i++) {
        crcData[i+5]= data[i];
    }
    return calculate_crc16(crcData, lenghtData+5);
}