routeur done

Dependencies:   mbed

Fork of APP4 by Évan Laverdure

Committer:
joGenie
Date:
Mon Feb 24 15:40:33 2014 +0000
Revision:
3:350f07072089
Parent:
2:7515831bb5f5
Child:
4:aac38b016952
NEW;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joGenie 2:7515831bb5f5 1 #include "trame.hpp"
joGenie 2:7515831bb5f5 2
joGenie 2:7515831bb5f5 3 Trame::Trame(PinName _tx, PinName _rx) :
joGenie 2:7515831bb5f5 4 tx(_tx),
joGenie 2:7515831bb5f5 5 rx(_rx)
joGenie 2:7515831bb5f5 6 {
joGenie 2:7515831bb5f5 7 }
joGenie 2:7515831bb5f5 8
joGenie 3:350f07072089 9 void Trame::sendATCommand(const char* command, const char* data, int length)
joGenie 2:7515831bb5f5 10 {
joGenie 3:350f07072089 11 int tmp = length + 8;
joGenie 3:350f07072089 12 char* trm = new char[tmp];
joGenie 3:350f07072089 13
joGenie 3:350f07072089 14 trm[0] = 0x7E; // Delimiter
joGenie 3:350f07072089 15 trm[1] = static_cast<char>((tmp >> 8) & 0xFF); // MSB de length
joGenie 3:350f07072089 16 trm[2] = static_cast<char>(tmp & 0xFF); // LSB length
joGenie 3:350f07072089 17 trm[3] = 0x08; // Type
joGenie 3:350f07072089 18 trm[4] = 0x01; // API
joGenie 3:350f07072089 19 trm[5] = command[0];
joGenie 3:350f07072089 20 trm[6] = command[1];
joGenie 3:350f07072089 21 for (int i = 0; i < length; i++)
joGenie 3:350f07072089 22 trm[7+i] = data[i]; //Data
joGenie 3:350f07072089 23 trm[7+length] = crc8(trm, tmp - 1);
joGenie 3:350f07072089 24
joGenie 2:7515831bb5f5 25 Serial xbee(tx, rx);
joGenie 3:350f07072089 26 for (int i = 0; i < tmp; i++)
joGenie 3:350f07072089 27 xbee.putc(trm[i]);
joGenie 3:350f07072089 28
joGenie 3:350f07072089 29 wait(0.01);
joGenie 3:350f07072089 30 delete trm;
joGenie 3:350f07072089 31 }
joGenie 3:350f07072089 32
joGenie 3:350f07072089 33 void Trame::sendTransmitRequest(const char* destination, const char* data, int length)
joGenie 3:350f07072089 34 {
joGenie 3:350f07072089 35 int tmp = length + 18;
joGenie 3:350f07072089 36 char* trm = new char[tmp];
joGenie 3:350f07072089 37
joGenie 3:350f07072089 38 trm[0] = 0x7E; // Delimiter
joGenie 3:350f07072089 39 trm[1] = static_cast<char>((tmp >> 8) & 0xFF); // MSB de length
joGenie 3:350f07072089 40 trm[2] = static_cast<char>(tmp & 0xFF); // LSB length
joGenie 3:350f07072089 41 trm[3] = 0x10; // Type
joGenie 3:350f07072089 42 trm[4] = 0x01; // API
joGenie 3:350f07072089 43 for (int i = 0; i < 8; i++)
joGenie 3:350f07072089 44 trm[5 + i] = destination[i]; //Destination
joGenie 3:350f07072089 45 trm[13] = 0x00; // 16 bits address
joGenie 3:350f07072089 46 trm[14] = 0x00; // 16 bits address
joGenie 3:350f07072089 47 trm[15] = 0x00; // Radius
joGenie 3:350f07072089 48 trm[16] = 0x00; // Options
joGenie 3:350f07072089 49
joGenie 3:350f07072089 50 for (int i = 0; i < length; i++)
joGenie 3:350f07072089 51 trm[17 + i] = data[i]; // Data
joGenie 3:350f07072089 52
joGenie 3:350f07072089 53 trm[17 + length] = crc8(trm, tmp - 1);
joGenie 3:350f07072089 54
joGenie 3:350f07072089 55 Serial xbee(tx, rx);
joGenie 3:350f07072089 56 for (int i = 0; i < tmp; i++)
joGenie 3:350f07072089 57 xbee.putc(trm[i]);
joGenie 3:350f07072089 58
joGenie 3:350f07072089 59 wait(0.01);
joGenie 3:350f07072089 60 delete trm;
joGenie 3:350f07072089 61 }
joGenie 3:350f07072089 62
joGenie 3:350f07072089 63 unsigned char Trame::crc8(const char* data, int length)
joGenie 3:350f07072089 64 {
joGenie 3:350f07072089 65 unsigned char crc = 0;
joGenie 3:350f07072089 66 for (int i = 1; i < length; i++)
joGenie 3:350f07072089 67 crc += data[i];
joGenie 3:350f07072089 68
joGenie 3:350f07072089 69 return (0xFF - crc);
joGenie 2:7515831bb5f5 70 }