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.
Dependents: Coordinator_node Router_node
Diff: xbee.cpp
- Revision:
- 9:902d0f74333c
- Parent:
- 8:ba349a2eeb37
- Child:
- 10:dac25a0076f5
- Child:
- 15:04e892ae9361
diff -r ba349a2eeb37 -r 902d0f74333c xbee.cpp
--- a/xbee.cpp Tue Feb 14 04:25:55 2017 +0000
+++ b/xbee.cpp Tue Feb 14 19:47:20 2017 +0000
@@ -11,6 +11,15 @@
wait(3); // waiting for initiation
}
+char XBee::getChar()
+{
+ while (!comm.readable())
+ {
+ wait(0.02);
+ }
+ return comm.getc();
+}
+
void XBee::pcPrint(char* c)
{
int i = 0;
@@ -111,7 +120,6 @@
while (i < paramLen)
{
- mail->put(&(optionalParam)[i]);
cmdtosend[7 + i] = (optionalParam)[i];
i++;
}
@@ -135,7 +143,7 @@
i++;
}
- wait(0.1);
+ wait(0.2);
}
char* XBee::InterpretMessage()
@@ -144,35 +152,37 @@
if (comm.readable())
{
-
- char start = comm.getc(); // = FRAMEDELIMITER
+ char start = getChar(); // = FRAMEDELIMITER
//assert
- char len_msb = comm.getc();
- char len_lsb = comm.getc();
-
- int len = ((int) len_msb << 4) + (int) len_lsb;
- char frame_data[len];
-
- // Resolving frame type
- char type = comm.getc();
- len--;
-
- switch (type){
- case 0x88: ATCommandResponse(len);
- break;
- case 0x8A: ModemStatus(len);
- break;
- case 0x8B: ZigBeeTransmitStatus(len);
- break;
- case 0x90: ZigBeeReceivePacket(len);
- break;
- default: pcPrint("Please implement response of type ");
- printHexa(type);
- pcPrint("\r\n\0");
- for (int i = 0; i <len; i++)
- {
- comm.getc();
- }
+ if (start == FRAMEDELIMITER)
+ {
+ char len_msb = getChar();
+ char len_lsb = getChar();
+
+ int len = ((int) len_msb << 4) + (int) len_lsb;
+ char frame_data[len];
+
+ // Resolving frame type
+ char type = getChar();
+ len--;
+
+ switch (type){
+ case 0x88: ATCommandResponse(len);
+ break;
+ case 0x8A: ModemStatus(len);
+ break;
+ case 0x8B: ZigBeeTransmitStatus(len);
+ break;
+ case 0x90: ZigBeeReceivePacket(len);
+ break;
+ default: pcPrint("Please implement response of type ");
+ printHexa(type);
+ pcPrint("\r\n\0");
+ for (int i = 0; i <len; i++)
+ {
+ getChar();
+ }
+ }
}
}
return response;
@@ -181,15 +191,15 @@
void XBee::ATCommandResponse(int len)
{
char total = 0x88;
- char id = comm.getc();
+ char id = getChar();
total += id;
char* command0 = mail->alloc();
char* command1 = mail->alloc();
- *command0 = comm.getc();
+ *command0 = getChar();
total += *command0;
- *command1 = comm.getc();
+ *command1 = getChar();
total += *command1;
- char status = comm.getc();
+ char status = getChar();
total += status;
int i = 0;
len-= 4;
@@ -217,14 +227,14 @@
{
if (comm.readable())
{
- data[i] = comm.getc();
+ data[i] = getChar();
total += data[i];
printHexa(data[i]);
i++;
}
}
- char checksum = comm.getc();
+ char checksum = getChar();
total += checksum;
// Verify checksum
if (total != 0xFF)
@@ -236,7 +246,7 @@
void XBee::ModemStatus(int len)
{
- char status = comm.getc();
+ char status = getChar();
switch (status){
case 0 : pcPrint("Hardware reset\r\n\0"); break;
@@ -250,7 +260,7 @@
default : pcPrint("stack error\r\n\0"); break;
}
- char checksum = comm.getc();
+ char checksum = getChar();
checksum += 0x8A + status;
@@ -262,12 +272,12 @@
void XBee::ZigBeeTransmitStatus(int len)
{
- char id = comm.getc();
- char msb = comm.getc();
- char lsb = comm.getc();
- char retry = comm.getc();
- char status = comm.getc();
- char discovery = comm.getc();
+ char id = getChar();
+ char msb = getChar();
+ char lsb = getChar();
+ char retry = getChar();
+ char status = getChar();
+ char discovery = getChar();
char checksum;
pcPrint("Response to transmit #");
@@ -300,7 +310,7 @@
}
}
- checksum = comm.getc();
+ checksum = getChar();
// Validate checksum TODO
}
@@ -314,30 +324,30 @@
while(i < 8)
{
- adresse64bit[i] = comm.getc();
+ adresse64bit[i] = getChar();
i++;
}
- adresse16bit[0] = comm.getc();
- adresse16bit[1] = comm.getc();
+ adresse16bit[0] = getChar();
+ adresse16bit[1] = getChar();
- receiveOptions = comm.getc();
+ receiveOptions = getChar();
pcPrint("Data received : ");
i = 11;
while (i < len)
{
- printHexa(comm.getc());
+ printHexa(getChar());
i++;
}
pcPrint("\r\n");
- checksum = comm.getc();
+ checksum = getChar();
// Validate checksum TODO
}
-void XBee::ZigBeeTransmit(int adresse16, int adresse64, char *data, int dataLength)
+void XBee::ZigBeeTransmit(int adresse16, int adresse64msb, int adresse64lsb, char *data, int dataLength)
{
// Frame Type 0x10
// 0x0000000000000000 - Reserved 64-bit address for the coordinator
@@ -359,14 +369,14 @@
cmdtosend[2] = 0x0E + dataLength;
cmdtosend[3] = 0x10; // Frame type
cmdtosend[4] = 0x01; // Frame number
- cmdtosend[5] = 0x00; // MSB adresse 64-bit
- cmdtosend[6] = 0x00;
- cmdtosend[7] = 0x00;
- cmdtosend[8] = 0x00;
- cmdtosend[9] = 0x00;
- cmdtosend[10] = 0x00;
- cmdtosend[11] = 0xFF;
- cmdtosend[12] = 0xFF; // LSB adresse 64-bit
+ cmdtosend[5] = adresse64msb & 0xFF000000; // MSB adresse 64-bit
+ cmdtosend[6] = adresse64msb & 0x00FF0000;
+ cmdtosend[7] = adresse64msb & 0x0000FF00;
+ cmdtosend[8] = adresse64msb & 0x000000FF;
+ cmdtosend[9] = adresse64msb & 0xFF000000;
+ cmdtosend[10] = adresse64msb & 0x00FF0000;
+ cmdtosend[11] = adresse64msb & 0x0000FF00;
+ cmdtosend[12] = adresse64msb & 0x000000FF; // LSB adresse 64-bit
cmdtosend[13] = adresse16 >> 16; // MSB adresse 16-bit
cmdtosend[14] = adresse16 && 0b0000000011111111; // LSB adresse 16-bit
cmdtosend[15] = 0x00; // Broadcast Radius
@@ -385,15 +395,6 @@
}
}
- //cmdtosend[17] = 0x00; // Start RF DATA
- //cmdtosend[18] = 0x00;
- //cmdtosend[19] = 0x00;
- //cmdtosend[20] = 0x00;
- //cmdtosend[21] = 0x00;
- //cmdtosend[22] = 0x00;
- //cmdtosend[23] = 0x00;
- //cmdtosend[24] = 0x00; // End RF DATA
-
// Calculate checksum
i = 3;
while (i < (cmdlength - 1))
@@ -411,4 +412,17 @@
i++;
}
wait(0.1);
+}
+
+void XBee::BroadcastHelloWorld()
+{
+ char hello[5] = {'H', 'e', 'l', 'l', 'o'};
+ char world[5] = {'w', 'o', 'r', 'l', 'd'};
+
+ while (1)
+ {
+ ZigBeeTransmit(0x0000, 0x00000000, 0x00000000, &hello[0], 5);
+ ZigBeeTransmit(0x0000, 0x00000000, 0x00000000, &world[0], 5);
+ wait(2);
+ }
}
\ No newline at end of file