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

Fork of xbee_lib by Tristan Hughes

Committer:
tristanjph
Date:
Fri Aug 31 15:46:34 2012 +0000
Revision:
5:714651141a83
Parent:
4:ede20c047d8b
Child:
6:6455a079bdb3
Updates to SetKey to take string

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tristanjph 4:ede20c047d8b 1 /* Copyright (c) 2012 Tristan Hughes, MIT License
tristanjph 4:ede20c047d8b 2 *
tristanjph 4:ede20c047d8b 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
tristanjph 4:ede20c047d8b 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
tristanjph 4:ede20c047d8b 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
tristanjph 4:ede20c047d8b 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
tristanjph 4:ede20c047d8b 7 * furnished to do so, subject to the following conditions:
tristanjph 4:ede20c047d8b 8 *
tristanjph 4:ede20c047d8b 9 * The above copyright notice and this permission notice shall be included in all copies or
tristanjph 4:ede20c047d8b 10 * substantial portions of the Software.
tristanjph 4:ede20c047d8b 11 *
tristanjph 4:ede20c047d8b 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
tristanjph 4:ede20c047d8b 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
tristanjph 4:ede20c047d8b 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
tristanjph 4:ede20c047d8b 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
tristanjph 4:ede20c047d8b 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tristanjph 4:ede20c047d8b 17 */
tristanjph 4:ede20c047d8b 18
tristanjph 4:ede20c047d8b 19 #include "xbee.h"
tristanjph 0:2656fb225c5d 20
tristanjph 0:2656fb225c5d 21
tristanjph 3:682615a0717e 22 xbee::xbee(PinName tx, PinName rx, PinName reset)
tristanjph 0:2656fb225c5d 23 {
tristanjph 0:2656fb225c5d 24 _tx = tx;
tristanjph 0:2656fb225c5d 25 _rx = rx;
tristanjph 3:682615a0717e 26 _reset = reset;
tristanjph 0:2656fb225c5d 27 }
tristanjph 0:2656fb225c5d 28
tristanjph 0:2656fb225c5d 29 xbee::~xbee()
tristanjph 0:2656fb225c5d 30 {
tristanjph 0:2656fb225c5d 31 }
tristanjph 0:2656fb225c5d 32
tristanjph 0:2656fb225c5d 33 int xbee::ConfigMode()
tristanjph 0:2656fb225c5d 34 {
tristanjph 0:2656fb225c5d 35 int a;
tristanjph 0:2656fb225c5d 36 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 37 wait(2);
tristanjph 0:2656fb225c5d 38 DATA.printf("+++");
tristanjph 0:2656fb225c5d 39 while (a != 75) {
tristanjph 0:2656fb225c5d 40 if (DATA.readable()) {
tristanjph 0:2656fb225c5d 41 a = DATA.getc();
tristanjph 0:2656fb225c5d 42 }
tristanjph 0:2656fb225c5d 43 }
tristanjph 0:2656fb225c5d 44 wait(1);
tristanjph 0:2656fb225c5d 45 printf("config mode\n");
tristanjph 0:2656fb225c5d 46 return 1;
tristanjph 0:2656fb225c5d 47 }
tristanjph 0:2656fb225c5d 48
tristanjph 2:cb627ea9b817 49 int xbee::GetSerial(int *serial_no)
tristanjph 0:2656fb225c5d 50 {
tristanjph 0:2656fb225c5d 51 int sh1,sh2,sh3,sl1,sl2,sl3,sl4;
tristanjph 0:2656fb225c5d 52 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 53 wait_ms(50);
tristanjph 0:2656fb225c5d 54 DATA.printf("ATSL \r");
tristanjph 0:2656fb225c5d 55 DATA.scanf ("%2x%2x%2x%2x",&sl1,&sl2,&sl3,&sl4);
tristanjph 0:2656fb225c5d 56 wait_ms(500);
tristanjph 0:2656fb225c5d 57 DATA.printf("ATSH \r");
tristanjph 0:2656fb225c5d 58 DATA.scanf ("%2x%2x%2x",&sh1,&sh2,&sh3);
tristanjph 0:2656fb225c5d 59
tristanjph 0:2656fb225c5d 60 serial_no[0] = sh1;
tristanjph 0:2656fb225c5d 61 serial_no[1] = sh2;
tristanjph 0:2656fb225c5d 62 serial_no[2] = sh3;
tristanjph 0:2656fb225c5d 63 serial_no[3] = sl1;
tristanjph 0:2656fb225c5d 64 serial_no[4] = sl2;
tristanjph 0:2656fb225c5d 65 serial_no[5] = sl3;
tristanjph 0:2656fb225c5d 66 serial_no[6] = sl4;
tristanjph 0:2656fb225c5d 67
tristanjph 0:2656fb225c5d 68 return 1;
tristanjph 0:2656fb225c5d 69 }
tristanjph 0:2656fb225c5d 70
tristanjph 5:714651141a83 71 int xbee::SetKey(char* key)
tristanjph 0:2656fb225c5d 72 {
tristanjph 0:2656fb225c5d 73 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 74 DATA.printf("ATEE 1 \r");
tristanjph 0:2656fb225c5d 75
tristanjph 0:2656fb225c5d 76 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 77 wait_ms(1);
tristanjph 5:714651141a83 78 DATA.printf("ATKY %s \r",key);
tristanjph 0:2656fb225c5d 79 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 80 return 1;
tristanjph 0:2656fb225c5d 81 }
tristanjph 0:2656fb225c5d 82
tristanjph 0:2656fb225c5d 83 int xbee::WriteSettings()
tristanjph 0:2656fb225c5d 84 {
tristanjph 0:2656fb225c5d 85 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 86 wait_ms(5);
tristanjph 0:2656fb225c5d 87 DATA.printf("ATWR \r");
tristanjph 0:2656fb225c5d 88 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 89 return 1;
tristanjph 0:2656fb225c5d 90 }
tristanjph 0:2656fb225c5d 91
tristanjph 0:2656fb225c5d 92 int xbee::ExitConfigMode()
tristanjph 0:2656fb225c5d 93 {
tristanjph 0:2656fb225c5d 94 Serial DATA(_tx,_rx);
tristanjph 0:2656fb225c5d 95 wait_ms(5);
tristanjph 0:2656fb225c5d 96 DATA.printf("ATCN \r");
tristanjph 0:2656fb225c5d 97 DATA.scanf ("%*s");
tristanjph 0:2656fb225c5d 98 return 1;
tristanjph 0:2656fb225c5d 99 }
tristanjph 0:2656fb225c5d 100
tristanjph 2:cb627ea9b817 101 int xbee::SendData(char *data_buf)
tristanjph 0:2656fb225c5d 102 {
tristanjph 0:2656fb225c5d 103 Serial DATA(_tx,_rx);
tristanjph 2:cb627ea9b817 104 DATA.printf("%s",data_buf);
tristanjph 0:2656fb225c5d 105 return 1;
tristanjph 0:2656fb225c5d 106 }
tristanjph 0:2656fb225c5d 107
tristanjph 2:cb627ea9b817 108 void xbee::RecieveData(char *data_buf, int numchar)
tristanjph 1:c3d9bdcb0b03 109 {
tristanjph 1:c3d9bdcb0b03 110 int count=0;
tristanjph 2:cb627ea9b817 111 if(numchar == 0) {
tristanjph 2:cb627ea9b817 112 numchar = sizeof(data_buf);
tristanjph 2:cb627ea9b817 113 }
tristanjph 1:c3d9bdcb0b03 114 Serial DATA(_tx,_rx);
tristanjph 1:c3d9bdcb0b03 115 while(numchar!=count) {
tristanjph 1:c3d9bdcb0b03 116 if(DATA.readable()) {
tristanjph 2:cb627ea9b817 117 *data_buf = DATA.getc();
tristanjph 3:682615a0717e 118 data_buf+=1;
tristanjph 3:682615a0717e 119 count++;
tristanjph 1:c3d9bdcb0b03 120 }
tristanjph 1:c3d9bdcb0b03 121
tristanjph 1:c3d9bdcb0b03 122 }
tristanjph 2:cb627ea9b817 123 }
tristanjph 1:c3d9bdcb0b03 124
tristanjph 2:cb627ea9b817 125 int xbee::SetPanId(int pan_id)
tristanjph 2:cb627ea9b817 126 {
tristanjph 2:cb627ea9b817 127 Serial DATA(_tx,_rx);
tristanjph 2:cb627ea9b817 128 wait_ms(5);
tristanjph 2:cb627ea9b817 129 DATA.printf("ATID %i\r",pan_id);
tristanjph 2:cb627ea9b817 130 DATA.scanf ("%*s");
tristanjph 2:cb627ea9b817 131 return 1;
tristanjph 2:cb627ea9b817 132 }
tristanjph 3:682615a0717e 133
tristanjph 3:682615a0717e 134 void xbee::Reset()
tristanjph 3:682615a0717e 135 {
tristanjph 3:682615a0717e 136 DigitalOut rssi(_reset);
tristanjph 3:682615a0717e 137 rssi = 0;
tristanjph 3:682615a0717e 138 wait_ms(10);
tristanjph 3:682615a0717e 139 rssi = 1;
tristanjph 3:682615a0717e 140 wait_ms(1);
tristanjph 3:682615a0717e 141 }
tristanjph 3:682615a0717e 142
tristanjph 3:682615a0717e 143