routeur done

Dependencies:   mbed

Fork of APP4 by Évan Laverdure

trame.cpp

Committer:
joGenie
Date:
2014-02-24
Revision:
3:350f07072089
Parent:
2:7515831bb5f5
Child:
4:aac38b016952

File content as of revision 3:350f07072089:

#include "trame.hpp"

Trame::Trame(PinName _tx, PinName _rx) :
    tx(_tx),
    rx(_rx)
{
}

void Trame::sendATCommand(const char* command, const char* data, int length)
{
    int tmp = length + 8;
    char* trm = new char[tmp];
    
    trm[0] = 0x7E;      // Delimiter
    trm[1] = static_cast<char>((tmp >> 8) & 0xFF);   // MSB de length
    trm[2] = static_cast<char>(tmp & 0xFF); // LSB length
    trm[3] = 0x08;      // Type
    trm[4] = 0x01;      // API
    trm[5] = command[0];
    trm[6] = command[1];
    for (int i = 0; i < length; i++)
        trm[7+i] = data[i]; //Data
    trm[7+length] = crc8(trm, tmp - 1);
    
    Serial xbee(tx, rx);
    for (int i = 0; i < tmp; i++)
        xbee.putc(trm[i]);
        
    wait(0.01);
    delete trm;
}

void Trame::sendTransmitRequest(const char* destination, const char* data, int length)
{
    int tmp = length + 18;
    char* trm = new char[tmp];
    
    trm[0] = 0x7E;      // Delimiter
    trm[1] = static_cast<char>((tmp >> 8) & 0xFF);   // MSB de length
    trm[2] = static_cast<char>(tmp & 0xFF); // LSB length
    trm[3] = 0x10;      // Type
    trm[4] = 0x01;      // API
    for (int i = 0; i < 8; i++)
        trm[5 + i] = destination[i]; //Destination
    trm[13] = 0x00;     // 16 bits address
    trm[14] = 0x00;     // 16 bits address
    trm[15] = 0x00;     // Radius
    trm[16] = 0x00;     // Options
        
    for (int i = 0; i < length; i++)
        trm[17 + i] = data[i]; // Data
        
    trm[17 + length] = crc8(trm, tmp - 1);
    
    Serial xbee(tx, rx);
    for (int i = 0; i < tmp; i++)
        xbee.putc(trm[i]);
        
    wait(0.01);
    delete trm;
}

unsigned char Trame::crc8(const char* data, int length)
{
    unsigned char crc = 0;
    for (int i = 1; i < length; i++)
        crc += data[i];
    
    return (0xFF - crc);
}