xbee_lib from Tristan Hughes with some changes by zac dannelly

Dependents:   potSend potGet serialRangeGet

Fork of xbee_lib by Tristan Hughes

Committer:
tristanjph
Date:
Wed Aug 29 13:27:02 2012 +0000
Revision:
2:cb627ea9b817
Parent:
1:c3d9bdcb0b03
Child:
3:682615a0717e
Update send/receive added setting network pan id

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tristanjph 0:2656fb225c5d 1 #include "xbee.h"
tristanjph 0:2656fb225c5d 2
tristanjph 0:2656fb225c5d 3
tristanjph 0:2656fb225c5d 4 xbee::xbee(PinName tx, PinName rx)
tristanjph 0:2656fb225c5d 5 {
tristanjph 0:2656fb225c5d 6 _tx = tx;
tristanjph 0:2656fb225c5d 7 _rx = rx;
tristanjph 0:2656fb225c5d 8 }
tristanjph 0:2656fb225c5d 9
tristanjph 0:2656fb225c5d 10 xbee::~xbee()
tristanjph 0:2656fb225c5d 11 {
tristanjph 0:2656fb225c5d 12 }
tristanjph 0:2656fb225c5d 13
tristanjph 0:2656fb225c5d 14 int xbee::ConfigMode()
tristanjph 0:2656fb225c5d 15 {
tristanjph 0:2656fb225c5d 16 int a;
tristanjph 0:2656fb225c5d 17 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 18 wait(2);
tristanjph 0:2656fb225c5d 19 DATA.printf("+++");
tristanjph 0:2656fb225c5d 20 while (a != 75) {
tristanjph 0:2656fb225c5d 21 if (DATA.readable()) {
tristanjph 0:2656fb225c5d 22 a = DATA.getc();
tristanjph 0:2656fb225c5d 23 }
tristanjph 0:2656fb225c5d 24 }
tristanjph 0:2656fb225c5d 25 wait(1);
tristanjph 0:2656fb225c5d 26 printf("config mode\n");
tristanjph 0:2656fb225c5d 27 return 1;
tristanjph 0:2656fb225c5d 28 }
tristanjph 0:2656fb225c5d 29
tristanjph 2:cb627ea9b817 30 int xbee::GetSerial(int *serial_no)
tristanjph 0:2656fb225c5d 31 {
tristanjph 0:2656fb225c5d 32 int sh1,sh2,sh3,sl1,sl2,sl3,sl4;
tristanjph 0:2656fb225c5d 33 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 34 wait_ms(50);
tristanjph 0:2656fb225c5d 35 DATA.printf("ATSL \r");
tristanjph 0:2656fb225c5d 36 DATA.scanf ("%2x%2x%2x%2x",&sl1,&sl2,&sl3,&sl4);
tristanjph 0:2656fb225c5d 37 wait_ms(500);
tristanjph 0:2656fb225c5d 38 DATA.printf("ATSH \r");
tristanjph 0:2656fb225c5d 39 DATA.scanf ("%2x%2x%2x",&sh1,&sh2,&sh3);
tristanjph 0:2656fb225c5d 40
tristanjph 0:2656fb225c5d 41 serial_no[0] = sh1;
tristanjph 0:2656fb225c5d 42 serial_no[1] = sh2;
tristanjph 0:2656fb225c5d 43 serial_no[2] = sh3;
tristanjph 0:2656fb225c5d 44 serial_no[3] = sl1;
tristanjph 0:2656fb225c5d 45 serial_no[4] = sl2;
tristanjph 0:2656fb225c5d 46 serial_no[5] = sl3;
tristanjph 0:2656fb225c5d 47 serial_no[6] = sl4;
tristanjph 0:2656fb225c5d 48
tristanjph 0:2656fb225c5d 49 return 1;
tristanjph 0:2656fb225c5d 50 }
tristanjph 0:2656fb225c5d 51
tristanjph 2:cb627ea9b817 52 int xbee::SetKey(char* key)
tristanjph 0:2656fb225c5d 53 {
tristanjph 0:2656fb225c5d 54 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 55 DATA.printf("ATEE 1 \r");
tristanjph 0:2656fb225c5d 56
tristanjph 0:2656fb225c5d 57 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 58 wait_ms(1);
tristanjph 2:cb627ea9b817 59 DATA.printf("ATKY %s \r",key);
tristanjph 0:2656fb225c5d 60 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 61 return 1;
tristanjph 0:2656fb225c5d 62 }
tristanjph 0:2656fb225c5d 63
tristanjph 0:2656fb225c5d 64 int xbee::WriteSettings()
tristanjph 0:2656fb225c5d 65 {
tristanjph 0:2656fb225c5d 66 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 67 wait_ms(5);
tristanjph 0:2656fb225c5d 68 DATA.printf("ATWR \r");
tristanjph 0:2656fb225c5d 69 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 70 return 1;
tristanjph 0:2656fb225c5d 71 }
tristanjph 0:2656fb225c5d 72
tristanjph 0:2656fb225c5d 73 int xbee::ExitConfigMode()
tristanjph 0:2656fb225c5d 74 {
tristanjph 0:2656fb225c5d 75 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 76 wait_ms(5);
tristanjph 0:2656fb225c5d 77 DATA.printf("ATCN \r");
tristanjph 0:2656fb225c5d 78 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 79 return 1;
tristanjph 0:2656fb225c5d 80 }
tristanjph 0:2656fb225c5d 81
tristanjph 2:cb627ea9b817 82 int xbee::SendData(char *data_buf)
tristanjph 0:2656fb225c5d 83 {
tristanjph 0:2656fb225c5d 84 Serial DATA(_tx,_rx);
tristanjph 2:cb627ea9b817 85 DATA.printf("%s",data_buf);
tristanjph 0:2656fb225c5d 86 return 1;
tristanjph 0:2656fb225c5d 87 }
tristanjph 0:2656fb225c5d 88
tristanjph 2:cb627ea9b817 89 void xbee::RecieveData(char *data_buf, int numchar)
tristanjph 1:c3d9bdcb0b03 90 {
tristanjph 1:c3d9bdcb0b03 91 int count=0;
tristanjph 2:cb627ea9b817 92 if(numchar == 0) {
tristanjph 2:cb627ea9b817 93 numchar = sizeof(data_buf);
tristanjph 2:cb627ea9b817 94 }
tristanjph 1:c3d9bdcb0b03 95 Serial DATA(_tx,_rx);
tristanjph 1:c3d9bdcb0b03 96 while(numchar!=count) {
tristanjph 1:c3d9bdcb0b03 97 if(DATA.readable()) {
tristanjph 2:cb627ea9b817 98 *data_buf = DATA.getc();
tristanjph 2:cb627ea9b817 99 data_buf+=1; count++;
tristanjph 1:c3d9bdcb0b03 100 }
tristanjph 1:c3d9bdcb0b03 101
tristanjph 1:c3d9bdcb0b03 102 }
tristanjph 2:cb627ea9b817 103 }
tristanjph 1:c3d9bdcb0b03 104
tristanjph 2:cb627ea9b817 105 int xbee::SetPanId(int pan_id)
tristanjph 2:cb627ea9b817 106 {
tristanjph 2:cb627ea9b817 107 Serial DATA(_tx,_rx);
tristanjph 2:cb627ea9b817 108 wait_ms(5);
tristanjph 2:cb627ea9b817 109 DATA.printf("ATID %i\r",pan_id);
tristanjph 2:cb627ea9b817 110 DATA.scanf ("%*s");
tristanjph 2:cb627ea9b817 111 return 1;
tristanjph 2:cb627ea9b817 112 }