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:
- EmileArseneault
- Date:
- 2017-02-13
- Revision:
- 3:5010d20e681f
- Parent:
- 1:318ad4f36a90
- Child:
- 4:04351e4aad49
File content as of revision 3:5010d20e681f:
#include "mbed.h"
#define FRAMEDELIMITER 0x7E
#include "rtos.h"
// 4269
DigitalOut resetswitch(p8);
Serial xbee(p13, p14);
Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);
int CalculateChecksum()
{
// Does not include frame delimiter and lenght
return 1;
}
int CalculateLenght()
{
// Calculate the number of bytes in the frame data
return 1;
}
void printhexa(char c)
{
char msb = c >> 4;
char lsb = c & 0xF;
if (msb < 10)
msb += 0x30;
else
msb += 0x37;
if (lsb < 10)
lsb += 0x30;
else
lsb += 0x37;
pc.putc('0');
pc.putc('x');
pc.putc(msb);
pc.putc(lsb);
pc.putc(' ');
}
void GetATResponse()
{
char start = xbee.getc(); // = FRAMEDELIMITER
//assert
char len_msb = xbee.getc();
char len_lsb = xbee.getc();
int len = ((int) len_msb << 4) + (int) len_lsb;
char frame_data[len];
int i = 0;
pc.printf("%d : ", len);
while (i < len)
{
if (xbee.readable())
{
frame_data[i] = xbee.getc();
printhexa(frame_data[i]);
i++;
}
}
char checksum = xbee.getc();
// Verify checksum
pc.printf(" checksum = ");
printhexa(checksum);
pc.printf("\r\n");
}
void ReadSerial()
{
// 00 13 A2 00
// 40 3E 09 63
char cmdtosend[8];
int i = 0;
cmdtosend[0] = FRAMEDELIMITER;
cmdtosend[1] = 0x00;
cmdtosend[2] = 0x04;
cmdtosend[3] = 0x08;
cmdtosend[4] = 0x52;
cmdtosend[5] = 'S'; // 0x53
cmdtosend[6] = 'L'; // 0x48
cmdtosend[7] = 0xFF - (cmdtosend[3]+cmdtosend[4]+cmdtosend[5]+cmdtosend[6]);
while (i < 8)
{
xbee.putc(cmdtosend[i]);
i++;
}
wait(0.5);
i = 0;
cmdtosend[0] = FRAMEDELIMITER;
cmdtosend[1] = 0x00;
cmdtosend[2] = 0x04;
cmdtosend[3] = 0x08;
cmdtosend[4] = 0x52;
cmdtosend[5] = 'S'; // 0x53
cmdtosend[6] = 'H'; // 0x48
cmdtosend[7] = 0xFF - (cmdtosend[3]+cmdtosend[4]+cmdtosend[5]+cmdtosend[6]);
while (i < 8)
{
xbee.putc(cmdtosend[i]);
i++;
}
}
void AtCommand(char FirstChar, char SecondChar, char *OptionalParam = NULL, int ParamLen = 0)
{
// Frame Type 0x08
// Two char as parameters
char cmdtosend[10];
char sum = 0;
int cmdlength = 8;
int i = 0;
cmdtosend[0] = FRAMEDELIMITER;
cmdtosend[1] = 0x00;
cmdtosend[2] = 0x04 + ParamLen;
cmdtosend[3] = 0x08;
cmdtosend[4] = 0x52;
cmdtosend[5] = FirstChar;
cmdtosend[6] = SecondChar;
// Ajouter les parametres au message
if(OptionalParam != NULL)
{
i = 0;
cmdlength += ParamLen;
while (i < ParamLen)
{
pc.putc((OptionalParam)[i]);
cmdtosend[7 + i] = (OptionalParam)[i];
i++;
}
pc.printf("\r\n");
}
// Calculate checksum
i = 3;
while (i < (cmdlength - 1))
{
sum += cmdtosend[i];
i++;
}
cmdtosend[cmdlength - 1] = 0xFF - sum;
// Envoyer la commande sur UART
i = 0;
while (i < cmdlength)
{
xbee.putc(cmdtosend[i]);
i++;
}
}
void xbee_reader()
{
while(1)
{
if (xbee.readable())
{
GetATResponse();
}
wait(0.001);
}
}
void tick()
{
myled = !myled;
}
int main() {
Thread thread(xbee_reader);
Ticker ticker;
ticker.attach(&tick, 1);
pc.printf("\r\n");
resetswitch = 0;
wait(0.4);
resetswitch = 1;
wait(3);
char Params[2] = {'a', 'b'};
ReadSerial();
wait(0.5);
AtCommand('O', 'I');
wait(0.5);
AtCommand('I', 'I', &Params[0], 2);
wait(0.5);
AtCommand('O', 'I');
wait(0.5);
while(1) {
}
}