The library for waveshare ZBee Core2530 (B):

Dependents:   zbee_rx_analog zbee_tx_analog simple_zb_rx simple_zb_tx

Committer:
ruslylove
Date:
Wed Apr 06 15:50:27 2016 +0000
Revision:
2:186e6cce954b
Parent:
1:86542d0d3c89
bug fixed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ruslylove 0:5092dcc261f2 1 /* Copyright (c) 2016 Ruslee Sutthaweekul, MIT License
ruslylove 0:5092dcc261f2 2 *
ruslylove 0:5092dcc261f2 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
ruslylove 0:5092dcc261f2 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
ruslylove 0:5092dcc261f2 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
ruslylove 0:5092dcc261f2 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
ruslylove 0:5092dcc261f2 7 * furnished to do so, subject to the following conditions:
ruslylove 0:5092dcc261f2 8 *
ruslylove 0:5092dcc261f2 9 * The above copyright notice and this permission notice shall be included in all copies or
ruslylove 0:5092dcc261f2 10 * substantial portions of the Software.
ruslylove 0:5092dcc261f2 11 *
ruslylove 0:5092dcc261f2 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
ruslylove 0:5092dcc261f2 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
ruslylove 0:5092dcc261f2 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
ruslylove 0:5092dcc261f2 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ruslylove 0:5092dcc261f2 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ruslylove 0:5092dcc261f2 17 */
ruslylove 0:5092dcc261f2 18
ruslylove 0:5092dcc261f2 19 #include "zbee.h"
ruslylove 0:5092dcc261f2 20
ruslylove 0:5092dcc261f2 21
ruslylove 0:5092dcc261f2 22 zbee::zbee(PinName tx, PinName rx, int baud)
ruslylove 0:5092dcc261f2 23 {
ruslylove 0:5092dcc261f2 24 _tx = tx;
ruslylove 0:5092dcc261f2 25 _rx = rx;
ruslylove 0:5092dcc261f2 26 _baud = baud;
ruslylove 0:5092dcc261f2 27 }
ruslylove 0:5092dcc261f2 28
ruslylove 0:5092dcc261f2 29 zbee::~zbee()
ruslylove 0:5092dcc261f2 30 {
ruslylove 0:5092dcc261f2 31 }
ruslylove 0:5092dcc261f2 32
ruslylove 0:5092dcc261f2 33
ruslylove 0:5092dcc261f2 34 int zbee::GetAddr(char *addr)
ruslylove 0:5092dcc261f2 35 {
ruslylove 0:5092dcc261f2 36 Serial DATA(_tx,_rx);
ruslylove 0:5092dcc261f2 37 DATA.baud(_baud);
ruslylove 0:5092dcc261f2 38 wait_ms(50);
ruslylove 0:5092dcc261f2 39 DATA.printf("AT+GETADDR \r");
ruslylove 0:5092dcc261f2 40 DATA.scanf ("%s",addr);
ruslylove 0:5092dcc261f2 41 return 1;
ruslylove 0:5092dcc261f2 42 }
ruslylove 0:5092dcc261f2 43
ruslylove 0:5092dcc261f2 44 int zbee::GetPanId(char *panid)
ruslylove 0:5092dcc261f2 45 {
ruslylove 0:5092dcc261f2 46 Serial DATA(_tx,_rx);
ruslylove 0:5092dcc261f2 47 DATA.baud(_baud);
ruslylove 0:5092dcc261f2 48 wait_ms(50);
ruslylove 0:5092dcc261f2 49 DATA.printf("AT+GETPANID \r");
ruslylove 0:5092dcc261f2 50 DATA.scanf ("%s",panid);
ruslylove 0:5092dcc261f2 51 return 1;
ruslylove 0:5092dcc261f2 52 }
ruslylove 0:5092dcc261f2 53
ruslylove 0:5092dcc261f2 54
ruslylove 0:5092dcc261f2 55 int zbee::SendData(char *data_buf)
ruslylove 0:5092dcc261f2 56 {
ruslylove 0:5092dcc261f2 57 Serial DATA(_tx,_rx);
ruslylove 0:5092dcc261f2 58 DATA.baud(_baud);
ruslylove 0:5092dcc261f2 59
ruslylove 0:5092dcc261f2 60 DATA.printf("%s",data_buf);
ruslylove 2:186e6cce954b 61 // DATA.scanf ("%*s"); // flush
ruslylove 0:5092dcc261f2 62 return 1;
ruslylove 0:5092dcc261f2 63 }
ruslylove 0:5092dcc261f2 64
ruslylove 1:86542d0d3c89 65 void zbee::ReceiveData(char *data_buf, int numchar)
ruslylove 0:5092dcc261f2 66 {
ruslylove 0:5092dcc261f2 67 int count=0;
ruslylove 0:5092dcc261f2 68 if(numchar == 0) {
ruslylove 0:5092dcc261f2 69 numchar = sizeof(data_buf);
ruslylove 0:5092dcc261f2 70 }
ruslylove 0:5092dcc261f2 71 Serial DATA(_tx,_rx);
ruslylove 0:5092dcc261f2 72 DATA.baud(_baud);
ruslylove 0:5092dcc261f2 73 while(numchar!=count) {
ruslylove 0:5092dcc261f2 74 if(DATA.readable()) {
ruslylove 0:5092dcc261f2 75 *data_buf = DATA.getc();
ruslylove 0:5092dcc261f2 76 data_buf+=1;
ruslylove 0:5092dcc261f2 77 count++;
ruslylove 0:5092dcc261f2 78 }
ruslylove 0:5092dcc261f2 79
ruslylove 0:5092dcc261f2 80 }
ruslylove 0:5092dcc261f2 81 }
ruslylove 0:5092dcc261f2 82
ruslylove 0:5092dcc261f2 83 int zbee::SetPanId(int pan_id)
ruslylove 0:5092dcc261f2 84 {
ruslylove 0:5092dcc261f2 85 Serial DATA(_tx,_rx);
ruslylove 0:5092dcc261f2 86 DATA.baud(_baud);
ruslylove 0:5092dcc261f2 87 char rx[20];
ruslylove 0:5092dcc261f2 88
ruslylove 0:5092dcc261f2 89 wait_ms(5);
ruslylove 0:5092dcc261f2 90
ruslylove 0:5092dcc261f2 91 DATA.printf("AT+SETPANID %i\r",pan_id);
ruslylove 0:5092dcc261f2 92 DATA.scanf ("%*s",rx);
ruslylove 0:5092dcc261f2 93
ruslylove 0:5092dcc261f2 94 if(strcmp(rx,"SETPANID OK"))
ruslylove 0:5092dcc261f2 95 return 1;
ruslylove 0:5092dcc261f2 96 else
ruslylove 0:5092dcc261f2 97 return 0;
ruslylove 0:5092dcc261f2 98 }
ruslylove 0:5092dcc261f2 99
ruslylove 0:5092dcc261f2 100 void zbee::Reset()
ruslylove 0:5092dcc261f2 101 {
ruslylove 0:5092dcc261f2 102 Serial DATA(_tx,_rx);
ruslylove 0:5092dcc261f2 103 DATA.baud(_baud);
ruslylove 0:5092dcc261f2 104 wait_ms(50);
ruslylove 0:5092dcc261f2 105 DATA.printf("AT+RESET \r");
ruslylove 0:5092dcc261f2 106 DATA.scanf ("%*s");
ruslylove 0:5092dcc261f2 107 }
ruslylove 0:5092dcc261f2 108
ruslylove 0:5092dcc261f2 109