Hiroshi Yamaguchi / Mbed 2 deprecated XBeeExamples

Dependencies:   mbed XBee mbed-rtos

Committer:
yamaguch
Date:
Thu Mar 21 07:16:44 2013 +0000
Revision:
14:50ee389d681f
Parent:
11:25a790b8feb0
updated to use new XBee lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yamaguch 11:25a790b8feb0 1 #include "XBee.h"
yamaguch 11:25a790b8feb0 2
yamaguch 11:25a790b8feb0 3 void xbee_at_command() {
yamaguch 11:25a790b8feb0 4 XBee xbee(p9, p10);
yamaguch 14:50ee389d681f 5 xbee.baud(115200);
yamaguch 11:25a790b8feb0 6 xbee.init();
yamaguch 11:25a790b8feb0 7
yamaguch 11:25a790b8feb0 8 while (true) {
yamaguch 11:25a790b8feb0 9 printf("Enter AT command or q (quit) => ");
yamaguch 11:25a790b8feb0 10 char command[32], param[32] = {};
yamaguch 11:25a790b8feb0 11 int length = 0;
yamaguch 11:25a790b8feb0 12 scanf("%s", command);
yamaguch 11:25a790b8feb0 13 if (command[0] == 'q')
yamaguch 11:25a790b8feb0 14 break;
yamaguch 11:25a790b8feb0 15 if (command[2]) {
yamaguch 11:25a790b8feb0 16 if ((command[0] == 'N' && command[1] == 'I') ||
yamaguch 11:25a790b8feb0 17 (command[0] == 'N' && command[1] == 'D') ||
yamaguch 11:25a790b8feb0 18 (command[0] == 'D' && command[1] == 'N')) {
yamaguch 11:25a790b8feb0 19 while ((param[length] = command[length + 2]) != 0)
yamaguch 11:25a790b8feb0 20 length++;
yamaguch 11:25a790b8feb0 21 } else {
yamaguch 11:25a790b8feb0 22 if (command[2] == '0' && (command[3] == 'x' || command[3] == 'X')) {
yamaguch 11:25a790b8feb0 23 length = strlen(&command[4]);
yamaguch 11:25a790b8feb0 24 if (length <= 2) {
yamaguch 11:25a790b8feb0 25 length = 1;
yamaguch 11:25a790b8feb0 26 sscanf(&command[4], "%hhX", (char *) param);
yamaguch 11:25a790b8feb0 27 } else if (length <= 4) {
yamaguch 11:25a790b8feb0 28 length = 2;
yamaguch 11:25a790b8feb0 29 unsigned short us;
yamaguch 11:25a790b8feb0 30 sscanf(&command[4], "%hX", &us);
yamaguch 11:25a790b8feb0 31 param[1] = (us >> 0) & 0xFF;
yamaguch 11:25a790b8feb0 32 param[0] = (us >> 8) & 0xFF;
yamaguch 11:25a790b8feb0 33 } else if (length <= 8) {
yamaguch 11:25a790b8feb0 34 length = 4;
yamaguch 11:25a790b8feb0 35 unsigned long ul;
yamaguch 11:25a790b8feb0 36 sscanf(&command[4], "%X", &ul);
yamaguch 11:25a790b8feb0 37 param[3] = (ul >> 0) & 0xFF;
yamaguch 11:25a790b8feb0 38 param[2] = (ul >> 8) & 0xFF;
yamaguch 11:25a790b8feb0 39 param[1] = (ul >> 16) & 0xFF;
yamaguch 11:25a790b8feb0 40 param[0] = (ul >> 24) & 0xFF;
yamaguch 11:25a790b8feb0 41 } else {
yamaguch 11:25a790b8feb0 42 length = 8;
yamaguch 11:25a790b8feb0 43 unsigned long long ull;
yamaguch 11:25a790b8feb0 44 sscanf(&command[4], "%llX", &ull);
yamaguch 11:25a790b8feb0 45 param[7] = (ull >> 0) & 0xFF;
yamaguch 11:25a790b8feb0 46 param[6] = (ull >> 8) & 0xFF;
yamaguch 11:25a790b8feb0 47 param[5] = (ull >> 16) & 0xFF;
yamaguch 11:25a790b8feb0 48 param[4] = (ull >> 24) & 0xFF;
yamaguch 11:25a790b8feb0 49 param[3] = (ull >> 32) & 0xFF;
yamaguch 11:25a790b8feb0 50 param[2] = (ull >> 40) & 0xFF;
yamaguch 11:25a790b8feb0 51 param[1] = (ull >> 48) & 0xFF;
yamaguch 11:25a790b8feb0 52 param[0] = (ull >> 56) & 0xFF;
yamaguch 11:25a790b8feb0 53 }
yamaguch 11:25a790b8feb0 54 } else {
yamaguch 11:25a790b8feb0 55 unsigned short us;
yamaguch 11:25a790b8feb0 56 sscanf(&command[2], "%hd", &us);
yamaguch 11:25a790b8feb0 57 if (us < 256) {
yamaguch 11:25a790b8feb0 58 length = 1;
yamaguch 11:25a790b8feb0 59 param[0] = us;
yamaguch 11:25a790b8feb0 60 } else {
yamaguch 11:25a790b8feb0 61 length = 2;
yamaguch 11:25a790b8feb0 62 param[1] = (us >> 0) & 0xFF;
yamaguch 11:25a790b8feb0 63 param[0] = (us >> 8) & 0xFF;
yamaguch 11:25a790b8feb0 64 }
yamaguch 11:25a790b8feb0 65 }
yamaguch 11:25a790b8feb0 66 }
yamaguch 11:25a790b8feb0 67 }
yamaguch 11:25a790b8feb0 68 command[2] = '\0';
yamaguch 11:25a790b8feb0 69
yamaguch 11:25a790b8feb0 70 xbee.sendCommand(command, param, length);
yamaguch 11:25a790b8feb0 71
yamaguch 11:25a790b8feb0 72 while (xbee.receive())
yamaguch 11:25a790b8feb0 73 xbee.dump();
yamaguch 11:25a790b8feb0 74 }
yamaguch 11:25a790b8feb0 75 printf("done.\n");
yamaguch 11:25a790b8feb0 76 }