A libery to connect to telegesis zigbee module. Bassed on implemtation of XBEE

Fork of xbee_lib by Tristan Hughes

Committer:
tristanjph
Date:
Wed Aug 29 14:04:27 2012 +0000
Revision:
3:682615a0717e
Parent:
2:cb627ea9b817
Child:
4:ede20c047d8b
Added reset, general fixes

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