Projet S5 Info / Mbed 2 deprecated Projet_S5

Dependencies:   mbed PowerControl

Fork of Projet_S5 by Jonathan Tousignant

Committer:
joGenie
Date:
Mon Apr 07 18:33:53 2014 +0000
Revision:
10:61b4d6c67dd7
Child:
12:16390cea4420
With xbee

Who changed what in which revision?

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