routeur done
Dependencies: mbed
Fork of APP4 by
trame.cpp@4:aac38b016952, 2014-02-24 (annotated)
- Committer:
- RufflesAllD
- Date:
- Mon Feb 24 19:06:11 2014 +0000
- Revision:
- 4:aac38b016952
- Parent:
- 3:350f07072089
l
Who changed what in which revision?
User | Revision | Line number | New 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 | { |
RufflesAllD | 4:aac38b016952 | 35 | int tmp = length + 14; |
RufflesAllD | 4:aac38b016952 | 36 | char* trm = new char[tmp + 4]; |
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 |
RufflesAllD | 4:aac38b016952 | 45 | trm[13] = 0xFF; // 16 bits address |
RufflesAllD | 4:aac38b016952 | 46 | trm[14] = 0xFE; // 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 | |
RufflesAllD | 4:aac38b016952 | 53 | trm[17 + length] = crc8(trm, tmp+3); |
joGenie | 3:350f07072089 | 54 | |
joGenie | 3:350f07072089 | 55 | Serial xbee(tx, rx); |
RufflesAllD | 4:aac38b016952 | 56 | for (int i = 0; i < tmp+4; i++) |
joGenie | 3:350f07072089 | 57 | xbee.putc(trm[i]); |
RufflesAllD | 4:aac38b016952 | 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; |
RufflesAllD | 4:aac38b016952 | 66 | for (int i = 3; 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 | } |