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

Fork of xbee_lib by Tristan Hughes

Revision:
11:18ff088287ea
Parent:
9:c8e4339ccc29
Child:
12:debf76f0c0bf
--- a/telegesis.cpp	Sun Oct 13 20:49:23 2013 +0000
+++ b/telegesis.cpp	Tue Oct 15 09:55:41 2013 +0000
@@ -1,32 +1,36 @@
 #include "telegesis.h"
 
 
-zigbee::zigbee(PinName tx, PinName rx, PinName reset)
-{
+zigbee::zigbee(PinName tx, PinName rx): _zbee(tx, rx) {
     _tx = tx;
     _rx = rx;
-    _reset = reset;
+     
+      _zbee.baud(19200);
 }
 
 zigbee::~zigbee()
 {
 }
 
+int zigbee::wait4OK() {
+    readPacket();
+    if (strstr(_responseFrameString,"OK"))  return 1; else return 0;
+}
 
-int zigbee::GetSerial(int *serial_no)
+int zigbee::GetSerial()
 { 
-/** comes with something like this
+    /** comes with something like this
   Telegesis ETRX357-LRS
   R305C
   000D6F0000D5F06A
   OK
 */   
-    Serial DATA(_tx,_rx);
-    wait_ms(50);
-    DATA.printf("ATI \r");
-    DATA.scanf("Telegesis ",HWType);
-    DATA.scanf ("%*s");
-    DATA.scanf ("%X",serial_no);
+    _zbee.printf("ATI\r");
+      readPacket(); readPacket(); //commando
+      readPacket();
+    sscanf(_responseFrameString,"Telegesis %s",HWType);
+      readPacket(); readPacket();
+    sscanf (_responseFrameString,"%s",LocalID);
     return 1;
 }
 
@@ -43,10 +47,10 @@
     if(numchar == 0) {
         numchar = sizeof(data_buf);
     }
-    Serial DATA(_tx,_rx);
+    
     while(numchar!=count) {
-        if(DATA.readable()) {
-            *data_buf = DATA.getc();
+        if(_zbee.readable()) {
+            *data_buf = _zbee.getc();
             data_buf+=1;
             count++;
         }
@@ -56,103 +60,77 @@
 
 int zigbee::ATI()
 {
-    Serial DATA(_tx,_rx);
-    wait_ms(5);
-    DATA.printf("ATI\r");
-    DATA.scanf ("%*s");
+    _zbee.printf("ATI\r");
+    _zbee.scanf ("%*s");
     return 1;
 }
 
 
 int zigbee::PingOut()
 {
-    Serial DATA(_tx,_rx);
-    wait_ms(5);
-    DATA.printf("AT+ANNCE\r");
-    DATA.scanf ("%*s");
+    _zbee.printf("AT+ANNCE\r");
+    _zbee.scanf ("%*s");
     return 1;
 }
 
 
 int zigbee::PanScan()
 {
-    Serial DATA(_tx,_rx);
-    wait_ms(5);
-    DATA.printf("AT+PANSCAN\r");
-    DATA.scanf ("%*s");
+    
+    
+    _zbee.printf("AT+PANSCAN\r");
+    _zbee.scanf ("%*s");
     return 1;
 }
 
 
 int zigbee::Establish_Network()
 {
-    Serial DATA(_tx,_rx);
-    wait_ms(5);
-    DATA.printf("AT+EN\r");
-    DATA.scanf ("%*s");
+    _zbee.printf("AT+EN\r");
+    _zbee.scanf ("%*s");
     return 1;
 }
 
 
 int zigbee::JoinNetwork()
 {
-    Serial DATA(_tx,_rx);
-    wait_ms(5);
-    DATA.printf("AT+JN\r");
-    DATA.scanf ("%*s");
+    _zbee.printf("AT+JN\r");
+    _zbee.scanf ("%*s");
     return 1;
 }
 
 int zigbee::ScanNetwork()
 {
-    Serial DATA(_tx,_rx);
-    wait_ms(5);
-    DATA.printf("AT+SN\r");
-    DATA.scanf ("%*s");
+    _zbee.printf("AT+SN\r");
+    _zbee.scanf ("%*s");
     return 1;
 }
 
 int zigbee::NetworkInfo()
 {
 //Return something like this "+N=COO,12,-11,29F0,55C0E0DCE605C522"
-    Serial DATA(_tx,_rx);
-    wait_ms(5);
-    DATA.printf("AT+N\r");
-    DATA.scanf ("+N=%s,%d,%d,%4X,%X",Devicetype,&channel,&NodeID,&EPID);
+    _zbee.printf("AT+N\r");
+    _zbee.scanf ("+N=%s,%d,%d,%4X,%X",Devicetype,&channel,&NodeID,&EPID);
     return 1;
 }
 
 int zigbee::UniCast(char *adr,char *payload)  //Ascii mode with null terminated string
 {
-    Serial DATA(_tx,_rx);
-    wait_ms(5);
-    DATA.printf("AT+UCAST:%s=%s\r",adr,payload);
-
-    DATA.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
+    _zbee.printf("AT+UCAST:%s=%s\r",adr,payload);
+    _zbee.scanf ("UCAST:%X,%X=%s ",&EUI64,&framesize,Zdata);
     return 1;
 }
 
 int zigbee::UniCastb(char *adr,char *payload, char payloadSize) //sends data in binary format
 {
-    Serial DATA(_tx,_rx);
-    wait_ms(5);
-    DATA.printf("AT+UCASTB:%X,%s\r",adr,payloadSize);
-    DATA.scanf ("%*s");
+    _zbee.printf("AT+UCASTB:%X,%s\r",adr,payloadSize);
+    _zbee.scanf ("%*s");
     return 1;
 }
 
-void zigbee::Reset()
-{
-    DigitalOut rst(_reset);
-    rst = 0;
-    wait_ms(10);
-    rst = 1;
-    wait_ms(1);
-}
-
 //-----------------------Håndtere indkommende data------------------------------
 
-unsigned long zigbee::hexToLong(const char *hex)
+unsigned long zigbee::hextolong(const char *hex)
 {
     //return 32 bit
     unsigned long result = 0;
@@ -171,7 +149,7 @@
 }
 
 
-unsigned int zigbee::hexToInt(const char *hex)
+unsigned int zigbee::hextoint(const char *hex)
 {
     //return 16 bit
     unsigned int result = 0;
@@ -189,4 +167,56 @@
     return result;
 }
 //---------------------------------------------------------------
+void zigbee::readPacket() {
+    //read and waits for the packet
+    while (SeePacket()!='\n') {
+    }
+}   
 
+
+uint8_t zigbee::SeePacket() {
+ char *p;   
+      _pos=0; b=0;
+    if (_zbee.readable()) {
+        b = _zbee.getc();
+              if ((b!=CR) & (b!=LF)) _responseFrameString[_pos++]=b;
+            if (b==CR) {
+              _responseFrameString[_pos]=0; //Nul terminate
+                 if (strstr(_responseFrameString,"+UCAST:")) { //returns on that we have sendt something
+          //Do something 
+         } else
+                 if (strstr(_responseFrameString,"UCAST:")) {//checke for incoming UCAST data
+           Zdat=1;
+           p=strstr(_responseFrameString,"="); p++;
+           strcpy(Zdata,p);
+         }
+               if (strstr(_responseFrameString,"LeftPAN:")) PanOnline=0;  //Local node has left the Pan
+
+                 if (strstr(_responseFrameString,"NEWNODE:")) { //NEWNODE: <NodeID>,<EUI64>,<Parent NodeID>
+                     //new node on the pan
+                 }
+
+                 if (strstr(_responseFrameString,"OK")) {  
+                        //if (ScriptState) ConnectScript();
+                        //Ok=1; Cmd=0; LineNo=0; 
+                 }
+                 
+                 if (strstr(_responseFrameString,"ACK")) { //
+                    PacketAck=1;
+                    p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
+                    SeqNumber=hextoint(p);
+                 }
+                 if (strstr(_responseFrameString,"NAK")) {
+                    PacketAck=0; 
+                    p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
+                    SeqNumber=hextoint(p);
+                 }
+                 if (strstr(_responseFrameString,"SEQ")) { //
+                    PacketAck=1;
+                    p=strstr(_responseFrameString,":"); p++; //P indholder nu SEQ nummer
+                    SeqNumber=hextoint(p);
+                 }
+            }   
+    }
+    return b;
+}