Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- ThierryLeonard
- Date:
- 2017-10-03
- Revision:
- 2:ddaf240af296
- Parent:
- 1:fe18dd0ba172
- Child:
- 3:eda76f539da3
File content as of revision 2:ddaf240af296:
#include "mbed.h"
DigitalOut myled(LED1);
Ticker flipper;
Serial pc(USBTX, USBRX);
Serial xBee(p13, p14);
DigitalOut reset(p8);
const uint8_t START_DELEMITER = 0x7E;
const uint8_t FRAME_TYPE_AT_REQUEST = 0x17;
const uint8_t FRAME_ID = 0x01;
const uint8_t ZERO = 0x00;
const uint8_t APPLY_CHANGES = 0x02;
const uint8_t FF_CONST = 0xFF;
const char OPEN_LED[2] = {'D','5'};
bool atCommand = false;
const int TRANSMIT_REQUEST_DATA = 12;
uint8_t addresse_64_bit[8] = {0xcd, 0x2b, 0x0, 0x13, 0xa2, 0x0, 0x40, 0x33};
uint8_t addresse_16_bit[2] = {0xFF,0xFE};
uint8_t pan_id[14]={0x7E, 0x00, 0x0A, 0x08, 0x01, 0x49, 0x44, 0x46, 0x46, 0x39, 0x39, 0x31, 0x31, 0x09};
uint8_t nd_Command[8] = { 0x7E, 0x00, 0x04, 0x08, 0x01, 0x4E, 0x44, 0x64 };
void afficherErreurLed()
{
uint8_t d5_command[9] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 'D', '5', 5, 0};
static bool nextState = true;
//check sum
uint8_t checkSum = 0;
for(int i = 3; i<9;i++)
{
checkSum += d5_command[i];
}
if(nextState)
{
d5_command[7] = 5;
}
else
{
d5_command[7] = 4;
}
checkSum = FF_CONST - checkSum;
d5_command[8] = checkSum;
for(int i = 0 ; i < 9; i++)
{
xBee.putc(d5_command[i]);
}
}
void sendNDCommand()
{
for ( int i = 0 ; i < 8; i++ )
{
xBee.putc(nd_Command[i]);
}
}
void sendRemoteAtCommand(){
atCommand = true;
}
void sendRemoteLedCommandPaquet(){
static bool nextState = true;
uint8_t length = 0xF;
uint8_t paquet[25];
uint8_t checkSum= 0x0;
uint8_t LSBLength;
uint8_t MSBLength;
char type[2] = { 'D','5'};
LSBLength = length&0xff;
MSBLength = (length &0xff00) >> 8;
paquet[0] = START_DELEMITER; //start
paquet[1] = MSBLength; //length MSB
paquet[2] = LSBLength; //length LSB
paquet[3] = FRAME_TYPE_AT_REQUEST; //frame type
paquet[4] = FRAME_ID; //frame id
for(int i = 5; i < 13;i++) //64 bit addresse
{
paquet[i] = addresse_64_bit[i-5];
}
for(int i = 13; i < 15;i++) //16bit address
{
paquet[i] = addresse_16_bit[i-13];
}
paquet[15] = APPLY_CHANGES; //BroadCast Radius
paquet[16] = type[0];
paquet[17] = type[1];
if(nextState)
{
paquet[18] = 0x05;
}
else
{
paquet[18] = 0x04;
}
//check sum
for(int i = 3; i<length+3;i++)
{
checkSum += paquet[i];
}
checkSum = FF_CONST - checkSum;
paquet[length+3] = checkSum;
for(int i =0;i<length + 4;i++)
{
xBee.putc(paquet[i]);
}
}
bool checkForError(int length,uint8_t type, uint8_t* data)
{
data[0] = type;
uint8_t origCheckSum;
uint8_t checkSum = type;
for (int i =1; i < length; i++)
{
data[i] = xBee.getc();
checkSum += data[i];
}
checkSum = 0xFF - checkSum;
origCheckSum = xBee.getc();
return checkSum != origCheckSum;
}
void receiveAtCommandResponse(int length, uint8_t* data)
{
// 1 Command id
uint8_t id = data[1];
// La command est identifie par les deux prochains bits
uint16_t command = data[2] << 8;
command += data[3];
pc.printf("\n Command %x\n",command);
uint8_t status = data[4];
// Data == 5-length
if(status != 0)
{
pc.printf("erreur");
afficherErreurLed();
return;
}
switch(command)
{
case 0x4e44: // Node Discovery AT Command
{
uint64_t serialNumber = 0;
for(int i = 5; i < 13; i++)
{
serialNumber += data[i] << (8 * (13-i -1) );
addresse_64_bit[i-5] = data[i];
pc.printf("\nSerial number %i %x\n",i,data[i]);
}
return;
}
break;
}
}
void receiveTransmitRequest(int length,uint8_t type){
uint8_t data[512];
data[0] = type;
uint8_t origCheckSum;
uint8_t checkSum = type;
for (int i =1; i < length; i++)
{
data[i] = xBee.getc();
checkSum += data[i];
}
checkSum = 0xFF - checkSum;
origCheckSum = xBee.getc();
if(origCheckSum == checkSum){
for(int i = TRANSMIT_REQUEST_DATA; i <length;i++)
{
pc.printf("%c", data[i]);
}
pc.printf("\n");
}
else{
}
}
void receiveNewPaquet()
{
int length;
if(xBee.getc() == START_DELEMITER)
{
// MSB
length = (((xBee.getc()) << 8));
// LSB
length +=(xBee.getc());
uint8_t frame_type = xBee.getc();
uint8_t data[512];
data[0] = frame_type;
switch (frame_type){
case 0x90:
receiveTransmitRequest(length, frame_type);
// AT command Response frame
break;
case 0x88:
pc.printf("\nat response : length %i, frame time %i\n",length,frame_type);
bool error = checkForError(length, frame_type,data);
if(true)
{
receiveAtCommandResponse(length, data);
}
else
{
pc.printf("Transmit or checksum error");
}
break;
}
}
}
int main() {
flipper.attach (&sendRemoteAtCommand,1);
reset =0;
wait_ms(500);
reset=1;
wait(1);
xBee.printf("+++");
wait(1);
sendNDCommand();
//
// for(int i =0;i <14;i++){
// xBee.putc(pan_id[i]);
// }
while(1) {
if(xBee.readable()){
receiveNewPaquet();
}
if (atCommand){
atCommand = false;
sendRemoteLedCommandPaquet();
}
}
}
